제출 #1316514

#제출 시각아이디문제언어결과실행 시간메모리
1316514Lakshya108Feast (NOI19_feast)C++20
4 / 100
42 ms6056 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<long long> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    vector<long long> pos, neg;
    for (int i = 0; i < N; ) {
        long long s = 0;
        int j = i;
        if (A[i] >= 0) {
            while (j < N && A[j] >= 0) s += A[j++];
            pos.push_back(s);
        } else {
            while (j < N && A[j] < 0) s += A[j++];
            neg.push_back(s);
        }
        i = j;
    }

    if (pos.empty()) {
        cout << 0 << "\n";
        return 0;
    }

    priority_queue<long long> pq;
    long long ans = 0;
    int m = pos.size();

    for (long long x : pos) {
        ans += x;
        pq.push(x);
    }

    for (int i = 0; i + 1 < m; i++) {
        pq.push(pos[i] + pos[i + 1] + neg[i]);
    }

    while ((int)pq.size() > K) {
        ans -= pq.top();
        pq.pop();
    }

    cout << ans << "\n";
    return 0;
}
#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...