#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define INF 10000000000000000ll
const int exp3[] = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441};
int n, bit_size;
ll a[24], y[4096], bit[4097];
inline void bit_init(int n){
bit_size = n;
for(int i=0; i<=bit_size; i++){
bit[i] = -INF;
}
}
inline ll bit_max(int i){
ll r = -INF;
for(; i; i-=i&-i){
r = max(r, bit[i]);
}
return r;
}
inline void bit_update(int i, ll x){
for(; i<=bit_size && bit[i]<x; i+=i&-i){
bit[i] = x;
}
}
struct jiken{
bool query;
ll x, y;
jiken(bool q=false, ll x=0, ll y=0): query(q), x(x), y(y){}
bool operator <(const jiken &b) const{
if(x-y == b.x-b.y){
return !query && b.query;
}
return x-y < b.x-b.y;
}
}sl[1062882];
ll loli(){
int m=0, ysz=0;
ll s = 0;
for(int i=0; i<=n-1; i++) s += a[i];
for(int i=0; i<exp3[n/2]; i++){
ll p=0, q=0;
for(int j=0, t=i; j<=n/2; j++, t/=3){
if(t%3 == 1){
p += 3*a[j];
}else if(t%3 == 2){
q += 3*a[j];
}
}
sl[m++] = jiken(false, p, q);
}
for(int i=0; i<(1<<n/2); i++){
ll t = 0;
for(int j=0; j<=n/2-1; j++){
t += 3*(i>>j&1)*a[j];
}
y[ysz++] = t;
}
sort(y, y+ysz);
ysz = unique(y, y+ysz)-y;
for(int i=0; i<exp3[(n+1)/2]; i++){
ll p=0, q=0;
for(int j=0, t=i; j<=(n+1)/2-1; j++, t/=3){
if(t%3 == 1){
p += 3*a[n/2+j];
}else if(t%3 == 2){
q += 3*a[n/2+j];
}
}
sl[m++] = jiken(true, s-p, s-q);
}
sort(sl, sl+m);
ll ans = INF;
bit_init(ysz);
for(int i=0; i<=m-1; i++){
int j = upper_bound(y, y+ysz, sl[i].y)-y;
if(sl[i].query){
ans = min(ans, 2*sl[i].x+sl[i].y-bit_max(j));
}else{
bit_update(j, 2*sl[i].x+sl[i].y);
}
}
return ans/3;
}
int main(){
scanf("%d", &n);
for(int i=0; i<=n-1; i++) scanf("%lld", a+i);
ll ans = loli();
for(int i=0; i<=n-1; i++) a[i] = -a[i];
ans = min(ans, loli());
printf("%lld\n", ans);
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwojZGVmaW5lIElORiAxMDAwMDAwMDAwMDAwMDAwMGxsCgpjb25zdCBpbnQgZXhwM1tdID0gezEsIDMsIDksIDI3LCA4MSwgMjQzLCA3MjksIDIxODcsIDY1NjEsIDE5NjgzLCA1OTA0OSwgMTc3MTQ3LCA1MzE0NDF9OwppbnQgbiwgYml0X3NpemU7CmxsIGFbMjRdLCB5WzQwOTZdLCBiaXRbNDA5N107CmlubGluZSB2b2lkIGJpdF9pbml0KGludCBuKXsKICAgIGJpdF9zaXplID0gbjsKICAgIGZvcihpbnQgaT0wOyBpPD1iaXRfc2l6ZTsgaSsrKXsKICAgICAgICBiaXRbaV0gPSAtSU5GOwogICAgfQp9CmlubGluZSBsbCBiaXRfbWF4KGludCBpKXsKICAgIGxsIHIgPSAtSU5GOwogICAgZm9yKDsgaTsgaS09aSYtaSl7CiAgICAgICAgciA9IG1heChyLCBiaXRbaV0pOwogICAgfQogICAgcmV0dXJuIHI7Cn0KaW5saW5lIHZvaWQgYml0X3VwZGF0ZShpbnQgaSwgbGwgeCl7CiAgICBmb3IoOyBpPD1iaXRfc2l6ZSAmJiBiaXRbaV08eDsgaSs9aSYtaSl7CiAgICAgICAgYml0W2ldID0geDsKICAgIH0KfQpzdHJ1Y3QgamlrZW57CiAgICBib29sIHF1ZXJ5OwogICAgbGwgeCwgeTsKICAgIGppa2VuKGJvb2wgcT1mYWxzZSwgbGwgeD0wLCBsbCB5PTApOiBxdWVyeShxKSwgeCh4KSwgeSh5KXt9CiAgICBib29sIG9wZXJhdG9yIDwoY29uc3QgamlrZW4gJmIpIGNvbnN0ewogICAgICAgIGlmKHgteSA9PSBiLngtYi55KXsKICAgICAgICAgICAgcmV0dXJuICFxdWVyeSAmJiBiLnF1ZXJ5OwogICAgICAgIH0KICAgICAgICByZXR1cm4geC15IDwgYi54LWIueTsKICAgIH0KfXNsWzEwNjI4ODJdOwoKbGwgbG9saSgpewogICAgaW50IG09MCwgeXN6PTA7CiAgICBsbCBzID0gMDsKICAgIGZvcihpbnQgaT0wOyBpPD1uLTE7IGkrKykgcyArPSBhW2ldOwogICAgZm9yKGludCBpPTA7IGk8ZXhwM1tuLzJdOyBpKyspewogICAgICAgIGxsIHA9MCwgcT0wOwogICAgICAgIGZvcihpbnQgaj0wLCB0PWk7IGo8PW4vMjsgaisrLCB0Lz0zKXsKICAgICAgICAgICAgaWYodCUzID09IDEpewogICAgICAgICAgICAgICAgcCArPSAzKmFbal07CiAgICAgICAgICAgIH1lbHNlIGlmKHQlMyA9PSAyKXsKICAgICAgICAgICAgICAgIHEgKz0gMyphW2pdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHNsW20rK10gPSBqaWtlbihmYWxzZSwgcCwgcSk7CiAgICB9CiAgICBmb3IoaW50IGk9MDsgaTwoMTw8bi8yKTsgaSsrKXsKICAgICAgICBsbCB0ID0gMDsKICAgICAgICBmb3IoaW50IGo9MDsgajw9bi8yLTE7IGorKyl7CiAgICAgICAgICAgIHQgKz0gMyooaT4+aiYxKSphW2pdOwogICAgICAgIH0KICAgICAgICB5W3lzeisrXSA9IHQ7CiAgICB9CiAgICBzb3J0KHksIHkreXN6KTsKICAgIHlzeiA9IHVuaXF1ZSh5LCB5K3lzeikteTsKICAgIGZvcihpbnQgaT0wOyBpPGV4cDNbKG4rMSkvMl07IGkrKyl7CiAgICAgICAgbGwgcD0wLCBxPTA7CiAgICAgICAgZm9yKGludCBqPTAsIHQ9aTsgajw9KG4rMSkvMi0xOyBqKyssIHQvPTMpewogICAgICAgICAgICBpZih0JTMgPT0gMSl7CiAgICAgICAgICAgICAgICBwICs9IDMqYVtuLzIral07CiAgICAgICAgICAgIH1lbHNlIGlmKHQlMyA9PSAyKXsKICAgICAgICAgICAgICAgIHEgKz0gMyphW24vMitqXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzbFttKytdID0gamlrZW4odHJ1ZSwgcy1wLCBzLXEpOwogICAgfQogICAgc29ydChzbCwgc2wrbSk7CiAgICBsbCBhbnMgPSBJTkY7CiAgICBiaXRfaW5pdCh5c3opOwogICAgZm9yKGludCBpPTA7IGk8PW0tMTsgaSsrKXsKICAgICAgICBpbnQgaiA9IHVwcGVyX2JvdW5kKHksIHkreXN6LCBzbFtpXS55KS15OwogICAgICAgIGlmKHNsW2ldLnF1ZXJ5KXsKICAgICAgICAgICAgYW5zID0gbWluKGFucywgMipzbFtpXS54K3NsW2ldLnktYml0X21heChqKSk7CiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIGJpdF91cGRhdGUoaiwgMipzbFtpXS54K3NsW2ldLnkpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBhbnMvMzsKfQoKaW50IG1haW4oKXsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGZvcihpbnQgaT0wOyBpPD1uLTE7IGkrKykgc2NhbmYoIiVsbGQiLCBhK2kpOwogICAgbGwgYW5zID0gbG9saSgpOwogICAgZm9yKGludCBpPTA7IGk8PW4tMTsgaSsrKSBhW2ldID0gLWFbaV07CiAgICBhbnMgPSBtaW4oYW5zLCBsb2xpKCkpOwogICAgcHJpbnRmKCIlbGxkXG4iLCBhbnMpOwogICAgcmV0dXJuIDA7Cn0K