#HK4164. 「JOI Open 2024」中暑
「JOI Open 2024」中暑
Statement
JOI Island consists of districts, numbered from to from west to east. There are roads in the island, numbered from to . Road () connects districts and 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 (), they prepare a hospital at district with capacity of people. Note that there are cases that .
- During the IOI event, when a person on road () gets heat stroke, they send to hospital in the following procedure:
- They send the patient to the hospital of either district or , 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, people will get heat stroke in JOI Island. The -th () patient occurs on road .
- For each (), when the -th patient gets heat stroke, the -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.
Input
Read the following data from the standard input.
Output
Write one line to the standard output. The output should contain the maximum number of patients to be sent by helicopter.
Constraints
- .
- ().
- .
- ().
- Given values are all integers.
Subtasks
- (6 points) .
- (7 points) ().
- (7 points) ().
- (25 points) ().
- (25 points) .
- (10 points) .
- (15 points) .
- (5 points) No additional constraints.
3
1 1 1
3
1 2 2
1
If the following case happens, patient will be sent by helicopter.
- Send the 1st patient to the hospital at district 2. Then, the number of patients in hospital at district 1, 2, 3 will be 0, 1, 0, respectively.
- Send the 2nd patient to the hospital at district 3. Then, the number of patients in hospital at district 1, 2, 3 will be 0, 1, 1, respectively.
- For the 3rd patient, since both hospitals at district 2 and 3 are full, send the patient to a general hospital outside the island by helicopter.
Since there are no cases that or more patients will be sent by helicopter, output 1. This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 5, 6, 7, 8.
6
1 1 1 1 1 1
7
1 3 5 4 2 2 3
3
If the following case happens, patient will be sent by helicopter.
- Send the 1st patient to the hospital at district 2. Then, the number of patients in hospital at district 1, 2, 3, 4, 5, 6 will be 0, 1, 0, 0, 0, 0, respectively.
- Send the 2nd patient to the hospital at district 4. Then, the number of patients in hospital at district 1, 2, 3, 4, 5, 6 will be 0, 1, 0, 1, 0, 0, respectively.
- Send the 3rd patient to the hospital at district 5. Then, the number of patients in hospital at district 1, 2, 3, 4, 5, 6 will be 0, 1, 0, 1, 1, 0, respectively.
- For the 4th patient, since both hospitals at district 4 and 5 are full, send the patient to a general hospital outside the island by helicopter.
- Send the 5th patient to the hospital at district 3. Then, the number of patients in hospital at district 1, 2, 3, 4, 5, 6 will be 0, 1, 1, 1, 1, 0, respectively.
- For the 6th patient, since both hospitals at district 2 and 3 are full, send the patient to a general hospital outside the island by helicopter.
- For the 7th patient, since both hospitals at district 3 and 4 are full, send the patient to a general hospital outside the island by helicopter.
Since there are no cases that or more patients will be sent by helicopter, output 3.
This sample input satisfies the constraints of Subtasks 2, 3, 4, 5, 6, 7, 8.
6
4000 1 1 0 4000 1
5
1 1 2 3 5
1
This sample input satisfies the constraints of Subtasks 1, 5, 6, 7, 8.
5
1 2 2 2 1
8
2 3 2 1 4 1 2 3
2
This sample input satisfies the constraints of Subtasks 5, 6, 7, 8.
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
This sample input satisfies the constraints of Subtasks 5, 6, 7, 8.