#include <bits/stdc++.h>
int main() {
  int n, a[2000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  long long int maxi = 0;
  long long int dest_a[2000];
  int diff[2000];
  for (int i = 0; i < n; i++) {
    long long int cur_happy = maxi;
    int maxi_j = i;
    for (int j = i - 1; j >= 0; j--) {
      cur_happy += a[i];
      if (diff[j] >= 0)
        cur_happy += dest_a[j];
      else
        cur_happy -= dest_a[j];
      if (cur_happy > maxi) {
        maxi = cur_happy;
        maxi_j = j;
      }
    }
    for (int j = i; maxi_j < j; j--) {
      dest_a[j] = dest_a[j - 1];
      diff[j] = diff[j - 1] + 1;
    }
    dest_a[maxi_j] = a[i];
    diff[maxi_j] = maxi_j - i;
  }
  printf("%lld\n", maxi);
}
