Galactic Railway

Interview

Time limit5sMemory limit512 MB

Summary
Add up to M edges between N galaxies one at a time; after each edge print the total planet count in the merged component.
Level

Medium6 of 10

Topics
Union-find, Graph, Prefix sum, Implementation
Solved
No attempts yet

Problem

A galaxy contains many planets. Technological progress has made it possible for every planet in a galaxy to travel to any other.

Today, a galactic railway connecting our galaxy to another one 80,000 light-years away finally opens.

Residents of every planet in the galaxy are excited at the prospect of traveling to more planets once the galactic railway opens.

The space railway authority G-Express has announced its future galactic railway plans.

Since space is vast, G-Express wants to report how many planets have become able to reach each other each time galaxies are connected.

You work on the technology development team at G-Express, and this program request has landed on your desk. Given the number of planets in each galaxy and the railway plan, build a program that reports, in real time, the number of planets reachable via the railways.

Input

The first line gives the number of galaxies NN and the number of railways MM.

Starting from the second line, NN lines give the number of planets in each of the NN galaxies, in order from galaxy 1. (The unit for counting planets is a trillion, 101210^{12}.)

Then, starting from line N+2N+2, MM lines give railways connecting pairs of galaxies. Multiple railways can be built between the same pair of galaxies.

The input satisfies 2≤N≤100,0002 \le N \le 100{,}000 and 1≤M≤100,0001 \le M \le 100{,}000. Each galaxy has at most 100 trillion planets, and no galaxy has zero planets.

Output

Each time a railway is connected, print the number of planets reachable via that railway, one value per line.

Hint

Because the input data is large, using fast input and output is recommended.

Examples2

  1. Example 1

    Input
    5 4
    3
    9
    10
    11
    15
    1 2
    2 3
    4 5
    4 3
    
    Expected output
    12
    22
    26
    48
    
  2. Example 2

    Input
    5 4
    3
    1
    4
    15
    9
    1 2
    3 1
    2 3
    2 4
    
    Expected output
    4
    8
    8
    23