Submission #831404

#TimeUsernameProblemLanguageResultExecution timeMemory
831404BamshooTFeast (NOI19_feast)C++14
100 / 100
125 ms12752 KiB
#include <iostream>

#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define fi first
#define se second
#define inf 1e18

using namespace std;

typedef pair<ll, int> li;

const int N = 3e5 + 9;
int n, k;
ll pre[N], a[N], ans;
li dp[N];

void maximize(li &a, li b){
    if (a.fi < b.fi) a = b;
    if (a.fi == b.fi) a = max(a, b);
}

li calc(ll cost){
    //FOR(i, 1, n) dp[i] = {pre[i] - cost, 1};
    li best = {0, 0};
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i-1];
        maximize(dp[i], {pre[i] + best.fi - cost, best.se + 1});
        maximize(best, {dp[i].fi - pre[i], dp[i].se});

    }
    return dp[n];
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   // freopen("nhap.inp", "r", stdin);
   // freopen("xuat.out", "w", stdout);
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i], pre[i] = pre[i-1] + a[i];
    ll lo = -1e15, hi = 1e15;
    while (lo <= hi) {
        ll mid = lo + hi >> 1;
        li tmp = calc(mid);
        if (tmp.se <= k) {
            ans = max(ans, tmp.fi + tmp.se*mid);
            hi = mid - 1;
        } else lo = mid + 1;
    }
    cout << ans;
    return 0;
}

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:43:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |         ll mid = lo + hi >> 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...