SAMER08G - Pole Position

no tags 

In car races, there is always a high pole next to the finish line of the track. Before the race starts, the pole is used to display the starting grid. The number of the first car in the grid is displayed at the top of the pole, the number of the car in second place is shown below that, and so on.

During the race, the pole is used to display the current position of each car: the car that is winning the race has its number displayed at the top of the pole, followed by the car that is in second place, and so on.

Besides showing the current position of a car, the pole is also used to display the number of positions the cars have won or lost, relative to the starting grid. This is done by showing, side by side to the car number, an integer number. A positive value v beside a car number in the pole means that car has won v positions relative to the starting grid. A negative value v means that car has lost v positions relative to the starting grid. A zero beside a car number in the pole means the car has neither won nor lost any positions relative to the starting grid (the car is in the same position it started).


subir imagenes


We are in the middle of the Swedish Grand Prix, the last race of the World Championship. The race director, Dr. Shoo Makra, is getting worried: there have been some complaints that the software that controls the pole position system is defective, showing information that does not reflect the true race order.

Dr. Shoo Makra devised a way to check whether the pole system is working properly. Given the information currently displayed in the pole, he wants to reconstruct the starting grid of the race. If it is possible to reconstruct a valid starting grid, he plans to check it against the real starting grid. On the other hand, if it is not possible to reconstruct a valid starting grid, the pole system is indeed defective.

Can you help Dr. Shoo Makra?


The input contains several test cases. The first line of a test case contains one integer N indicating the number of cars in the race (2 ≤ N ≤ 103). Each of the next N lines contains two integers C and P, separated by one space, representing respectively a car number (1 ≤ C ≤ 104) and the number of positions that car has won or lost relative to the starting grid ( -106P ≤ 106), according to the pole system. All cars in a race have different numbers.

The end of input is indicated by a line containing only one zero.


For each test case in the input, your program must print a single line, containing the reconstructed starting grid, with car numbers separated by single spaces. If it is not possible to reconstruct a valid starting grid, the line must contain only the value -1.


1 0
3 1
2 -1
4 0
22 1
9 1
13 0
21 -2
19 1
9 -345
17 0
2 2
8 0
5 -2
7 1
1 1
9 1
3 -3

1 2 3 4
5 8 2 3 7 1 9

hide comments
vengatesh15: 2017-02-14 10:35:39

easy one ...

gautam: 2016-04-15 16:21:09 50th....;-)

(Tjandra Satria Gunawan)(曾毅昆): 2015-08-19 13:37:28

Think simply, no need to implement complicated data structure :)

Shanks: 2014-05-13 22:14:58

its simple really.. read the problem statement carefully :)

J. Rewieñski: 2013-12-29 23:02:53

@Maanick Baasha stated the problem, how is it possible to gain milion places if theres a maximum of thousand cars :D?

Shashi Kant Prasad: 2013-07-18 10:23:22

Last edit: 2013-07-19 03:36:33
dhanno power!! B-): 2013-07-11 01:43:59

don't put extra's a simple problem.!

Bharath Reddy: 2013-06-14 16:51:34

check for array bounds(both upper and lower).. if you are using arrays in C++

mukund: 2013-03-28 19:12:39

nice problem

Akhil Rao: 2013-01-26 15:05:21

can anyone explain me the testcases ??

Added by:Diego Satoba
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource:South American Regional Contests 2008