Problem J
Jealous Youngsters

Tom has been studying the behavior of the kids and now
understands exactly under what circumstances they start crying.
First, a child will cry if they have no toy to play with.
Second, even having a toy, a child
-
Envious: children envy those who have played more than them with any given toy. If child
played strictly more with toy yesterday than child did, then will be envious of for playing with today. -
Inflexible: a child would rather play with some toy it played with yesterday, and the earlier they first played with it yesterday, the more they want to play with it today. All toys that a child did not play with at all yesterday are equally (un)desirable to the child.
-
Cannot multitask: a child never plays with more than one toy at a time.
-
Uncooperative: children are bad at sharing, and two children never play with the same toy at the same time.
Tom has recorded which toys were played with by which kid yesterday, taking note of when each kid started playing with a toy. Using this information, Tom aims to make one fixed assignment of toys for all of today that, if possible, prevents any crying.
Input
The first line of input contains two integers
Then follow
The events are given in non-decreasing order of time (i.e.,
the values of
Output
If there is an assignment of toys to kids such that no kid
will start crying today, output
Sample Input 1 | Sample Output 1 |
---|---|
2 3 6 7 0 1 1 0 2 2 1 1 3 2 1 2 2 2 1 3 2 3 4 2 1 |
1 2 |
Sample Input 2 | Sample Output 2 |
---|---|
2 1 20 3 0 1 1 10 1 0 10 2 1 |
impossible |
Sample Input 3 | Sample Output 3 |
---|---|
1 3 3 3 0 1 1 1 1 2 2 1 3 |
2 |