#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"n,*a=map(int,open(0).read().split())\n"
"for i in range(n-1):a[i+1]+=a[i]\n"
"j,k,m=0,2,1e18\n"
"for i in range(1,n-1):\n"
" while abs(a[j]*2-a[i])>abs(a[j+1]*2-a[i]):j+=1\n"
" while abs(a[k]*2-a[i]-a[n-1])>abs(a[k+1]*2-a[i]-a[n-1]):k+=1\n"
" t=a[j],a[i]-a[j],a[k]-a[i],a[n-1]-a[k];m=min(m,max(t)-min(t))\n"
"print(m)\n"
"\"";
  system(s);
  return 0;
}            