Heat Stroke
Time limit2sMemory limit1024 MB
Given hospital capacities along a line and a sequence of patients on roads, find the maximum number of patients sent by helicopter when both nearby hospitals are full.
- Level
Hard8 of 10
- Topics
- Segment tree, Dynamic programming, Divide and conquer
- Solved
- No attempts yet
Problem
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 in both directions.

The International Olympiad in Informatics (IOI 20XX) will be held on JOI Island. The island is known for its extreme heat, and foreign contestants who are not acclimatized to hot weather are at high risk of heat stroke. The organizers of IOI have taken the following measures.
- First, for each (), they set up a hospital at district with capacity . Some hospitals have .
- When a person on road () gets heat stroke, the patient is sent to a hospital as follows.
- The patient is sent to the hospital in district or district , whichever is not full. If both are not full, either one may be chosen. If both are full, the patient is sent by helicopter to a general hospital outside the island.
Helicopter transport is expensive, so the organizers want to estimate the maximum number of patients who will be sent by helicopter. The scenario is as follows.
- Before the event, no hospital has any patients.
- During the event, people get heat stroke. The -th patient () occurs on road .
- For each (), when patient gets heat stroke, patients through have already been sent to hospitals. Because the symptoms are severe, no patient leaves a hospital during the event.
Given the number of districts, the hospital capacities, and the patients, write a program that computes the maximum number of patients sent by helicopter in this scenario.
Input
Read the following data from standard input.
L
C_1 C_2 ... C_L
N
X_1 X_2 ... X_N
Output
Write one line to standard output containing the maximum number of patients sent by helicopter.
Constraints
- .
- ().
- .
- ().
- All given values are integers.