#include <bits/stdc++.h>
using namespace std;
long long int dp[81][10000][100];
long long int solve(long long int i, long long int j, long long int n,
                    vector<long long int> v, long long int sum) {
  if (i == n) return dp[i][sum][j] = sum;
  if (dp[i][sum][j] != -1) return dp[i][sum][j];
  long long int curr = 0, best = sum, cnt = 0;
  best = max(best, solve(i + 1, j, n, v, sum));
  while (1) {
    curr += v[i], cnt++;
    if (curr <= sum) best = max(best, solve(i + 1, j + cnt, n, v, sum - curr));
    if (cnt <= j) best = max(best, solve(i + 1, j - cnt, n, v, sum + curr));
    if (cnt > j && curr > sum) break;
  }
  return dp[i][sum][j] = best;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  vector<long long int> v(n);
  for (long long int i = 0; i < n; i++) cin >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0, n, v, 1000) << endl;
  return 0;
}
