제출 #1316512

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

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

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

    vector<long long> v;
    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++];
        } else {
            while (j < N && A[j] < 0) s += A[j++];
        }
        v.push_back(s);
        i = j;
    }

    priority_queue<long long> pq;
    long long ans = 0;
    int cnt = 0;

    for (long long x : v) {
        if (x > 0) {
            ans += x;
            pq.push(x);
            cnt++;
        } else if (!pq.empty()) {
            long long t = pq.top();
            pq.pop();
            pq.push(t + x);
        }
    }

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

    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...