Taking Items
Time limit1sMemory limit1024 MB
Each item may require others first, cycles mean all-or-nothing; pick a feasible set of items maximizing total mood change.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Greedy, Implementation
- Solved
- No attempts yet
Problem
Gukryeol went to a shooting range somewhere in Sinchon where he can obtain items. The range has N items, numbered 1 through N. If he shoots an item, he can take it.
Taking items comes with a restriction. Each item has items he must obtain beforehand, and he can take that item only after obtaining all of them. Otherwise he cannot take the item even if he shoots it. For example, suppose A can be taken only after obtaining both B and C, and C can be taken only after obtaining D first. Then to take A he must obtain B, C, and D, in that order, before taking A.
The order in which he shoots does not matter. For example, suppose A can be taken only after obtaining B, and B can be taken only after obtaining A. Then it does not matter whether he obtains B first and then takes A, or obtains A first and then takes B. That is, in this case he either takes neither or takes both.
Obtaining each item changes Gukryeol's mood by a certain amount. Obtaining one may fill him with disgust and revulsion and lower his mood, or it may fill him with joy and raise his mood.
He has as many bullets as there are items, and Gukryeol is a master of shooting, so he can always hit whatever he wants. Find the set of items that gives the maximum possible mood.
Input
The first line gives N and M, the number of items and the number of relations between items. (1 ≤ N ≤ 200, 1 ≤ M ≤ 400)
The second line through the N + 1-th line each give an integer ti, the change in mood when the corresponding item is obtained. (−100,000 ≤ ti ≤ 100,000)
The N + 2-th line through the N + M + 1-th line each give two distinct integers si, ei describing a relation between items. It means that to obtain item si, he must obtain ei. (1 ≤ si, ei ≤ N)
Output
On the first line, print the number of items to be obtained. On the next line, print the numbers of those items separated by spaces.
If there are multiple answers, print any of them.