The Sadness of Division

Split N members into two camps, respecting fixed members, to minimize the total weight of pairs split across camps, and output the lexicographically smallest camp A.

Medium7GraphMinimum spanning treeGreedyImplementationNo attempts yetTime limit1sMemory limit512 MB

Problem

A club has NN members. An old argument has split the club into camp A and camp B. Every member belongs to exactly one camp, and nobody belongs to both.

If member ii and member jj belong to different camps, that pair creates sadness w[i][j]w[i][j]. Some members hold their position so firmly that they must join camp A, and some must join camp B. Others do not care which camp they join.

Divide the NN members between the two camps so that the total sadness is minimal.

Input

The first line contains the number of members NN (1N5001 \le N \le 500).

The second line contains NN integers. If the ii-th number is 1, member ii must join camp A; if it is 2, member ii must join camp B; if it is 0, member ii may join either camp.

Each of the next NN lines contains NN integers. The jj-th number on line i+2i+2 is w[i][j]w[i][j]. The input always satisfies w[i][j]=w[j][i]w[i][j] = w[j][i] and w[i][i]=0w[i][i] = 0, and every w[i][j]w[i][j] is a non-negative integer no larger than 1000.

Output

Print the minimum total sadness on the first line.

On the second line print the numbers of the members in camp A in increasing order, separated by single spaces. On the third line print the numbers of the members in camp B the same way. If a camp holds nobody, print an empty line for it.

Several divisions can reach the minimum total sadness, so only one of them counts as correct. Put a member in camp A only if that member belongs to camp A in every division that reaches the minimum, and put everyone else in camp B. The division built this way also reaches the minimum total sadness, and its camp A is contained in the camp A of every other minimal division, so exactly one division satisfies the rule.