Submission #1168988

#TimeUsernameProblemLanguageResultExecution timeMemory
1168988tnknguyen_Feast (NOI19_feast)C++20
18 / 100
79 ms12104 KiB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n' 
#define ll long long 
#define len(s) (int)s.size() 
#define int long long 

#define pii pair<int, int>
#define fi first
#define se second
#define MASK(k) (1LL << (k))

int n, k;
const int MX = 3e5 + 5;
int a[MX];
pii dp[MX][2];

pii solve(int lmb){
    // {sum with current penalty, # of arrays}
    dp[1][0] = {0, 0}; //do not create new array
    dp[1][1] = {a[1] - lmb, 1}; // create 1 array with penalty

    for(int i=2; i<=n; ++i){
        // do not do anything
        dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
        // create new array with penalty or assign to previous array
        pii p1 = {dp[i-1][0].fi + a[i] - lmb, dp[i-1][0].se + 1};
        pii p2 = {dp[i-1][1].fi + a[i], dp[i-1][1].se};
        dp[i][1] = max(p1, p2);
    }    

    return max(dp[n][0], dp[n][1]);
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);

    cin >> n >> k;
    for(int i=1; i<=n; ++i) cin >> a[i];

    int ans = 0;
    for(int l=0, r=1e18; l<=r; ){
        int mid = (l + r) >> 1;
        pii cost = solve(mid);
        if(cost.second >= k){
            l = mid + 1;
            ans = cost.first + mid*k;
        }
        else r = mid - 1;
    }
    cout << ans;

    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...
#Verdict Execution timeMemoryGrader output
Fetching results...