Pick Heroes
Contest: Mail.Ru Cup 2018 Round 3
Difficulty rating: 1700
Tags: greedy, implementation, interactive, sortings

If you say that Arkady is a bit old-fashioned playing checkers, you won't be right. There is also a modern computer game Arkady and his friends are keen on. We won't discuss its rules, the only feature important to this problem is that each player has to pick a distinct hero in the beginning of the game.

There are $$$2$$$ teams each having $$$n$$$ players and $$$2n$$$ heroes to distribute between the teams. The teams take turns picking heroes: at first, the first team chooses a hero in its team, after that the second team chooses a hero and so on. Note that after a hero is chosen it becomes unavailable to both teams.

The friends estimate the power of the $$$i$$$-th of the heroes as $$$p_i$$$. Each team wants to maximize the total power of its heroes. However, there is one exception: there are $$$m$$$ pairs of heroes that are especially strong against each other, so when any team chooses a hero from such a pair, the other team must choose the other one on its turn. Each hero is in at most one such pair.

This is an interactive problem. You are to write a program that will optimally choose the heroes for one team, while the jury's program will play for the other team. Note that the jury's program may behave inefficiently, in this case you have to take the opportunity and still maximize the total power of your team. Formally, if you ever have chance to reach the total power of $$$q$$$ or greater regardless of jury's program choices, you must get $$$q$$$ or greater to pass a test.

Input format:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^3$$$, $$$0 \le m \le n$$$) — the number of players in one team and the number of special pairs of heroes.

The second line contains $$$2n$$$ integers $$$p_1, p_2, \ldots, p_{2n}$$$ ($$$1 \le p_i \le 10^3$$$) — the powers of the heroes.

Each of the next $$$m$$$ lines contains two integer $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 2n$$$, $$$a \ne b$$$) — a pair of heroes that are especially strong against each other. It is guaranteed that each hero appears at most once in this list.

The next line contains a single integer $$$t$$$ ($$$1 \le t \le 2$$$) — the team you are to play for. If $$$t = 1$$$, the first turn is yours, otherwise you have the second turn.

Hacks

In order to hack, use the format described above with one additional line. In this line output $$$2n$$$ distinct integers from $$$1$$$ to $$$2n$$$ — the priority order for the jury's team. The jury's team will on each turn select the first possible hero from this list. Here possible means that it is not yet taken and does not contradict the rules about special pair of heroes.

Interaction format:
When it is your turn, print a single integer $$$x$$$ ($$$1 \le x \le 2n$$$) — the index of the hero chosen by you. Note that you can't choose a hero previously chosen by either you of the other player, and you must follow the rules about special pairs of heroes.

When it is the other team's turn, read a line containing a single integer $$$x$$$ ($$$1 \le x \le 2n$$$) — the index of the hero chosen by the other team. It is guaranteed that this index is not chosen before and that the other team also follows the rules about special pairs of heroes.

After the last turn you should terminate without printing anything.

After printing your choice do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:

- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.

Jury's answer $$$-1$$$ instead of a valid choice means that you made an invalid turn. Exit immediately after receiving $$$-1$$$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example the first turn is yours. In example, you choose $$$6$$$, the other team is forced to reply with $$$2$$$. You choose $$$5$$$, the other team chooses $$$4$$$. Finally, you choose $$$3$$$ and the other team choose $$$1$$$.

In the second example you have the second turn. The other team chooses $$$6$$$, you choose $$$5$$$, forcing the other team to choose $$$1$$$. Now you choose $$$4$$$, the other team chooses $$$3$$$ and you choose $$$2$$$.

Example input:
3 1
1 2 3 4 5 6
2 6
1

2

4

1

Example output:
6

5

3

Example input:
3 1
1 2 3 4 5 6
1 5
2
6

1

3

Example output:
5

4

2

Editorial:
First suppose we have the first turn. Let's look at what total power we can grab. There are two types of heroes: those who have a special pair and those who don't.
In each pair, one of the heroes will go into your team and one into the other. So the maximum we can get here is the sum of powers of the best in corresponding pairs heroes.
Now consider only the heroes that don't have pairs. I claim that you and your opponent will pick heroes from this list alternating turns. Indeed, picking a "paired" hero doesn't change the turns order because the opponent will also pick a "paired" hero. From this point it is easy to see that the best you can get from "unpaired" heroes is the sum of powers of the first, the third, the fifth and so on heroes, if sorted in decreasing order of power.
Now that we have proved the maximum constraint on our total power, let's build an algorithm that always reaches this power. If the first turn is ours, we have the initiative and the natural idea is not to lose it. Indeed, we can first pick all best "paired" heroes and then just keep picking the best remaining hero till the end. Since in the first part the opponent has to follow the rule about "paired" heroes, he won't be able to prevent us from taking the desired sum of power.
If our turn is the second one, we don't have any option until all "paired" heroes are gone or the opponent gives the initiative back to us choosing an "unpaired" hero. Starting from this point we can use the same strategy as if we had the first turn.
The solution can be implemented in linear time.