import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
auto solve(string s_) {
    immutable N = s_.to!int();
    auto a = readln.split.map!(to!int).array();
    uint n=1; while(n<N) n*=2;
    auto s = new int[n*2]; s[]=-1;
    void update(int v, int i) {
        for(uint p=v-1+n; p>0; p/=2) s[p]=max(s[p],i);
    }
    int get(int v) {
        if(v<=0 || n<v) return -1;
        uint l=0, r=n, p=1;
        int w=-1;
        while(v<r) {
            auto m = (l+r)/2;
            if(v>m) w=max(w,s[p*2]), p=p*2+1, l=m;
            else p*=2, r=m;
        }
        return max(w,s[p]);
    }
    auto dp = new long[N];
    foreach(i,v;a) {
        immutable k = get(v-1);
        if(k<0) dp[i]=v*(i+1);
        else    dp[i]=dp[k]+v*(i-k);
        update(v,i.to!int());
    }
    return dp.sum();
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }