This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
const int inf = 1e15;
const int N = 1502;
 
struct line {
    mutable long long m, b, l;
    line(long long _m, long long _b) : m(_m), b(_b), l(0) {}
    bool operator<(const line& other) const { return m < other.m; }
    bool operator<(const long long x) const { return l < x; }
};
template<bool GET_MAX = false>
struct CHT : multiset<line, less<>> {
    // double: inf = 1/.0, div(a, b) = a / b
    static const long long inf = 1e15;
    long long div(long long a, long long b) { return a / b - ((a ^ b) < 0 && a & b); }
    bool isect(iterator x, iterator y) {
        if (y == end()) { x->l = inf; return false; }
        if (x->m == y->m) x->l = x->b > y->b ? inf : -inf;
        else x->l = div(y->b - x->b, x->m - y->m);
        return x->l >= y->l;
    }
    void add(line l) {
        if (!GET_MAX) l.m = -l.m, l.b = -l.b; 
        auto z = insert(l), y = z++, x = y;
        while(isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->l >= y->l) isect(x, erase(y));
    }
    long long qry(long long x) {
        auto l = *lower_bound(x);
        return (l.m * x + l.b) * (GET_MAX ? 1 : -1);
    }
};
 
 
signed main() {
    std::ios::sync_with_stdio(0);
    std::cout.tie(0);
    std::cin.tie(0);
    int n; cin >> n;
    vector<int> a(n + 1), dp(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    CHT<> cht;
    dp[1] = a[1] * n;
    cht.add({n, 0}); //querying all n at one time
    int mx = a[1];
    for(int i = 2; i <= n; i++) {
        mx = max(mx, a[i]);
        cht.add({n - i + 1, dp[i - 1]});
        dp[i] = cht.qry(mx);
        //we can qry for mx cuz the x coordinate has to be atleast mx (all under same time mx)
    }
    cout << dp[n] << '\n';
    return 0;
}
| # | 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... |