The Sadness of Division
Time limit1sMemory limit512 MB
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.
- Level
Medium7 of 10
- Topics
- Graph, Minimum spanning tree, Greedy, Implementation
- Solved
- No attempts yet
Problem
A club has 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 and member belong to different camps, that pair creates sadness . 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 members between the two camps so that the total sadness is minimal.
Input
The first line contains the number of members ().
The second line contains integers. If the -th number is 1, member must join camp A; if it is 2, member must join camp B; if it is 0, member may join either camp.
Each of the next lines contains integers. The -th number on line is . The input always satisfies and , and every 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.