Under construction

Still being built. Things may change

Driving Practice

Time limit1sMemory limit1024 MB

Summary
For each station where the wizard may appear, find the teleport station that lets the car run out of electricity farthest from the origin, and print it with the charge change.
Level

Medium7 of 10

Topics
Greedy, Prefix sum, Stack
Solved
No attempts yet

Problem

Sieun, who holds a license that has never been used on the road, practices driving on a number line. Sieun starts at the origin and always moves in the positive direction. The car uses 1 kWh of electricity for each unit of distance it travels, and moving is the only way it uses electricity. The car starts with no electricity, and a car with no electricity cannot move until it is charged.

There are N+1N + 1 charging stations, numbered from 0 to NN, on the number line. Station ii is at point AiA_i, and visiting it charges the car by FiF_i kWh. One of the stations other than station 0 has a mischievous wizard hidden at it. When Sieun arrives at that station, the wizard teleports Sieun to one of the stations that come before it (that is, closer to the origin). Sieun chooses which station to teleport to. Teleporting uses no electricity, and the wizard plays this trick only once.

The car cannot be charged at the station where the wizard plays the trick. After being teleported, Sieun can charge at the stations again. For example, suppose Sieun meets the wizard at station 3 and chooses to teleport to station 1. Then the car can be charged in this order: 0 to 1 to 2 to (no charge at station 3 because of the wizard) to 1 to 2 to 3 to ...

Assume Sieun meets the wizard at each station in turn. Find which station Sieun should choose so that the car runs out of electricity as far from the origin as possible.

Input

The first line contains NN, the number of charging stations excluding station 0.

Each of the next N+1N + 1 lines contains the information for station ii, in order of increasing number from station 0: AiA_i and FiF_i, separated by a space.

Output

Print NN lines in total. On line ii, print two integers xix_i and yiy_i separated by a space (1≤i≤N1 \le i \le N).

If station ii cannot be reached without meeting the wizard:

  • xix_i and yiy_i are both -1.

Otherwise:

  • xix_i is the number of the station Sieun should choose when meeting the wizard at station ii.
  • yiy_i is the change in the electricity stored in the car when Sieun teleports to station xix_i and then returns to station ii.
  • If several values of xix_i satisfy the condition, print the largest one.

Constraints

  • 1≤N≤200,0001 \le N \le 200{,}000
  • A0=0A_0 = 0
  • A0<A1<A2<⋯<AN≤1012A_0 < A_1 < A_2 < \cdots < A_N \le 10^{12}
  • 1≤Fi≤1061 \le F_i \le 10^6 (0≤i≤N0 \le i \le N)
  • The car can be charged with an unlimited amount of electricity.
  • All input numbers are integers.

Examples2

  1. Example 1

    Input
    3
    0 2
    2 5
    6 8
    100 1
    
    Expected output
    0 0
    1 1
    -1 -1
    
  2. Example 2

    Input
    5
    0 5
    3 1
    6 5
    10 2
    13 8
    15 2
    
    Expected output
    0 2
    0 0
    2 1
    2 0
    4 6