#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = -(1LL << 61);
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector<vector<int>> b(n);
for (int i = 0; i < m; i++) {
b[x[i]].push_back(i);
}
vector d(n, vector<int>{-1});
vector<vector<int>> r(n), p(n);
for (int i = 0; i < n; i++) {
sort(b[i].begin(), b[i].end(), [&](int j, int k) {
return y[j] < y[k];
});
for (int j : b[i]) {
r[i].push_back(y[j]);
p[i].push_back(w[j]);
if (i > 0) {
d[i - 1].push_back(y[j]);
}
if (i < n - 1) {
d[i + 1].push_back(y[j]);
}
}
}
vector<vector<long long>> ssum(n), psum(n);
for (int i = 0; i < n; i++) {
int k = p[i].size();
psum[i] = vector<long long>(k + 1);
ssum[i] = vector<long long>(k + 1);
for (int j = 0; j < k; j++) {
psum[i][j + 1] = psum[i][j] + p[i][j];
}
for (int j = k - 1; j >= 0; j--) {
ssum[i][j] = ssum[i][j + 1] + p[i][j];
}
}
auto suf_sum = [&](int i, int x) -> long long {
if (i == -1) {
return 0;
}
return ssum[i][upper_bound(r[i].begin(), r[i].end(), x) - r[i].begin()];
};
auto pre_sum = [&](int i, int x) -> long long {
if (i == -1) {
return 0;
}
return psum[i][upper_bound(r[i].begin(), r[i].end(), x) - r[i].begin()];
};
vector<int> e{-1};
vector<array<long long, 2>> dp{{0, 0}};
for (int i = 0; i < n; i++) {
sort(d[i].begin(), d[i].end());
auto pd = dp;
int l = d[i].size();
dp = vector(l, array<long long, 2>{inf, inf});
int k = pd.size();
vector<long long> pmax(k + 1, inf);
for (int j = 0; j < k; j++) {
pmax[j + 1] = max(pmax[j], pd[j][0] + suf_sum(i - 1, e[j]));
}
vector<long long> smax(k + 1, inf);
for (int j = k - 1; j >= 0; j--) {
smax[j] = max(smax[j + 1], max(pd[j][0], pd[j][1]) - suf_sum(i, e[j]));
}
for (int j = 0; j < k; j++) {
dp[0][0] = max(dp[0][0], max(pd[j][0], pd[j][1]));
dp[0][1] = max(dp[0][1], max(pd[j][0], pd[j][1]) + pre_sum(i, e[j]));
}
for (int j = 1; j < l; j++) {
dp[j][0] = max({dp[j][0], max(pd[0][1], pd[0][0] + pre_sum(i - 1, d[i][j])), pmax[upper_bound(e.begin(), e.end(), d[i][j]) - e.begin()] - suf_sum(i - 1, d[i][j])});
dp[j][1] = max({dp[j][1], max(pd[0][1], pd[0][0] + pre_sum(i - 1, d[i][j])), smax[lower_bound(e.begin(), e.end(), d[i][j]) - e.begin()] + suf_sum(i, d[i][j])});
}
e.swap(d[i]);
}
long long ans = 0;
for (auto v : dp) {
ans = max(ans, max(v[0], v[1]));
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
28372 KB |
Output is correct |
2 |
Correct |
99 ms |
34040 KB |
Output is correct |
3 |
Correct |
35 ms |
23724 KB |
Output is correct |
4 |
Correct |
34 ms |
23792 KB |
Output is correct |
5 |
Correct |
269 ms |
47640 KB |
Output is correct |
6 |
Correct |
271 ms |
46540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
165 ms |
34624 KB |
Output is correct |
3 |
Correct |
208 ms |
42152 KB |
Output is correct |
4 |
Correct |
78 ms |
29516 KB |
Output is correct |
5 |
Correct |
92 ms |
33852 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
41 ms |
23716 KB |
Output is correct |
11 |
Correct |
38 ms |
23780 KB |
Output is correct |
12 |
Correct |
94 ms |
30760 KB |
Output is correct |
13 |
Correct |
104 ms |
35172 KB |
Output is correct |
14 |
Correct |
87 ms |
29232 KB |
Output is correct |
15 |
Correct |
111 ms |
32492 KB |
Output is correct |
16 |
Correct |
97 ms |
29204 KB |
Output is correct |
17 |
Correct |
96 ms |
32392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
23788 KB |
Output is correct |
2 |
Correct |
34 ms |
23772 KB |
Output is correct |
3 |
Correct |
83 ms |
28356 KB |
Output is correct |
4 |
Correct |
72 ms |
28356 KB |
Output is correct |
5 |
Correct |
107 ms |
37020 KB |
Output is correct |
6 |
Correct |
106 ms |
36184 KB |
Output is correct |
7 |
Correct |
115 ms |
36912 KB |
Output is correct |
8 |
Correct |
108 ms |
36892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
476 KB |
Output is correct |
17 |
Correct |
43 ms |
3160 KB |
Output is correct |
18 |
Correct |
42 ms |
3808 KB |
Output is correct |
19 |
Correct |
38 ms |
3700 KB |
Output is correct |
20 |
Correct |
36 ms |
3660 KB |
Output is correct |
21 |
Correct |
39 ms |
3664 KB |
Output is correct |
22 |
Correct |
78 ms |
7032 KB |
Output is correct |
23 |
Correct |
8 ms |
984 KB |
Output is correct |
24 |
Correct |
27 ms |
2316 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
7 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
476 KB |
Output is correct |
17 |
Correct |
43 ms |
3160 KB |
Output is correct |
18 |
Correct |
42 ms |
3808 KB |
Output is correct |
19 |
Correct |
38 ms |
3700 KB |
Output is correct |
20 |
Correct |
36 ms |
3660 KB |
Output is correct |
21 |
Correct |
39 ms |
3664 KB |
Output is correct |
22 |
Correct |
78 ms |
7032 KB |
Output is correct |
23 |
Correct |
8 ms |
984 KB |
Output is correct |
24 |
Correct |
27 ms |
2316 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
7 ms |
980 KB |
Output is correct |
27 |
Correct |
4 ms |
1236 KB |
Output is correct |
28 |
Correct |
257 ms |
16384 KB |
Output is correct |
29 |
Correct |
316 ms |
21452 KB |
Output is correct |
30 |
Correct |
260 ms |
20468 KB |
Output is correct |
31 |
Correct |
283 ms |
20568 KB |
Output is correct |
32 |
Correct |
283 ms |
23444 KB |
Output is correct |
33 |
Correct |
276 ms |
20464 KB |
Output is correct |
34 |
Correct |
266 ms |
20428 KB |
Output is correct |
35 |
Correct |
105 ms |
9072 KB |
Output is correct |
36 |
Correct |
297 ms |
22208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
23788 KB |
Output is correct |
2 |
Correct |
34 ms |
23772 KB |
Output is correct |
3 |
Correct |
83 ms |
28356 KB |
Output is correct |
4 |
Correct |
72 ms |
28356 KB |
Output is correct |
5 |
Correct |
107 ms |
37020 KB |
Output is correct |
6 |
Correct |
106 ms |
36184 KB |
Output is correct |
7 |
Correct |
115 ms |
36912 KB |
Output is correct |
8 |
Correct |
108 ms |
36892 KB |
Output is correct |
9 |
Correct |
103 ms |
36572 KB |
Output is correct |
10 |
Correct |
85 ms |
21416 KB |
Output is correct |
11 |
Correct |
171 ms |
41528 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
34 ms |
23764 KB |
Output is correct |
19 |
Correct |
34 ms |
23764 KB |
Output is correct |
20 |
Correct |
33 ms |
23764 KB |
Output is correct |
21 |
Correct |
35 ms |
23800 KB |
Output is correct |
22 |
Correct |
118 ms |
34556 KB |
Output is correct |
23 |
Correct |
203 ms |
42888 KB |
Output is correct |
24 |
Correct |
169 ms |
43228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
28372 KB |
Output is correct |
2 |
Correct |
99 ms |
34040 KB |
Output is correct |
3 |
Correct |
35 ms |
23724 KB |
Output is correct |
4 |
Correct |
34 ms |
23792 KB |
Output is correct |
5 |
Correct |
269 ms |
47640 KB |
Output is correct |
6 |
Correct |
271 ms |
46540 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
165 ms |
34624 KB |
Output is correct |
9 |
Correct |
208 ms |
42152 KB |
Output is correct |
10 |
Correct |
78 ms |
29516 KB |
Output is correct |
11 |
Correct |
92 ms |
33852 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
41 ms |
23716 KB |
Output is correct |
17 |
Correct |
38 ms |
23780 KB |
Output is correct |
18 |
Correct |
94 ms |
30760 KB |
Output is correct |
19 |
Correct |
104 ms |
35172 KB |
Output is correct |
20 |
Correct |
87 ms |
29232 KB |
Output is correct |
21 |
Correct |
111 ms |
32492 KB |
Output is correct |
22 |
Correct |
97 ms |
29204 KB |
Output is correct |
23 |
Correct |
96 ms |
32392 KB |
Output is correct |
24 |
Correct |
38 ms |
23788 KB |
Output is correct |
25 |
Correct |
34 ms |
23772 KB |
Output is correct |
26 |
Correct |
83 ms |
28356 KB |
Output is correct |
27 |
Correct |
72 ms |
28356 KB |
Output is correct |
28 |
Correct |
107 ms |
37020 KB |
Output is correct |
29 |
Correct |
106 ms |
36184 KB |
Output is correct |
30 |
Correct |
115 ms |
36912 KB |
Output is correct |
31 |
Correct |
108 ms |
36892 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
2 ms |
476 KB |
Output is correct |
48 |
Correct |
43 ms |
3160 KB |
Output is correct |
49 |
Correct |
42 ms |
3808 KB |
Output is correct |
50 |
Correct |
38 ms |
3700 KB |
Output is correct |
51 |
Correct |
36 ms |
3660 KB |
Output is correct |
52 |
Correct |
39 ms |
3664 KB |
Output is correct |
53 |
Correct |
78 ms |
7032 KB |
Output is correct |
54 |
Correct |
8 ms |
984 KB |
Output is correct |
55 |
Correct |
27 ms |
2316 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
7 ms |
980 KB |
Output is correct |
58 |
Correct |
4 ms |
1236 KB |
Output is correct |
59 |
Correct |
257 ms |
16384 KB |
Output is correct |
60 |
Correct |
316 ms |
21452 KB |
Output is correct |
61 |
Correct |
260 ms |
20468 KB |
Output is correct |
62 |
Correct |
283 ms |
20568 KB |
Output is correct |
63 |
Correct |
283 ms |
23444 KB |
Output is correct |
64 |
Correct |
276 ms |
20464 KB |
Output is correct |
65 |
Correct |
266 ms |
20428 KB |
Output is correct |
66 |
Correct |
105 ms |
9072 KB |
Output is correct |
67 |
Correct |
297 ms |
22208 KB |
Output is correct |
68 |
Correct |
103 ms |
36572 KB |
Output is correct |
69 |
Correct |
85 ms |
21416 KB |
Output is correct |
70 |
Correct |
171 ms |
41528 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
292 KB |
Output is correct |
77 |
Correct |
34 ms |
23764 KB |
Output is correct |
78 |
Correct |
34 ms |
23764 KB |
Output is correct |
79 |
Correct |
33 ms |
23764 KB |
Output is correct |
80 |
Correct |
35 ms |
23800 KB |
Output is correct |
81 |
Correct |
118 ms |
34556 KB |
Output is correct |
82 |
Correct |
203 ms |
42888 KB |
Output is correct |
83 |
Correct |
169 ms |
43228 KB |
Output is correct |
84 |
Correct |
371 ms |
48392 KB |
Output is correct |
85 |
Correct |
360 ms |
50216 KB |
Output is correct |
86 |
Correct |
313 ms |
48700 KB |
Output is correct |
87 |
Correct |
301 ms |
51524 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
318 ms |
51520 KB |
Output is correct |
90 |
Correct |
295 ms |
49592 KB |
Output is correct |
91 |
Correct |
285 ms |
49268 KB |
Output is correct |