Problem list
Log in
#r03338 · Online Judge > GoldOJ

Guild Synergy

Gold IV 2.0 s 1024 MB

Each adventurer has a power; initially all are in separate guilds. Each command merges the guilds of two adventurers. A guild with powers $x_1, \dots, x_k$ has synergy $\sum_{i<j} x_i x_j$. After each command print the synergy of the guild the two adventurers now belong to. If they are already in the same guild, print that guild's current synergy.

Input

First line $N$ and $Q$; second line $N$ powers $A_i$; then $Q$ lines each with $a$, $b$ ($1 \le N, Q \le 10^5$, $1 \le A_i \le 1000$, $a \ne b$).

Output

Print the resulting synergy after each command, one per line.

Example 1

Input
5 3
2 4 3 5 1
1 2
3 4
2 3
Output
8
15
71

Example 2

Input
4 4
10 20 30 40
1 2
1 2
3 4
2 4
Output
200
200
1200
3500
Source
Online Judge > GoldOJ
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code