Submission #1025650

#TimeUsernameProblemLanguageResultExecution timeMemory
1025650ZicrusFeast (NOI19_feast)C++17
71 / 100
110 ms51660 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

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

    if ((n <= 2000 && k <= 2000) || k == 1) {
        vector<vector<ll>> dp(n, vector<ll>(k+1));
        vector<vector<ll>> dpSeg(n, vector<ll>(k+1));
        for (int j = 1; j <= k; j++) {
            dpSeg[0][j] = max(0ll, a[0]);
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                dp[i][j] = max(dp[i-1][j], dpSeg[i-1][j]);
                dpSeg[i][j] = max(dp[i-1][j-1], dpSeg[i-1][j]) + a[i];
            }
        }

        cout << max(dp.back().back(), dpSeg.back().back());
        return 0;
    }

    ll sum = 0;
    ll pre = 0, post = 0;
    bool neg = false;
    for (auto &e : a) {
        if (neg) post += e;
        if (e < 0) { neg = true; pre = sum; }
        sum += e;
    }
    cout << (neg ? pre + post : sum);
}
#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...