#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define pb push_back
#define mk make_pair
#define ins insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define X first
#define Y second
#define umap unordered_map
#define speed() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define setvalue(d,s,e,n) for(int qwe = s; qwe < e; ++qwe) d[qwe] = n
#define mset multiset
#define pqueue priority_queue
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 314;
const int INF = 1e9;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const long long LINF = 1e18 + 3141;
int n;
int a[N];
int dps[70][N];
int dpp[70][N];
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> a[i];
for(int i = a[n]; i <= 30; ++i) {
dps[i + 30][n] = max(a[n], 0);
}
for(int i = n - 1; i >= 1; --i) {
for(int j = a[i]; j <= 30 ; ++j) {
// <= j
int mx = 0;
for(int k = -30; k <= j; k++) {
mx = :: max(dps[k + 30][i + 1], mx);
}
dps[j + 30][i] = max(0, a[i] + mx) ;
}
}
for(int i = a[1]; i <= 30; ++i) {
dpp[i + 30][1] = max(a[1], 0);
}
for(int i = 2; i <= n; ++i) {
for(int j = a[i]; j <= 30 ; ++j) {
// <= j
int mx = 0;
for(int k = -30; k <= j; k++) {
mx = :: max(dpp[k + 30][i - 1], mx);
}
dpp[j + 30][i] = max(0, a[i] + mx) ;
}
}
int ans = 0;
for(int i = 1; i <= n; ++i) {
ans = max(ans, dpp[a[i] + 30][i - 1] + dps[a[i] + 30][i + 1]);
}
cout << ans;
}
int main(){
speed();
int t = 1;
// cin >> t;
while(t--)solve();
}