Submission #894617

# Submission time Handle Problem Language Result Execution time Memory
894617 2023-12-28T14:30:14 Z IWKR Feast (NOI19_feast) C++17
100 / 100
76 ms 25536 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

int arr[300001];
int a[300001];
int s[300001];
int cost[300001];
int pre[300001];
int nex[300001];
bool mark[300001];
priority_queue<tuple<int, int, int>> pq;

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    int start = 0;
    int end = n - 1;
    while (start < n && arr[start] <= 0) {
        start++;
    }
    while (end >= 0 && arr[end] <= 0) {
        end--;
    }
    if (start > end) {
        cout << 0;
        return 0;
    }
    n = 0;
    for (int i = start; i <= end; i++) {
        a[++n] = arr[i];
    }
    for (int i = 1; i <= n; i++) {
        s[i] = s[i - 1] + a[i];
    }
    int last = 0;
    pre[0] = nex[n] = 1e9;
    for (int i = 1; i <= n; i++) {
        if (i > 1 && (a[i] > 0) != (a[i - 1] > 0)) {
            if (a[i - 1] > 0) {
                --k;
            }
            cost[i - 1] = -abs(s[i - 1] - s[last]);
            pre[i - 1] = last;
            nex[last] = i - 1;
            pq.emplace(cost[i - 1], last, i - 1);
            last = i - 1;
        }
    }
    --k;
    cost[n] = -abs(s[n] - s[last]);
    pre[n] = last;
    nex[last] = n;
    pq.emplace(cost[n], last, n);
    int ans = 0;
    while (!pq.empty() && k < 0) {
        int price, l, r;
        tie(price, l, r) = pq.top();
        pq.pop();
        if (mark[l] || mark[r] || price != cost[r]) {
            continue;
        }
        ++k;
        ans += cost[r];
        mark[l] = mark[r] = true;
        int ll = pre[l];
        int rr = nex[r];
        if (ll < rr && ll >= 0 && rr <= n) {
            pre[rr] = ll;
            nex[ll] = rr;
            cost[rr] += cost[l] - cost[r];
            cost[r] = -1e18;
        }
        if (ll < rr && ll >= 0 && rr <= n && !mark[ll] && !mark[rr]) {
            pq.emplace(cost[rr], ll, rr);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (a[i] > 0) {
            ans += a[i];
        }
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 32 ms 16316 KB Output is correct
2 Correct 30 ms 16252 KB Output is correct
3 Correct 28 ms 16568 KB Output is correct
4 Correct 25 ms 16468 KB Output is correct
5 Correct 25 ms 16464 KB Output is correct
6 Correct 25 ms 16208 KB Output is correct
7 Correct 26 ms 16444 KB Output is correct
8 Correct 25 ms 16464 KB Output is correct
9 Correct 25 ms 16212 KB Output is correct
10 Correct 26 ms 16440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15196 KB Output is correct
2 Correct 20 ms 15352 KB Output is correct
3 Correct 17 ms 15196 KB Output is correct
4 Correct 18 ms 14940 KB Output is correct
5 Correct 26 ms 16296 KB Output is correct
6 Correct 18 ms 15176 KB Output is correct
7 Correct 17 ms 15448 KB Output is correct
8 Correct 26 ms 16472 KB Output is correct
9 Correct 26 ms 16208 KB Output is correct
10 Correct 18 ms 15092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 23980 KB Output is correct
2 Correct 76 ms 25528 KB Output is correct
3 Correct 67 ms 24372 KB Output is correct
4 Correct 66 ms 24632 KB Output is correct
5 Correct 66 ms 25000 KB Output is correct
6 Correct 66 ms 24772 KB Output is correct
7 Correct 73 ms 24108 KB Output is correct
8 Correct 68 ms 24004 KB Output is correct
9 Correct 68 ms 25536 KB Output is correct
10 Correct 67 ms 25032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 1 ms 10588 KB Output is correct
18 Correct 1 ms 10588 KB Output is correct
19 Correct 1 ms 10588 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 1 ms 10588 KB Output is correct
18 Correct 1 ms 10588 KB Output is correct
19 Correct 1 ms 10588 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10756 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 2 ms 10840 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10732 KB Output is correct
30 Correct 2 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 16316 KB Output is correct
2 Correct 30 ms 16252 KB Output is correct
3 Correct 28 ms 16568 KB Output is correct
4 Correct 25 ms 16468 KB Output is correct
5 Correct 25 ms 16464 KB Output is correct
6 Correct 25 ms 16208 KB Output is correct
7 Correct 26 ms 16444 KB Output is correct
8 Correct 25 ms 16464 KB Output is correct
9 Correct 25 ms 16212 KB Output is correct
10 Correct 26 ms 16440 KB Output is correct
11 Correct 17 ms 15196 KB Output is correct
12 Correct 20 ms 15352 KB Output is correct
13 Correct 17 ms 15196 KB Output is correct
14 Correct 18 ms 14940 KB Output is correct
15 Correct 26 ms 16296 KB Output is correct
16 Correct 18 ms 15176 KB Output is correct
17 Correct 17 ms 15448 KB Output is correct
18 Correct 26 ms 16472 KB Output is correct
19 Correct 26 ms 16208 KB Output is correct
20 Correct 18 ms 15092 KB Output is correct
21 Correct 69 ms 23980 KB Output is correct
22 Correct 76 ms 25528 KB Output is correct
23 Correct 67 ms 24372 KB Output is correct
24 Correct 66 ms 24632 KB Output is correct
25 Correct 66 ms 25000 KB Output is correct
26 Correct 66 ms 24772 KB Output is correct
27 Correct 73 ms 24108 KB Output is correct
28 Correct 68 ms 24004 KB Output is correct
29 Correct 68 ms 25536 KB Output is correct
30 Correct 67 ms 25032 KB Output is correct
31 Correct 1 ms 10588 KB Output is correct
32 Correct 2 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 1 ms 10588 KB Output is correct
35 Correct 2 ms 10588 KB Output is correct
36 Correct 2 ms 10588 KB Output is correct
37 Correct 2 ms 10588 KB Output is correct
38 Correct 2 ms 10588 KB Output is correct
39 Correct 2 ms 10588 KB Output is correct
40 Correct 1 ms 10588 KB Output is correct
41 Correct 2 ms 10588 KB Output is correct
42 Correct 2 ms 10588 KB Output is correct
43 Correct 1 ms 10588 KB Output is correct
44 Correct 1 ms 10588 KB Output is correct
45 Correct 2 ms 10588 KB Output is correct
46 Correct 1 ms 10588 KB Output is correct
47 Correct 1 ms 10588 KB Output is correct
48 Correct 1 ms 10588 KB Output is correct
49 Correct 1 ms 10588 KB Output is correct
50 Correct 1 ms 10588 KB Output is correct
51 Correct 2 ms 10588 KB Output is correct
52 Correct 2 ms 10588 KB Output is correct
53 Correct 2 ms 10756 KB Output is correct
54 Correct 2 ms 10588 KB Output is correct
55 Correct 2 ms 10840 KB Output is correct
56 Correct 2 ms 10588 KB Output is correct
57 Correct 2 ms 10588 KB Output is correct
58 Correct 2 ms 10588 KB Output is correct
59 Correct 2 ms 10732 KB Output is correct
60 Correct 2 ms 10588 KB Output is correct
61 Correct 41 ms 25348 KB Output is correct
62 Correct 34 ms 25404 KB Output is correct
63 Correct 62 ms 24260 KB Output is correct
64 Correct 40 ms 25352 KB Output is correct
65 Correct 45 ms 24780 KB Output is correct
66 Correct 48 ms 23488 KB Output is correct
67 Correct 48 ms 24772 KB Output is correct
68 Correct 67 ms 25480 KB Output is correct
69 Correct 70 ms 24364 KB Output is correct
70 Correct 67 ms 24004 KB Output is correct