Submission #694662

# Submission time Handle Problem Language Result Execution time Memory
694662 2023-02-04T07:56:48 Z ghostwriter Feast (NOI19_feast) C++17
100 / 100
260 ms 16200 KB
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define bg begin
#define ed end
#define ft front
#define bk back
#define sz(x) (int)(x).size()
#define all(x) (x).bg(), (x).ed()
#define mtp make_tuple
#define ins insert
#define ers erase
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define str string
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { WHILE(b) { a %= b; swap(a, b); } return b; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
const ll oo = (ll)1e18 + 5;
const int N = 3e5 + 5;
int n, k, a[N], ln[N], rn[N], cnt = 0;
ll sum[N], rs = 0;
int sign(int v) { return v < 0; }
multiset<pll> s[2];
void del(int pos) {
    if (sum[pos] >= 0) s[0].ers({sum[pos], pos});
    else s[1].ers({sum[pos], pos});
}
void add(pll v) {
    if (v.st >= 0) s[0].ins(v);
    else s[1].ins(v);
}
void del() {
    auto cur = *s[0].bg();
    int pp = ln[cur.nd], np = rn[cur.nd];
    del(cur.nd);
    del(pp);
    del(np);
    add({sum[pp] + sum[np] + cur.st, cur.nd});
    add({-oo, cur.nd});
    ln[cur.nd] = ln[pp];
    rn[cur.nd] = rn[np];
    rn[ln[cur.nd]] = cur.nd;
    ln[rn[cur.nd]] = cur.nd;
    sum[cur.nd] = sum[pp] + sum[np] + cur.st;
}
void mrg() {
    auto cur = *--s[1].ed();
    int pp = ln[cur.nd], np = rn[cur.nd];
    del(cur.nd);
    del(pp);
    del(np);
    add({sum[pp] + sum[np] + cur.st, cur.nd});
    ln[cur.nd] = ln[pp];
    rn[cur.nd] = rn[np];
    rn[ln[cur.nd]] = cur.nd;
    ln[rn[cur.nd]] = cur.nd;
    sum[cur.nd] = sum[pp] + sum[np] + cur.st;
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    freopen(file".inp", "r", stdin);
//    freopen(file".out", "w", stdout);
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, n) {
        if (!cnt && a[i] < 0) continue;
        int j = i;
        ll csum = 0;
        WHILE(j <= n && sign(a[j]) == sign(a[i])) {
            csum += a[j];
            ++j;
        }
        --j;
        if (a[i] < 0 && j == n) break;
        add({csum, ++cnt});
        ln[cnt] = cnt - 1;
        rn[cnt - 1] = cnt;
        sum[cnt] = csum;
        i = j;
    }
    ln[cnt + 1] = cnt;
    rn[cnt] = cnt + 1;
    sum[0] = sum[cnt + 1] = -oo;
    add({-oo, 0});
    add({-oo, cnt + 1});
    WHILE(!s[0].empty()) {
        if (sz(s[0]) <= k) {
            EACH(i, s[0]) rs += i.st;
            break;
        }
        if (-(*s[0].bg()).st >= (*--s[1].ed()).st) del();
        else mrg();
    }
    cout << rs;
//    cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}
/*
12 2
6 -8 -10 10 -10 9 -4 3 -4 7 -4 8

*/

# Verdict Execution time Memory Grader output
1 Correct 29 ms 4256 KB Output is correct
2 Correct 29 ms 4304 KB Output is correct
3 Correct 29 ms 4260 KB Output is correct
4 Correct 29 ms 4228 KB Output is correct
5 Correct 28 ms 4296 KB Output is correct
6 Correct 30 ms 4184 KB Output is correct
7 Correct 31 ms 4140 KB Output is correct
8 Correct 36 ms 4344 KB Output is correct
9 Correct 30 ms 4172 KB Output is correct
10 Correct 31 ms 4196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 2548 KB Output is correct
2 Correct 19 ms 2556 KB Output is correct
3 Correct 18 ms 2472 KB Output is correct
4 Correct 18 ms 2508 KB Output is correct
5 Correct 34 ms 4264 KB Output is correct
6 Correct 28 ms 2540 KB Output is correct
7 Correct 19 ms 2632 KB Output is correct
8 Correct 33 ms 4244 KB Output is correct
9 Correct 29 ms 4192 KB Output is correct
10 Correct 40 ms 2548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 16048 KB Output is correct
2 Correct 235 ms 15872 KB Output is correct
3 Correct 222 ms 16032 KB Output is correct
4 Correct 205 ms 15840 KB Output is correct
5 Correct 205 ms 15976 KB Output is correct
6 Correct 239 ms 16104 KB Output is correct
7 Correct 226 ms 16124 KB Output is correct
8 Correct 224 ms 15948 KB Output is correct
9 Correct 238 ms 16200 KB Output is correct
10 Correct 225 ms 16188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 368 KB Output is correct
28 Correct 2 ms 360 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 4256 KB Output is correct
2 Correct 29 ms 4304 KB Output is correct
3 Correct 29 ms 4260 KB Output is correct
4 Correct 29 ms 4228 KB Output is correct
5 Correct 28 ms 4296 KB Output is correct
6 Correct 30 ms 4184 KB Output is correct
7 Correct 31 ms 4140 KB Output is correct
8 Correct 36 ms 4344 KB Output is correct
9 Correct 30 ms 4172 KB Output is correct
10 Correct 31 ms 4196 KB Output is correct
11 Correct 30 ms 2548 KB Output is correct
12 Correct 19 ms 2556 KB Output is correct
13 Correct 18 ms 2472 KB Output is correct
14 Correct 18 ms 2508 KB Output is correct
15 Correct 34 ms 4264 KB Output is correct
16 Correct 28 ms 2540 KB Output is correct
17 Correct 19 ms 2632 KB Output is correct
18 Correct 33 ms 4244 KB Output is correct
19 Correct 29 ms 4192 KB Output is correct
20 Correct 40 ms 2548 KB Output is correct
21 Correct 211 ms 16048 KB Output is correct
22 Correct 235 ms 15872 KB Output is correct
23 Correct 222 ms 16032 KB Output is correct
24 Correct 205 ms 15840 KB Output is correct
25 Correct 205 ms 15976 KB Output is correct
26 Correct 239 ms 16104 KB Output is correct
27 Correct 226 ms 16124 KB Output is correct
28 Correct 224 ms 15948 KB Output is correct
29 Correct 238 ms 16200 KB Output is correct
30 Correct 225 ms 16188 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
38 Correct 1 ms 328 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 328 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 348 KB Output is correct
54 Correct 2 ms 344 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 2 ms 368 KB Output is correct
58 Correct 2 ms 360 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 2 ms 340 KB Output is correct
61 Correct 140 ms 15832 KB Output is correct
62 Correct 152 ms 16160 KB Output is correct
63 Correct 254 ms 15780 KB Output is correct
64 Correct 132 ms 16080 KB Output is correct
65 Correct 184 ms 16076 KB Output is correct
66 Correct 247 ms 16056 KB Output is correct
67 Correct 169 ms 15696 KB Output is correct
68 Correct 260 ms 16132 KB Output is correct
69 Correct 230 ms 15752 KB Output is correct
70 Correct 243 ms 15728 KB Output is correct