This page is still under construction.

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

Emergency Rations

Time limit1sMemory limit128 MB

Summary
Choose boxes with a capacity and an expiry day to eat one unit daily starting day 1; report the last reachable day and the fewest boxes needed.
Level

Medium7 of 10

Topics
Greedy, Sorting, Intervals, Binary search
Solved
No attempts yet

Problem

You have prepared for the coming apocalypse by building an underground bunker. Now you need to fill that bunker with food and other rations so you can last as long as possible.

Emergency rations come in boxes, and every box is the same size. Each box has two properties: (1) how many units of consumption it holds (DiD_i), and (2) how many days after the apocalyptic event it expires, after which it can no longer be consumed (its expiration date EiE_i).

You must consume your first unit of rations on day 11 and then exactly one unit every day after that, without a break. A box with expiration date XX that holds YY units can be consumed on up to YY different days, and only on days less than or equal to XX. For example, a box with expiration date 33 worth 22 units can be consumed on days 11 and 22, or days 22 and 33, or days 11 and 33, or even on day 33 alone (leaving the box only partially consumed).

You want to know the last day DD on which you can still eat without ever breaking your daily streak. Because every box is the same size and you do not want to waste bunker space, you also want the minimum number of boxes BB needed to last through day DD.

Input

The first line contains the number of data sets KK. Then KK data sets follow, each in the form below.

The first line of each data set contains the integer NN (1≤N≤10001 \le N \le 1000), the number of emergency ration boxes you may choose from to store in your bunker. Two lines of NN integers each follow. The ii-th integer on the first line is the number of days DiD_i that box ii will last you (that is, how many units it can be consumed for), and the ii-th integer on the second line is the expiration date EiE_i of box ii. Every DiD_i and EiE_i is between 11 and 1,000,000,0001{,}000{,}000{,}000 inclusive.

Output

For each data set, first output Data Set x: on a line by itself, where xx is its number. On the next line, output two integers DD and BB separated by a single space, where DD is the last day you can consume rations and BB is the minimum number of boxes needed to reach day DD. Output a blank line after each data set.

Examples3

  1. Example 1

    Input
    3
    1
    10
    7
    5
    6 4 7 5 2
    3 2 11 7 1
    3
    1 1 1
    3 1 2
    
    Expected output
    Data Set 1:
    7 1
    
    Data Set 2:
    11 2
    
    Data Set 3:
    3 3
    
  2. Example 2

    Input
    1
    1
    3
    10
    
    Expected output
    Data Set 1:
    3 1
    
  3. Example 3

    Input
    1
    1
    100
    5
    
    Expected output
    Data Set 1:
    5 1