#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL ;
int main() {
    int n;
    cin >> n;
    LL arr[n], sum[n];
    memset(sum, 0, sizeof(sum));
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        sum[i]=arr[i];
    }
    LL res = 0;
    // sum[0]=arr[0];
    for (int i = 1; i < n; i++) {
        for (int j = i; j >= 0; j--) {
            if (arr[j] < arr[i]) {
                sum[i] =max(sum[j]+ arr[i], sum[i]);
                //比arr[i]小的值不止一个,因此不能break结束
            }
        }
         res = max(res,sum[i]);
    }
    cout<<res<<endl;


}
// 64 位输出请用 printf("%lld")