Submission #689719

#TimeUsernameProblemLanguageResultExecution timeMemory
689719true22Discharging (NOI20_discharging)C++14
9 / 100
1067 ms40548 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

typedef pair<ll, ll> pl;
typedef vector<ll> vl;   
typedef vector<pl> vp;

#define nl "\n"
#define fr first
#define sc second
#define pb push_back

#define all(x) x.begin(), x.end()
#define fur(i, a, b) for(ll i = a; i <= b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= b; --i)
#define pv(x) for(auto k : x){cout << k << " ";} cout << nl

const ll inf = 1e17;

struct dt{
    ll cost, li, wt;

    dt(){
        cost = li = wt = inf;
    }

    void print(){
        cout << cost << ' ' << li << ' ' << wt << nl;
    }

};

ll n, sz;
vl t, st;

ll maxr(ll l, ll r, ll k, ll kl, ll kr){
    if (kl > r || kr < l)
        return 0;
    if (kl >= l && kr <= r)
        return st[k];
    ll m = (kl + kr)/2;
    return max(maxr(l, r, 2 * k, kl, m), maxr(l, r, 2 *k + 1, m + 1, kr));
}


void solve(){
    vector<pl> dp;
    dp.resize(n + 1, {inf, inf});
    //dp[0] = {0, 0, 0};
    dp[0] = {0, 0};
    //dp[1] = {t[1], t[1]};
   
    fur(i, 1, n){
        fur(j, 0, i - 1){ // i -> i - 1
            ll mx = maxr(j + 1, i, 1, 1, sz);
            // fur(p, j + 1, i)
            //     mx = max(mx, t[p]);

            ll cost = (mx + dp[j].sc) * (i - j);

            if (dp[j].fr + cost < dp[i].fr){
                dp[i].fr = dp[j].fr + cost;
                dp[i].sc =  dp[j].sc + mx;
            }
        }
    }
    cout << dp[n].fr << nl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);


    cin >> n;
    t.resize(n + 1);
    t[0] =  0;
    fur(i, 1, n)
        cin >> t[i];
    sz = n;
    while(__builtin_popcountll(sz) != 1)
        sz++;
    st.resize(2*sz, 0);
    fur(i, 0, n - 1)
        st[i + sz] = t[i + 1];
    ruf(i, sz - 1, 1)
        st[i] = max(st[2 * i], st[2*i + 1]);
    
    

    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...