#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int N = 3000;
constexpr i64 inf = 1E18;
void chmax(i64 &a, i64 b) {
if (a < b) {
a = b;
}
}
long long max_weights(int n, int m, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<pair<int, int>>> a(n);
vector<map<int, int>> f(n);
for (int i = 0; i < m; i++) {
f[X[i]][Y[i]] += W[i];
if (X[i] > 0) {
f[X[i] - 1][Y[i]] += 0;
}
if (X[i] < n - 1) {
f[X[i] + 1][Y[i]] += 0;
}
}
for (int i = 0; i < n; i++) {
for (auto [j, x] : f[i]) {
a[i].push_back({j, x});
}
}
vector<vector<i64>> pre(n);
vector<vector<array<i64, 2>>> dp(n);
for (int i = 0; i < n; i++) {
int k = a[i].size();
sort(a[i].begin(), a[i].end());
pre[i].resize(k + 1);
for (int j = 0; j < k; j++) {
pre[i][j + 1] = pre[i][j] + a[i][j].second;
}
dp[i].resize(k + 1, {-inf, -inf});
}
for (int i = 0; i <= int(a[0].size()); i++) {
dp[0][i][0] = dp[0][i][1] = 0;
}
for (int i = 1; i < n; i++) {
i64 best = -inf;
int sz = a[i].size();
int j = 0;
for (int k = 0; k <= sz; k++) {
while (j < int(dp[i - 1].size()) && ((j == 0 ? -1 : a[i - 1][j - 1].first) <= (k == 0 ? -1 : a[i][k - 1].first))) {
chmax(best, dp[i - 1][j][0] - pre[i - 1][j]);
j++;
}
// chmax(best, dp[i - 1][k][0] - pre[i - 1][k]);
// if (i == 2) {
// cout << best << " " << pre[i - 1][k] << " h\n";
// }
if (k == 0) {
chmax(dp[i][k][0], best);
} else {
int p = upper_bound(a[i - 1].begin(), a[i - 1].end(), pair<int, int>{a[i][k - 1].first, 2E9}) - a[i - 1].begin();
chmax(dp[i][k][0], best + pre[i - 1][p]);
}
}
best = -inf;
j = a[i - 1].size();
for (int k = sz; k >= 0; k--) {
while (j >= 0 && ((j == 0 ? -1 : a[i - 1][j - 1].first) >= (k == 0 ? -1 : a[i][k - 1].first))) {
if (j == 0) {
chmax(best, dp[i - 1][j][1]);
} else {
int p = upper_bound(a[i].begin(), a[i].end(), pair<int, int>{a[i - 1][j - 1].first, 2E9}) - a[i].begin();
chmax(best, dp[i - 1][j][1] + pre[i][p]);
}
j--;
}
// if (i == 3)
// cerr << "a " << k << " " << best << "\n";
// chmax(best, dp[i - 1][k][1] + pre[i][k]);
if (k == 0) {
chmax(dp[i][k][1], best);
} else {
chmax(dp[i][k][1], best - pre[i][k]);
}
}
// cerr << "F\n";
if (i >= 2) {
best = -inf;
j = a[i - 2].size();
for (int k = sz; k >= 0; k--) {
while (j >= 0 && ((j == 0 ? -1 : a[i - 2][j - 1].first) >= (k == 0 ? -1 : a[i][k - 1].first))) {
if (j == 0) {
chmax(best, dp[i - 2][j][1]);
} else {
int p = upper_bound(a[i - 1].begin(), a[i - 1].end(), pair<int, int>{a[i - 2][j - 1].first, 2E9}) - a[i - 1].begin();
chmax(best, dp[i - 2][j][1] + pre[i - 1][p]);
}
j--;
}
// chmax(best, dp[i - 2][k][1] + pre[i - 1][k]);
chmax(dp[i][k][0], best);
}
best = -inf;
j = 0;
for (int k = 0; k <= sz; k++) {
while (j < int(dp[i - 2].size()) && ((j == 0 ? -1 : a[i - 2][j - 1].first) <= (k == 0 ? -1 : a[i][k - 1].first))) {
chmax(best, dp[i - 2][j][1]);
j++;
}
// chmax(best, dp[i - 2][k][1]);
if (k == 0) {
chmax(dp[i][k][0], best);
} else {
int p = upper_bound(a[i - 1].begin(), a[i - 1].end(), pair<int, int>{a[i][k - 1].first, 2E9}) - a[i - 1].begin();
chmax(dp[i][k][0], best + pre[i - 1][p]);
}
}
}
// cerr << "D\n";
for (int j = 0; j <= sz; j++) {
chmax(dp[i][j][1], dp[i][j][0]);
}
if (i + 1 < n)
chmax(dp[i + 1][0][0], dp[i][0][1]);
}
// cout << dp[3][0][1] << "\n";
// cout << dp[3][1][1] << "\n";
i64 ans = 0;
for (int i = 0; i <= int(a[n - 1].size()); i++) {
ans = max(ans, dp[n - 1][i][1]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
32404 KB |
Output is correct |
2 |
Correct |
130 ms |
37940 KB |
Output is correct |
3 |
Correct |
18 ms |
18260 KB |
Output is correct |
4 |
Correct |
18 ms |
18260 KB |
Output is correct |
5 |
Correct |
817 ms |
102208 KB |
Output is correct |
6 |
Correct |
563 ms |
105320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
234 ms |
43400 KB |
Output is correct |
3 |
Correct |
311 ms |
49916 KB |
Output is correct |
4 |
Correct |
105 ms |
32460 KB |
Output is correct |
5 |
Correct |
145 ms |
37952 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
18 ms |
18260 KB |
Output is correct |
11 |
Correct |
17 ms |
18216 KB |
Output is correct |
12 |
Correct |
164 ms |
39048 KB |
Output is correct |
13 |
Correct |
209 ms |
45832 KB |
Output is correct |
14 |
Correct |
130 ms |
35652 KB |
Output is correct |
15 |
Correct |
116 ms |
33320 KB |
Output is correct |
16 |
Correct |
125 ms |
35648 KB |
Output is correct |
17 |
Correct |
144 ms |
39484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
18260 KB |
Output is correct |
2 |
Correct |
18 ms |
18260 KB |
Output is correct |
3 |
Correct |
40 ms |
24216 KB |
Output is correct |
4 |
Correct |
40 ms |
24140 KB |
Output is correct |
5 |
Correct |
63 ms |
31564 KB |
Output is correct |
6 |
Correct |
61 ms |
30924 KB |
Output is correct |
7 |
Correct |
65 ms |
31564 KB |
Output is correct |
8 |
Correct |
66 ms |
31568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
42 ms |
7252 KB |
Output is correct |
18 |
Correct |
36 ms |
6408 KB |
Output is correct |
19 |
Correct |
34 ms |
6604 KB |
Output is correct |
20 |
Correct |
35 ms |
6004 KB |
Output is correct |
21 |
Correct |
32 ms |
5844 KB |
Output is correct |
22 |
Correct |
72 ms |
11468 KB |
Output is correct |
23 |
Correct |
12 ms |
2644 KB |
Output is correct |
24 |
Correct |
35 ms |
6612 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
10 ms |
2400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
42 ms |
7252 KB |
Output is correct |
18 |
Correct |
36 ms |
6408 KB |
Output is correct |
19 |
Correct |
34 ms |
6604 KB |
Output is correct |
20 |
Correct |
35 ms |
6004 KB |
Output is correct |
21 |
Correct |
32 ms |
5844 KB |
Output is correct |
22 |
Correct |
72 ms |
11468 KB |
Output is correct |
23 |
Correct |
12 ms |
2644 KB |
Output is correct |
24 |
Correct |
35 ms |
6612 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
10 ms |
2400 KB |
Output is correct |
27 |
Correct |
3 ms |
1620 KB |
Output is correct |
28 |
Correct |
261 ms |
31236 KB |
Output is correct |
29 |
Correct |
539 ms |
52080 KB |
Output is correct |
30 |
Correct |
734 ms |
85200 KB |
Output is correct |
31 |
Correct |
747 ms |
84464 KB |
Output is correct |
32 |
Correct |
319 ms |
38052 KB |
Output is correct |
33 |
Correct |
786 ms |
86716 KB |
Output is correct |
34 |
Correct |
763 ms |
86788 KB |
Output is correct |
35 |
Correct |
156 ms |
26400 KB |
Output is correct |
36 |
Correct |
691 ms |
74204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
18260 KB |
Output is correct |
2 |
Correct |
18 ms |
18260 KB |
Output is correct |
3 |
Correct |
40 ms |
24216 KB |
Output is correct |
4 |
Correct |
40 ms |
24140 KB |
Output is correct |
5 |
Correct |
63 ms |
31564 KB |
Output is correct |
6 |
Correct |
61 ms |
30924 KB |
Output is correct |
7 |
Correct |
65 ms |
31564 KB |
Output is correct |
8 |
Correct |
66 ms |
31568 KB |
Output is correct |
9 |
Correct |
126 ms |
46976 KB |
Output is correct |
10 |
Correct |
53 ms |
21196 KB |
Output is correct |
11 |
Correct |
142 ms |
42068 KB |
Output is correct |
12 |
Correct |
1 ms |
420 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
224 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
17 ms |
18384 KB |
Output is correct |
19 |
Correct |
25 ms |
18196 KB |
Output is correct |
20 |
Correct |
18 ms |
18260 KB |
Output is correct |
21 |
Correct |
18 ms |
18260 KB |
Output is correct |
22 |
Correct |
149 ms |
47104 KB |
Output is correct |
23 |
Correct |
201 ms |
57292 KB |
Output is correct |
24 |
Correct |
213 ms |
59524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
32404 KB |
Output is correct |
2 |
Correct |
130 ms |
37940 KB |
Output is correct |
3 |
Correct |
18 ms |
18260 KB |
Output is correct |
4 |
Correct |
18 ms |
18260 KB |
Output is correct |
5 |
Correct |
817 ms |
102208 KB |
Output is correct |
6 |
Correct |
563 ms |
105320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
234 ms |
43400 KB |
Output is correct |
9 |
Correct |
311 ms |
49916 KB |
Output is correct |
10 |
Correct |
105 ms |
32460 KB |
Output is correct |
11 |
Correct |
145 ms |
37952 KB |
Output is correct |
12 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
18 ms |
18260 KB |
Output is correct |
17 |
Correct |
17 ms |
18216 KB |
Output is correct |
18 |
Correct |
164 ms |
39048 KB |
Output is correct |
19 |
Correct |
209 ms |
45832 KB |
Output is correct |
20 |
Correct |
130 ms |
35652 KB |
Output is correct |
21 |
Correct |
116 ms |
33320 KB |
Output is correct |
22 |
Correct |
125 ms |
35648 KB |
Output is correct |
23 |
Correct |
144 ms |
39484 KB |
Output is correct |
24 |
Correct |
17 ms |
18260 KB |
Output is correct |
25 |
Correct |
18 ms |
18260 KB |
Output is correct |
26 |
Correct |
40 ms |
24216 KB |
Output is correct |
27 |
Correct |
40 ms |
24140 KB |
Output is correct |
28 |
Correct |
63 ms |
31564 KB |
Output is correct |
29 |
Correct |
61 ms |
30924 KB |
Output is correct |
30 |
Correct |
65 ms |
31564 KB |
Output is correct |
31 |
Correct |
66 ms |
31568 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
300 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
3 ms |
596 KB |
Output is correct |
48 |
Correct |
42 ms |
7252 KB |
Output is correct |
49 |
Correct |
36 ms |
6408 KB |
Output is correct |
50 |
Correct |
34 ms |
6604 KB |
Output is correct |
51 |
Correct |
35 ms |
6004 KB |
Output is correct |
52 |
Correct |
32 ms |
5844 KB |
Output is correct |
53 |
Correct |
72 ms |
11468 KB |
Output is correct |
54 |
Correct |
12 ms |
2644 KB |
Output is correct |
55 |
Correct |
35 ms |
6612 KB |
Output is correct |
56 |
Correct |
2 ms |
596 KB |
Output is correct |
57 |
Correct |
10 ms |
2400 KB |
Output is correct |
58 |
Correct |
3 ms |
1620 KB |
Output is correct |
59 |
Correct |
261 ms |
31236 KB |
Output is correct |
60 |
Correct |
539 ms |
52080 KB |
Output is correct |
61 |
Correct |
734 ms |
85200 KB |
Output is correct |
62 |
Correct |
747 ms |
84464 KB |
Output is correct |
63 |
Correct |
319 ms |
38052 KB |
Output is correct |
64 |
Correct |
786 ms |
86716 KB |
Output is correct |
65 |
Correct |
763 ms |
86788 KB |
Output is correct |
66 |
Correct |
156 ms |
26400 KB |
Output is correct |
67 |
Correct |
691 ms |
74204 KB |
Output is correct |
68 |
Correct |
126 ms |
46976 KB |
Output is correct |
69 |
Correct |
53 ms |
21196 KB |
Output is correct |
70 |
Correct |
142 ms |
42068 KB |
Output is correct |
71 |
Correct |
1 ms |
420 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
224 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
17 ms |
18384 KB |
Output is correct |
78 |
Correct |
25 ms |
18196 KB |
Output is correct |
79 |
Correct |
18 ms |
18260 KB |
Output is correct |
80 |
Correct |
18 ms |
18260 KB |
Output is correct |
81 |
Correct |
149 ms |
47104 KB |
Output is correct |
82 |
Correct |
201 ms |
57292 KB |
Output is correct |
83 |
Correct |
213 ms |
59524 KB |
Output is correct |
84 |
Correct |
906 ms |
91012 KB |
Output is correct |
85 |
Correct |
822 ms |
91864 KB |
Output is correct |
86 |
Correct |
564 ms |
102476 KB |
Output is correct |
87 |
Correct |
569 ms |
105040 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
546 ms |
105044 KB |
Output is correct |
90 |
Correct |
466 ms |
84812 KB |
Output is correct |
91 |
Correct |
277 ms |
62744 KB |
Output is correct |