Under construction

Still being built. Things may change

Survivor (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Pick which foods to eat and in what order, respecting each shelf life, to maximize total survival time.
Level

Medium5 of 10

Topics
Backtracking, Dynamic programming
Solved
No attempts yet

Problem

You are shipwrecked on a deserted island. You saved one box of food, but the island is bare rock with no plants and you have no way to fish, so no more food arrives.

The box holds NN foods. For each food ii you know the remaining shelf life PiP_i, counted from now, and the time it holds off hunger SiS_i once you eat it.

The rules for eating are these.

  • You eat the first food right now, at minute 0.
  • PiP_i and SiS_i are both measured in minutes.
  • Food past its shelf life is thrown out at once. Food ii can only be eaten at a time of at most PiP_i minutes, and a food whose remaining shelf life is 0 must be eaten right now or thrown out.
  • You eat nothing while hunger is held off. If you eat food ii at minute tt, the next food goes down at exactly minute t+Sit + S_i.
  • If hunger arrives and no food is left to eat, you starve on the spot.

Choose which foods to eat and in what order, then report the longest time you survive on the island. If the last food is eaten at minute tt and its value is SS, the survival time is t+St + S minutes.

Input

The first line holds the number of test cases TT. Each test case follows in this format.

N
P1 S1
P2 S2
...
PN SN

Constraints

  • Every input value is an integer.
  • 1≤T≤1001 \le T \le 100
  • 1≤N≤101 \le N \le 10
  • 0≤Pi≤1000 \le P_i \le 100
  • 1≤Si≤1001 \le S_i \le 100

Output

For each test case xx, print one line in the form Case #x: y, where yy is the longest time you survive on the island in that test case.

Examples5

  1. Example 1

    Input
    2
    3
    3 4
    0 4
    4 4
    3
    3 9
    0 4
    4 4
    
    Expected output
    Case #1: 8
    Case #2: 9
    
  2. Example 2

    Input
    1
    1
    0 1
    
    Expected output
    Case #1: 1
    
  3. Example 3

    Input
    3
    1
    100 100
    1
    0 100
    1
    50 7
    
    Expected output
    Case #1: 100
    Case #2: 100
    Case #3: 7
    
  4. Example 4

    Input
    2
    4
    0 3
    0 9
    0 1
    0 5
    3
    0 100
    0 100
    0 100
    
    Expected output
    Case #1: 9
    Case #2: 100
    
  5. Example 5

    Input
    2
    2
    10 1
    5 100
    2
    5 100
    10 1
    
    Expected output
    Case #1: 101
    Case #2: 101