#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> nums;
map<vector<long long>, long long> mp;
long long solve(int p, long long rem, long long bought, int limit) {
  if (p == n) return rem;
  if (mp.count({p, rem, bought, limit})) return mp[{p, rem, bought, limit}];
  long long ans = solve(p + 1, rem, bought, 0);
  if (rem >= nums[p] and limit != 1)
    ans = max(ans, solve(p, rem - nums[p], bought + 1, 2));
  if (bought > 0 and limit != 2)
    ans = max(ans, solve(p, rem + nums[p], bought - 1, 1));
  mp[{p, rem, bought, limit}] = ans;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  nums.resize(n);
  for (int i = 0; i < n; i++) cin >> nums[i];
  cout << solve(0, 1000, 0, 0) << endl;
  return 0;
}
