#include <bits/stdc++.h>

using namespace std;

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;


template<typename T>
struct Segtree{
    i64 n;
    T op;
    vector<T> elm;
    function<T(T, T)> f;

    Segtree(i64 n, T init, function<T(T, T)> f, T op = T()) :
        n(n),
        op(op),
        elm(2 * n, init),
        f(f)
    {
    }

    void set(i64 x, T val){
        x += n;
        elm[x] = val;
        while(x >>= 1)
            elm[x] = f(elm[2 * x], elm[2 * x + 1]);
    }

    void update(i64 x, T val){
        x += n;
        elm[x] = f(val, elm[x]);
        while(x >>= 1)
            elm[x] = f(elm[2 * x], elm[2 * x + 1]);
    }

    T get(i64 x, i64 y) const{
        T val = op;
        for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
            if(x & 1)
                val = f(val, elm[x++]);
            if(!(y & 1))
                val = f(val, elm[y--]);
        }
        return val;
    }
};



template<typename T, typename U>
struct LazySegtree{
    const int n;
    const T op_t;
    const U op_u;
    vector<T> elm;
    vector<U> lazy;
    vector<int> length;
    function<T(T, T)> f;
    function<T(T, U, int)> g;
    function<U(U, U)> h;

    LazySegtree(int n, T init, function<T(T, T)> f, function<T(T, U, int)> g, function<U(U, U)> h, T op_t = T(), U op_u = U()) :
        n(n),
        op_t(op_t),
        op_u(op_u),
        elm(2 * n, init),
        lazy(2 * n, op_u),
        length(2 * n, 0),
        f(f),
        g(g),
        h(h)
    {
        for(int i = n - 1; i > 0; --i){
            elm[i] = f(elm[2 * i], elm[2 * i + 1]);
            length[i] = length[2 * i] + 1;
        }
    }

    vector<int> get_list(int x, int y){

        vector<int> ret_list;
        for(x += n, y += n - 1; x; x >>= 1, y >>= 1){
            ret_list.emplace_back(x);
            if(x != y)
                ret_list.emplace_back(y);
        }

        return ret_list;
    }

    void eval(int x){

        elm[x] = g(elm[x], lazy[x], 1 << length[x]);
        if(x < n){
            lazy[2 * x] = h(lazy[2 * x], lazy[x]);
            lazy[2 * x + 1] = h(lazy[2 * x + 1], lazy[x]);
        }
        lazy[x] = op_u;
    }

    void update(int x, int y, U val){

        vector<int> index_list = get_list(x, y);
        for(int i = index_list.size() - 1; i >= 0; --i)
            eval(index_list[i]);

        for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
            if(x & 1){
                lazy[x] = h(lazy[x], val);
                eval(x++);
            }
            if(!(y & 1)){
                lazy[y] = h(lazy[y], val);
                eval(y--);
            }
        }

        for(auto index : index_list){
            if(index < n){
                eval(2 * index);
                eval(2 * index + 1);
                elm[index] = f(elm[2 * index], elm[2 * index + 1]);
            }
        }
    }

    T get(int x, int y){

        vector<int> index_list = get_list(x, y);
        for(int i = index_list.size() - 1; i >= 0; --i)
            eval(index_list[i]);

        T val = op_t;
        for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
            if(x & 1){
                eval(x);
                val = f(val, elm[x++]);
            }
            if(!(y & 1)){
                eval(y);
                val = f(val, elm[y--]);
            }
        }
        return val;
    }
};


signed main(){
    // E shibh308
    i64 n;
    cin >> n;
    vector<i64> a(n);
    for(auto& x : a)
        cin >> x;

    map<i64, vector<i64>> v;
    for(i64 i = 0; i < n; ++i)
        v[a[i]].emplace_back(i);
    Segtree<i64> seg(n, 0, [](i64 x, i64 y){return max(x, y);}, 0);

    for(auto& p : v){
        vector<i64> t(p.second.size());

        for(i64 i = 0; i < t.size(); ++i)
            t[i] = seg.get(0, p.second[i]) + 1;

        for(i64 i = 0; i < t.size(); ++i)
            seg.update(p.second[i], t[i]);
    }

    i64 ma = seg.get(0, n);
    vector<vector<pair<i64,i64>>> m(ma + 1);
    for(i64 i = 0; i < n; ++i){
        m[seg.get(i, i + 1)].emplace_back(a[i], i);
    }

    for(auto& x : m)
        sort(x.begin(), x.end());

    vector<i64> dp(n, 0);

    // Segtree<i64> sum(n, 0, [](i64 x, i64 y){return max(x, y);}, 0);
    LazySegtree<i64,i64> sum(n, 0, [](i64 x, i64 y){return max(x, y);}, [](i64 x, i64 y, int){return y == INF ? x : y;}, [](i64 x, i64 y){return y == INF ? x : y;}, 0, INF);
    for(i64 i = 1; i <= ma; ++i){
        map<i64, vector<pair<i64, i64>>> mv;

        sum.update(0, n, 0);

        for(auto& x : m[i - 1])
            mv[x.first].emplace_back(x.second, 0);
        for(auto& x : m[i])
            mv[x.first].emplace_back(x.second, 1);

        for(auto& pa : mv){
            i64 val, index, flag;
            for(auto& elm : pa.second){
                tie(index, flag) = elm;
                if(flag)
                    dp[index] = sum.get(0, index) + a[index];
            }
            for(auto& elm : pa.second){
                tie(index, flag) = elm;
                if(!flag)
                    sum.update(index, index + 1, dp[index]);
            }
        }
    }

    i64 ans = -MOD;
    for(auto& x : m.back()){
        ans = max(ans, dp[x.second]);
    }

    cout << ans << endl;
}



Measured memory_bytes: 33284