55582023-07-26 13:46:27marcipan5000Majomházcpp17Accepted 100/1001.225s24776 KiB
#include <bits/stdc++.h>

using namespace std;
#define int long long int

int n,k;
int t[100010];
int ans;
vector<int> dp;
deque<int> st;
vector<int> sums;
vector<int> db;

int sum(int x,int y) {
    return(sums[y]-sums[x-1]);
}

int a,b;
int log_search(int x,int y) {
    if (x==y) {
        return(x);
    }
    int koz=(x+y)/2;
    if ( dp[a]+sum(a+1,koz)*(koz-a) <= dp[b]+sum(b+1,koz)*(koz-b) ) {
        return(log_search(koz+1,y));
    } else {
        return(log_search(x,koz));
    }
}

int solve(int l) {
    int kor,kes;
    dp.assign(n+1,0);
    db.assign(n+1,0);
    st.clear();
    dp[0]=0;
    db[0]=0;
    st.push_front(0);
    for (int i=1;i<=n;i++) {
        while ((st.size()>=2) && (dp[st[0]]+sum(st[0]+1,i)*(i-st[0]) >= dp[st[1]]+sum(st[1]+1,i)*(i-st[1])  )) {
            //cout << st[0] << " " << dp[st[0]] << " " << st[1] << " " << st[st[1]] << " " << sum(st[0]+1,i) << " " << sum(st[1]+1,i) << endl;
            st.pop_front();
        }
        dp[i]=l+sum(st[0]+1,i)*(i-st[0])+dp[st[0]];
        db[i]=db[st[0]]+1;
        while (i<n) {
            if (st.size()<=1) {
                break;
            }
            a=st[st.size()-1]; b=i;
            kes=log_search(b+1,n+1);
            a=st[st.size()-2]; b=st[st.size()-1];
            kor=log_search(b+1,n+1);
            if (kes<=kor) {
                st.pop_back();
            } else {
                break;
            }
        }
        st.push_back(i);
    }
    ans=dp[n]-db[n]*l;
/*
    if (k==db[n]-1) {
        for (int i=0;i<=n;i++) {
            cout << dp[i] << " " << db[i] << endl;
        }
    }
*/
    return(db[n]-1);
}

int bin_ker(int x,int y) {
    

    int z=solve((x+y)/2);
    if (z==k) {
        return(ans);
    }
    if (z<k) {
        return(bin_ker(x,(x+y)/2-1));
    } else {
        return(bin_ker((x+y)/2+1,y));
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    for (int i=1;i<=n;i++) {
        cin >> t[i];
    }
    sums.assign(n+1,0);
    for (int i=1;i<=n;i++) {
        sums[i]=sums[i-1]+t[i];
    }
    cout << bin_ker(0,1e17) << endl;
    return(0);
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1840 KiB
2Accepted25ms2328 KiB
subtask210/10
3Accepted3ms2212 KiB
4Accepted3ms2344 KiB
5Accepted3ms2564 KiB
6Accepted3ms2652 KiB
7Accepted3ms2740 KiB
subtask310/10
8Accepted4ms3020 KiB
9Accepted4ms3236 KiB
10Accepted4ms3220 KiB
11Accepted4ms3368 KiB
12Accepted4ms3448 KiB
subtask420/20
13Accepted16ms3776 KiB
14Accepted17ms4100 KiB
15Accepted20ms4028 KiB
16Accepted28ms4224 KiB
17Accepted32ms4480 KiB
18Accepted39ms4620 KiB
subtask529/29
19Accepted160ms7644 KiB
20Accepted246ms8436 KiB
21Accepted272ms8420 KiB
22Accepted328ms8948 KiB
23Accepted384ms9220 KiB
subtask631/31
24Accepted32ms13072 KiB
25Accepted131ms14108 KiB
26Accepted163ms14848 KiB
27Accepted193ms15636 KiB
28Accepted230ms16200 KiB
29Accepted298ms16908 KiB
30Accepted412ms17832 KiB
31Accepted479ms18512 KiB
32Accepted541ms19336 KiB
33Accepted694ms19960 KiB
34Accepted783ms20596 KiB
35Accepted883ms21220 KiB
36Accepted981ms21892 KiB
37Accepted941ms22464 KiB
38Accepted1.215s23172 KiB
39Accepted1.116s24140 KiB
40Accepted1.225s24776 KiB