#include <iostream>
using namespace std;

const int N = 1100;

int f[N];
int a[N];

int main() {
    int n, ans = 0;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
    	cin >> a[i];
    	f[i] = a[i];
    	for(int j = 1;j < i;j++)
    	{
    		if(a[j] < a[i])f[i] = max(f[i],f[j] + a[i]);
		}
		ans = max(ans,f[i]);
	}
	cout << ans << endl;
    return 0;
}