JBIRDS - Jeremías y sus loros

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

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

hide comments
2023-01-06 15:38:33
There is no need to make an array also.
2021-08-05 22:21:52
Array won't work use vectors
2018-12-02 10:33:43
With simple array implementation you can do it in 0(n)...
2018-10-06 23:08:33 Vadim
Is there a solution better than O(n^2)? Given limits(N <= 10000 and wi <= 1000) look too large
2018-07-02 17:40:02
simple recursion works.
2018-07-02 09:55:44 Muhammad Faishol Amirul Mukminin
The same problem
https://uva.onlinejudge.org/external/5/562.pdf

but with different constraint.

Last edit: 2018-07-02 09:56:33
2018-06-29 01:43:29 mehmetin
It turns out that n <= 10 and sum <= 100
2018-06-27 11:17:57
DP nailed it :)
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
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.