#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();
while (l < r-1) {
int mid = (l+r) / 2;
if (pref[c][mid].first <= right) l = mid;
else r= mid;
}
return l==-1 ? 0 : pref[c][l].second;
};
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 |
89 ms |
50708 KB |
Output is correct |
2 |
Correct |
103 ms |
57340 KB |
Output is correct |
3 |
Correct |
51 ms |
46428 KB |
Output is correct |
4 |
Correct |
46 ms |
46380 KB |
Output is correct |
5 |
Correct |
254 ms |
84416 KB |
Output is correct |
6 |
Correct |
253 ms |
87104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
179 ms |
60640 KB |
Output is correct |
3 |
Correct |
220 ms |
69204 KB |
Output is correct |
4 |
Correct |
91 ms |
50716 KB |
Output is correct |
5 |
Correct |
101 ms |
57360 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 |
212 KB |
Output is correct |
10 |
Correct |
46 ms |
46368 KB |
Output is correct |
11 |
Correct |
46 ms |
46436 KB |
Output is correct |
12 |
Correct |
97 ms |
51376 KB |
Output is correct |
13 |
Correct |
117 ms |
58224 KB |
Output is correct |
14 |
Correct |
115 ms |
51120 KB |
Output is correct |
15 |
Correct |
122 ms |
56988 KB |
Output is correct |
16 |
Correct |
99 ms |
51304 KB |
Output is correct |
17 |
Correct |
108 ms |
56772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
46424 KB |
Output is correct |
2 |
Correct |
46 ms |
46396 KB |
Output is correct |
3 |
Correct |
81 ms |
47188 KB |
Output is correct |
4 |
Correct |
89 ms |
50164 KB |
Output is correct |
5 |
Correct |
117 ms |
56608 KB |
Output is correct |
6 |
Correct |
111 ms |
56600 KB |
Output is correct |
7 |
Correct |
112 ms |
56536 KB |
Output is correct |
8 |
Correct |
118 ms |
56532 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 |
1 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 |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
1 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 |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
51 ms |
6216 KB |
Output is correct |
18 |
Correct |
44 ms |
7628 KB |
Output is correct |
19 |
Correct |
39 ms |
7280 KB |
Output is correct |
20 |
Correct |
37 ms |
7368 KB |
Output is correct |
21 |
Correct |
38 ms |
7360 KB |
Output is correct |
22 |
Correct |
81 ms |
14252 KB |
Output is correct |
23 |
Correct |
8 ms |
1620 KB |
Output is correct |
24 |
Correct |
28 ms |
4548 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 |
1 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 |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
51 ms |
6216 KB |
Output is correct |
18 |
Correct |
44 ms |
7628 KB |
Output is correct |
19 |
Correct |
39 ms |
7280 KB |
Output is correct |
20 |
Correct |
37 ms |
7368 KB |
Output is correct |
21 |
Correct |
38 ms |
7360 KB |
Output is correct |
22 |
Correct |
81 ms |
14252 KB |
Output is correct |
23 |
Correct |
8 ms |
1620 KB |
Output is correct |
24 |
Correct |
28 ms |
4548 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
1492 KB |
Output is correct |
27 |
Correct |
8 ms |
2004 KB |
Output is correct |
28 |
Correct |
209 ms |
32404 KB |
Output is correct |
29 |
Correct |
302 ms |
43100 KB |
Output is correct |
30 |
Correct |
274 ms |
42828 KB |
Output is correct |
31 |
Correct |
276 ms |
42764 KB |
Output is correct |
32 |
Correct |
287 ms |
45732 KB |
Output is correct |
33 |
Correct |
276 ms |
42720 KB |
Output is correct |
34 |
Correct |
280 ms |
42720 KB |
Output is correct |
35 |
Correct |
101 ms |
18512 KB |
Output is correct |
36 |
Correct |
291 ms |
46472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
46424 KB |
Output is correct |
2 |
Correct |
46 ms |
46396 KB |
Output is correct |
3 |
Correct |
81 ms |
47188 KB |
Output is correct |
4 |
Correct |
89 ms |
50164 KB |
Output is correct |
5 |
Correct |
117 ms |
56608 KB |
Output is correct |
6 |
Correct |
111 ms |
56600 KB |
Output is correct |
7 |
Correct |
112 ms |
56536 KB |
Output is correct |
8 |
Correct |
118 ms |
56532 KB |
Output is correct |
9 |
Correct |
121 ms |
56548 KB |
Output is correct |
10 |
Correct |
108 ms |
35128 KB |
Output is correct |
11 |
Correct |
180 ms |
70120 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 |
276 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
46 ms |
46444 KB |
Output is correct |
19 |
Correct |
49 ms |
46412 KB |
Output is correct |
20 |
Correct |
56 ms |
46372 KB |
Output is correct |
21 |
Correct |
47 ms |
46380 KB |
Output is correct |
22 |
Correct |
154 ms |
56904 KB |
Output is correct |
23 |
Correct |
180 ms |
70164 KB |
Output is correct |
24 |
Correct |
179 ms |
70200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
50708 KB |
Output is correct |
2 |
Correct |
103 ms |
57340 KB |
Output is correct |
3 |
Correct |
51 ms |
46428 KB |
Output is correct |
4 |
Correct |
46 ms |
46380 KB |
Output is correct |
5 |
Correct |
254 ms |
84416 KB |
Output is correct |
6 |
Correct |
253 ms |
87104 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
179 ms |
60640 KB |
Output is correct |
9 |
Correct |
220 ms |
69204 KB |
Output is correct |
10 |
Correct |
91 ms |
50716 KB |
Output is correct |
11 |
Correct |
101 ms |
57360 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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
46 ms |
46368 KB |
Output is correct |
17 |
Correct |
46 ms |
46436 KB |
Output is correct |
18 |
Correct |
97 ms |
51376 KB |
Output is correct |
19 |
Correct |
117 ms |
58224 KB |
Output is correct |
20 |
Correct |
115 ms |
51120 KB |
Output is correct |
21 |
Correct |
122 ms |
56988 KB |
Output is correct |
22 |
Correct |
99 ms |
51304 KB |
Output is correct |
23 |
Correct |
108 ms |
56772 KB |
Output is correct |
24 |
Correct |
51 ms |
46424 KB |
Output is correct |
25 |
Correct |
46 ms |
46396 KB |
Output is correct |
26 |
Correct |
81 ms |
47188 KB |
Output is correct |
27 |
Correct |
89 ms |
50164 KB |
Output is correct |
28 |
Correct |
117 ms |
56608 KB |
Output is correct |
29 |
Correct |
111 ms |
56600 KB |
Output is correct |
30 |
Correct |
112 ms |
56536 KB |
Output is correct |
31 |
Correct |
118 ms |
56532 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 |
1 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 |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 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 |
51 ms |
6216 KB |
Output is correct |
49 |
Correct |
44 ms |
7628 KB |
Output is correct |
50 |
Correct |
39 ms |
7280 KB |
Output is correct |
51 |
Correct |
37 ms |
7368 KB |
Output is correct |
52 |
Correct |
38 ms |
7360 KB |
Output is correct |
53 |
Correct |
81 ms |
14252 KB |
Output is correct |
54 |
Correct |
8 ms |
1620 KB |
Output is correct |
55 |
Correct |
28 ms |
4548 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
7 ms |
1492 KB |
Output is correct |
58 |
Correct |
8 ms |
2004 KB |
Output is correct |
59 |
Correct |
209 ms |
32404 KB |
Output is correct |
60 |
Correct |
302 ms |
43100 KB |
Output is correct |
61 |
Correct |
274 ms |
42828 KB |
Output is correct |
62 |
Correct |
276 ms |
42764 KB |
Output is correct |
63 |
Correct |
287 ms |
45732 KB |
Output is correct |
64 |
Correct |
276 ms |
42720 KB |
Output is correct |
65 |
Correct |
280 ms |
42720 KB |
Output is correct |
66 |
Correct |
101 ms |
18512 KB |
Output is correct |
67 |
Correct |
291 ms |
46472 KB |
Output is correct |
68 |
Correct |
121 ms |
56548 KB |
Output is correct |
69 |
Correct |
108 ms |
35128 KB |
Output is correct |
70 |
Correct |
180 ms |
70120 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 |
276 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
46 ms |
46444 KB |
Output is correct |
78 |
Correct |
49 ms |
46412 KB |
Output is correct |
79 |
Correct |
56 ms |
46372 KB |
Output is correct |
80 |
Correct |
47 ms |
46380 KB |
Output is correct |
81 |
Correct |
154 ms |
56904 KB |
Output is correct |
82 |
Correct |
180 ms |
70164 KB |
Output is correct |
83 |
Correct |
179 ms |
70200 KB |
Output is correct |
84 |
Correct |
359 ms |
81204 KB |
Output is correct |
85 |
Correct |
355 ms |
85344 KB |
Output is correct |
86 |
Correct |
317 ms |
80660 KB |
Output is correct |
87 |
Correct |
314 ms |
85188 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
317 ms |
85232 KB |
Output is correct |
90 |
Correct |
299 ms |
87232 KB |
Output is correct |
91 |
Correct |
298 ms |
88076 KB |
Output is correct |