#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 302020
typedef long long ll;
typedef pair<int, int> pii;
ll tdp[MAX]; // full dp
ll bdp[MAX]; // zero dp
ll sum[MAX];
vector<pii> locs[MAX];
struct bit {
int N;
bool rev;
vector<ll> tree;
bit(int N, bool rev) :N(N + 2), rev(rev) { tree = vector<ll>(N + 3, -1e18); }
void upd(int i, ll v) {
if (rev) i = N - i - 1;
i++;
while (i <= N) {
tree[i] = max(tree[i], v);
i += i & -i;
}
}
ll get(int i) {
if (rev) i = N - i - 1;
i++;
ll ans = -1e18;
while (i) {
ans = max(ans, tree[i]);
i -= i & -i;
}
return ans;
}
};
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
int i;
for (auto& v : X) v++;
for (auto& v : Y) v++;
for (i = 0; i < M; i++) locs[X[i]].emplace_back(Y[i], W[i]);
for (i = 1; i <= N; i++) if (locs[i].size()) {
sort(locs[i].begin(), locs[i].end());
for (auto& [_, v] : locs[i]) sum[i] += v;
}
bit fenu(N, false), fend(N, true);
fenu.upd(0, 0);
for (i = 0; i <= N; i++) tdp[i] = bdp[i] = -1e18;
for (i = 1; i <= N; i++) {
tdp[i] = tdp[i - 1];
bdp[i] = bdp[i - 1];
if (i > 2) fend.upd(N + 1, tdp[i - 2]);
if (i > 1) fenu.upd(0, bdp[i - 1]);
if (i > 1) fend.upd(N + 1, 0);
for (auto& [y, w] : locs[i]) fenu.upd(y, fenu.get(y - 1) + w);
tdp[i] = max(tdp[i], fenu.get(N + 1));
for (int j = (int)locs[i].size() - 1; j >= 0; j--) {
ll y = locs[i][j].first;
ll w = locs[i][j].second;
fend.upd(y, fend.get(y + 1) + w);
}
bdp[i] = max(bdp[i], fend.get(0));
if (i > 2) tdp[i] = max(tdp[i], sum[i] + tdp[i - 2]);
}
ll ans = fend.get(0);
for (i = 1; i < N; i++) ans = max(ans, tdp[i]);
for (i = 1; i <= N; i++) ans = max(ans, bdp[i]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
12872 KB |
Output is correct |
2 |
Correct |
57 ms |
13736 KB |
Output is correct |
3 |
Correct |
17 ms |
10452 KB |
Output is correct |
4 |
Correct |
18 ms |
10452 KB |
Output is correct |
5 |
Correct |
155 ms |
20144 KB |
Output is correct |
6 |
Correct |
170 ms |
22688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
88 ms |
15340 KB |
Output is correct |
3 |
Correct |
104 ms |
17024 KB |
Output is correct |
4 |
Correct |
53 ms |
12884 KB |
Output is correct |
5 |
Correct |
64 ms |
13816 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
17 ms |
10452 KB |
Output is correct |
11 |
Correct |
21 ms |
10452 KB |
Output is correct |
12 |
Correct |
49 ms |
12916 KB |
Output is correct |
13 |
Correct |
57 ms |
13768 KB |
Output is correct |
14 |
Correct |
55 ms |
12856 KB |
Output is correct |
15 |
Correct |
74 ms |
13576 KB |
Output is correct |
16 |
Correct |
50 ms |
12992 KB |
Output is correct |
17 |
Correct |
53 ms |
13572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
10452 KB |
Output is correct |
2 |
Correct |
22 ms |
10452 KB |
Output is correct |
3 |
Correct |
41 ms |
13872 KB |
Output is correct |
4 |
Correct |
32 ms |
13012 KB |
Output is correct |
5 |
Correct |
65 ms |
16780 KB |
Output is correct |
6 |
Correct |
66 ms |
16808 KB |
Output is correct |
7 |
Correct |
65 ms |
16768 KB |
Output is correct |
8 |
Correct |
59 ms |
16772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7384 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7384 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
26 ms |
8976 KB |
Output is correct |
18 |
Correct |
19 ms |
9260 KB |
Output is correct |
19 |
Correct |
21 ms |
9172 KB |
Output is correct |
20 |
Correct |
21 ms |
9172 KB |
Output is correct |
21 |
Correct |
21 ms |
9092 KB |
Output is correct |
22 |
Correct |
44 ms |
10884 KB |
Output is correct |
23 |
Correct |
7 ms |
7764 KB |
Output is correct |
24 |
Correct |
14 ms |
8472 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
7 ms |
7744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7384 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
26 ms |
8976 KB |
Output is correct |
18 |
Correct |
19 ms |
9260 KB |
Output is correct |
19 |
Correct |
21 ms |
9172 KB |
Output is correct |
20 |
Correct |
21 ms |
9172 KB |
Output is correct |
21 |
Correct |
21 ms |
9092 KB |
Output is correct |
22 |
Correct |
44 ms |
10884 KB |
Output is correct |
23 |
Correct |
7 ms |
7764 KB |
Output is correct |
24 |
Correct |
14 ms |
8472 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
7 ms |
7744 KB |
Output is correct |
27 |
Correct |
5 ms |
7596 KB |
Output is correct |
28 |
Correct |
86 ms |
19512 KB |
Output is correct |
29 |
Correct |
126 ms |
23868 KB |
Output is correct |
30 |
Correct |
152 ms |
23444 KB |
Output is correct |
31 |
Correct |
135 ms |
23588 KB |
Output is correct |
32 |
Correct |
115 ms |
24480 KB |
Output is correct |
33 |
Correct |
142 ms |
23476 KB |
Output is correct |
34 |
Correct |
160 ms |
23660 KB |
Output is correct |
35 |
Correct |
67 ms |
13736 KB |
Output is correct |
36 |
Correct |
132 ms |
24356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
10452 KB |
Output is correct |
2 |
Correct |
22 ms |
10452 KB |
Output is correct |
3 |
Correct |
41 ms |
13872 KB |
Output is correct |
4 |
Correct |
32 ms |
13012 KB |
Output is correct |
5 |
Correct |
65 ms |
16780 KB |
Output is correct |
6 |
Correct |
66 ms |
16808 KB |
Output is correct |
7 |
Correct |
65 ms |
16768 KB |
Output is correct |
8 |
Correct |
59 ms |
16772 KB |
Output is correct |
9 |
Correct |
71 ms |
16772 KB |
Output is correct |
10 |
Correct |
58 ms |
13288 KB |
Output is correct |
11 |
Correct |
118 ms |
19180 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
23 ms |
10452 KB |
Output is correct |
19 |
Correct |
23 ms |
10452 KB |
Output is correct |
20 |
Correct |
18 ms |
10452 KB |
Output is correct |
21 |
Correct |
18 ms |
10452 KB |
Output is correct |
22 |
Correct |
75 ms |
15760 KB |
Output is correct |
23 |
Correct |
120 ms |
22636 KB |
Output is correct |
24 |
Correct |
129 ms |
23180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
12872 KB |
Output is correct |
2 |
Correct |
57 ms |
13736 KB |
Output is correct |
3 |
Correct |
17 ms |
10452 KB |
Output is correct |
4 |
Correct |
18 ms |
10452 KB |
Output is correct |
5 |
Correct |
155 ms |
20144 KB |
Output is correct |
6 |
Correct |
170 ms |
22688 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
88 ms |
15340 KB |
Output is correct |
9 |
Correct |
104 ms |
17024 KB |
Output is correct |
10 |
Correct |
53 ms |
12884 KB |
Output is correct |
11 |
Correct |
64 ms |
13816 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
17 ms |
10452 KB |
Output is correct |
17 |
Correct |
21 ms |
10452 KB |
Output is correct |
18 |
Correct |
49 ms |
12916 KB |
Output is correct |
19 |
Correct |
57 ms |
13768 KB |
Output is correct |
20 |
Correct |
55 ms |
12856 KB |
Output is correct |
21 |
Correct |
74 ms |
13576 KB |
Output is correct |
22 |
Correct |
50 ms |
12992 KB |
Output is correct |
23 |
Correct |
53 ms |
13572 KB |
Output is correct |
24 |
Correct |
17 ms |
10452 KB |
Output is correct |
25 |
Correct |
22 ms |
10452 KB |
Output is correct |
26 |
Correct |
41 ms |
13872 KB |
Output is correct |
27 |
Correct |
32 ms |
13012 KB |
Output is correct |
28 |
Correct |
65 ms |
16780 KB |
Output is correct |
29 |
Correct |
66 ms |
16808 KB |
Output is correct |
30 |
Correct |
65 ms |
16768 KB |
Output is correct |
31 |
Correct |
59 ms |
16772 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
4 ms |
7380 KB |
Output is correct |
34 |
Correct |
3 ms |
7380 KB |
Output is correct |
35 |
Correct |
4 ms |
7384 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
4 ms |
7380 KB |
Output is correct |
38 |
Correct |
5 ms |
7380 KB |
Output is correct |
39 |
Correct |
4 ms |
7380 KB |
Output is correct |
40 |
Correct |
4 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7508 KB |
Output is correct |
42 |
Correct |
4 ms |
7380 KB |
Output is correct |
43 |
Correct |
5 ms |
7380 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
5 ms |
7380 KB |
Output is correct |
46 |
Correct |
4 ms |
7380 KB |
Output is correct |
47 |
Correct |
4 ms |
7380 KB |
Output is correct |
48 |
Correct |
26 ms |
8976 KB |
Output is correct |
49 |
Correct |
19 ms |
9260 KB |
Output is correct |
50 |
Correct |
21 ms |
9172 KB |
Output is correct |
51 |
Correct |
21 ms |
9172 KB |
Output is correct |
52 |
Correct |
21 ms |
9092 KB |
Output is correct |
53 |
Correct |
44 ms |
10884 KB |
Output is correct |
54 |
Correct |
7 ms |
7764 KB |
Output is correct |
55 |
Correct |
14 ms |
8472 KB |
Output is correct |
56 |
Correct |
5 ms |
7380 KB |
Output is correct |
57 |
Correct |
7 ms |
7744 KB |
Output is correct |
58 |
Correct |
5 ms |
7596 KB |
Output is correct |
59 |
Correct |
86 ms |
19512 KB |
Output is correct |
60 |
Correct |
126 ms |
23868 KB |
Output is correct |
61 |
Correct |
152 ms |
23444 KB |
Output is correct |
62 |
Correct |
135 ms |
23588 KB |
Output is correct |
63 |
Correct |
115 ms |
24480 KB |
Output is correct |
64 |
Correct |
142 ms |
23476 KB |
Output is correct |
65 |
Correct |
160 ms |
23660 KB |
Output is correct |
66 |
Correct |
67 ms |
13736 KB |
Output is correct |
67 |
Correct |
132 ms |
24356 KB |
Output is correct |
68 |
Correct |
71 ms |
16772 KB |
Output is correct |
69 |
Correct |
58 ms |
13288 KB |
Output is correct |
70 |
Correct |
118 ms |
19180 KB |
Output is correct |
71 |
Correct |
4 ms |
7380 KB |
Output is correct |
72 |
Correct |
4 ms |
7380 KB |
Output is correct |
73 |
Correct |
4 ms |
7380 KB |
Output is correct |
74 |
Correct |
4 ms |
7380 KB |
Output is correct |
75 |
Correct |
4 ms |
7380 KB |
Output is correct |
76 |
Correct |
4 ms |
7380 KB |
Output is correct |
77 |
Correct |
23 ms |
10452 KB |
Output is correct |
78 |
Correct |
23 ms |
10452 KB |
Output is correct |
79 |
Correct |
18 ms |
10452 KB |
Output is correct |
80 |
Correct |
18 ms |
10452 KB |
Output is correct |
81 |
Correct |
75 ms |
15760 KB |
Output is correct |
82 |
Correct |
120 ms |
22636 KB |
Output is correct |
83 |
Correct |
129 ms |
23180 KB |
Output is correct |
84 |
Correct |
150 ms |
26288 KB |
Output is correct |
85 |
Correct |
149 ms |
25908 KB |
Output is correct |
86 |
Correct |
168 ms |
28924 KB |
Output is correct |
87 |
Correct |
208 ms |
29668 KB |
Output is correct |
88 |
Correct |
4 ms |
7404 KB |
Output is correct |
89 |
Correct |
174 ms |
29600 KB |
Output is correct |
90 |
Correct |
173 ms |
27724 KB |
Output is correct |
91 |
Correct |
149 ms |
27164 KB |
Output is correct |