#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<set>
using namespace std;
multiset<long long>k;
long long a[1005] = { 0 };
int main()
{
int n;
long long ans = 100000000, kk;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] += a[i - 1];
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
k.insert(a[j] - a[i - 1]);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
auto p = k.find(a[j] - a[i - 1]);
k.erase(p);
}
for (int j = 1; j <= i; j++)
{
kk = a[j] - a[i - 1];
auto p = k.lower_bound(kk);
if (p != k.end()) ans = min(ans, abs(*p - kk));
if (p != k.begin()) p--, ans = min(ans, abs(*p - kk));
}
}
cout << ans << endl;
return 0;
}