#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <string>
#include <queue>
#include <unordered_set>
#include <deque>
#include <numeric>
#include <cmath>
#include <unordered_map>
#include <set>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<ll> t(n);
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
    }
    ll ans = 0;
    ll beg = 0;
    ll summ = t[0];
    ll curmax = t[0];
    for (int i = 1; i < n; ++i) {
        if (t[i] <= curmax) {
            summ += t[i];
        } else {
            ll opt1 = ((ll)n - i) * curmax;
            ll opt2 = ((ll)i - beg) * (t[i] - curmax);
            if (opt1 > opt2) {
                summ += t[i];
                curmax = t[i];
            } else {
                ans += ((ll)n - beg) * curmax;
                beg = i;
                summ = t[i];
                curmax = t[i];
            }
        }
    }
    ans += ((ll)n - beg) * curmax;
    cout << ans << '\n';
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |