Array Game
Time limit1sMemory limit128 MB
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 lands on a cell that holds a sign , that integer leaves the array and the score grows by , where + counts as and - counts as .
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 (), the number of integers, then (), the number of + signs, then (), the number of - signs. Each of the next lines gives two integers (), the position, and (), the value of the -th integer. The next line gives the positions of the + signs, and the line after it gives the positions of the - signs. Every position lies between and , 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.