Submission #1207790

#TimeUsernameProblemLanguageResultExecution timeMemory
1207790AmadooFeast (NOI19_feast)C++20
100 / 100
134 ms12168 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "local_debug.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif

#define int         long long
#define nl          '\n'
#define sp          ' '
#define F           first
#define S           second
#define SZ(s)       (int)((s).size())

const int N = 3e5 + 5;

int a[N];

void solve() {
    int n, k; cin >> n >> k;
    for(int i = 0; i < n; ++i) cin >> a[i];
    auto solve = [&](const int lmb) {
        pair <int, int> dp[n][2];
        dp[0][0] = {0, 0};
        dp[0][1] = {a[0] - lmb, 1};
        for(int i = 1; i < n; ++i) {
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
            dp[i][1] = max(make_pair(dp[i - 1][0].F + a[i] - lmb, dp[i - 1][0].S + 1), make_pair(dp[i - 1][1].F + a[i], dp[i - 1][1].S));
        }
        return max(dp[n - 1][0], dp[n - 1][1]);
    };
    int l = 0, r = 1e18, mid, lmb_opt;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(solve(mid).S >= k) lmb_opt = mid, l = mid + 1;
        else r = mid - 1;
    }
    cout << solve(lmb_opt).F + lmb_opt * solve(lmb_opt).S << nl;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int tc = 1;
    // cin >> tc;
    while(tc--) solve();
}
#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...