#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
using ll = long long;
constexpr ll infl = 2e18;
#define int ll
ll max_weights(
int32_t n, int32_t m, vector<int32_t> x, vector<int32_t> y, vector<int32_t> w) {
vector pref(n+1, vector<pair<int,ll>>());
for (int i = 0; i < m; i++) {
pref[x[i]+1].emplace_back(y[i], w[i]);
}
for (int i = 0; i <= n; i++) {
sort(pref[i].begin(), pref[i].end());
for (int j = 1; j < (int)pref[i].size(); j++) {
pref[i][j].second += pref[i][j-1].second;
assert(pref[i][j].first != pref[i][j-1].first);
}
}
auto sum_right = [&](int c, int right)->ll{
int l = -1, r = pref[c].size();
ll ans = 0;
while (l < r-1) {
int mid = (l+r) / 2;
if (pref[c][mid].first<= right) {
l = mid, ans = pref[c][mid].second;
} else r = mid;
}
return ans;
};
auto sum = [&](int c, int l, int r) { // [l, r]
if (l <= r) return sum_right(c, r) - sum_right(c, l-1);
else return 0ll;
};
vector<vector<int>> byx(n+2), used(n+1);
for (int i = 0; i < m; i++)
byx[x[i]+1].push_back(y[i]);
for (int i = 0; i <= n; i++) {
if (i) for (int j : byx[i-1])
used[i].push_back(j+1);
if (n-i) for (int j : byx[i+1])
used[i].push_back(j+1);
used[i].push_back(0);
sort(used[i].begin(), used[i].end());
}
vector memo(2, vector(n+1, vector<ll>()));
for (int i = 1; i <= n; i++) {
memo[0][i].assign(used[i-1].size(), -1);
memo[1][i].assign(used[i-1].size(), -1);
}
vector memp(2, vector(n+1, vector<ll>{}));
auto dp = [&](auto& dp, bool inc, int c, int h2_key)->ll{
if (c == n+1) return 0;
int h2 = used[c-1][h2_key];
ll& res = memo[inc][c][h2_key];
if (res!=-1) return res;
res = 0;
if (inc) {
if (memp[inc][c].empty()) {
memp[inc][c].resize(used[c].size());
ll mx = -infl;
for (int j = (int)used[c].size()-1; j >= 0; j--) {
int h3 = used[c][j];
ll val = sum(c-1, 0, h3-1);
mx = max(mx, val + max(
dp(dp, true, c+1, j),
dp(dp, false, c+1, j)));
memp[inc][c][j] = mx;
}
}
int start = lower_bound(used[c].begin(), used[c].end(), h2) - used[c].begin();
ll off = -sum(c-1, 0, h2-1);
for (int j = start; j < (int)used[c].size(); j++) {
res = max(res, memp[inc][c][j] + off);
break;
}
} else {
if (memp[inc][c].empty()) {
memp[inc][c].resize(used[c].size());
ll mx = -infl;
for (int j = 0; j < (int)used[c].size(); j++) {
int h3 = used[c][j];
ll val = -sum(c, 0, h3-1);
mx = max(mx, val + dp(dp, false, c+1, j));
memp[inc][c][j] = mx;
}
}
int finish = upper_bound(used[c].begin(), used[c].end(), h2) - used[c].begin();
ll off = sum(c, 0, h2-1);
for (int j = finish-1; j >= 0; j--) {
res = max(res, memp[inc][c][j] + off);
break;
}
}
res = max(res, dp(dp, true, c+1, 0));
res = max(res, 0ll);
return res;
};
ll res = dp(dp, true, 1, 0);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
52028 KB |
Output is correct |
2 |
Correct |
105 ms |
59172 KB |
Output is correct |
3 |
Correct |
47 ms |
46364 KB |
Output is correct |
4 |
Correct |
45 ms |
46372 KB |
Output is correct |
5 |
Correct |
268 ms |
89036 KB |
Output is correct |
6 |
Correct |
251 ms |
92040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
183 ms |
63468 KB |
Output is correct |
3 |
Correct |
232 ms |
72620 KB |
Output is correct |
4 |
Correct |
89 ms |
52100 KB |
Output is correct |
5 |
Correct |
108 ms |
59120 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
224 KB |
Output is correct |
10 |
Correct |
46 ms |
46376 KB |
Output is correct |
11 |
Correct |
46 ms |
46372 KB |
Output is correct |
12 |
Correct |
100 ms |
52732 KB |
Output is correct |
13 |
Correct |
116 ms |
59884 KB |
Output is correct |
14 |
Correct |
100 ms |
52448 KB |
Output is correct |
15 |
Correct |
123 ms |
58380 KB |
Output is correct |
16 |
Correct |
98 ms |
52608 KB |
Output is correct |
17 |
Correct |
109 ms |
58412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
46368 KB |
Output is correct |
2 |
Correct |
47 ms |
46380 KB |
Output is correct |
3 |
Correct |
86 ms |
48180 KB |
Output is correct |
4 |
Correct |
71 ms |
50628 KB |
Output is correct |
5 |
Correct |
112 ms |
58288 KB |
Output is correct |
6 |
Correct |
109 ms |
57620 KB |
Output is correct |
7 |
Correct |
114 ms |
58120 KB |
Output is correct |
8 |
Correct |
122 ms |
58416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
284 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 |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
284 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 |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
0 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 |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
43 ms |
6300 KB |
Output is correct |
18 |
Correct |
44 ms |
7588 KB |
Output is correct |
19 |
Correct |
36 ms |
7376 KB |
Output is correct |
20 |
Correct |
35 ms |
7352 KB |
Output is correct |
21 |
Correct |
34 ms |
7300 KB |
Output is correct |
22 |
Correct |
81 ms |
14328 KB |
Output is correct |
23 |
Correct |
8 ms |
1660 KB |
Output is correct |
24 |
Correct |
26 ms |
4424 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
284 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 |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
0 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 |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
43 ms |
6300 KB |
Output is correct |
18 |
Correct |
44 ms |
7588 KB |
Output is correct |
19 |
Correct |
36 ms |
7376 KB |
Output is correct |
20 |
Correct |
35 ms |
7352 KB |
Output is correct |
21 |
Correct |
34 ms |
7300 KB |
Output is correct |
22 |
Correct |
81 ms |
14328 KB |
Output is correct |
23 |
Correct |
8 ms |
1660 KB |
Output is correct |
24 |
Correct |
26 ms |
4424 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
1492 KB |
Output is correct |
27 |
Correct |
3 ms |
1876 KB |
Output is correct |
28 |
Correct |
216 ms |
32332 KB |
Output is correct |
29 |
Correct |
287 ms |
43100 KB |
Output is correct |
30 |
Correct |
260 ms |
42720 KB |
Output is correct |
31 |
Correct |
265 ms |
42720 KB |
Output is correct |
32 |
Correct |
290 ms |
45820 KB |
Output is correct |
33 |
Correct |
266 ms |
43044 KB |
Output is correct |
34 |
Correct |
267 ms |
42740 KB |
Output is correct |
35 |
Correct |
98 ms |
18504 KB |
Output is correct |
36 |
Correct |
282 ms |
46412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
46368 KB |
Output is correct |
2 |
Correct |
47 ms |
46380 KB |
Output is correct |
3 |
Correct |
86 ms |
48180 KB |
Output is correct |
4 |
Correct |
71 ms |
50628 KB |
Output is correct |
5 |
Correct |
112 ms |
58288 KB |
Output is correct |
6 |
Correct |
109 ms |
57620 KB |
Output is correct |
7 |
Correct |
114 ms |
58120 KB |
Output is correct |
8 |
Correct |
122 ms |
58416 KB |
Output is correct |
9 |
Correct |
111 ms |
57980 KB |
Output is correct |
10 |
Correct |
83 ms |
36836 KB |
Output is correct |
11 |
Correct |
171 ms |
73608 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
45 ms |
46464 KB |
Output is correct |
19 |
Correct |
47 ms |
46460 KB |
Output is correct |
20 |
Correct |
46 ms |
46396 KB |
Output is correct |
21 |
Correct |
46 ms |
46348 KB |
Output is correct |
22 |
Correct |
125 ms |
58964 KB |
Output is correct |
23 |
Correct |
184 ms |
73740 KB |
Output is correct |
24 |
Correct |
178 ms |
73592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
52028 KB |
Output is correct |
2 |
Correct |
105 ms |
59172 KB |
Output is correct |
3 |
Correct |
47 ms |
46364 KB |
Output is correct |
4 |
Correct |
45 ms |
46372 KB |
Output is correct |
5 |
Correct |
268 ms |
89036 KB |
Output is correct |
6 |
Correct |
251 ms |
92040 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
183 ms |
63468 KB |
Output is correct |
9 |
Correct |
232 ms |
72620 KB |
Output is correct |
10 |
Correct |
89 ms |
52100 KB |
Output is correct |
11 |
Correct |
108 ms |
59120 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 |
0 ms |
224 KB |
Output is correct |
16 |
Correct |
46 ms |
46376 KB |
Output is correct |
17 |
Correct |
46 ms |
46372 KB |
Output is correct |
18 |
Correct |
100 ms |
52732 KB |
Output is correct |
19 |
Correct |
116 ms |
59884 KB |
Output is correct |
20 |
Correct |
100 ms |
52448 KB |
Output is correct |
21 |
Correct |
123 ms |
58380 KB |
Output is correct |
22 |
Correct |
98 ms |
52608 KB |
Output is correct |
23 |
Correct |
109 ms |
58412 KB |
Output is correct |
24 |
Correct |
47 ms |
46368 KB |
Output is correct |
25 |
Correct |
47 ms |
46380 KB |
Output is correct |
26 |
Correct |
86 ms |
48180 KB |
Output is correct |
27 |
Correct |
71 ms |
50628 KB |
Output is correct |
28 |
Correct |
112 ms |
58288 KB |
Output is correct |
29 |
Correct |
109 ms |
57620 KB |
Output is correct |
30 |
Correct |
114 ms |
58120 KB |
Output is correct |
31 |
Correct |
122 ms |
58416 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 |
284 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 |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
0 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 |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
43 ms |
6300 KB |
Output is correct |
49 |
Correct |
44 ms |
7588 KB |
Output is correct |
50 |
Correct |
36 ms |
7376 KB |
Output is correct |
51 |
Correct |
35 ms |
7352 KB |
Output is correct |
52 |
Correct |
34 ms |
7300 KB |
Output is correct |
53 |
Correct |
81 ms |
14328 KB |
Output is correct |
54 |
Correct |
8 ms |
1660 KB |
Output is correct |
55 |
Correct |
26 ms |
4424 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
7 ms |
1492 KB |
Output is correct |
58 |
Correct |
3 ms |
1876 KB |
Output is correct |
59 |
Correct |
216 ms |
32332 KB |
Output is correct |
60 |
Correct |
287 ms |
43100 KB |
Output is correct |
61 |
Correct |
260 ms |
42720 KB |
Output is correct |
62 |
Correct |
265 ms |
42720 KB |
Output is correct |
63 |
Correct |
290 ms |
45820 KB |
Output is correct |
64 |
Correct |
266 ms |
43044 KB |
Output is correct |
65 |
Correct |
267 ms |
42740 KB |
Output is correct |
66 |
Correct |
98 ms |
18504 KB |
Output is correct |
67 |
Correct |
282 ms |
46412 KB |
Output is correct |
68 |
Correct |
111 ms |
57980 KB |
Output is correct |
69 |
Correct |
83 ms |
36836 KB |
Output is correct |
70 |
Correct |
171 ms |
73608 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 |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
45 ms |
46464 KB |
Output is correct |
78 |
Correct |
47 ms |
46460 KB |
Output is correct |
79 |
Correct |
46 ms |
46396 KB |
Output is correct |
80 |
Correct |
46 ms |
46348 KB |
Output is correct |
81 |
Correct |
125 ms |
58964 KB |
Output is correct |
82 |
Correct |
184 ms |
73740 KB |
Output is correct |
83 |
Correct |
178 ms |
73592 KB |
Output is correct |
84 |
Correct |
341 ms |
81176 KB |
Output is correct |
85 |
Correct |
354 ms |
85344 KB |
Output is correct |
86 |
Correct |
298 ms |
80652 KB |
Output is correct |
87 |
Correct |
312 ms |
85176 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
303 ms |
85184 KB |
Output is correct |
90 |
Correct |
295 ms |
87236 KB |
Output is correct |
91 |
Correct |
327 ms |
88124 KB |
Output is correct |