var input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
  input += chunk;
})
process.stdin.on('end', function() {
  main()
});

function main () {
  var lines = input.split('\n');
  var n = lines[0] - 0;
  var a = [];
  var i,j,k;
  var sum = 0;
  for (i=0; i <n; i++) {
    a[i] = lines[i + 1] - 0;
    a[i + n] = a[i];
    sum += a[i];
  }
  
  var max = 0;
  var restMin = 0;
  var first, rest, second, third;
  for (j = 0;j < n;j++) {
    first = a[j];
    rest = sum - first;
    k = j + 2;
    second = a[j + 1];
    third = rest - second;
    while (k < j + n - 1 && second + a[k] <= third - a[k]) {
      second += a[k];
      third = rest - second;
      k++;
    }
    if((first <= second && first <= third) || (first <= second + a[k] && first <= third - a[k])) {
      max = Math.max(first, max);
    }
    for (i = 1;i < n - 2;i++){
      first += a[j + i];
      rest -= a[j + i];
      second -= a[j + i];
      
      while (k < j + n - 1 && second + a[k] <= third - a[k]) {
        second += a[k];
        third = rest -second;
        k++;
      }
      
      if((first <= second && first <= third) || (first <= second + a[k] && first <= third - a[k])) {
        max = Math.max(first, max);
      }
    }
  }
  console.log(max);

}