#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
struct AIB {
int n;
vll V;
AIB(int N) : n(N + 2), V(N + 2, 0) {}
void update(int p, ll v) {
++p;
while(p < n) {
V[p] = max(V[p], v);
p += p & -p;
}
}
ll query(int p) {
++p;
ll re = 0;
if(p < 0) return 0;
while(p) {
re = max(re, V[p]);
p -= p & -p;
}
return re;
}
};
struct maxSuf {
int n;
AIB A;
maxSuf(int N) : n(N), A(N) {}
void update(int p, ll v) { A.update(n - p - 1, v); }
ll query(int p) { return A.query(n - p - 1); }
};
ll max_weights(int n, int m, vi X, vi Y, vi W) {
vll DP0(m, 0), DP1(m, 0), MaTot(n, 0), Ma0(n, 0);
vector<vi> P(n);
for(int i = 0; i < m; ++i) {
P[X[i]].push_back(i);
}
AIB MP(n);
maxSuf MS(n);
for(int i = 0; i < n; ++i) {
sort(P[i].begin(), P[i].end(), [&](auto a, auto b) { return Y[a] > Y[b]; });
if(i > 0) {
for(auto it : P[i]) {
DP0[it] = (i > 1 ? MaTot[i - 2] : 0) + W[it];
DP0[it] = max(DP0[it], W[it] + MS.query(Y[it] + 1));
MS.update(Y[it], DP0[it]);
MaTot[i] = max(MaTot[i], DP0[it]);
Ma0[i] = max(Ma0[i], DP0[it]);
}
}
reverse(P[i].begin(), P[i].end());
if(i + 1 < n) {
for(auto it : P[i]) {
DP1[it] = max((i > 1 ? MaTot[i - 2] : 0), (i ? Ma0[i - 1] : 0)) + W[it];
DP1[it] = max(DP1[it], W[it] + MP.query(Y[it] - 1));
MP.update(Y[it], DP1[it]);
MaTot[i] = max(MaTot[i], DP1[it]);
}
}
if(i) {
MaTot[i] = max(MaTot[i], MaTot[i - 1]);
Ma0[i] = max(Ma0[i], Ma0[i - 1]);
}
}
return MaTot.back();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
10192 KB |
Output is correct |
2 |
Correct |
27 ms |
11980 KB |
Output is correct |
3 |
Correct |
3 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
88 ms |
25284 KB |
Output is correct |
6 |
Correct |
94 ms |
26964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
49 ms |
15372 KB |
Output is correct |
3 |
Correct |
56 ms |
18212 KB |
Output is correct |
4 |
Correct |
28 ms |
10196 KB |
Output is correct |
5 |
Correct |
27 ms |
12080 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
23 ms |
10356 KB |
Output is correct |
13 |
Correct |
35 ms |
11988 KB |
Output is correct |
14 |
Correct |
27 ms |
10392 KB |
Output is correct |
15 |
Correct |
26 ms |
11588 KB |
Output is correct |
16 |
Correct |
23 ms |
10456 KB |
Output is correct |
17 |
Correct |
25 ms |
11480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
17 ms |
10028 KB |
Output is correct |
4 |
Correct |
12 ms |
9120 KB |
Output is correct |
5 |
Correct |
30 ms |
14532 KB |
Output is correct |
6 |
Correct |
31 ms |
13764 KB |
Output is correct |
7 |
Correct |
32 ms |
14420 KB |
Output is correct |
8 |
Correct |
29 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
10 ms |
2500 KB |
Output is correct |
18 |
Correct |
9 ms |
2508 KB |
Output is correct |
19 |
Correct |
10 ms |
2396 KB |
Output is correct |
20 |
Correct |
11 ms |
2508 KB |
Output is correct |
21 |
Correct |
10 ms |
2396 KB |
Output is correct |
22 |
Correct |
20 ms |
4580 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
6 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
10 ms |
2500 KB |
Output is correct |
18 |
Correct |
9 ms |
2508 KB |
Output is correct |
19 |
Correct |
10 ms |
2396 KB |
Output is correct |
20 |
Correct |
11 ms |
2508 KB |
Output is correct |
21 |
Correct |
10 ms |
2396 KB |
Output is correct |
22 |
Correct |
20 ms |
4580 KB |
Output is correct |
23 |
Correct |
3 ms |
860 KB |
Output is correct |
24 |
Correct |
6 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
47 ms |
10396 KB |
Output is correct |
29 |
Correct |
73 ms |
14168 KB |
Output is correct |
30 |
Correct |
70 ms |
19540 KB |
Output is correct |
31 |
Correct |
68 ms |
19540 KB |
Output is correct |
32 |
Correct |
66 ms |
19536 KB |
Output is correct |
33 |
Correct |
69 ms |
19540 KB |
Output is correct |
34 |
Correct |
78 ms |
19676 KB |
Output is correct |
35 |
Correct |
25 ms |
7772 KB |
Output is correct |
36 |
Correct |
79 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
17 ms |
10028 KB |
Output is correct |
4 |
Correct |
12 ms |
9120 KB |
Output is correct |
5 |
Correct |
30 ms |
14532 KB |
Output is correct |
6 |
Correct |
31 ms |
13764 KB |
Output is correct |
7 |
Correct |
32 ms |
14420 KB |
Output is correct |
8 |
Correct |
29 ms |
14412 KB |
Output is correct |
9 |
Correct |
29 ms |
14168 KB |
Output is correct |
10 |
Correct |
27 ms |
10328 KB |
Output is correct |
11 |
Correct |
54 ms |
20184 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
2 ms |
5724 KB |
Output is correct |
20 |
Correct |
2 ms |
5724 KB |
Output is correct |
21 |
Correct |
2 ms |
5724 KB |
Output is correct |
22 |
Correct |
36 ms |
14020 KB |
Output is correct |
23 |
Correct |
64 ms |
20320 KB |
Output is correct |
24 |
Correct |
58 ms |
20648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
10192 KB |
Output is correct |
2 |
Correct |
27 ms |
11980 KB |
Output is correct |
3 |
Correct |
3 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
88 ms |
25284 KB |
Output is correct |
6 |
Correct |
94 ms |
26964 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
49 ms |
15372 KB |
Output is correct |
9 |
Correct |
56 ms |
18212 KB |
Output is correct |
10 |
Correct |
28 ms |
10196 KB |
Output is correct |
11 |
Correct |
27 ms |
12080 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
23 ms |
10356 KB |
Output is correct |
19 |
Correct |
35 ms |
11988 KB |
Output is correct |
20 |
Correct |
27 ms |
10392 KB |
Output is correct |
21 |
Correct |
26 ms |
11588 KB |
Output is correct |
22 |
Correct |
23 ms |
10456 KB |
Output is correct |
23 |
Correct |
25 ms |
11480 KB |
Output is correct |
24 |
Correct |
2 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
17 ms |
10028 KB |
Output is correct |
27 |
Correct |
12 ms |
9120 KB |
Output is correct |
28 |
Correct |
30 ms |
14532 KB |
Output is correct |
29 |
Correct |
31 ms |
13764 KB |
Output is correct |
30 |
Correct |
32 ms |
14420 KB |
Output is correct |
31 |
Correct |
29 ms |
14412 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
10 ms |
2500 KB |
Output is correct |
49 |
Correct |
9 ms |
2508 KB |
Output is correct |
50 |
Correct |
10 ms |
2396 KB |
Output is correct |
51 |
Correct |
11 ms |
2508 KB |
Output is correct |
52 |
Correct |
10 ms |
2396 KB |
Output is correct |
53 |
Correct |
20 ms |
4580 KB |
Output is correct |
54 |
Correct |
3 ms |
860 KB |
Output is correct |
55 |
Correct |
6 ms |
1628 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
860 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
47 ms |
10396 KB |
Output is correct |
60 |
Correct |
73 ms |
14168 KB |
Output is correct |
61 |
Correct |
70 ms |
19540 KB |
Output is correct |
62 |
Correct |
68 ms |
19540 KB |
Output is correct |
63 |
Correct |
66 ms |
19536 KB |
Output is correct |
64 |
Correct |
69 ms |
19540 KB |
Output is correct |
65 |
Correct |
78 ms |
19676 KB |
Output is correct |
66 |
Correct |
25 ms |
7772 KB |
Output is correct |
67 |
Correct |
79 ms |
20052 KB |
Output is correct |
68 |
Correct |
29 ms |
14168 KB |
Output is correct |
69 |
Correct |
27 ms |
10328 KB |
Output is correct |
70 |
Correct |
54 ms |
20184 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
344 KB |
Output is correct |
76 |
Correct |
0 ms |
344 KB |
Output is correct |
77 |
Correct |
2 ms |
5724 KB |
Output is correct |
78 |
Correct |
2 ms |
5724 KB |
Output is correct |
79 |
Correct |
2 ms |
5724 KB |
Output is correct |
80 |
Correct |
2 ms |
5724 KB |
Output is correct |
81 |
Correct |
36 ms |
14020 KB |
Output is correct |
82 |
Correct |
64 ms |
20320 KB |
Output is correct |
83 |
Correct |
58 ms |
20648 KB |
Output is correct |
84 |
Correct |
87 ms |
24656 KB |
Output is correct |
85 |
Correct |
92 ms |
24676 KB |
Output is correct |
86 |
Correct |
102 ms |
26448 KB |
Output is correct |
87 |
Correct |
91 ms |
27216 KB |
Output is correct |
88 |
Correct |
0 ms |
344 KB |
Output is correct |
89 |
Correct |
97 ms |
27368 KB |
Output is correct |
90 |
Correct |
84 ms |
25704 KB |
Output is correct |
91 |
Correct |
100 ms |
25200 KB |
Output is correct |