#include using namespace std; #include struct S { long long value; int size; }; using F = int; S op(S a, S b) { return {a.value + b.value, a.size + b.size}; } S e() { return {0, 1}; } S mapping(F f, S x) { if (f == -1) return x; return {x.size * f, x.size}; } F composition(F f, F g) { if (f == -1) return g; return f; } F id() { return -1; } int main() { int n; cin >> n; assert(1 <= n && n <= 200000); vector a(n); for (int i = 0; i < n; ++i) cin >> a[i], assert(1 <= a[i] && a[i] <= 1000000000); atcoder::lazy_segtree seg(n); long long ans = 0; for (int i = 0; i < n; ++i) { int l = 0, r = n; while (r - l > 1) { int m = (l + r) / 2; if (seg.get(m).value < a[n - i - 1]) l = m; else r = m; } seg.apply(n - i - 1, r, a[n - i - 1]); ans += seg.prod(n - i - 1, n).value; } cout << ans << endl; }