Heat Stroke

시간 제한2초메모리 제한1024 MB

요약
도로 x에서 발생한 환자를 x번 또는 x+1번 병원으로 보낼 때, 병원 정원과 환자 발생 순서가 주어질 때 헬리콥터로 보내야 하는 최대 환자 수를 구한다.
난이도

보통10점 중 7점

유형
그리디, 동적 계획법
정답자
아직 제출이 없습니다

문제

JOI Island consists of LL districts, numbered from 11 to LL from west to east. There are L−1L -1 roads in the island, numbered from 11 to L−1L - 1. Road ii (1≤i≤L−11 ≤ i ≤ L - 1) connects districts ii and i+1i + 1 bidirectionally.

Now, the International Olympiad in Informatics (IOI 20XX) is planned to be held in JOI Island! The concern is that the island is famous for its extreme heat. There is a high risk of heat stroke, especially for foreign contestants who are not acclimatized to hot environment. So the organizers of IOI decided to take the following measures:

  • First, for each ii (1≤i≤L1 ≤ i ≤ L), they prepare a hospital at district ii with capacity of C_iC\_i people. Note that there are cases that C_i=0C\_i = 0.
  • During the IOI event, when a person on road xx (1≤x≤L−11 ≤ x ≤ L - 1) gets heat stroke, they send to hospital in the following procedure:
    • They send the patient to the hospital of either district xx or x+1x + 1, whichever is not full. If both hospitals are not full, either choice is possible. If both hospital are full, they send the patient to a general hospital outside the island by helicopter.

Since the usage of helicopter is costly, the organizers want to estimate the maximum number of patients to be sent by helicopter. They consider the following scenario as an example:

  • Before the IOI event, there are no patients in any hospital.
  • During the IOI event, NN people will get heat stroke in JOI Island. The jj-th (1≤j≤N1 ≤ j ≤ N) patient occurs on road X_jX\_j.
  • For each jj (1≤j≤N−11 ≤ j ≤ N - 1), when the (j+1)(j + 1)-th patient gets heat stroke, the jj-th patient and earlier is already sent to hospital. Due to the severe symptoms of heat stroke, no patients leave hospital during the IOI event.

Write a program which, given the number of districts and the information of hospitals and heat stroke patients, computes the maximum number of patients to be sent by helicopter in the scenario above.

입력

Read the following data from the standard input.

LL

C_1C\_1 C_2C\_2 ⋯\cdots C_LC\_L

NN

X_1X\_1 X_2X\_2 ⋯\cdots X_NX\_N

출력

Write one line to the standard output. The output should contain the maximum number of patients to be sent by helicopter.

제한

  • 2≤L≤8,0002 ≤ L ≤ 8\\, 000.
  • 0≤C_i≤8,0000 ≤ C\_i ≤ 8\\, 000 (1≤i≤L1 ≤ i ≤ L).
  • 1≤N≤8,0001 ≤ N ≤ 8\\, 000.
  • 1≤X_j≤L−11 ≤ X\_j ≤ L - 1 (1≤j≤N1 ≤ j ≤ N).
  • Given values are all integers.

예제5

  1. 예제 1

    입력
    3
    1 1 1
    3
    1 2 2
    
    예상 출력
    1
    
  2. 예제 2

    입력
    6
    1 1 1 1 1 1
    7
    1 3 5 4 2 2 3
    
    예상 출력
    3
    
  3. 예제 3

    입력
    6
    4000 1 1 0 4000 1
    5
    1 1 2 3 5
    
    예상 출력
    1
    
  4. 예제 4

    입력
    5
    1 2 2 2 1
    8
    2 3 2 1 4 1 2 3
    
    예상 출력
    2
    
  5. 예제 5

    입력
    10
    2 2 2 2 2 2 2 2 2 2
    18
    1 3 5 7 9 2 4 6 8 1 3 5 7 9 2 4 6 8
    
    예상 출력
    3