#include <stdio.h>
#include <string.h>
#define N 400000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
int n, k;
int ww[N]; long long ss[N + 1];
int xx[N], yy[N], dd[N], pp[N], qq[N], hh[N], tt[N], ii[N]; long long dp[N];
int cross(int i, int j) {
int lower = -1, upper = k - 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];
}
void dc(int il, int ir, int jl, int jr) {
int i, i_, j, d;
long long z, z_;
if (jl > jr)
return;
j = (jl + jr) / 2;
d = k - yy[j];
z_ = -INF;
for (i_ = i = max(il, ii[j] + 1); i <= ir && i <= j; i++)
if (dp[i] != -INF) {
z = dp[i] - ss[yy[i] + d];
if (z_ < z)
z_ = z, i_ = i;
}
if (i_ == il - 1)
i_ = il;
if (ii[j] >= 0)
z_ = max(z_, dp[ii[j]] - ss[xx[ii[j]]]);
else
z_ = max(z_, -ss[d]);
z_ += ss[k];
dc(il, i_, jl, j - 1), printf("%lld ", z_), dc(i_, ir, j + 1, jr);
}
int main() {
static int aa[N], qu[N];
int cnt, h, i, i_, j, a, y;
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]);
k = 0;
for (a = 0; a < n; a++)
if ((i = ii[a]) != -1)
xx[i] = k;
else
ss[k + 1] = ss[k] + ww[a], k++;
for (i = 0; i < n; i++)
pp[i] = qq[i] = -1, hh[i] = tt[i] = i;
y = 0, a = -1, cnt = 0, h = 0;
for (j = 0; j < n; j++) {
if (aa[j] < 0)
y++;
yy[j] = y, dd[j] = xx[j] - yy[j];
a = max(a, aa[j]);
if (aa[j] == a && dd[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]));
dp[j] += ss[xx[j]] + ww[aa[j]];
if (i_ != -1)
merge(i_, j);
qu[cnt++] = j;
} else
dp[j] = -INF;
h = min(h, cnt);
while (h < cnt && dd[qu[h]] >= k - y)
h++;
ii[j] = h == 0 ? -1 : qu[h - 1];
}
dc(0, n - 1, 0, n - 1), printf("\n");
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:81:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:84:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
Main.c:89:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d", &ww[a]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
9 ms |
1540 KB |
Output is correct |
4 |
Correct |
58 ms |
13180 KB |
Output is correct |
5 |
Correct |
107 ms |
25980 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
292 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
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 |
Correct |
1 ms |
284 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
292 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
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 |
Correct |
1 ms |
284 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
1 ms |
428 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
432 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
23368 KB |
Output is correct |
2 |
Correct |
94 ms |
25220 KB |
Output is correct |
3 |
Correct |
105 ms |
24364 KB |
Output is correct |
4 |
Correct |
118 ms |
25176 KB |
Output is correct |
5 |
Correct |
42 ms |
6572 KB |
Output is correct |
6 |
Correct |
67 ms |
13036 KB |
Output is correct |
7 |
Correct |
125 ms |
19160 KB |
Output is correct |
8 |
Correct |
73 ms |
16320 KB |
Output is correct |
9 |
Correct |
93 ms |
25276 KB |
Output is correct |
10 |
Correct |
22 ms |
4820 KB |
Output is correct |
11 |
Correct |
20 ms |
5560 KB |
Output is correct |
12 |
Correct |
103 ms |
25428 KB |
Output is correct |
13 |
Correct |
102 ms |
25964 KB |
Output is correct |
14 |
Correct |
90 ms |
26276 KB |
Output is correct |
15 |
Correct |
101 ms |
26368 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
9 ms |
1540 KB |
Output is correct |
14 |
Correct |
58 ms |
13180 KB |
Output is correct |
15 |
Correct |
107 ms |
25980 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
292 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 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 |
Correct |
1 ms |
284 KB |
Output is correct |
30 |
Correct |
1 ms |
288 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
436 KB |
Output is correct |
35 |
Correct |
1 ms |
428 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
432 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
112 ms |
23368 KB |
Output is correct |
56 |
Correct |
94 ms |
25220 KB |
Output is correct |
57 |
Correct |
105 ms |
24364 KB |
Output is correct |
58 |
Correct |
118 ms |
25176 KB |
Output is correct |
59 |
Correct |
42 ms |
6572 KB |
Output is correct |
60 |
Correct |
67 ms |
13036 KB |
Output is correct |
61 |
Correct |
125 ms |
19160 KB |
Output is correct |
62 |
Correct |
73 ms |
16320 KB |
Output is correct |
63 |
Correct |
93 ms |
25276 KB |
Output is correct |
64 |
Correct |
22 ms |
4820 KB |
Output is correct |
65 |
Correct |
20 ms |
5560 KB |
Output is correct |
66 |
Correct |
103 ms |
25428 KB |
Output is correct |
67 |
Correct |
102 ms |
25964 KB |
Output is correct |
68 |
Correct |
90 ms |
26276 KB |
Output is correct |
69 |
Correct |
101 ms |
26368 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
0 ms |
292 KB |
Output is correct |
74 |
Correct |
0 ms |
340 KB |
Output is correct |
75 |
Correct |
111 ms |
30668 KB |
Output is correct |
76 |
Correct |
102 ms |
31416 KB |
Output is correct |
77 |
Correct |
100 ms |
25340 KB |
Output is correct |
78 |
Correct |
160 ms |
31016 KB |
Output is correct |
79 |
Correct |
117 ms |
25164 KB |
Output is correct |
80 |
Correct |
39 ms |
7136 KB |
Output is correct |
81 |
Correct |
23 ms |
6112 KB |
Output is correct |
82 |
Correct |
19 ms |
5460 KB |
Output is correct |
83 |
Correct |
15 ms |
4988 KB |
Output is correct |
84 |
Correct |
19 ms |
3868 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
0 ms |
340 KB |
Output is correct |
87 |
Correct |
0 ms |
340 KB |
Output is correct |
88 |
Correct |
13 ms |
3288 KB |
Output is correct |
89 |
Correct |
105 ms |
30524 KB |
Output is correct |
90 |
Correct |
120 ms |
24360 KB |
Output is correct |
91 |
Correct |
90 ms |
25276 KB |
Output is correct |
92 |
Correct |
99 ms |
30408 KB |
Output is correct |
93 |
Correct |
169 ms |
30740 KB |
Output is correct |
94 |
Correct |
208 ms |
30312 KB |
Output is correct |
95 |
Correct |
217 ms |
29588 KB |
Output is correct |
96 |
Correct |
26 ms |
7648 KB |
Output is correct |
97 |
Correct |
27 ms |
7664 KB |
Output is correct |
98 |
Correct |
24 ms |
6348 KB |
Output is correct |
99 |
Correct |
43 ms |
7744 KB |
Output is correct |
100 |
Correct |
44 ms |
6476 KB |
Output is correct |
101 |
Correct |
25 ms |
7628 KB |
Output is correct |
102 |
Correct |
27 ms |
6380 KB |
Output is correct |
103 |
Correct |
23 ms |
6500 KB |
Output is correct |
104 |
Correct |
18 ms |
4944 KB |
Output is correct |
105 |
Correct |
22 ms |
6576 KB |
Output is correct |
106 |
Correct |
29 ms |
7956 KB |
Output is correct |
107 |
Correct |
52 ms |
7440 KB |
Output is correct |
108 |
Correct |
1 ms |
340 KB |
Output is correct |
109 |
Correct |
0 ms |
340 KB |
Output is correct |