JBIRDS - Jeremías y sus loros

no tags 

The pirate Jeremiah plans to offer a show to his crew. For this, Jeremiah has N parrots, which he plans to balance on his shoulders. Each of the N parrots has a weight, in grams, which Jeremiah knows beforehand.

Jeremiah has only two shoulders, and therefore he must separate the parrots into two groups. The imbalance of Jeremiah will be equal to the (positive) difference in weight between the group that leads on the left shoulder and the one on the right shoulder.

What is the lowest value that the pirate's imbalance can take?

Input

The entry begins with a line containing a single integer, N (0 <= N <= 10000)

The following N lines describe the weights of the parrots. Each line contains a single integer wi (0 <= wi <= 1000)

Output

A line with a single integer, the minimum imbalance (in absolute value).

Example

Input:
4
2 1 5 3

Output:
1

hide comments
the_brokenhero: 2023-01-06 15:38:33

There is no need to make an array also.

rangey_18o3_20: 2021-08-05 22:21:52

Array won't work use vectors

y17prashant: 2018-12-02 10:33:43

With simple array implementation you can do it in 0(n)...

Vadim: 2018-10-06 23:08:33

Is there a solution better than O(n^2)? Given limits(N <= 10000 and wi <= 1000) look too large

anirudnits: 2018-07-02 17:40:02

simple recursion works.

Muhammad Faishol Amirul Mukminin: 2018-07-02 09:55:44

The same problem
https://uva.onlinejudge.org/external/5/562.pdf

but with different constraint.

Last edit: 2018-07-02 09:56:33
mehmetin: 2018-06-29 01:43:29

It turns out that n <= 10 and sum <= 100

prakash1108: 2018-06-27 11:17:57

DP nailed it :)

prakash1108: 2018-06-26 15:03:57

The pirate Jeremiah plans to offer a show to his crew. For this, Jeremiah has N parrots, which he plans to balance on his shoulders. Each of the N parrots has a weight, in grams, which Jeremiah knows beforehand.



Jeremiah has only two shoulders, and therefore he must separate the parrots into two groups. The imbalance of Jeremiah will be equal to the (positive) difference in weight between the group that leads on the left shoulder and the one on the right shoulder.



What is the lowest value that the pirate's imbalance can take?

Input
The entry begins with a line containing a single integer, N (0 <= N <= 10000)

The following N lines describe the weights of the parrots. Each line contains a single integer wi (0 <= wi <= 1000)

Output
A line with a single integer, the minimum imbalance (in absolute value).

Last edit: 2018-06-26 20:36:22

Added by:BerSub
Date:2018-06-25
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All