Under construction

Still being built. Things may change

Heat Stroke

Time limit2sMemory limit1024 MB

Summary
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 LL districts, numbered from 11 to LL from west to east. There are L−1L-1 roads in the island, numbered from 11 to L−1L-1. Road ii (1≤i≤L−11 ≤ i ≤ L-1) connects districts ii and i+1i+1 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 ii (1≤i≤L1 ≤ i ≤ L), they set up a hospital at district ii with capacity CiC_i. Some hospitals have Ci=0C_i = 0.
  • When a person on road xx (1≤x≤L−11 ≤ x ≤ L-1) gets heat stroke, the patient is sent to a hospital as follows.
    • The patient is sent to the hospital in district xx or district x+1x+1, 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, NN people get heat stroke. The jj-th patient (1≤j≤N1 ≤ j ≤ N) occurs on road XjX_j.
  • For each jj (1≤j≤N−11 ≤ j ≤ N-1), when patient j+1j+1 gets heat stroke, patients 11 through jj 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

  • 2≤L≤80002 ≤ L ≤ 8000.
  • 0≤Ci≤80000 ≤ C_i ≤ 8000 (1≤i≤L1 ≤ i ≤ L).
  • 1≤N≤80001 ≤ N ≤ 8000.
  • 1≤Xj≤L−11 ≤ X_j ≤ L-1 (1≤j≤N1 ≤ j ≤ N).
  • All given values are integers.

Examples5

  1. Example 1

    Input
    3
    1 1 1
    3
    1 2 2
    
    Expected output
    1
    
  2. Example 2

    Input
    6
    1 1 1 1 1 1
    7
    1 3 5 4 2 2 3
    
    Expected output
    3
    
  3. Example 3

    Input
    6
    4000 1 1 0 4000 1
    5
    1 1 2 3 5
    
    Expected output
    1
    
  4. Example 4

    Input
    5
    1 2 2 2 1
    8
    2 3 2 1 4 1 2 3
    
    Expected output
    2
    
  5. Example 5

    Input
    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
    
    Expected output
    3