Submission #998237

# Submission time Handle Problem Language Result Execution time Memory
998237 2024-06-13T12:19:16 Z LOLOLO Feast (NOI19_feast) C++17
30 / 100
159 ms 17936 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (ll)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 3e5 + 10;
int p[N], sz[N], l[N], r[N], a[N];
ll val[N];
 
set <pair <ll, ll>> st;
 
int get(int x) {
    return p[x] ? get(p[x]) : x;
}

int same(int a, int b) {
    a = get(a), b = get(b);
    return a == b;
}
 
void unite(int a, int b) {
    a = get(a), b = get(b);
    if (a == b)
        return;
 
    if (sz[a] < sz[b])
        swap(a, b);
 
    sz[a] += sz[b];
    p[b] = a;
    l[a] = min(l[a], l[b]);
    r[a] = max(r[a], r[b]);
    val[a] += val[b];
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int n, k;
    cin >> n >> k;
 
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sz[i] = 1;
        l[i] = r[i] = i;
    }
 
    while (a[n] <= 0 && n)  
        n--;
 
    if (n == 0) {
        cout << 0 << '\n';
        return 0;
    } 
 
    int ft = 1;
    while (a[ft] <= 0)
        ft++;
 
    for (int j = ft; j <= n; j++) 
        a[j - ft + 1] = a[j];
 
    for (int i = 1; i <= n; i++) {
        val[i] = a[i];
    }
 
    for (int i = 1; i < n; i++) {
        if (bool(a[i] >= 0) == bool(a[i + 1] >= 0))
            unite(i, i + 1);
    }

    ll cc = 0;
    for (int i = 1; i <= n; i++) {
        if (get(i) == i) {
            cc += val[i] > 0;
            st.insert({abs(val[i]), i});
        }
    }
 
    while (cc > k) {
        auto it = *st.begin();
        st.erase(it);
        int p = get(it.s);

        cc -= val[p] > 0;
        if (l[p] > 1) {
            int x = get(l[p] - 1);
            st.erase({abs(val[x]), x});
            cc -= val[x] > 0;
            unite(p, x);
        }

        if (r[p] < n) {
            int x = get(r[p] + 1);
            st.erase({abs(val[x]), x});
            cc -= val[x] > 0;
            unite(p, x);
        }
 
        p = get(p);
        if ((same(p, 1) == 0 && same(p, n) == 0) || (val[p] > 0)) {
            st.insert({abs(val[p]), p});
            cc += val[p] > 0;
        }
    }
 
    ll ans = 0;
    while (sz(st)) {
        auto it = *st.begin();
        st.erase(it);
        int p = it.s;
        ans += max((ll)0, val[p]);
    }
 
    cout << ans << '\n';
    return 0;
 
}
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8424 KB Output is correct
2 Correct 22 ms 8532 KB Output is correct
3 Correct 26 ms 8412 KB Output is correct
4 Correct 24 ms 8532 KB Output is correct
5 Correct 24 ms 8520 KB Output is correct
6 Correct 21 ms 8384 KB Output is correct
7 Correct 22 ms 8528 KB Output is correct
8 Correct 22 ms 8532 KB Output is correct
9 Correct 21 ms 8540 KB Output is correct
10 Correct 21 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8540 KB Output is correct
2 Correct 16 ms 8664 KB Output is correct
3 Correct 15 ms 8540 KB Output is correct
4 Correct 18 ms 8536 KB Output is correct
5 Correct 21 ms 8532 KB Output is correct
6 Correct 14 ms 8568 KB Output is correct
7 Correct 15 ms 8548 KB Output is correct
8 Correct 22 ms 8552 KB Output is correct
9 Correct 23 ms 8540 KB Output is correct
10 Correct 15 ms 8572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 17744 KB Output is correct
2 Correct 132 ms 17488 KB Output is correct
3 Correct 159 ms 17616 KB Output is correct
4 Correct 147 ms 17656 KB Output is correct
5 Correct 140 ms 17812 KB Output is correct
6 Correct 138 ms 17748 KB Output is correct
7 Correct 139 ms 17768 KB Output is correct
8 Correct 150 ms 17792 KB Output is correct
9 Correct 141 ms 17936 KB Output is correct
10 Correct 150 ms 17744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8424 KB Output is correct
2 Correct 22 ms 8532 KB Output is correct
3 Correct 26 ms 8412 KB Output is correct
4 Correct 24 ms 8532 KB Output is correct
5 Correct 24 ms 8520 KB Output is correct
6 Correct 21 ms 8384 KB Output is correct
7 Correct 22 ms 8528 KB Output is correct
8 Correct 22 ms 8532 KB Output is correct
9 Correct 21 ms 8540 KB Output is correct
10 Correct 21 ms 8532 KB Output is correct
11 Correct 15 ms 8540 KB Output is correct
12 Correct 16 ms 8664 KB Output is correct
13 Correct 15 ms 8540 KB Output is correct
14 Correct 18 ms 8536 KB Output is correct
15 Correct 21 ms 8532 KB Output is correct
16 Correct 14 ms 8568 KB Output is correct
17 Correct 15 ms 8548 KB Output is correct
18 Correct 22 ms 8552 KB Output is correct
19 Correct 23 ms 8540 KB Output is correct
20 Correct 15 ms 8572 KB Output is correct
21 Correct 151 ms 17744 KB Output is correct
22 Correct 132 ms 17488 KB Output is correct
23 Correct 159 ms 17616 KB Output is correct
24 Correct 147 ms 17656 KB Output is correct
25 Correct 140 ms 17812 KB Output is correct
26 Correct 138 ms 17748 KB Output is correct
27 Correct 139 ms 17768 KB Output is correct
28 Correct 150 ms 17792 KB Output is correct
29 Correct 141 ms 17936 KB Output is correct
30 Correct 150 ms 17744 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Incorrect 1 ms 4444 KB Output isn't correct
33 Halted 0 ms 0 KB -