Problems > Fairest Split

Fairest Split

Difficulty: 1500 4000 ms 128 MB

bitwise brute-force

Split all N apples (with given weights) into two groups minimizing the absolute difference of group sums. Print that minimum difference.

Input: the first line has N; the second line has N weights.

Output: the minimum |sum1 − sum2|.

Constraints:

  • 1 ≤ N ≤ 16
  • 1 ≤ wi ≤ 109

Sample Tests

Input 1
5
3 2 7 4 1
Output 1
1
Input 2
1
5
Output 2
5

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.