#C2094. Merge Fish Parties
Merge Fish Parties
Merge Fish Parties
You are given several fish parties, each having an initial number of fish. With a series of merging events, two parties combine and the resulting party size is the sum of the two merging parties. After each event, you need to output the size of the resulted merged party. If two parties have already been merged in a previous event, simply output the current size of that merged group.
More formally, suppose there are \(p\) fish parties with initial sizes \(S_1, S_2, \ldots, S_p\). Each merging event is given as a pair of indices \(X\) and \(Y\) (1-indexed). When merging parties \(X\) and \(Y\), the resulting size is computed as: \[ S_{merged} = S_X + S_Y \] If either party has been part of a previous merge, consider its updated size. You must process all events in order and print the merged sizes accordingly.
inputFormat
The input is given via standard input (stdin) and has the following format:
p m S1 S2 ... Sp X1 Y1 X2 Y2 ... Xm Ym
Here:
- \(p\) is the number of fish parties.
- \(m\) is the number of merging events.
- \(S_i\) is the initial size of the \(i\)-th fish party.
- Each of the next \(m\) lines contains two integers \(X\) and \(Y\) (1-indexed), indicating the parties to be merged.
outputFormat
After processing each merging event, output the size of the merged party on a separate line in standard output (stdout).
## sample3 2
5 10 15
1 2
2 3
15
30
</p>