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

    ID: 11975 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>2024O2优化高校校赛

[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 x≤yx \le y. Then the possible results are:

  • If x=yx = y, then both stones will be completely smashed.
  • If x≠yx \neq y, then the stone of weight xx will be completely smashed, and the stone of weight yy will have a new weight of y−xy-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 (1≤n≤100001 \leq n \leq 10000), representing the number of stones.

The second line contains nn integers aia_i (1≤ai≤50001 \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