#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(states[i].begin(), states[i].end());
states[i].erase(unique(states[i].begin(), states[i].end()), states[i].end());
fishes[i].resize(states[i].size());
}
//
F(i, 0, M) {
int x = X[i];
int y = lower_bound(states[x].begin(), states[x].end(), Y[i]) - states[x].begin();
fishes[x][y] += W[i];
}
vector<vector<long long>> dp_up(N+1), dp_down(N+1);
for (int i = 0; i <= N; ++i) {
int sz = states[i].size();
dp_up[i].assign(sz, 0);
dp_down[i].assign(sz, 0);
if (i > 1) {
long long 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);
}
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) {
long long 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);
}
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]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
27012 KB |
Output is correct |
2 |
Correct |
81 ms |
32700 KB |
Output is correct |
3 |
Correct |
32 ms |
22100 KB |
Output is correct |
4 |
Correct |
27 ms |
22288 KB |
Output is correct |
5 |
Correct |
241 ms |
63948 KB |
Output is correct |
6 |
Correct |
330 ms |
62800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
125 ms |
35960 KB |
Output is correct |
3 |
Correct |
149 ms |
41472 KB |
Output is correct |
4 |
Correct |
69 ms |
28528 KB |
Output is correct |
5 |
Correct |
96 ms |
32732 KB |
Output is correct |
6 |
Correct |
1 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 |
600 KB |
Output is correct |
10 |
Correct |
28 ms |
22240 KB |
Output is correct |
11 |
Correct |
28 ms |
22108 KB |
Output is correct |
12 |
Correct |
79 ms |
31028 KB |
Output is correct |
13 |
Correct |
102 ms |
35852 KB |
Output is correct |
14 |
Correct |
74 ms |
29784 KB |
Output is correct |
15 |
Correct |
84 ms |
31032 KB |
Output is correct |
16 |
Correct |
76 ms |
29752 KB |
Output is correct |
17 |
Correct |
85 ms |
32924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
22104 KB |
Output is correct |
2 |
Correct |
30 ms |
22380 KB |
Output is correct |
3 |
Correct |
66 ms |
23428 KB |
Output is correct |
4 |
Correct |
50 ms |
24404 KB |
Output is correct |
5 |
Correct |
100 ms |
28080 KB |
Output is correct |
6 |
Correct |
89 ms |
27564 KB |
Output is correct |
7 |
Correct |
126 ms |
28212 KB |
Output is correct |
8 |
Correct |
96 ms |
28052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
25 ms |
5208 KB |
Output is correct |
18 |
Correct |
23 ms |
4688 KB |
Output is correct |
19 |
Correct |
22 ms |
4692 KB |
Output is correct |
20 |
Correct |
22 ms |
4488 KB |
Output is correct |
21 |
Correct |
21 ms |
4592 KB |
Output is correct |
22 |
Correct |
49 ms |
8672 KB |
Output is correct |
23 |
Correct |
7 ms |
1628 KB |
Output is correct |
24 |
Correct |
18 ms |
4220 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
6 ms |
1572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
25 ms |
5208 KB |
Output is correct |
18 |
Correct |
23 ms |
4688 KB |
Output is correct |
19 |
Correct |
22 ms |
4692 KB |
Output is correct |
20 |
Correct |
22 ms |
4488 KB |
Output is correct |
21 |
Correct |
21 ms |
4592 KB |
Output is correct |
22 |
Correct |
49 ms |
8672 KB |
Output is correct |
23 |
Correct |
7 ms |
1628 KB |
Output is correct |
24 |
Correct |
18 ms |
4220 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
6 ms |
1572 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
118 ms |
21524 KB |
Output is correct |
29 |
Correct |
197 ms |
33892 KB |
Output is correct |
30 |
Correct |
223 ms |
45648 KB |
Output is correct |
31 |
Correct |
236 ms |
45652 KB |
Output is correct |
32 |
Correct |
155 ms |
28756 KB |
Output is correct |
33 |
Correct |
231 ms |
46300 KB |
Output is correct |
34 |
Correct |
233 ms |
46336 KB |
Output is correct |
35 |
Correct |
69 ms |
15568 KB |
Output is correct |
36 |
Correct |
210 ms |
40272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
22104 KB |
Output is correct |
2 |
Correct |
30 ms |
22380 KB |
Output is correct |
3 |
Correct |
66 ms |
23428 KB |
Output is correct |
4 |
Correct |
50 ms |
24404 KB |
Output is correct |
5 |
Correct |
100 ms |
28080 KB |
Output is correct |
6 |
Correct |
89 ms |
27564 KB |
Output is correct |
7 |
Correct |
126 ms |
28212 KB |
Output is correct |
8 |
Correct |
96 ms |
28052 KB |
Output is correct |
9 |
Correct |
118 ms |
32852 KB |
Output is correct |
10 |
Correct |
61 ms |
18000 KB |
Output is correct |
11 |
Correct |
164 ms |
35548 KB |
Output is correct |
12 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
37 ms |
22180 KB |
Output is correct |
19 |
Correct |
29 ms |
22364 KB |
Output is correct |
20 |
Correct |
30 ms |
22096 KB |
Output is correct |
21 |
Correct |
33 ms |
22204 KB |
Output is correct |
22 |
Correct |
135 ms |
33220 KB |
Output is correct |
23 |
Correct |
204 ms |
41420 KB |
Output is correct |
24 |
Correct |
191 ms |
42084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
27012 KB |
Output is correct |
2 |
Correct |
81 ms |
32700 KB |
Output is correct |
3 |
Correct |
32 ms |
22100 KB |
Output is correct |
4 |
Correct |
27 ms |
22288 KB |
Output is correct |
5 |
Correct |
241 ms |
63948 KB |
Output is correct |
6 |
Correct |
330 ms |
62800 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
125 ms |
35960 KB |
Output is correct |
9 |
Correct |
149 ms |
41472 KB |
Output is correct |
10 |
Correct |
69 ms |
28528 KB |
Output is correct |
11 |
Correct |
96 ms |
32732 KB |
Output is correct |
12 |
Correct |
1 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 |
600 KB |
Output is correct |
16 |
Correct |
28 ms |
22240 KB |
Output is correct |
17 |
Correct |
28 ms |
22108 KB |
Output is correct |
18 |
Correct |
79 ms |
31028 KB |
Output is correct |
19 |
Correct |
102 ms |
35852 KB |
Output is correct |
20 |
Correct |
74 ms |
29784 KB |
Output is correct |
21 |
Correct |
84 ms |
31032 KB |
Output is correct |
22 |
Correct |
76 ms |
29752 KB |
Output is correct |
23 |
Correct |
85 ms |
32924 KB |
Output is correct |
24 |
Correct |
32 ms |
22104 KB |
Output is correct |
25 |
Correct |
30 ms |
22380 KB |
Output is correct |
26 |
Correct |
66 ms |
23428 KB |
Output is correct |
27 |
Correct |
50 ms |
24404 KB |
Output is correct |
28 |
Correct |
100 ms |
28080 KB |
Output is correct |
29 |
Correct |
89 ms |
27564 KB |
Output is correct |
30 |
Correct |
126 ms |
28212 KB |
Output is correct |
31 |
Correct |
96 ms |
28052 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
544 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
432 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
440 KB |
Output is correct |
48 |
Correct |
25 ms |
5208 KB |
Output is correct |
49 |
Correct |
23 ms |
4688 KB |
Output is correct |
50 |
Correct |
22 ms |
4692 KB |
Output is correct |
51 |
Correct |
22 ms |
4488 KB |
Output is correct |
52 |
Correct |
21 ms |
4592 KB |
Output is correct |
53 |
Correct |
49 ms |
8672 KB |
Output is correct |
54 |
Correct |
7 ms |
1628 KB |
Output is correct |
55 |
Correct |
18 ms |
4220 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
6 ms |
1572 KB |
Output is correct |
58 |
Correct |
3 ms |
1372 KB |
Output is correct |
59 |
Correct |
118 ms |
21524 KB |
Output is correct |
60 |
Correct |
197 ms |
33892 KB |
Output is correct |
61 |
Correct |
223 ms |
45648 KB |
Output is correct |
62 |
Correct |
236 ms |
45652 KB |
Output is correct |
63 |
Correct |
155 ms |
28756 KB |
Output is correct |
64 |
Correct |
231 ms |
46300 KB |
Output is correct |
65 |
Correct |
233 ms |
46336 KB |
Output is correct |
66 |
Correct |
69 ms |
15568 KB |
Output is correct |
67 |
Correct |
210 ms |
40272 KB |
Output is correct |
68 |
Correct |
118 ms |
32852 KB |
Output is correct |
69 |
Correct |
61 ms |
18000 KB |
Output is correct |
70 |
Correct |
164 ms |
35548 KB |
Output is correct |
71 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
432 KB |
Output is correct |
77 |
Correct |
37 ms |
22180 KB |
Output is correct |
78 |
Correct |
29 ms |
22364 KB |
Output is correct |
79 |
Correct |
30 ms |
22096 KB |
Output is correct |
80 |
Correct |
33 ms |
22204 KB |
Output is correct |
81 |
Correct |
135 ms |
33220 KB |
Output is correct |
82 |
Correct |
204 ms |
41420 KB |
Output is correct |
83 |
Correct |
191 ms |
42084 KB |
Output is correct |
84 |
Correct |
271 ms |
58864 KB |
Output is correct |
85 |
Correct |
265 ms |
60412 KB |
Output is correct |
86 |
Correct |
317 ms |
60268 KB |
Output is correct |
87 |
Correct |
376 ms |
62548 KB |
Output is correct |
88 |
Correct |
1 ms |
344 KB |
Output is correct |
89 |
Correct |
344 ms |
62560 KB |
Output is correct |
90 |
Correct |
247 ms |
58448 KB |
Output is correct |
91 |
Correct |
195 ms |
52712 KB |
Output is correct |