Problems
Bessie is going on a trip in Cowland, which has
A trip of length
For each town, Bessie wants to know the longest possible trip starting at it. For some starting owns, there are multiple longest trips - out of these, she prefers the trip with the lexicographically minimum sequence of road labels. A sequence is lexicographically smaller than another sequence of the same length if, at the first position in which they differ, the first sequence has a smaller element than the second sequence.
Output the length and sum of road labels of Bessie's preferred trip starting at each town.
Input
The first line contains
The next
Output
Output N lines. The ith should contain two space-separated integers, the length and sum of road labels of Bessie's preferred trip starting at town i.
Subtask
| # | Score | Condition |
|---|---|---|
| #1 | 10 | All labels are the same. |
| #2 | 20 | All labels are distinct. |
| #3 | 30 | N,M≤5000 |
| #4 | 40 | No additional constraints. |
Example #1
4 5
4 3 10
4 2 10
3 1 10
2 1 10
4 1 10
0 0
1 10
1 10
2 20
Example #2
4 5
4 3 4
4 2 2
3 1 5
2 1 10
4 1 1
0 0
1 10
1 5
2 12
In the following explanation, we let
There are several trips starting from vertex
Example #3
4 5
4 3 2
4 2 2
3 1 5
2 1 10
4 1 1
0 0
1 10
1 5
2 7
Example #4
4 5
4 3 2
4 2 2
3 1 10
2 1 5
4 1 1
0 0
1 5
1 10
2 7