CF BUDDY
← Problems·

990F · Flow Control

2400 · dfs and similar, dp, greedy

Problem: You have to handle a very complex water distribution system. The system consists of nn junctions and mm pipes, ii-th pipe connects junctions xix_i and yiy_i.

The only thing you can do is adjusting the pipes. You have to choose mm integer numbers f1f_1, f2f_2, ..., fmf_m and use them as pipe settings. ii-th pipe will distribute fif_i units of water per second from junction xix_i to junction yiy_i (if fif_i is negative, then the pipe will distribute fi|f_i| units of water per second from junction yiy_i to junction xix_i). It is allowed to set fif_i to any integer from 2109-2 \cdot 10^9 to 21092 \cdot 10^9.

In order for the system to work properly, there are some constraints: for every i[1,n]i \in [1, n], ii-th junction has a number sis_i associated with it meaning that the difference between incoming and outcoming flow for ii-th junction must be exactly sis_i (if sis_i is not negative, then ii-th junction must receive sis_i units of water per second; if it is negative, then ii-th junction must transfer si|s_i| units of water per second to other junctions).

Can you choose the integers f1f_1, f2f_2, ..., fmf_m in such a way that all requirements on incoming and outcoming flows are satisfied?

Input Format: The first line contains an integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of junctions.

The second line contains nn integers s1,s2,,sns_1, s_2, \dots, s_n (104si104-10^4 \le s_i \le 10^4) — constraints for the junctions.

The third line contains an integer mm (0m21050 \le m \le 2 \cdot 10^5) — the number of pipes.

ii-th of the next mm lines contains two integers xix_i and yiy_i (1xi,yin1 \le x_i, y_i \le n, xiyix_i \ne y_i) — the description of ii-th pipe. It is guaranteed that each unordered pair (x,y)(x, y) will appear no more than once in the input (it means that there won't be any pairs (x,y)(x, y) or (y,x)(y, x) after the first occurrence of (x,y)(x, y)). It is guaranteed that for each pair of junctions there exists a path along the pipes connecting them.

Output Format: If you can choose such integer numbers f1,f2,,fmf_1, f_2, \dots, f_m in such a way that all requirements on incoming and outcoming flows are satisfied, then output "Possible" in the first line. Then output mm lines, ii-th line should contain fif_i — the chosen setting numbers for the pipes. Pipes are numbered in order they appear in the input.

Otherwise output "Impossible" in the only line.

Sample Cases

Case 1

Input

4
3 -10 6 1
5
1 2
3 2
2 4
3 4
3 1

Output

Possible
4
-6
8
-7
7

Case 2

Input

4
3 -10 6 4
5
1 2
3 2
2 4
3 4
3 1

Output

Impossible

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback