CosmoCraft

Time limit1sMemory limit128 MB

Summary
Decide how to split income among workers, facilities, and army each turn so all attacks are survived and the final army is as large as possible.
Level

Hard8 of 10

Topics
Greedy, Simulation, Math, Implementation
Solved
No attempts yet

Problem

In the two-player strategy game CosmoCraft you build up an economy so that you can field an army strong enough to defeat your opponent. You manage workers, production facilities, and army units, and the whole game is about how you divide your money among them. The game is played in turns.

  • Each worker gives you an income of 1 dollar per turn.
  • Each production facility can, once per turn, produce either one army unit or one worker, at a cost of 1 dollar (a single facility makes at most one unit per turn).
  • Creating a new production facility costs 1 dollar.
  • Your army is what you fight your opponent with.

You start with nn workers and kk production facilities. On each turn you first collect income equal to your current number of active workers (added to any money carried over from earlier turns), and then you may spend that money on any mix of new workers, new army units, and new production facilities. Workers and facilities created on a turn only become active on the next turn, while army units are available immediately. Any money you do not spend carries over to the next turn.

The opponent attacks you at the end of each of the first t−1t-1 turns. To survive the attack of strength aia_i on turn ii, the number of army units you have at that moment must be at least aia_i; surviving then removes aia_i units from your army (you keep the surplus). If you have fewer than aia_i units you are defeated. The game lasts tt turns, and there is no incoming attack on the final turn.

You have faced this opponent so often that you know exactly how strong each of their attacks will be. You decide to play purely defensively: survive every attack while stockpiling the largest possible army for a final counterattack. What is the largest army you can have at the end of the game if you must survive all of the opponent's attacks?

Input

The input contains several test cases. Each test case begins with a line of three integers:

n k t

where nn (1≤n≤1001 \le n \le 100) is the number of workers you start with, kk (1≤k≤1001 \le k \le 100) is the number of production facilities you start with, and tt (1≤t≤100001 \le t \le 10000) is the number of turns. The next line contains t−1t-1 integers aia_i (0≤ai≤263−10 \le a_i \le 2^{63}-1) separated by single spaces, where aia_i is the strength of the opponent's attack (the number of army units they use) at the end of turn ii. The input ends with a line containing three zeros.

Output

For each test case, output a single integer: the largest army you can have at the end of the game. Output −1-1 if it is impossible to survive every attack. Print each answer on its own line, with no extra spaces and no blank lines between answers. Every valid input is guaranteed to yield an answer that fits in a signed 64-bit integer.

Examples1

  1. Example 1

    Input
    8 4 6
    22 6 10 14 0 
    4 3 3
    0 0 
    6 9 7
    0 0 11 0 7 0 
    0 0 0
    
    Expected output
    -1
    11
    101