#K40272. Minimum Difference Partition

    ID: 26606 Type: Default 1000ms 256MiB

Minimum Difference Partition

Minimum Difference Partition

You are given a bookshelf with N books, where the thickness of each book is provided as an integer in a list B. Your task is to divide the bookshelf into two sections such that the absolute difference between the total thickness of each section is minimized.

Let \( S = \sum_{i=1}^{N} B_i \) be the total thickness of all books. You need to choose a subset of books whose total thickness is \( j \) (with \( j \leq \lfloor S/2 \rfloor \)) and minimize the difference between \( S- j \) and \( j \), which is given by:

[ \text{difference} = S - 2j ]

If there are no books (i.e., \(N = 0\)), consider the answer to be 0. If only one book exists, the answer is simply its thickness.

inputFormat

The first line of input contains a single integer \(N\) indicating the number of books. If \(N > 0\), the second line contains \(N\) space-separated integers representing the thickness of each book.

For example:

5
1 3 2 4 5

outputFormat

Output a single integer representing the minimum possible absolute difference between the two sections.

For example:

1
## sample
5
1 3 2 4 5
1