This page is still under construction.

Parts of this page are still being built. What you see may change.

Array Game

Time limit1sMemory limit128 MB

Summary
The player shifts all numbers left or right each turn to maximize the total signed value collected when numbers land on fixed plus and minus cells.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

Problem

A one player game runs on a one dimensional array that stretches without end in both directions. Each cell holds an integer, a + sign, a - sign, or nothing at all. On every turn the player shifts all of the integers that are still in the array one cell to the left or one cell to the right. The signs never move.

The score starts at 0. When an integer II lands on a cell that holds a sign SS, that integer leaves the array and the score grows by S×IS \times I, where + counts as +1+1 and - counts as −1-1.

The player may stop the game at any moment.

The picture below shows the starting array and the two arrays that follow it after two moves to the right.

Given the starting array, find the largest score the player can reach.

Input

The input holds several test cases. The first line of a test case gives NN (1≤N≤1001 \le N \le 100), the number of integers, then NpN_p (1≤Np≤1001 \le N_p \le 100), the number of + signs, then NmN_m (1≤Nm≤1001 \le N_m \le 100), the number of - signs. Each of the next NN lines gives two integers pip_i (−300≤pi≤300-300 \le p_i \le 300), the position, and viv_i (−9≤vi≤9-9 \le v_i \le 9), the value of the ii-th integer. The next line gives the positions of the NpN_p + signs, and the line after it gives the positions of the NmN_m - signs. Every position lies between −300-300 and 300300, and no two elements, integers and signs alike, start on the same position. A line holding 0 0 0 ends the input.

Output

For each test case print one line with the largest score the player can reach.

Examples1

  1. Example 1

    Input
    3 2 1
    0 2
    6 -1
    3 5
    5 9
    1
    1 1 1
    10 5
    3
    7
    0 0 0
    
    Expected output
    3
    0