Submission #674618

#TimeUsernameProblemLanguageResultExecution timeMemory
674618stevancvFeast (NOI19_feast)C++14
100 / 100
239 ms13940 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const int mod = 1e9 + 7;
ll a[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
    }
    auto Can = [&] (ll penalty) {
        vector<ll> dp(n + 1);
        vector<int> cnt(n + 1);
        vector<pair<ll, int>> p(n + 1);
        p[0] = {0, 0};
        for (int i = 1; i <= n; i++) {
            dp[i] = dp[i - 1];
            cnt[i] = cnt[i - 1];
            p[i] = p[i - 1];
            if (-a[i] > p[i].first) p[i] = {-a[i], i};
            if (dp[i] < p[i].first + penalty + a[i]) {
                dp[i] = p[i].first + penalty + a[i];
                cnt[i] = cnt[p[i].second] + 1;
            }
            if (dp[i] - a[i] > p[i].first) p[i] = {dp[i] - a[i], i};
        }
        if (cnt[n] > m) return (ll)-1e18;
        return dp[n] - (ll) cnt[n] * penalty;
    };
    ll l = -1e18, r = 0, ans = 0;
    while (l <= r) {
        ll penalty = l + r >> 1;
        if (Can(penalty) != -1e18) {
            ans = penalty;
            l = penalty + 1;
        }
        else r = penalty - 1;
    }
    ll o = Can(ans);
    if (o < 0) o = 0;
    cout << o << en;
    return 0;
}

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:43:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |         ll penalty = l + r >> 1;
      |                      ~~^~~
#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...