#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, 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) {
while (cnt && dd[i = qu[cnt - 1]] < dd[j]) {
if (i < j - 1)
merge(i, j - 1);
cnt--;
}
dp[j] = cnt ? dp[i] - ss[xx[i]] : -ss[dd[j]];
qu[cnt++] = j;
if (j > 0) {
dp[j] = max(dp[j], query(j - 1, dd[j]));
merge(j - 1, j);
}
dp[j] += ss[xx[j]] + ww[aa[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 |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
820 KB |
Output is correct |
4 |
Correct |
52 ms |
6928 KB |
Output is correct |
5 |
Correct |
94 ms |
13552 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
15312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
820 KB |
Output is correct |
14 |
Correct |
52 ms |
6928 KB |
Output is correct |
15 |
Correct |
94 ms |
13552 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |