#C3877. The Optimal Dragon Order
The Optimal Dragon Order
The Optimal Dragon Order
You are given a knight with an initial power p
and a sequence of n dragons. Each dragon is characterized by two integers: its strength s
and its time cost t
. The knight must confront the dragons in a certain order.
The knight can defeat a dragon if and only if his current power is at least the dragon's strength, i.e., \( p \ge s \). When the knight defeats a dragon, his power decreases by the dragon's strength. The prescribed strategy is to face the dragons in an order sorted by increasing strength, and if two dragons have the same strength, then by increasing time cost.
If the knight is able to defeat all dragons in this order, output the order of their indices (1-indexed) as a space-separated list. Otherwise, output IMPOSSIBLE
.
inputFormat
The input is read from stdin
and consists of several test cases. The first line contains an integer T
denoting the number of test cases.
For each test case, the first line contains two integers n
and p
, where n
is the number of dragons and p
is the knight's initial power. This is followed by n
lines, each containing two integers s
and t
representing the strength and the time cost of a dragon respectively.
T n p s1 t1 s2 t2 ... sn tn
outputFormat
For each test case, output a single line. If the knight can defeat all dragons in the prescribed order, output the order of dragon indices (based on their initial order) separated by a space. Otherwise, output IMPOSSIBLE
. The result should be written to stdout
.
2
3 15
3 1
5 3
2 2
2 10
6 5
10 15
3 1 2
IMPOSSIBLE
</p>