#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
using ll = long long;
using pii = pair<int, int>;
const ll inf = 1e16;
int n, m;
map<pii, ll> F;
// ll max_weights_y0(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
// n = N, m = M;
// vector<ll> A(n);
// for(int i = 0; i < m; i++) A[X[i]] = W[i];
// auto get = [&](int i) {
// return (i < 0 || i >= n ? 0 : A[i]);
// };
// ll mx1 = 0, mx2 = 0, mx3 = 0;
// for(int i = 0; i < n; i++) {
// ll nmx1, nmx2, nmx3;
// nmx3 = max(mx3, mx2);
// nmx2 = max(mx2, mx1);
// nmx1 = max(mx1, mx3+get(i-1)+get(i+1));
// nmx1 = max(nmx1, mx2+get(i+1));
// nmx1 = max(nmx1, mx1-get(i)+get(i+1));
// mx1 = nmx1, mx2 = nmx2, mx3 = nmx3;
// }
// return max({mx1, mx2, mx3});
// }
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
//return max_weights_y0(N, M, X, Y, W);
n = N, m = M;
vector<vector<int>> pos(n);
for(int i = 0; i < m; i++) {
if(X[i] > 0) pos[X[i]-1].pb(Y[i]);
if(X[i]+1 < n) pos[X[i]+1].pb(Y[i]);
F[{X[i], Y[i]}] = W[i];
}
for(int i = 0; i < n; i++) {
if(pos[i].empty()) continue;
sort(pos[i].begin(), pos[i].end());
pos[i].erase(unique(pos[i].begin(), pos[i].end()), pos[i].end());
}
// printf("interesting positions:\n");
// for(int i = 0; i < n; i++) {
// printf("i = %d\n", i);
// for(int j : pos[i]) {
// printf("(%d, %d) ", i, j);
// }
// printf("\n");
// }
vector<vector<ll>> dp[2]; // 0-inc, 1-dec
dp[0] = dp[1] = vector<vector<ll>>(n);
for(int k = 0; k < 2; k++) {
for(int i = 0; i < n; i++) {
dp[k][i] = vector<ll>(pos[i].size(), -inf);
}
}
ll mx_with = 0, mx_wo = 0; // 'with' as in with the fishes on the right
for(int i = 0; i < n; i++) {
ll sum;
mx_wo = max(mx_wo, mx_with);
if(i > 1) for(int j = 0; j < (int)pos[i-2].size(); j++) {
mx_wo = max(mx_wo, dp[0][i-2][j]);
mx_wo = max(mx_wo, dp[1][i-2][j]);
}
sum = 0;
if(i > 1) for(int j = 0; j < (int)pos[i-2].size(); j++) {
sum += F[pii{i-1, pos[i-2][j]}];
mx_with = max(mx_with, dp[0][i-2][j]+sum);
mx_with = max(mx_with, dp[1][i-2][j]+sum);
}
// try starting a new seq
sum = 0;
for(int j = 0; j < (int)pos[i].size(); j++) {
sum += F[pii{i-1, pos[i][j]}];
dp[0][i][j] = max(dp[0][i][j], mx_wo+sum);
dp[0][i][j] = max(dp[0][i][j], mx_with);
}
// inc to inc
ll mx = -inf;
if(i > 0) for(int j = 0, k = 0; j < (int)pos[i].size(); j++) {
while(k < (int)pos[i-1].size() && pos[i-1][k] < pos[i][j]) {
mx = max(mx, dp[0][i-1][k]);
k++;
}
mx += F[pii{i-1, pos[i][j]}];
dp[0][i][j] = max(dp[0][i][j], mx);
}
mx = -inf;
// inc/dec to dec
if(i > 0) for(int j = (int)pos[i].size()-1, k = (int)pos[i-1].size()-1;
j >= 0; j--) {
ll delayed = 0;
while(k >= 0 && pos[i-1][k] >= pos[i][j]) {
mx = max(mx, dp[0][i-1][k]);
mx = max(mx, dp[1][i-1][k]);
if(pos[i-1][k] != pos[i][j]) mx += F[pii{i, pos[i-1][k]}];
else delayed += F[pii{i, pos[i-1][k]}];
k--;
}
dp[1][i][j] = max(dp[1][i][j], mx);
mx += delayed;
}
}
ll ans = 0;
for(int i = 0; i < n; i++) {
ll sum = 0;
for(int j = 0; j < (int)pos[i].size(); j++) {
sum += F[pii{i+1, pos[i][j]}];
// printf("sum = %lld\n", sum);
// printf("dp[%d][%d][%d] = %lld\n", 0, i, j, dp[0][i][j]);
// printf("dp[%d][%d][%d] = %lld\n", 1, i, j, dp[1][i][j]);
ans = max(ans, dp[0][i][j]+sum);
ans = max(ans, dp[1][i][j]+sum);
}
}
//cout << ans << "!\n";
return ans;
}
// int main() {
// int N, M;
// vector<int> X, Y, W;
// cin >> N >> M;
// for(int i = 0; i < M; i++) {
// int x, y, w; cin >> x >> y >> w;
// X.pb(x), Y.pb(y), W.pb(w);
// }
// ll ans1 = max_weights(N, M, X, Y, W);
// ll ans2 = max_weights_y0(N, M, X, Y, W);
// cerr << ans1 << " " << ans2 << "\n";
// assert(ans1 == ans2);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
20192 KB |
Output is correct |
2 |
Correct |
110 ms |
24100 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
621 ms |
82440 KB |
Output is correct |
6 |
Correct |
614 ms |
86476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
243 ms |
40456 KB |
Output is correct |
3 |
Correct |
319 ms |
49216 KB |
Output is correct |
4 |
Correct |
90 ms |
20172 KB |
Output is correct |
5 |
Correct |
120 ms |
24124 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
128 ms |
26956 KB |
Output is correct |
13 |
Correct |
174 ms |
32372 KB |
Output is correct |
14 |
Correct |
113 ms |
23616 KB |
Output is correct |
15 |
Correct |
129 ms |
26200 KB |
Output is correct |
16 |
Correct |
108 ms |
23560 KB |
Output is correct |
17 |
Correct |
123 ms |
26184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
64 ms |
17024 KB |
Output is correct |
4 |
Correct |
43 ms |
15404 KB |
Output is correct |
5 |
Correct |
135 ms |
26972 KB |
Output is correct |
6 |
Correct |
145 ms |
26256 KB |
Output is correct |
7 |
Correct |
143 ms |
26920 KB |
Output is correct |
8 |
Correct |
128 ms |
26916 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 |
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 |
1 ms |
212 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 |
440 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 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 |
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 |
1 ms |
212 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 |
440 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
46 ms |
7508 KB |
Output is correct |
18 |
Correct |
42 ms |
6904 KB |
Output is correct |
19 |
Correct |
44 ms |
7500 KB |
Output is correct |
20 |
Correct |
39 ms |
6388 KB |
Output is correct |
21 |
Correct |
40 ms |
6296 KB |
Output is correct |
22 |
Correct |
83 ms |
12236 KB |
Output is correct |
23 |
Correct |
13 ms |
2600 KB |
Output is correct |
24 |
Correct |
40 ms |
6520 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
11 ms |
2200 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 |
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 |
1 ms |
212 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 |
440 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
46 ms |
7508 KB |
Output is correct |
18 |
Correct |
42 ms |
6904 KB |
Output is correct |
19 |
Correct |
44 ms |
7500 KB |
Output is correct |
20 |
Correct |
39 ms |
6388 KB |
Output is correct |
21 |
Correct |
40 ms |
6296 KB |
Output is correct |
22 |
Correct |
83 ms |
12236 KB |
Output is correct |
23 |
Correct |
13 ms |
2600 KB |
Output is correct |
24 |
Correct |
40 ms |
6520 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
11 ms |
2200 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
262 ms |
33260 KB |
Output is correct |
29 |
Correct |
479 ms |
61284 KB |
Output is correct |
30 |
Correct |
570 ms |
79256 KB |
Output is correct |
31 |
Correct |
570 ms |
78204 KB |
Output is correct |
32 |
Correct |
372 ms |
40416 KB |
Output is correct |
33 |
Correct |
566 ms |
80224 KB |
Output is correct |
34 |
Correct |
571 ms |
80212 KB |
Output is correct |
35 |
Correct |
172 ms |
25932 KB |
Output is correct |
36 |
Correct |
568 ms |
71260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
64 ms |
17024 KB |
Output is correct |
4 |
Correct |
43 ms |
15404 KB |
Output is correct |
5 |
Correct |
135 ms |
26972 KB |
Output is correct |
6 |
Correct |
145 ms |
26256 KB |
Output is correct |
7 |
Correct |
143 ms |
26920 KB |
Output is correct |
8 |
Correct |
128 ms |
26916 KB |
Output is correct |
9 |
Correct |
174 ms |
39116 KB |
Output is correct |
10 |
Correct |
102 ms |
18860 KB |
Output is correct |
11 |
Correct |
359 ms |
37428 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
292 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 |
4 ms |
7336 KB |
Output is correct |
19 |
Correct |
4 ms |
7252 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
4 ms |
7252 KB |
Output is correct |
22 |
Correct |
219 ms |
39228 KB |
Output is correct |
23 |
Correct |
410 ms |
54632 KB |
Output is correct |
24 |
Correct |
484 ms |
62252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
20192 KB |
Output is correct |
2 |
Correct |
110 ms |
24100 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
621 ms |
82440 KB |
Output is correct |
6 |
Correct |
614 ms |
86476 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
243 ms |
40456 KB |
Output is correct |
9 |
Correct |
319 ms |
49216 KB |
Output is correct |
10 |
Correct |
90 ms |
20172 KB |
Output is correct |
11 |
Correct |
120 ms |
24124 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
300 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
128 ms |
26956 KB |
Output is correct |
19 |
Correct |
174 ms |
32372 KB |
Output is correct |
20 |
Correct |
113 ms |
23616 KB |
Output is correct |
21 |
Correct |
129 ms |
26200 KB |
Output is correct |
22 |
Correct |
108 ms |
23560 KB |
Output is correct |
23 |
Correct |
123 ms |
26184 KB |
Output is correct |
24 |
Correct |
4 ms |
7252 KB |
Output is correct |
25 |
Correct |
4 ms |
7252 KB |
Output is correct |
26 |
Correct |
64 ms |
17024 KB |
Output is correct |
27 |
Correct |
43 ms |
15404 KB |
Output is correct |
28 |
Correct |
135 ms |
26972 KB |
Output is correct |
29 |
Correct |
145 ms |
26256 KB |
Output is correct |
30 |
Correct |
143 ms |
26920 KB |
Output is correct |
31 |
Correct |
128 ms |
26916 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 |
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 |
1 ms |
212 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 |
440 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
296 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
46 ms |
7508 KB |
Output is correct |
49 |
Correct |
42 ms |
6904 KB |
Output is correct |
50 |
Correct |
44 ms |
7500 KB |
Output is correct |
51 |
Correct |
39 ms |
6388 KB |
Output is correct |
52 |
Correct |
40 ms |
6296 KB |
Output is correct |
53 |
Correct |
83 ms |
12236 KB |
Output is correct |
54 |
Correct |
13 ms |
2600 KB |
Output is correct |
55 |
Correct |
40 ms |
6520 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
11 ms |
2200 KB |
Output is correct |
58 |
Correct |
4 ms |
1364 KB |
Output is correct |
59 |
Correct |
262 ms |
33260 KB |
Output is correct |
60 |
Correct |
479 ms |
61284 KB |
Output is correct |
61 |
Correct |
570 ms |
79256 KB |
Output is correct |
62 |
Correct |
570 ms |
78204 KB |
Output is correct |
63 |
Correct |
372 ms |
40416 KB |
Output is correct |
64 |
Correct |
566 ms |
80224 KB |
Output is correct |
65 |
Correct |
571 ms |
80212 KB |
Output is correct |
66 |
Correct |
172 ms |
25932 KB |
Output is correct |
67 |
Correct |
568 ms |
71260 KB |
Output is correct |
68 |
Correct |
174 ms |
39116 KB |
Output is correct |
69 |
Correct |
102 ms |
18860 KB |
Output is correct |
70 |
Correct |
359 ms |
37428 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
292 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 |
4 ms |
7336 KB |
Output is correct |
78 |
Correct |
4 ms |
7252 KB |
Output is correct |
79 |
Correct |
4 ms |
7252 KB |
Output is correct |
80 |
Correct |
4 ms |
7252 KB |
Output is correct |
81 |
Correct |
219 ms |
39228 KB |
Output is correct |
82 |
Correct |
410 ms |
54632 KB |
Output is correct |
83 |
Correct |
484 ms |
62252 KB |
Output is correct |
84 |
Correct |
632 ms |
82852 KB |
Output is correct |
85 |
Correct |
575 ms |
81848 KB |
Output is correct |
86 |
Correct |
694 ms |
92104 KB |
Output is correct |
87 |
Correct |
692 ms |
93600 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
795 ms |
93652 KB |
Output is correct |
90 |
Correct |
603 ms |
75152 KB |
Output is correct |
91 |
Correct |
538 ms |
56188 KB |
Output is correct |