#K1626. Divide Crates for Minimal Weight Difference

    ID: 24539 Type: Default 1000ms 256MiB

Divide Crates for Minimal Weight Difference

Divide Crates for Minimal Weight Difference

You are given n crates with specific weights. Your task is to partition the crates into two groups such that the absolute difference between the total weights of the two groups is minimized. This problem can be formulated as follows:

minimize S1S2\text{minimize } |S_1 - S_2|

where

S1+S2=i=1nwi.S_1 + S_2 = \sum_{i=1}^{n} w_i.

Solve this problem using an efficient algorithm based on dynamic programming.

inputFormat

The input is read from stdin and consists of two lines. The first line contains an integer n (1 ≤ n ≤ 1000) representing the number of crates. The second line contains n space-separated integers, each representing the weight of a crate.

outputFormat

Output to stdout a single integer which is the minimum possible difference between the total weights of the two groups.## sample

5
1 2 3 4 5
1