James is fat, hungry, and mad at trees for existing.
To resolve his problems, he plans on deconstructing and eating a structure, called his tree, made out of marshmallows and toothpicks! His structure consists of marshmallows and
toothpicks, all connected such that by moving through marshmallows and along toothpicks, he can travel between any two marshmallows.
James is planning on eating his tree. However, he is a perfectionist, only consuming marshmallows and throwing out the toothpicks in a very specific fashion! He has a list of operations, which he will apply in the order that they are given in. Each operation is of one of the following forms:
- James will eat every marshmallow that has one or less toothpicks attached to it, throwing out the single toothpick if it exists.
- James will throw out the toothpick between marshmallow
and marshmallow
. If either of the marshmallows now has no toothpicks attached to it, he will eat it!
When running these operations, it is possible for James to become disappointed by his tree in the following ways:
- If there are no marshmallows left to eat when he runs an
operation.
- If either marshmallow
, marshmallow
, or the toothpick between them does not exist when he runs a
operation.
- If, following a
operation, the remaining toothpicks and marshmallows in his graph no longer form a tree.
After he is finished running his operations, he will eat all the remaining marshmallows in his tree anyways. As you may have noticed, James is in need of dieting, so he would like to minimize the total number of marshmallows he consumes. Can you find a tree that won't disappoint him, such that the initial number of marshmallows, , is minimized?
Constraints
If there is at least one operation, all unique marshmallows mentioned as
or
in
operations form a set of all integers in the range
for some integer
.
Subtask 1 [10%]
There will only be operations.
Subtask 2 [90%]
No additional constraints.
Input Specification
The first line of input contains a single integer .
The next lines contain an operation, as specified in the problem statement.
Output Specification
If a tree cannot be constructed to appease James, print -1
on a single line. You will receive an AC verdict if it was not possible, and WA otherwise.
The first line of output should be a single integer .
should be within the range
.
The next lines should contain two integers
and
, representing a toothpick that connects the marshmallows
and
.
If your output follows this format and your output describes a tree that will not disappoint James, you will receive an AC verdict.
If your tree receives an AC verdict, it will be awarded at least 30% of the points. For the remaining 70%, the tree's value must also be minimal.
Otherwise, you will receive a WA verdict.
Sample Input 1
8
L
D 8 7
D 4 8
D 4 6
D 4 5
L
D 1 3
D 1 2
Sample Output 1
12
1 2
2 11
11 12
1 3
3 5
5 4
4 6
6 10
4 8
8 7
7 9
Explanation for Sample 1
The tree the sample output represents can be visualized as follows:
The operations remove the following marshmallows from the tree, taking all toothpicks attached to them as they fall:
- The first
operation removes the marshmallows
,
, and
.
- The first
operation removes the marshmallow
.
- The second and third
operations remove the marshmallows
and
, respectively.
- The fourth
operation removes the marshmallow
.
- The second
operation removes the marshmallows
and
.
- The fifth
operation removes the marshmallow
.
- The sixth
operation removes the marshmallows
and
.
Note that this is not the only possible sample output that would receive an AC verdict.
Sample Input 2
3
D 1 2
D 2 3
D 1 3
Sample Output 2
-1
Explanation for Sample 2
It can be shown that it is not possible to construct a tree that will satisfy James.
Comments