#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
using ll = long long;
#define endl "\n"
#define long int64_t
#define sz(x) int(size(x))
ll max_weights(int n, vector<array<int, 3>> fish) {
vector<pair<int, long>> arr[n];
for (auto& [x, y, w] : fish) {
arr[x].emplace_back(y, w);
}
for (auto& a : arr) {
sort(begin(a), end(a));
long psum = 0;
for (auto& [_, x] : a) {
psum += x;
x = psum;
}
}
auto ibs = [&](int i) -> bool {
return 0 <= i && i < n;
};
auto psum = [&](int i, int x) -> long {
if (!ibs(i)) {
return 0;
}
auto it =
lower_bound(begin(arr[i]), end(arr[i]), pair<int, long> {x, 0});
if (it == arr[i].begin()) {
return 0;
}
return (--it)->second;
};
auto sum = [&](int i, int l, int r) -> long {
return max(long(0), psum(i, r) - psum(i, l));
};
auto get = [&](int i, int j) -> int {
if (!ibs(i)) {
return 0;
}
if (j < sz(arr[i])) {
return arr[i][j].first;
} else {
return n;
}
};
auto mx = [&](const vector<long>& arr) -> long {
return *max_element(begin(arr), end(arr));
};
vector<long> dp[n + 1][2];
dp[n][0] = dp[n][1] = {0};
for (int i = n - 1; i >= 0; i--) {
int m = sz(arr[i]), m2 = sz(dp[i + 1][0]);
{
dp[i][0].resize(m + 1, max(mx(dp[i + 1][1]), mx(dp[i + 1][0])));
long cmx = 0;
for (int k = 0; k < m2; k++) {
cmx = max(cmx, dp[i + 1][0][k] - psum(i + 1, get(i + 1, k)));
}
for (int j = 0; j <= m; j++) {
dp[i][0][j] = max(dp[i][0][j], cmx + psum(i + 1, get(i, j)));
}
// for (int j = 0; j <= m; j++) {
// for (int k = 0; k < sz(dp[i + 1][0]); k++) {
// dp[i][0][j] =
// max({dp[i][0][j], dp[i + 1][1][k],
// dp[i + 1][0][k] +
// sum(i + 1, get(i + 1, k), get(i,
// j))});
// }
// }
}
{
dp[i][1].resize(m + 1, mx(dp[i + 1][1]));
{
long cmx = 0;
for (int k = 0; k < m2; k++) {
cmx = max(cmx, dp[i + 1][1][k] + psum(i, get(i + 1, k)));
}
for (int j = 0; j <= m; j++) {
dp[i][1][j] = max(dp[i][1][j], cmx - psum(i, get(i, j)));
}
}
{
long cmx = 0;
for (int k = 0; k < m2; k++) {
cmx =
max(cmx, dp[i + 1][0][k] - psum(i + 1, get(i + 1, k)));
}
for (int j = 0; j <= m; j++) {
dp[i][1][j] =
max(dp[i][1][j], cmx + psum(i + 1, get(i, j)));
}
}
// for (int j = 0; j <= m; j++) {
// for (int k = 0; k < sz(dp[i + 1][0]); k++) {
// long x = max(
// dp[i + 1][1][k],
// dp[i + 1][0][k] + sum(i + 1, get(i + 1,
// k), get(i, j)));
// dp[i][1][j] =
// max(dp[i][1][j], x + sum(i, get(i, j),
// get(i + 1, k)));
// }
// }
}
}
return mx(dp[0][1]);
}
ll max_weights(int n, int m, vector<int> xs, vector<int> ys, vector<int> ws) {
vector<array<int, 3>> arr;
for (int i = 0; i < m; i++) {
arr.push_back({xs[i], ys[i], ws[i]});
}
return max_weights(n, arr);
}
Compilation message
fish.cpp: In function 'll max_weights(int, std::vector<std::array<int, 3> >)':
fish.cpp:61:10: warning: variable 'sum' set but not used [-Wunused-but-set-variable]
61 | auto sum = [&](int i, int l, int r) -> long {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
18512 KB |
Output is correct |
2 |
Correct |
59 ms |
21416 KB |
Output is correct |
3 |
Correct |
16 ms |
13504 KB |
Output is correct |
4 |
Correct |
20 ms |
13476 KB |
Output is correct |
5 |
Correct |
182 ms |
37700 KB |
Output is correct |
6 |
Correct |
172 ms |
38192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
104 ms |
26652 KB |
Output is correct |
3 |
Correct |
125 ms |
32392 KB |
Output is correct |
4 |
Correct |
56 ms |
19644 KB |
Output is correct |
5 |
Correct |
59 ms |
22880 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
16 ms |
13540 KB |
Output is correct |
11 |
Correct |
16 ms |
13552 KB |
Output is correct |
12 |
Correct |
59 ms |
19756 KB |
Output is correct |
13 |
Correct |
73 ms |
22924 KB |
Output is correct |
14 |
Correct |
57 ms |
19680 KB |
Output is correct |
15 |
Correct |
66 ms |
22212 KB |
Output is correct |
16 |
Correct |
57 ms |
20288 KB |
Output is correct |
17 |
Correct |
65 ms |
22012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
13524 KB |
Output is correct |
2 |
Correct |
17 ms |
13540 KB |
Output is correct |
3 |
Correct |
44 ms |
16368 KB |
Output is correct |
4 |
Correct |
36 ms |
16624 KB |
Output is correct |
5 |
Correct |
68 ms |
21328 KB |
Output is correct |
6 |
Correct |
61 ms |
21328 KB |
Output is correct |
7 |
Correct |
72 ms |
21920 KB |
Output is correct |
8 |
Correct |
89 ms |
21952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
0 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 |
1 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
0 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 |
1 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
27 ms |
4016 KB |
Output is correct |
18 |
Correct |
25 ms |
5184 KB |
Output is correct |
19 |
Correct |
24 ms |
5040 KB |
Output is correct |
20 |
Correct |
26 ms |
4996 KB |
Output is correct |
21 |
Correct |
23 ms |
4996 KB |
Output is correct |
22 |
Correct |
48 ms |
9916 KB |
Output is correct |
23 |
Correct |
4 ms |
1128 KB |
Output is correct |
24 |
Correct |
17 ms |
3244 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
6 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
0 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 |
1 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
27 ms |
4016 KB |
Output is correct |
18 |
Correct |
25 ms |
5184 KB |
Output is correct |
19 |
Correct |
24 ms |
5040 KB |
Output is correct |
20 |
Correct |
26 ms |
4996 KB |
Output is correct |
21 |
Correct |
23 ms |
4996 KB |
Output is correct |
22 |
Correct |
48 ms |
9916 KB |
Output is correct |
23 |
Correct |
4 ms |
1128 KB |
Output is correct |
24 |
Correct |
17 ms |
3244 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
6 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
132 ms |
22964 KB |
Output is correct |
29 |
Correct |
186 ms |
30384 KB |
Output is correct |
30 |
Correct |
154 ms |
29780 KB |
Output is correct |
31 |
Correct |
164 ms |
31120 KB |
Output is correct |
32 |
Correct |
172 ms |
32044 KB |
Output is correct |
33 |
Correct |
152 ms |
31024 KB |
Output is correct |
34 |
Correct |
152 ms |
30408 KB |
Output is correct |
35 |
Correct |
61 ms |
12036 KB |
Output is correct |
36 |
Correct |
168 ms |
30316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
13524 KB |
Output is correct |
2 |
Correct |
17 ms |
13540 KB |
Output is correct |
3 |
Correct |
44 ms |
16368 KB |
Output is correct |
4 |
Correct |
36 ms |
16624 KB |
Output is correct |
5 |
Correct |
68 ms |
21328 KB |
Output is correct |
6 |
Correct |
61 ms |
21328 KB |
Output is correct |
7 |
Correct |
72 ms |
21920 KB |
Output is correct |
8 |
Correct |
89 ms |
21952 KB |
Output is correct |
9 |
Correct |
61 ms |
21380 KB |
Output is correct |
10 |
Correct |
52 ms |
14016 KB |
Output is correct |
11 |
Correct |
117 ms |
27948 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
16 ms |
13492 KB |
Output is correct |
19 |
Correct |
16 ms |
13576 KB |
Output is correct |
20 |
Correct |
20 ms |
13652 KB |
Output is correct |
21 |
Correct |
18 ms |
13580 KB |
Output is correct |
22 |
Correct |
73 ms |
20792 KB |
Output is correct |
23 |
Correct |
124 ms |
27836 KB |
Output is correct |
24 |
Correct |
117 ms |
27984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
18512 KB |
Output is correct |
2 |
Correct |
59 ms |
21416 KB |
Output is correct |
3 |
Correct |
16 ms |
13504 KB |
Output is correct |
4 |
Correct |
20 ms |
13476 KB |
Output is correct |
5 |
Correct |
182 ms |
37700 KB |
Output is correct |
6 |
Correct |
172 ms |
38192 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
104 ms |
26652 KB |
Output is correct |
9 |
Correct |
125 ms |
32392 KB |
Output is correct |
10 |
Correct |
56 ms |
19644 KB |
Output is correct |
11 |
Correct |
59 ms |
22880 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 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 |
16 ms |
13540 KB |
Output is correct |
17 |
Correct |
16 ms |
13552 KB |
Output is correct |
18 |
Correct |
59 ms |
19756 KB |
Output is correct |
19 |
Correct |
73 ms |
22924 KB |
Output is correct |
20 |
Correct |
57 ms |
19680 KB |
Output is correct |
21 |
Correct |
66 ms |
22212 KB |
Output is correct |
22 |
Correct |
57 ms |
20288 KB |
Output is correct |
23 |
Correct |
65 ms |
22012 KB |
Output is correct |
24 |
Correct |
16 ms |
13524 KB |
Output is correct |
25 |
Correct |
17 ms |
13540 KB |
Output is correct |
26 |
Correct |
44 ms |
16368 KB |
Output is correct |
27 |
Correct |
36 ms |
16624 KB |
Output is correct |
28 |
Correct |
68 ms |
21328 KB |
Output is correct |
29 |
Correct |
61 ms |
21328 KB |
Output is correct |
30 |
Correct |
72 ms |
21920 KB |
Output is correct |
31 |
Correct |
89 ms |
21952 KB |
Output is correct |
32 |
Correct |
1 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 |
0 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 |
0 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 |
1 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 |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
27 ms |
4016 KB |
Output is correct |
49 |
Correct |
25 ms |
5184 KB |
Output is correct |
50 |
Correct |
24 ms |
5040 KB |
Output is correct |
51 |
Correct |
26 ms |
4996 KB |
Output is correct |
52 |
Correct |
23 ms |
4996 KB |
Output is correct |
53 |
Correct |
48 ms |
9916 KB |
Output is correct |
54 |
Correct |
4 ms |
1128 KB |
Output is correct |
55 |
Correct |
17 ms |
3244 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
6 ms |
980 KB |
Output is correct |
58 |
Correct |
2 ms |
852 KB |
Output is correct |
59 |
Correct |
132 ms |
22964 KB |
Output is correct |
60 |
Correct |
186 ms |
30384 KB |
Output is correct |
61 |
Correct |
154 ms |
29780 KB |
Output is correct |
62 |
Correct |
164 ms |
31120 KB |
Output is correct |
63 |
Correct |
172 ms |
32044 KB |
Output is correct |
64 |
Correct |
152 ms |
31024 KB |
Output is correct |
65 |
Correct |
152 ms |
30408 KB |
Output is correct |
66 |
Correct |
61 ms |
12036 KB |
Output is correct |
67 |
Correct |
168 ms |
30316 KB |
Output is correct |
68 |
Correct |
61 ms |
21380 KB |
Output is correct |
69 |
Correct |
52 ms |
14016 KB |
Output is correct |
70 |
Correct |
117 ms |
27948 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
16 ms |
13492 KB |
Output is correct |
78 |
Correct |
16 ms |
13576 KB |
Output is correct |
79 |
Correct |
20 ms |
13652 KB |
Output is correct |
80 |
Correct |
18 ms |
13580 KB |
Output is correct |
81 |
Correct |
73 ms |
20792 KB |
Output is correct |
82 |
Correct |
124 ms |
27836 KB |
Output is correct |
83 |
Correct |
117 ms |
27984 KB |
Output is correct |
84 |
Correct |
211 ms |
40192 KB |
Output is correct |
85 |
Correct |
215 ms |
40560 KB |
Output is correct |
86 |
Correct |
188 ms |
37136 KB |
Output is correct |
87 |
Correct |
188 ms |
37936 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
225 ms |
38604 KB |
Output is correct |
90 |
Correct |
175 ms |
38664 KB |
Output is correct |
91 |
Correct |
166 ms |
39056 KB |
Output is correct |