#include "fish.h"
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vector<vl> fishes(N+1);
vector<vl> states(N+1, {0});
// initializing states
F(i, 0, M) {
++Y[i];
F(j, max(X[i]-1, 0), min(X[i]+1, N-1) + 1)
states[j].push_back(Y[i]);
}
// rank reduction and correction, i suppose
F(i, 0, N+1) {
sort(A(states[i]));
states[i].erase(unique(A(states[i])), states[i].end());
fishes[i].resize(states[i].size());
}
//
F(i, 0, M) {
int x = X[i];
int y = lower_bound(A(states[x]), Y[i]) - states[x].begin();
fishes[x][y] += W[i];
}
vector<vl> dp_up(N+1), dp_down(N+1);
F(i, 0, N+1) {
int sz = states[i].size();
dp_up[i].assign(sz, 0);
dp_down[i].assign(sz, 0);
if (i > 1) {
ll best = max(dp_up[i-2][0], dp_down[i-2][0]), sum = 0;
for (int j = 0, lef = 0, mid = 0; j < sz; ++j) {
while (lef+1 < (int)states[i-2].size() && states[i-2][lef+1] <= states[i][j]) {
++lef;
while (mid+1 < (int)states[i-1].size() && states[i-1][mid+1] <= states[i-2][lef]) {
++mid;
best += fishes[i-1][mid];
sum += fishes[i-1][mid];
}
best = max(best, max(dp_up[i-2][lef], dp_down[i-2][lef]) + sum);
}
dp_up[i][j] = max(dp_up[i][j], best);
}
}
if (i > 1) {
ll best = 0, sum = accumulate(fishes[i-1].begin(), fishes[i-1].end(), 0LL);
for (int j = sz-1, lef = states[i-2].size(), mid = states[i-1].size(); j >= 0; --j) {
while (lef > 0 && states[i-2][lef-1] >= states[i][j]) {
--lef;
while (mid > 0 && states[i-1][mid-1] > states[i-2][lef]) {
--mid;
sum -= fishes[i-1][mid];
}
best = max(best, max(dp_up[i-2][lef], dp_down[i-2][lef]) + sum);
}
dp_up[i][j] = max(dp_up[i][j], best);
}
}
if (i > 0) {
ll best = dp_up[i-1][0], sum = 0;
for (int j = 0, lef = 0; j < sz; ++j) {
while (lef+1 < (int)states[i-1].size() && states[i-1][lef+1] <= states[i][j]) {
++lef;
best += fishes[i-1][lef];
best = max(best, dp_up[i-1][lef]);
}
dp_up[i][j] = max(dp_up[i][j], best);
}
}
if (i > 0) {
ll best = 0, sum = accumulate(fishes[i].begin(), fishes[i].end(), 0LL);
for (int j = sz-1, lef = states[i-1].size(); j >= 0; --j) {
while (lef > 0 && states[i-1][lef-1] >= states[i][j]) {
--lef;
best = max(best, max(dp_down[i-1][lef], dp_up[i-1][lef]) + sum);
}
dp_down[i][j] = max(dp_down[i][j], best-sum);
sum -= fishes[i][j];
}
}
}
return max(dp_up[N][0], dp_down[N][0]);
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:87:34: warning: unused variable 'sum' [-Wunused-variable]
87 | ll best = dp_up[i-1][0], sum = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
27020 KB |
Output is correct |
2 |
Correct |
83 ms |
30856 KB |
Output is correct |
3 |
Correct |
29 ms |
22312 KB |
Output is correct |
4 |
Correct |
28 ms |
22308 KB |
Output is correct |
5 |
Correct |
239 ms |
57500 KB |
Output is correct |
6 |
Correct |
357 ms |
56152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
136 ms |
33140 KB |
Output is correct |
3 |
Correct |
148 ms |
38016 KB |
Output is correct |
4 |
Correct |
69 ms |
26988 KB |
Output is correct |
5 |
Correct |
84 ms |
30844 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
29 ms |
22104 KB |
Output is correct |
11 |
Correct |
29 ms |
22108 KB |
Output is correct |
12 |
Correct |
80 ms |
29488 KB |
Output is correct |
13 |
Correct |
94 ms |
34016 KB |
Output is correct |
14 |
Correct |
76 ms |
28216 KB |
Output is correct |
15 |
Correct |
80 ms |
29636 KB |
Output is correct |
16 |
Correct |
76 ms |
28220 KB |
Output is correct |
17 |
Correct |
84 ms |
31284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
22364 KB |
Output is correct |
2 |
Correct |
30 ms |
22296 KB |
Output is correct |
3 |
Correct |
55 ms |
22348 KB |
Output is correct |
4 |
Correct |
54 ms |
23632 KB |
Output is correct |
5 |
Correct |
89 ms |
26568 KB |
Output is correct |
6 |
Correct |
88 ms |
26692 KB |
Output is correct |
7 |
Correct |
109 ms |
26636 KB |
Output is correct |
8 |
Correct |
91 ms |
26700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
25 ms |
4436 KB |
Output is correct |
18 |
Correct |
23 ms |
3924 KB |
Output is correct |
19 |
Correct |
26 ms |
3932 KB |
Output is correct |
20 |
Correct |
28 ms |
3852 KB |
Output is correct |
21 |
Correct |
21 ms |
3676 KB |
Output is correct |
22 |
Correct |
44 ms |
7140 KB |
Output is correct |
23 |
Correct |
6 ms |
1372 KB |
Output is correct |
24 |
Correct |
20 ms |
3920 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
25 ms |
4436 KB |
Output is correct |
18 |
Correct |
23 ms |
3924 KB |
Output is correct |
19 |
Correct |
26 ms |
3932 KB |
Output is correct |
20 |
Correct |
28 ms |
3852 KB |
Output is correct |
21 |
Correct |
21 ms |
3676 KB |
Output is correct |
22 |
Correct |
44 ms |
7140 KB |
Output is correct |
23 |
Correct |
6 ms |
1372 KB |
Output is correct |
24 |
Correct |
20 ms |
3920 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
117 ms |
17808 KB |
Output is correct |
29 |
Correct |
183 ms |
28244 KB |
Output is correct |
30 |
Correct |
223 ms |
40052 KB |
Output is correct |
31 |
Correct |
216 ms |
40016 KB |
Output is correct |
32 |
Correct |
159 ms |
23636 KB |
Output is correct |
33 |
Correct |
227 ms |
40516 KB |
Output is correct |
34 |
Correct |
231 ms |
40528 KB |
Output is correct |
35 |
Correct |
68 ms |
13396 KB |
Output is correct |
36 |
Correct |
205 ms |
34668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
22364 KB |
Output is correct |
2 |
Correct |
30 ms |
22296 KB |
Output is correct |
3 |
Correct |
55 ms |
22348 KB |
Output is correct |
4 |
Correct |
54 ms |
23632 KB |
Output is correct |
5 |
Correct |
89 ms |
26568 KB |
Output is correct |
6 |
Correct |
88 ms |
26692 KB |
Output is correct |
7 |
Correct |
109 ms |
26636 KB |
Output is correct |
8 |
Correct |
91 ms |
26700 KB |
Output is correct |
9 |
Correct |
101 ms |
31816 KB |
Output is correct |
10 |
Correct |
59 ms |
16216 KB |
Output is correct |
11 |
Correct |
155 ms |
32268 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
29 ms |
22316 KB |
Output is correct |
19 |
Correct |
29 ms |
22268 KB |
Output is correct |
20 |
Correct |
28 ms |
22360 KB |
Output is correct |
21 |
Correct |
30 ms |
22104 KB |
Output is correct |
22 |
Correct |
134 ms |
31108 KB |
Output is correct |
23 |
Correct |
196 ms |
37456 KB |
Output is correct |
24 |
Correct |
200 ms |
38224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
27020 KB |
Output is correct |
2 |
Correct |
83 ms |
30856 KB |
Output is correct |
3 |
Correct |
29 ms |
22312 KB |
Output is correct |
4 |
Correct |
28 ms |
22308 KB |
Output is correct |
5 |
Correct |
239 ms |
57500 KB |
Output is correct |
6 |
Correct |
357 ms |
56152 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
136 ms |
33140 KB |
Output is correct |
9 |
Correct |
148 ms |
38016 KB |
Output is correct |
10 |
Correct |
69 ms |
26988 KB |
Output is correct |
11 |
Correct |
84 ms |
30844 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
29 ms |
22104 KB |
Output is correct |
17 |
Correct |
29 ms |
22108 KB |
Output is correct |
18 |
Correct |
80 ms |
29488 KB |
Output is correct |
19 |
Correct |
94 ms |
34016 KB |
Output is correct |
20 |
Correct |
76 ms |
28216 KB |
Output is correct |
21 |
Correct |
80 ms |
29636 KB |
Output is correct |
22 |
Correct |
76 ms |
28220 KB |
Output is correct |
23 |
Correct |
84 ms |
31284 KB |
Output is correct |
24 |
Correct |
28 ms |
22364 KB |
Output is correct |
25 |
Correct |
30 ms |
22296 KB |
Output is correct |
26 |
Correct |
55 ms |
22348 KB |
Output is correct |
27 |
Correct |
54 ms |
23632 KB |
Output is correct |
28 |
Correct |
89 ms |
26568 KB |
Output is correct |
29 |
Correct |
88 ms |
26692 KB |
Output is correct |
30 |
Correct |
109 ms |
26636 KB |
Output is correct |
31 |
Correct |
91 ms |
26700 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
25 ms |
4436 KB |
Output is correct |
49 |
Correct |
23 ms |
3924 KB |
Output is correct |
50 |
Correct |
26 ms |
3932 KB |
Output is correct |
51 |
Correct |
28 ms |
3852 KB |
Output is correct |
52 |
Correct |
21 ms |
3676 KB |
Output is correct |
53 |
Correct |
44 ms |
7140 KB |
Output is correct |
54 |
Correct |
6 ms |
1372 KB |
Output is correct |
55 |
Correct |
20 ms |
3920 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
5 ms |
1372 KB |
Output is correct |
58 |
Correct |
3 ms |
1112 KB |
Output is correct |
59 |
Correct |
117 ms |
17808 KB |
Output is correct |
60 |
Correct |
183 ms |
28244 KB |
Output is correct |
61 |
Correct |
223 ms |
40052 KB |
Output is correct |
62 |
Correct |
216 ms |
40016 KB |
Output is correct |
63 |
Correct |
159 ms |
23636 KB |
Output is correct |
64 |
Correct |
227 ms |
40516 KB |
Output is correct |
65 |
Correct |
231 ms |
40528 KB |
Output is correct |
66 |
Correct |
68 ms |
13396 KB |
Output is correct |
67 |
Correct |
205 ms |
34668 KB |
Output is correct |
68 |
Correct |
101 ms |
31816 KB |
Output is correct |
69 |
Correct |
59 ms |
16216 KB |
Output is correct |
70 |
Correct |
155 ms |
32268 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
29 ms |
22316 KB |
Output is correct |
78 |
Correct |
29 ms |
22268 KB |
Output is correct |
79 |
Correct |
28 ms |
22360 KB |
Output is correct |
80 |
Correct |
30 ms |
22104 KB |
Output is correct |
81 |
Correct |
134 ms |
31108 KB |
Output is correct |
82 |
Correct |
196 ms |
37456 KB |
Output is correct |
83 |
Correct |
200 ms |
38224 KB |
Output is correct |
84 |
Correct |
259 ms |
52912 KB |
Output is correct |
85 |
Correct |
264 ms |
55032 KB |
Output is correct |
86 |
Correct |
342 ms |
54124 KB |
Output is correct |
87 |
Correct |
340 ms |
56168 KB |
Output is correct |
88 |
Correct |
1 ms |
344 KB |
Output is correct |
89 |
Correct |
399 ms |
56168 KB |
Output is correct |
90 |
Correct |
255 ms |
53084 KB |
Output is correct |
91 |
Correct |
193 ms |
47440 KB |
Output is correct |