#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 505050;
int n, a[N], d[N], yes[N];
struct seg {
ll sum[8 * N]; int tree[8 * N], lazy[8 * N];
void push(int l, int r, int x) {
if (lazy[x]) {
tree[x] = lazy[x];
sum[x] = 1ll * (r - l + 1) * lazy[x];
if (l != r) {
lazy[2 * x] = lazy[x];
lazy[2 * x + 1] = lazy[x];
}
lazy[x] = 0;
}
}
void update(int a, int b, int v, int l, int r, int x) {
push(l, r, x);
if (b < l || a > r)
return;
if (a <= l && r <= b) {
lazy[x] = v; push(l, r, x);
return;
}
int m = (l + r) / 2;
update(a, b, v, l, m, 2 * x);
update(a, b, v, m + 1, r, 2 * x + 1);
tree[x] = max(tree[2 * x], tree[2 * x + 1]);
sum[x] = sum[2 * x] + sum[2 * x + 1];
}
int query(int k, int l, int r, int x) {
push(l, r, x);
if (l == r)
return l;
int m = (l + r) / 2;
push(l, m, 2 * x);
if (tree[2 * x] > k)
return query(k, l, m, 2 * x);
else
return query(k, m + 1, r, 2 * x + 1);
}
ll get_sum(int a, int b, int l, int r, int x) {
push(l, r, x);
if (b < l || a > r)
return 0ll;
if (a <= l && r <= b)
return sum[x];
int m = (l + r) / 2;
return get_sum(a, b, l, m, 2 * x) + get_sum(a, b, m + 1, r, 2 * x + 1);
}
} t1;
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] != -1)
ans += a[i];
d[i] = 1e9;
}
int p = -1, q = -1;
for (int i = 1; i <= n; i++) {
if (a[i] == -1)
p = i;
else {
if (p != -1)
d[i] = i - p;
}
}
p = -1;
for (int i = n; i >= 1; i--) {
if (a[i] == -1)
p = i;
else {
if (p != -1)
d[i] = min(d[i], p - i);
if (p != -1 && p < q)
yes[i] = 1;
if (q == -1)
yes[i] = 2;
q = i;
}
}
t1.update(n, 2 * n, 1e9, 1, 2 * n, 1);
int l = n, x = -1, offset = 0, store = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == -1) {
offset++;
continue;
}
a[i] += store;
store = 0;
if (a[i] == 0)
continue;
a[i] -= 1;
store = 1;
int p = t1.query(2 * d[i], 1, 2 * n, 1);
ll s = t1.get_sum(1, p - 1, 1, 2 * n, 1) + offset;
if (s <= 2 * (i - 1)) {
int r = (2 * (i - 1) - s) / (2 * d[i]);
r = min(r, a[i] - 1);
if (r != -1)
t1.update(p, p + r, 2 * d[i], 1, 2 * n, 1);
}
if (store && yes[i] == 1)
store = 0, --l;
if (yes[i] == 2) {
x = i;
break;
}
++offset;
}
p = t1.query((int)1e9 - 1, 1, 2 * n, 1);
if (store && offset + t1.get_sum(l, p - 1, 1, 2 * n, 1) <= 2 * (x - 1))
--l;
cout << ans - (p - l);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
328 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 |
328 KB |
Output is correct |
16 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
328 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 |
328 KB |
Output is correct |
16 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
328 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 |
328 KB |
Output is correct |
16 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
328 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 |
328 KB |
Output is correct |
16 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
328 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 |
328 KB |
Output is correct |
16 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |