Social Monsters
Time limit3sMemory limit512 MB
Choose K monsters maximizing total friendship, where given pairs have weight c (0 means the pair is forbidden) and all other pairs weigh 0. Each monster appears in at most two given pairs.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Greedy, Implementation
- Solved
- No attempts yet
Problem
There is a world where strange creatures and humans help each other and live together. In this world, tournaments where people pit the monsters they have caught against each other are held often, and many boys and girls aim to become the world's best trainer.
In a tournament, you build a party from the monsters you have caught and battle other parties. A pair of monsters has an affinity, which is either very bad or normal. If the affinity is very bad, the two monsters of that pair cannot be put in the same party. If the affinity is normal, the strength of the affinity is expressed by a number called friendship. In a battle, the sum of the friendship over the whole party decides the outcome.
Now you want to build a party of monsters out of monsters. The affinities of pairs of monsters are known, each given as an integer . If , the -th monster and the -th monster have a very bad affinity. If , the -th monster and the -th monster have a normal affinity, and their friendship is . For any pair other than the given pairs, the affinity is normal and the friendship is .
Think about how to choose the party so that the sum of friendship is as large as possible.
Input
The input is given in the following format.
Output
If a party of monsters cannot be built, output "Impossible" on one line.
If a party can be built, output the maximum sum of friendship.
Constraints
- Each monster appears at most twice in .