#P10627. Maximizing Helicopter Transports on JOI Island

    ID: 12653 Type: Default 1000ms 256MiB

Maximizing Helicopter Transports on JOI Island

Maximizing Helicopter Transports on JOI Island

JOI Island is composed of \(L\) districts numbered from \(1\) to \(L\) from west to east. There are \(L-1\) bidirectional roads, numbered from \(1\) to \(L-1\); road \(i\) connects district \(i\) and district \(i+1\) for \(1 \le i \le L-1\).

For the upcoming IOI 20XX held on JOI Island, each district \(i\) has a hospital with capacity \(C_i\) (which can be zero). During the event, \(N\) people will suffer heatstroke one after the other. The \(j\)-th person (\(1 \le j \le N\)) suffers heatstroke on road \(X_j\) (with \(1 \le X_j \le L-1\)). When a person suffers heatstroke on road \(x\), the following procedure is applied:

  • If at least one of the hospitals in district \(x\) or district \(x+1\) has not reached its capacity, the patient is sent to one of them. (If both hospitals have available capacity, the organizers may choose arbitrarily.)
  • If both hospitals are already full, the patient is sent via helicopter to a hospital off the island.

At the start of the event, all hospitals are empty, and no patient is discharged during the event. The organizers want to estimate the maximum possible number of patients who might need a helicopter, by choosing the assignments optimally whenever there is a choice.

Input and output format: See below.

inputFormat

The input is given as follows:

L
C_1 C_2 ... C_L
N
X_1 X_2 ... X_N

Where:

  • \(L\) (\(2 \le L\)) is the number of districts.
  • \(C_i\) (\(0 \le C_i\)) is the capacity of the hospital in district \(i\).
  • \(N\) (\(1 \le N\)) is the number of people who suffer heatstroke.
  • \(X_j\) (\(1 \le X_j \le L-1\)) indicates that the \(j\)-th person suffers heatstroke on road \(X_j\).

outputFormat

Output a single integer: the maximum possible number of patients that will require a helicopter.

sample

2
1 1
2
1 1
0