#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int INF = 1e9 + 100;
int a[500500];
struct Seg {
int tree[2002000], lazy[2002000];
void init(int i, int l, int r, const vector<int> &V) {
lazy[i] = 0;
if (l==r) {tree[i] = V[l]; return;}
int m = (l+r)/2;
init(i<<1, l, m, V);
init(i<<1|1, m+1, r, V);
tree[i] = min(tree[i<<1], tree[i<<1|1]);
}
void propagate(int i, int l, int r) {
tree[i] += lazy[i];
if (l!=r) lazy[i<<1] += lazy[i], lazy[i<<1|1] += lazy[i];
lazy[i] = 0;
}
void update(int i, int l, int r, int s, int e, int x) {
propagate(i, l, r);
if (r<s || e<l) return;
if (s<=l && r<=e) {
lazy[i] += x;
propagate(i, l, r);
return;
}
int m = (l+r)/2;
update(i<<1, l, m, s, e, x);
update(i<<1|1, m+1, r, s, e, x);
tree[i] = min(tree[i<<1], tree[i<<1|1]);
}
int query(int i, int l, int r, int s, int e) {
propagate(i, l, r);
if (r<s || e<l) return INF;
if (s<=l && r<=e) return tree[i];
int m = (l+r)/2;
return min(query(i<<1, l, m, s, e), query(i<<1|1, m+1, r, s, e));
}
}tree;
int main() {
int n;
scanf("%d", &n);
for (int i=1;i<=n;i++) scanf("%d", a+i);
a[0] = a[n+1] = -1;
ll ans = 0;
for (int i=1;i<=n;i++) ans += max(a[i], 0);
if (ans==0) {
printf("0\n");
return 0;
}
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> pq;
vector<int> V;
for (int i=0,j=0;i<=n;i=j) {
if (a[i]!=-1){j = i+1; continue;}
j = i+1;
while(j <= n && a[j]!=-1) j++;
int ti = i==0?-INF:i, tj = j==n+1?INF:j;
int r = -1, mx = -1, idx = -1;
for (int k=i+1;k<j;k++) if (a[k] > 0) {
r = k;
if (mx < min(k-ti, tj-k)) {
mx = min(k-ti, tj-k);
idx = k;
}
}
if (r < 0) continue;
V.push_back(r-1);
ans--;
for (int k=i+1;k<j;k++) if (a[k] > 0){
int c = k==idx?a[k]-1:a[k];
if (c==0) continue;
if (k-ti <= tj-k) pq.push({k-ti, c, (int)V.size()-1});
else pq.push({tj-k, c, (int)V.size()-1});
}
}
int sz = V.size();
tree.init(1, 0, sz-1, V);
while(!pq.empty()) {
auto [w, c, i] = pq.top(); pq.pop();
if (tree.query(1, 0, sz-1, i, sz-1) < w*2) continue;
ans--;
tree.update(1, 0, sz-1, i, sz-1, -w*2);
if (c > 1) pq.push({w, c-1, i});
}
printf("%lld\n", ans);
}
Compilation message
tortoise.cpp: In function 'int main()':
tortoise.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
tortoise.cpp:57:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | for (int i=1;i<=n;i++) scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4540 KB |
Output is correct |
24 |
Correct |
1 ms |
4604 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4540 KB |
Output is correct |
24 |
Correct |
1 ms |
4604 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4540 KB |
Output is correct |
24 |
Correct |
1 ms |
4604 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4540 KB |
Output is correct |
24 |
Correct |
1 ms |
4604 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4600 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Correct |
1 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4540 KB |
Output is correct |
24 |
Correct |
1 ms |
4604 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
1 ms |
4432 KB |
Output is correct |
27 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |