#C10251. Minimum Difference Partition

    ID: 39436 Type: Default 1000ms 256MiB

Minimum Difference Partition

Minimum Difference Partition

You are given an integer M representing the number of levels in a game, and a list of M integers where each integer pi represents the points earned in the i-th level. The task is to partition the levels into two groups such that the absolute difference between the sum of points in the two groups is minimized.

Let \(S = \sum_{i=1}^{M} p_i\) be the total points. Find a subset of levels with a sum \(S_1\) as close as possible to \(\frac{S}{2}\). The answer is then given by \(|S - 2 \times S_1|\).

inputFormat

The input is provided via standard input (stdin) and consists of two lines:

  1. The first line contains an integer M, the number of game levels.
  2. The second line contains M space-separated integers, representing the points earned in each level.

outputFormat

Output a single integer via standard output (stdout), representing the minimum absolute difference between the points collected by the two groups.

## sample
4
1 4 2 7
0

</p>