Under construction

Still being built. Things may change

Guessing

Time limit2sMemory limit1024 MB

Summary
Given modular sum equations between hidden card values, find the minimum flip cost that determines all values and output one consistent assignment.
Level

Medium6 of 10

Topics
Graph, DFS, Math
Solved
No attempts yet

Problem

There are NN cards in a row, all face down. Each card has a positive integer at most mm written on it.

Let AiA_i be the integer written on the ii-th card.

Your goal is to guess A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N correctly.

The only operation you can perform is:

  • Choose the ii-th card and flip it to check the integer written on it. This costs XiX_i.

You are also given the following information:

  • For each i=1,2,⋯ ,Mi = 1, 2, \cdots, M, Aai+Abi≡ci(modm)A_{a_i} + A_{b_i} \equiv c_i \pmod m.

What is the minimum cost needed to guess all of A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N correctly?

Input

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

The second line contains NN integers X1,X2,⋯ ,XNX_1, X_2, \cdots, X_N.

The following MM lines describe the information. The ii-th line contains three integers aia_i, bib_i, and cic_i.

Output

If the integers on the NN cards cannot be determined no matter how much you pay (that is, if the given information is contradictory), print only -1.

Otherwise, print the minimum cost needed to guess all AiA_i correctly on the first line.

On the second line, print NN integers A1′,A2′,⋯ ,AN′A'_1, A'_2, \cdots, A'_N, one possible assignment for A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces.

Constraints

  • 2≤N≤2×1052 \le N \le 2 \times 10^5
  • 1≤M≤1061 \le M \le 10^6
  • 2≤m≤1092 \le m \le 10^9
  • 1≤Xi≤1041 \le X_i \le 10^4 (1≤i≤N1 \le i \le N)
  • 1≤ai,bi≤N1 \le a_i, b_i \le N; 0≤ci<m0 \le c_i < m (1≤i≤M1 \le i \le M)
  • All values in the input are integers.

Examples2

  1. Example 1

    Input
    5 2 7
    1 2 1 3 3
    1 3 6
    4 5 0
    
    Expected output
    6
    5 2 1 3 4
    
  2. Example 2

    Input
    2 2 3
    3 5
    1 2 0
    1 2 1
    
    Expected output
    -1