Guessing
Time limit2sMemory limit1024 MB
Given modular sum equations between hidden card values, find the minimum flip cost that determines all values and output one consistent assignment.
Problem
There are cards in a row, all face down. Each card has a positive integer at most written on it.
Let be the integer written on the -th card.
Your goal is to guess correctly.
The only operation you can perform is:
- Choose the -th card and flip it to check the integer written on it. This costs .
You are also given the following information:
- For each , .
What is the minimum cost needed to guess all of correctly?
Input
The first line contains three integers , , and .
The second line contains integers .
The following lines describe the information. The -th line contains three integers , , and .
Output
If the integers on the 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 correctly on the first line.
On the second line, print integers , one possible assignment for , separated by spaces.
Constraints
- ()
- ; ()
- All values in the input are integers.