Guessing

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

There are NN cards placed face down in a row. A positive integer less than or equal to mm is written on each card.

Let A_iA\_i be the integer written on the ii-th card.

Your goal is to guess A_1,,A_2,,,,A_NA\_1, \\, A\_2, \\, \cdots , \\, A\_N correctly.

The only operation you can do is:

  • choose the ii-th card and flip it to check the integer written on it. The cost of doing this is X_iX\_i.

Also, you are given the following information:

  • for each i=1,,2,,,,Mi = 1, \\, 2, \\, \cdots , \\, M, A_a_i+A_b_ic_i(modm)A\_{a\_i} + A\_{b\_i} \equiv c\_i \pmod m.

What is the minimum cost you need to pay to guess all of A_1,,A_2,,,,A_NA\_1, \\, A\_2, \\, \cdots , \\, A\_N correctly?

입력

The first line contains three integers NN, MM and mm.

The second line contains NN integers X_1,,X_2,,,,X_NX\_1, \\, X\_2, \\, \cdots, \\, X\_N.

This is followed by MM lines, the ii-th line contains three integers a_ia\_i, b_ib\_i and c_ic\_i.

출력

If the NN integers written on each card can't be determined no matter how much you pay(i.e. if there's a contradiction in the information), print only -1.

Otherwise, in the first line, print a single integer — the minimum cost you need to pay to guess all of A_iA\_i correctly.

In the second line, print NN integers A_1,,A_2,,,,A_NA^\prime\_1, \\, A^\prime\_2, \\, \cdots , \\, A^\prime\_N — one of the possibilities of A_1,,A_2,,,,A_NA\_1, \\, A\_2, \\, \cdots , \\, A\_N.

제한

  • 2 N2×1052 \le N \le 2 \times 10^5
  • 1M1061 \le M \le 10^6
  • 2m1092 \le m \le 10^9
  • 1X_i1041 \le X\_i \le 10^4 (1iN1 \le i \le N)
  • 1a_i,,b_iN1 \le a\_i, \\, b\_i \le N0c_i<m0 \le c\_i < m (1iM1 \le i \le M)
  • All values in input are integers.