Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Premier Round 9
  3. 27E

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

27E. Chopsticks

2750 pts·3s·256 MB

Constraints

Input

Output

Samples

Input
t
2
Case 1
2 1
5 5
1 2
Case 2
3 2
9 6 2
1 2
2 3
Explanation
Case 1

The two chopsticks already have equal lengths, so nothing is removed.

Case 2

Eating sushi 1 shortens chopstick 1 from 9 to 6. Eating sushi 2 then shortens chopstick 2 from 6 to 2. The total removed length is 3 + 4 = 7.

Output
Case 1
0
1
Case 2
7
1 2
Input
t
3
Case 1
3 2
9 6 2
1 2
1 3
Case 2
3 2
8 8 3
1 2
1 3
Case 3
5 5
12 9 6 3 6
1 2
1 3
2 4
3 5
1 5
Explanation
Case 1

Eating sushi 2 first shortens chopstick 1 from 9 to 2. Sushi 1 then shortens chopstick 2 from 6 to 2, removing 11 units in total. The opposite order would shorten chopstick 1 twice and is not allowed.

Case 2

Eating sushi 1 changes nothing. Sushi 2 then shortens chopstick 1 from 8 to 3. Only 5 units are removed, which is unavoidable when eating sushi 2.

Case 3

Sushi 4 changes nothing. The remaining sushi in the shown order shorten chopsticks 2, 1, 3, and 5 to length 3, removing 6, 9, 3, and 3 units, respectively. Every chopstick is shortened at most once, and the minimum total removed length is 21.

Output
Case 1
11
2 1
Case 2
5
1 2
Case 3
21
4 3 1 2 5

You have nnn chopsticks, numbered from 111 to nnn. Initially, chopstick iii has length aia_iai​.

There are mmm pieces of sushi, numbered from 111 to mmm. Sushi jjj must be eaten using chopsticks uju_juj​ and vjv_jvj​.

When you eat a piece of sushi, compare the current lengths of its two chopsticks. If they differ, shorten the longer chopstick until the two lengths are equal. If they are equal, neither chopstick is changed.

Each chopstick may be used any number of times, but it may be shortened at most once in total.

Find an order in which to eat all the sushi that minimizes the total length removed from the chopsticks. Output this minimum total and one optimal order. If several optimal orders exist, output any of them.

Different pieces of sushi may require the same pair of chopsticks. A valid order always exists.

  • 1≤t≤1041 \le t \le 10^41≤t≤104
  • 2≤n≤2×1052 \le n \le 2 \times 10^52≤n≤2×105
  • 1≤m≤2×1051 \le m \le 2 \times 10^51≤m≤2×105
  • 1≤ai≤1091 \le a_i \le 10^91≤ai​≤109
  • 1≤uj,vj≤n1 \le u_j, v_j \le n1≤uj​,vj​≤n
  • uj≠vju_j \ne v_juj​=vj​
  • The sums of nnn and mmm over all test cases do not exceed 2×1052 \times 10^52×105.
tnma1a2⋯anu1v1u2v2⋮umvm}×tt \\[0.5em] \left. \begin{array}{l} n \quad m \\ a_1 \quad a_2 \quad \cdots \quad a_n \\ u_1 \quad v_1 \\ u_2 \quad v_2 \\ \vdots \\ u_m \quad v_m \end{array} \right\} \times ttnma1​a2​⋯an​u1​v1​u2​v2​⋮um​vm​​⎭⎬⎫​×t

For each test case, print the minimum total length removed as answer\mathrm{answer}answer on the first line. On the second line, print a permutation p1,p2,…,pmp_1, p_2, \ldots, p_mp1​,p2​,…,pm​ of 1,2,…,m1,2,\ldots,m1,2,…,m, where sushi pip_ipi​ is eaten on turn iii. This order must attain the printed minimum and shorten each chopstick at most once. If several optimal orders exist, print any of them.

answerp1p2⋯pm}×t\left. \begin{array}{l} \mathrm{answer} \\ p_1 \quad p_2 \quad \cdots \quad p_m \end{array} \right\} \times tanswerp1​p2​⋯pm​​}×t