#P10527. [XJTUPC 2024] 最后一块石头的重量

[XJTUPC 2024] 最后一块石头的重量

Problem Description

There is a pile of stones represented by an integer array aa, where aia_i is the weight of the ii-th stone.

In each round, choose any two stones and smash them together. Suppose their weights are xx and yy, with xyx \le y. Then the possible results are:

  • If x=yx = y, then both stones will be completely smashed.
  • If xyx \neq y, then the stone of weight xx will be completely smashed, and the stone of weight yy will have a new weight of yxy-x.

In the end, at most one stone will remain. Output the minimum possible weight of this stone. If no stone remains, output 00.

Input Format

The input has two lines.

The first line contains an integer nn (1n100001 \leq n \leq 10000), representing the number of stones.

The second line contains nn integers aia_i (1ai50001 \leq a_i \leq 5000), representing the weight of the ii-th stone.

Output Format

The output has one line.

Output one integer representing the answer.

6
2 7 4 1 8 1

1

5
31 26 33 21 40

5

Hint

Translated by ChatGPT 5