제출 #1026001

#제출 시각아이디문제언어결과실행 시간메모리
1026001ZicrusFeast (NOI19_feast)C++17
100 / 100
207 ms5720 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll n, k;
vector<ll> a;

pair<ll, ll> solve(ll p) {
    ll dp = 0;
    ll dpSeg = a[0] - p;
    ll ps = 0;
    ll psSeg = 1;
    for (int i = 1; i < n; i++) {
        ll tdp = dp, tdpSeg = dpSeg, tps = ps, tpsSeg = psSeg;
        if (tdpSeg > tdp) {
            dp = tdpSeg;
            ps = tpsSeg;
        }
        if (tdp - p > tdpSeg) {
            dpSeg = tdp - p;
            psSeg = tps + 1;
        }
        dpSeg += a[i];
    }
    if (dpSeg > dp) {
        dp = dpSeg;
        ps = psSeg;
    }
    return {dp, ps};
}

int main() {
    cin >> n >> k;
    a = vector<ll>(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    ll l = 0, r = 1ll << 50;
    ll bestK = 1ll << 50;
    ll bestAns = 0;
    ll pen = 0;
    while (l <= r) {
        ll mid = pen = (l + r) / 2;
        bestK = solve(mid).second;
        bestAns = solve(mid).first;

        if (bestK > k) {
            l = mid + 1;
        }
        else {
            if (l == r) break;
            r = mid;
        }
    }
    ll val = bestAns + k * pen;
    cout << val;
}
#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...