# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
810317 |
2023-08-06T08:25:19 Z |
vuavisao |
Feast (NOI19_feast) |
C++17 |
|
69 ms |
12156 KB |
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;
const ll N = 3e5 + 10;
const ll INF = 1e18;
struct dsu {
ll n = 0;
vector<ll> parent = {};
void resize(ll _n) {
n = _n;
parent.resize(n + 10); for(ll i = 1; i <= n; ++ i) parent[i] = i;
}
dsu() {};
dsu(ll _n) { this -> resize(_n); };
ll ancestor(ll u) {
if(parent[u] == u) return u;
return parent[u] = ancestor(parent[u]);
}
bool join(ll u, ll v) {
u = ancestor(u); v = ancestor(v);
if(u == v) return false;
parent[v] = u;
return true;
}
};
struct state {
ll val = 0, idx = 0;
state() {};
state(ll _val, ll _idx) : val(_val), idx(_idx) {};
bool operator < (const state& other) const {
return abs(this -> val) > abs(other.val);
}
};
ll n, k;
ll a[N];
ll nxt[N];
ll res;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> k;
for(ll i = 1; i <= n; ++ i) cin >> a[i];
vector<ll> List = {};
for(ll i = 1, j = 1; i <= n; i = j) {
ll s = 0;
while(j <= n && a[i] * a[j] >= 0) {
s += a[j];
++ j;
}
// cout << s << '\n';
List.push_back(s);
}
if(!List.empty() && List.back() < 0) List.pop_back();
if(!List.empty() && List.front() < 0) {
reverse(List.begin(), List.end());
List.pop_back();
}
n = (ll) List.size();
ll have_p = 0;
priority_queue<state> pq = {};
for(ll i = 1; i <= n; ++ i) {
a[i] = List[i - 1];
nxt[i] = i + 1;
if(a[i] >= 0) {
++ have_p;
}
pq.push(state(a[i], i));
}
dsu dsu(n);
while(!pq.empty() && have_p > k) {
ll val = pq.top().val, idx = pq.top().idx;
pq.pop();
if(val != a[idx]) continue;
if(val >= 0) -- have_p;
if(idx > 1) {
ll le = dsu.ancestor(idx - 1);
val += a[le];
if(a[le] >= 0) -- have_p;
dsu.join(le, idx);
a[idx] = - INF; nxt[le] = nxt[idx]; idx = le;
}
ll ri = nxt[idx];
if(ri <= n) {
val += a[ri];
if(a[ri] >= 0) -- have_p;
dsu.join(idx, ri);
a[ri] = - INF; nxt[idx] = nxt[ri];
}
if(val >= 0) ++ have_p;
a[idx] = val;
pq.push(state(val, idx));
}
ll res = 0;
while(!pq.empty()) {
ll val = pq.top().val, idx = pq.top().idx;
pq.pop();
if(val < 0 || val != a[idx]) continue;
// cout << val << ' ' << idx << '\n';
res += val;
}
cout << res;
return (0 ^ 0);
}
/// Code by vuavisao
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
5368 KB |
Output is correct |
2 |
Correct |
24 ms |
5480 KB |
Output is correct |
3 |
Correct |
25 ms |
5452 KB |
Output is correct |
4 |
Correct |
25 ms |
5488 KB |
Output is correct |
5 |
Correct |
23 ms |
5452 KB |
Output is correct |
6 |
Correct |
24 ms |
5324 KB |
Output is correct |
7 |
Correct |
24 ms |
5324 KB |
Output is correct |
8 |
Correct |
24 ms |
5420 KB |
Output is correct |
9 |
Correct |
32 ms |
5384 KB |
Output is correct |
10 |
Correct |
36 ms |
5344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
3648 KB |
Output is correct |
2 |
Correct |
24 ms |
3788 KB |
Output is correct |
3 |
Correct |
15 ms |
3668 KB |
Output is correct |
4 |
Correct |
16 ms |
3760 KB |
Output is correct |
5 |
Correct |
24 ms |
5356 KB |
Output is correct |
6 |
Correct |
15 ms |
3704 KB |
Output is correct |
7 |
Correct |
16 ms |
3708 KB |
Output is correct |
8 |
Correct |
24 ms |
5452 KB |
Output is correct |
9 |
Correct |
35 ms |
5296 KB |
Output is correct |
10 |
Correct |
24 ms |
3716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
12092 KB |
Output is correct |
2 |
Correct |
69 ms |
11952 KB |
Output is correct |
3 |
Correct |
67 ms |
11980 KB |
Output is correct |
4 |
Correct |
66 ms |
11900 KB |
Output is correct |
5 |
Correct |
64 ms |
12084 KB |
Output is correct |
6 |
Correct |
66 ms |
12104 KB |
Output is correct |
7 |
Correct |
65 ms |
12156 KB |
Output is correct |
8 |
Correct |
65 ms |
11980 KB |
Output is correct |
9 |
Correct |
65 ms |
12124 KB |
Output is correct |
10 |
Correct |
69 ms |
12092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
5368 KB |
Output is correct |
2 |
Correct |
24 ms |
5480 KB |
Output is correct |
3 |
Correct |
25 ms |
5452 KB |
Output is correct |
4 |
Correct |
25 ms |
5488 KB |
Output is correct |
5 |
Correct |
23 ms |
5452 KB |
Output is correct |
6 |
Correct |
24 ms |
5324 KB |
Output is correct |
7 |
Correct |
24 ms |
5324 KB |
Output is correct |
8 |
Correct |
24 ms |
5420 KB |
Output is correct |
9 |
Correct |
32 ms |
5384 KB |
Output is correct |
10 |
Correct |
36 ms |
5344 KB |
Output is correct |
11 |
Correct |
20 ms |
3648 KB |
Output is correct |
12 |
Correct |
24 ms |
3788 KB |
Output is correct |
13 |
Correct |
15 ms |
3668 KB |
Output is correct |
14 |
Correct |
16 ms |
3760 KB |
Output is correct |
15 |
Correct |
24 ms |
5356 KB |
Output is correct |
16 |
Correct |
15 ms |
3704 KB |
Output is correct |
17 |
Correct |
16 ms |
3708 KB |
Output is correct |
18 |
Correct |
24 ms |
5452 KB |
Output is correct |
19 |
Correct |
35 ms |
5296 KB |
Output is correct |
20 |
Correct |
24 ms |
3716 KB |
Output is correct |
21 |
Correct |
67 ms |
12092 KB |
Output is correct |
22 |
Correct |
69 ms |
11952 KB |
Output is correct |
23 |
Correct |
67 ms |
11980 KB |
Output is correct |
24 |
Correct |
66 ms |
11900 KB |
Output is correct |
25 |
Correct |
64 ms |
12084 KB |
Output is correct |
26 |
Correct |
66 ms |
12104 KB |
Output is correct |
27 |
Correct |
65 ms |
12156 KB |
Output is correct |
28 |
Correct |
65 ms |
11980 KB |
Output is correct |
29 |
Correct |
65 ms |
12124 KB |
Output is correct |
30 |
Correct |
69 ms |
12092 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |