제출 #1340669

#제출 시각아이디문제언어결과실행 시간메모리
1340669justfadyFeast (NOI19_feast)C++20
100 / 100
896 ms68512 KiB
#include <bits/stdc++.h> //  +
using namespace std;     // +++
#define inf 1e18         //  +
#define int long long
#define ll __int128_t
#define all(a) a.begin(), a.end()

int const N = 3e5 + 5;
pair<ll, ll> dp[N][2];
int vis[N][2], id;
int n, k, a[N];
ll lamda;
pair<ll, ll> rec(int i, bool lst) {
    if (i == n) return {0, 0};
    auto &ret = dp[i][lst];
    if (vis[i][lst] == id) return ret;
    vis[i][lst] = id;
    auto leave = rec(i + 1, 0);
    auto take = rec(i + 1, 1);
    take.first += a[i];
    if (!lst) {
        take.first -= lamda;
        take.second--;
    }

    return ret = max(leave, take);
}

void solve() {
    cin >> n >> k;

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

    int st = 0, en = 1e15, md, ans = 0;
    while (st <= en) {
        md = (st + en) / 2;

        lamda = md;
        ++id;
        auto [d, cnt] = rec(0, 0);

        ll used = -cnt;
        if (used > k) st = md + 1;
        else {
            ans = (int)(d + lamda * k);
            en = md - 1;
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    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...