cswork / OAOpen practice workspace

FactSetOA #2

Last Stone Weight

Estimated EasyJudge-ready

Topics (AI-labelled)Stacks & Heaps

Each turn, take the two heaviest stones and smash them together. If their weights are equal, both are destroyed; otherwise the lighter one is destroyed and the heavier one is left with the difference of the two weights. Repeat until at most one stone remains, then output the weight of the last stone; output 0 if there are no stones.

The problem rules, examples, and n/weight constraints are complete.

This site adds the standard input/output protocol.

Input

The first line contains n (1 ≤ n ≤ 100000), and the second line contains n positive integers weights[i] (1 ≤ weights[i] ≤ 10^9).

Output

Output the weight of the last remaining stone; output 0 if all stones are destroyed.

Example 1

Input:

6
1 2 3 6 7 7

Output:

0

Example 2

Input:

1
2

Output:

2

Example 3

Input:

2
5 5

Output:

0

Editorial

Loading solution…