#P10627. [JOI Open 2024] 中暑 / Heat Stroke

[JOI Open 2024] 中暑 / Heat Stroke

Problem Description

JOI Island consists of LL districts, numbered from 11 to LL from west to east. There are (L1)(L - 1) roads on the island, numbered from 11 to L1L - 1. Road ii (1iL11 \le i \le L - 1) connects districts ii and i+1i + 1 in both directions.

Now, IOI 20XX is planned to be held on JOI Island. However, there is a concern: JOI Island is famous for being a “stove”. The risk of heat stroke on the island is high, especially for foreigners who are not used to JOI Island’s hot climate. Therefore, the IOI organizers decide to take the following measures:

  • For each 1iL1 \le i \le L, there is a hospital in district ii with capacity CiC_i people. Note that CiC_i can be 00.

  • During the IOI event, when a person suffers heat stroke on road xx (1xL11 \le x \le L - 1), they will be taken to a hospital by the following procedure:

    • Take the person to a hospital in district xx or district x+1x + 1 that is not full. If the hospitals in both districts are not full, either one may be chosen. If both hospitals are full, use a helicopter to take the person to a hospital outside the island.

Since using a helicopter is expensive, the organizers want to estimate the maximum possible number of patients who may need a helicopter. They consider the following situation:

  • Before the IOI event, there are no patients in any hospital.
  • During the IOI event, NN people will suffer heat stroke one by one. The jj-th person (1jN1 \le j \le N) suffers heat stroke on road XjX_j.
  • For any 1jN11 \le j \le N - 1, when the (j+1)(j + 1)-th person suffers heat stroke, persons 1,2,,j1, 2, \cdots, j have already arrived at hospitals. Since heat stroke is severe, nobody leaves the hospital during the IOI event.

You need to write a program. Given the number of districts, the hospital information, and the heat stroke information, compute the maximum possible number of patients who need a helicopter in the above situation.

Input Format

The input format is as follows:

LL
C1C_1 C2C_2 \cdots CLC_L
NN
X1X_1 X2X_2 \cdots XNX_N

Output Format

Output one line containing one integer, which is the maximum possible number of patients who need a helicopter.

3
1 1 1
3
1 2 2
1
6
1 1 1 1 1 1
7
1 3 5 4 2 2 3
3
6
4000 1 1 0 4000 1
5
1 1 2 3 5
1
5
1 2 2 2 1
8
2 3 2 1 4 1 2 3
2
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

Hint

Sample Explanation

For sample 11, consider the following:

  • Send the first patient to the hospital in district 22. The numbers of patients in the hospitals of the three districts are then 0,1,00, 1, 0.
  • Send the second patient to the hospital in district 33. The numbers of patients in the hospitals of the three districts are then 0,1,10, 1, 1.
  • For the third patient, since the hospitals in districts 22 and 33 are both full, the only option is to use a helicopter to take them off the island.

In this case, 11 person is taken off the island by helicopter. It can be proven that this is the maximum.

For sample 22, consider the following:

  • Send the first patient to the hospital in district 22. The numbers of patients in the hospitals of the six districts are then 0,1,0,0,0,00, 1, 0, 0, 0, 0.
  • Send the second patient to the hospital in district 44. The numbers of patients in the hospitals of the six districts are then 0,1,0,1,0,00, 1, 0, 1, 0, 0.
  • Send the third patient to the hospital in district 55. The numbers of patients in the hospitals of the six districts are then 0,1,0,1,1,00, 1, 0, 1, 1, 0.
  • For the fourth patient, since the hospitals in districts 44 and 55 are both full, the only option is to use a helicopter to take them off the island.
  • Send the fifth patient to the hospital in district 33. The numbers of patients in the hospitals of the six districts are then 0,1,1,1,1,00, 1, 1, 1, 1, 0.
  • For the sixth patient, since the hospitals in districts 22 and 33 are both full, the only option is to use a helicopter to take them off the island.
  • For the seventh patient, since the hospitals in districts 33 and 44 are both full, the only option is to use a helicopter to take them off the island.

In this case, 33 people are taken off the island by helicopter. It can be proven that this is the maximum.

Sample 11 satisfies the conditions of subtasks 181 \sim 8.

Sample 22 satisfies the conditions of subtasks 282 \sim 8.

Sample 33 satisfies the conditions of subtasks 1,581, 5 \sim 8.

Samples 44 and 55 satisfy the conditions of subtasks 585 \sim 8.

Constraints

  • 2L80002 \le L \le 8\,000.
  • 0Ci80000 \le C_i \le 8\,000 (1iL1 \le i \le L).
  • 1N80001 \le N \le 8\,000.
  • 1XjL11 \le X_j \le L − 1 (1jN1 \le j \le N).
  • All input numbers are integers.

【Subtasks】

  1. (66 points) X1X2XNX_1 \le X_2 \le\cdots\le X_N.
  2. (77 points) L18,N18,Ci=1L \le 18, N \le 18, C_i = 1 (1iL1 \le i \le L).
  3. (77 points) L18,N100,Ci=1L \le 18, N \le 100, C_i = 1 (1iL1 \le i \le L).
  4. (2525 points) L100,N100,Ci=1L \le 100, N \le 100, C_i = 1 (1iL1 \le i \le L).
  5. (2525 points) L100,N100L \le 100, N \le 100.
  6. (1010 points) L600,N600L \le 600, N \le 600.
  7. (1515 points) L3500,N3500L \le 3\,500, N \le 3\,500.
  8. (55 points) No additional constraints.

Translated by Starrykiller from the English statement.

Translated by ChatGPT 5