#P10627. [JOI Open 2024] 中暑 / Heat Stroke
[JOI Open 2024] 中暑 / Heat Stroke
Problem Description
JOI Island consists of districts, numbered from to from west to east. There are roads on the island, numbered from to . Road () connects districts and 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 , there is a hospital in district with capacity people. Note that can be .
-
During the IOI event, when a person suffers heat stroke on road (), they will be taken to a hospital by the following procedure:
- Take the person to a hospital in district or district 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, people will suffer heat stroke one by one. The -th person () suffers heat stroke on road .
- For any , when the -th person suffers heat stroke, persons 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:
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 , consider the following:
- Send the first patient to the hospital in district . The numbers of patients in the hospitals of the three districts are then .
- Send the second patient to the hospital in district . The numbers of patients in the hospitals of the three districts are then .
- For the third patient, since the hospitals in districts and are both full, the only option is to use a helicopter to take them off the island.
In this case, person is taken off the island by helicopter. It can be proven that this is the maximum.
For sample , consider the following:
- Send the first patient to the hospital in district . The numbers of patients in the hospitals of the six districts are then .
- Send the second patient to the hospital in district . The numbers of patients in the hospitals of the six districts are then .
- Send the third patient to the hospital in district . The numbers of patients in the hospitals of the six districts are then .
- For the fourth patient, since the hospitals in districts and 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 . The numbers of patients in the hospitals of the six districts are then .
- For the sixth patient, since the hospitals in districts and 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 and are both full, the only option is to use a helicopter to take them off the island.
In this case, people are taken off the island by helicopter. It can be proven that this is the maximum.
Sample satisfies the conditions of subtasks .
Sample satisfies the conditions of subtasks .
Sample satisfies the conditions of subtasks .
Samples and satisfy the conditions of subtasks .
Constraints
- .
- ().
- .
- ().
- All input numbers are integers.
【Subtasks】
- ( points) .
- ( points) ().
- ( points) ().
- ( points) ().
- ( points) .
- ( points) .
- ( points) .
- ( points) No additional constraints.
Translated by Starrykiller from the English statement.
Translated by ChatGPT 5