#include <stdio.h>
#include <string.h>
#define N 400000
long long max(long long a, long long b) { return a > b ? a : b; }
long long ss[N + 1];
int xx[N], yy[N], dd[N]; long long dp[N]; int pp[N], qq[N], hh[N], tt[N];
int n, m;
int cross(int i, int j) {
int lower = -1, upper = m - yy[j] + 1;
while (upper - lower > 1) {
int d = (lower + upper) / 2;
if (dp[i] - ss[yy[i] + d] > dp[j] - ss[yy[j] + d])
upper = d;
else
lower = d;
}
return lower;
}
void merge(int i, int j) {
int u, v, d;
u = tt[i], v = hh[j];
while (1) {
d = cross(u, v);
if (u != hh[i] && cross(pp[u], u) <= d)
u = pp[u];
else if (v != tt[j] && cross(v, qq[v]) >= d)
v = qq[v];
else {
qq[u] = v, pp[v] = u;
break;
}
}
hh[j] = hh[i];
}
long long query(int i, int d) {
while (hh[i] != tt[i] && cross(hh[i], qq[hh[i]]) >= d)
hh[i] = qq[hh[i]];
return dp[hh[i]] - ss[yy[hh[i]] + d];
}
int main() {
static int aa[N], ii[N], ww[N], qu[N];
int cnt, i, i_, j, a, x, y, d;
long long z;
scanf("%d", &n);
memset(ii, -1, n * sizeof *ii);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]), aa[i]--;
if (aa[i] >= 0)
ii[aa[i]] = i;
}
for (a = 0; a < n; a++)
scanf("%d", &ww[a]);
m = 0;
for (a = 0; a < n; a++)
if ((i = ii[a]) != -1)
xx[i] = m;
else
ss[m + 1] = ss[m] + ww[a], m++;
if (m == n) {
for (i = 0; i < n; i++)
printf("%lld ", ss[n] - ss[n - 1 - i]);
printf("\n");
return 0;
}
for (i = 0; i < n; i++)
pp[i] = qq[i] = -1, hh[i] = tt[i] = i;
cnt = 0, y = 0, a = -1;
for (j = 0; j < n; j++) {
if (aa[j] == -2)
y++;
else if (a > aa[j])
aa[j] = -1;
else {
a = aa[j];
yy[j] = y, dd[j] = xx[j] - yy[j];
if (dd[j] < 0)
aa[j] = -1;
}
if (aa[j] >= 0) {
i_ = -1;
while (cnt && dd[i = qu[cnt - 1]] < dd[j]) {
if (i_ == -1)
i_ = i;
else
merge(i, i_);
cnt--;
}
dp[j] = cnt ? dp[i] - ss[xx[i]] : -ss[dd[j]];
if (i_ != -1)
dp[j] = max(dp[j], query(i_, dd[j])), merge(i_, j);
dp[j] += ss[xx[j]] + ww[aa[j]];
qu[cnt++] = j;
}
x = m, d = x - y;
z = 0;
for (i = j; i >= 0; i--) {
if (aa[i] < 0)
continue;
if (dd[i] >= d)
break;
z = max(z, dp[i] + ss[x] - ss[yy[i] + d]);
}
if (i >= 0)
z = max(z, dp[i] + ss[x] - ss[xx[i]]);
else
z = max(z, ss[x] - ss[d]);
printf("%lld ", z);
}
printf("\n");
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
Main.c:63:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d", &ww[a]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
5 ms |
1108 KB |
Output is correct |
4 |
Correct |
44 ms |
7148 KB |
Output is correct |
5 |
Correct |
86 ms |
13816 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
284 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
284 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1071 ms |
15596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
284 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
5 ms |
1108 KB |
Output is correct |
14 |
Correct |
44 ms |
7148 KB |
Output is correct |
15 |
Correct |
86 ms |
13816 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
292 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |