#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 9;
int arr[N];
string convert(long long int n) {
  string temp = "";
  while (n) {
    if (n % 2) {
      temp += '1';
    } else
      temp += '0';
    n = n / 2;
  }
  reverse(temp.begin(), temp.end());
  return temp;
}
long long int power(long long int n) {
  long long int res = 1;
  while (n) {
    res = res * 2;
    n--;
  }
  return res;
}
long long int binary(string s) {
  long long int res = 0;
  long long int cnt = 0;
  for (int i = s.size() - 1; i >= 0; --i) {
    if (s[i] == '1') res += power(cnt);
    cnt++;
  }
  return res;
}
int n;
long long int dp[80][16008][80];
long long int go(int i, int have, int candy) {
  if (i == n) {
    return have - 1000;
  }
  long long int &ret = dp[i][have][candy];
  if (~ret) return ret;
  ret = go(i + 1, have, candy);
  if (candy > 0) {
    ret = max(ret, max(go(i, have + arr[i], candy - 1) + arr[i],
                       go(i + 1, have + arr[i], candy - 1) + arr[i]));
  }
  if (have >= arr[i]) {
    ret = max(ret, max(go(i, have - arr[i], candy + 1) - arr[i],
                       go(i + 1, have - arr[i], candy + 1) - arr[i]));
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  cout << go(0, 1000, 0);
  return 0;
}
