// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 100005;
const int MAXM = 300005;
int n, m;
vii fish[MAXN];
vector<pair<int, ll>> dp[2][MAXN];
ll max_weights(int N, int M, vi X, vi Y, vi W) {
n = N; m = M;
REP (i, 0, m) {
X[i]++; Y[i]++;
fish[X[i]].pb({Y[i], W[i]});
}
REP (i, 1, n + 1) {
sort(ALL(fish[i]));
}
dp[0][0].pb({0, 0});
dp[1][0].pb({0, 0});
REP (i, 1, n + 1) {
vi cand;
for (auto [y, w] : fish[i - 1]) {
cand.pb(y);
}
for (auto [y, w] : fish[i]) {
cand.pb(y - 1);
}
for (auto [y, w] : fish[i + 1]) {
cand.pb(y);
}
cand.pb(0);
sort(ALL(cand));
cand.resize(unique(ALL(cand)) - cand.begin());
REP (z, 0, 2) {
REP (j, 0, SZ(cand)) {
dp[z][i].pb({cand[j], 0});
}
}
{ // .^
auto fptr = fish[i - 1].begin();
auto fdptr = fish[i - 1].begin();
auto dptr = dp[0][i - 1].begin();
ll sm = 0;
ll sub = 0;
ll mx = 0;
REP (j, 0, SZ(cand)) {
while (fptr != fish[i - 1].end() && fptr -> FI <= cand[j]) {
sm += fptr -> SE;
++fptr;
}
while (dptr != dp[0][i - 1].end() && dptr -> FI <= cand[j]) {
while (fdptr != fish[i - 1].end() && fdptr -> FI <= dptr -> FI) {
sub += fdptr -> SE;
++fdptr;
}
mxto(mx, dptr -> SE - sub);
++dptr;
}
mxto(dp[0][i][j].SE, mx + sm);
mxto(dp[1][i][j].SE, mx + sm);
}
}
{ // ^.
ll sm = 0;
auto fdptr = fish[i].begin();
auto fptr = fish[i].begin();
auto dptr = dp[1][i - 1].end();
while (fdptr != fish[i].end() && fdptr -> FI <= prev(dptr) -> FI) {
sm += fdptr -> SE;
++fdptr;
}
ll sub = 0;
while (fptr != fish[i].end() && fptr -> FI <= cand.back()) {
sub += fptr -> SE;
fptr++;
}
ll mx = 0;
RREP (j, SZ(cand) - 1, 0) {
while (fptr != fish[i].begin() && prev(fptr) -> FI > cand[j]) {
--fptr;
sub -= fptr -> SE;
}
while (dptr != dp[1][i - 1].begin() && prev(dptr) -> FI >= cand[j]) {
--dptr;
while (fdptr != fish[i].begin() && prev(fdptr) -> FI > dptr -> FI) {
--fdptr;
sm -= fdptr -> SE;
}
mxto(mx, dptr -> SE + sm);
}
mxto(dp[1][i][j].SE, mx - sub);
}
}
{ // ._^
auto fptr = fish[i - 1].begin();
auto dptr = dp[1][i - 2].begin();
ll sm = 0;
ll mx = 0;
REP (j, 0, SZ(cand)) {
while (fptr != fish[i - 1].end() && fptr -> FI <= cand[j]) {
sm += fptr -> SE;
++fptr;
}
while (dptr != dp[1][i - 2].end() && dptr -> FI <= cand[j]) {
mxto(mx, dptr -> SE);
++dptr;
}
mxto(dp[0][i][j].SE, mx + sm);
mxto(dp[1][i][j].SE, mx + sm);
}
}
{ // ^_.
ll sm = 0;
auto fdptr = fish[i - 1].begin();
auto dptr = dp[1][i - 2].end();
while (fdptr != fish[i - 1].end() && fdptr -> FI <= prev(dptr) -> FI) {
sm += fdptr -> SE;
++fdptr;
}
ll mx = 0;
RREP (j, SZ(cand) - 1, 0) {
while (dptr != dp[1][i - 2].begin() && prev(dptr) -> FI >= cand[j]) {
--dptr;
while (fdptr != fish[i - 1].begin() && prev(fdptr) -> FI > dptr -> FI) {
--fdptr;
sm -= fdptr -> SE;
}
mxto(mx, dptr -> SE + sm);
}
mxto(dp[0][i][j].SE, mx);
mxto(dp[1][i][j].SE, mx);
}
}
cerr << i << ' ' << 0 << '\n';
for (auto [y, v] : dp[0][i]) {
cerr << ' ' << y << ' ' << v << '\n';
}
cerr << i << ' ' << 1 << '\n';
for (auto [y, v] : dp[1][i]) {
cerr << ' ' << y << ' ' << v << '\n';
}
}
ll ans = 0;
for (auto [y, v] : dp[1][n]) {
mxto(ans, v);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
21756 KB |
Output is correct |
2 |
Correct |
84 ms |
24380 KB |
Output is correct |
3 |
Correct |
20 ms |
13556 KB |
Output is correct |
4 |
Correct |
21 ms |
13512 KB |
Output is correct |
5 |
Correct |
265 ms |
59240 KB |
Output is correct |
6 |
Correct |
309 ms |
70680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
127 ms |
28720 KB |
Output is correct |
3 |
Correct |
156 ms |
32512 KB |
Output is correct |
4 |
Correct |
83 ms |
21604 KB |
Output is correct |
5 |
Correct |
82 ms |
24484 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7348 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
20 ms |
13524 KB |
Output is correct |
11 |
Correct |
20 ms |
13616 KB |
Output is correct |
12 |
Correct |
81 ms |
24324 KB |
Output is correct |
13 |
Correct |
108 ms |
27592 KB |
Output is correct |
14 |
Correct |
65 ms |
23016 KB |
Output is correct |
15 |
Correct |
81 ms |
22280 KB |
Output is correct |
16 |
Correct |
75 ms |
22960 KB |
Output is correct |
17 |
Correct |
83 ms |
24792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
13560 KB |
Output is correct |
2 |
Correct |
21 ms |
13608 KB |
Output is correct |
3 |
Correct |
62 ms |
18308 KB |
Output is correct |
4 |
Correct |
56 ms |
16972 KB |
Output is correct |
5 |
Correct |
78 ms |
22148 KB |
Output is correct |
6 |
Correct |
76 ms |
22876 KB |
Output is correct |
7 |
Correct |
84 ms |
23428 KB |
Output is correct |
8 |
Correct |
85 ms |
23512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7508 KB |
Output is correct |
17 |
Correct |
32 ms |
12744 KB |
Output is correct |
18 |
Correct |
32 ms |
12500 KB |
Output is correct |
19 |
Correct |
26 ms |
12600 KB |
Output is correct |
20 |
Correct |
23 ms |
12092 KB |
Output is correct |
21 |
Correct |
25 ms |
12108 KB |
Output is correct |
22 |
Correct |
45 ms |
16964 KB |
Output is correct |
23 |
Correct |
10 ms |
8864 KB |
Output is correct |
24 |
Correct |
23 ms |
11064 KB |
Output is correct |
25 |
Correct |
5 ms |
7524 KB |
Output is correct |
26 |
Correct |
11 ms |
8728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7508 KB |
Output is correct |
17 |
Correct |
32 ms |
12744 KB |
Output is correct |
18 |
Correct |
32 ms |
12500 KB |
Output is correct |
19 |
Correct |
26 ms |
12600 KB |
Output is correct |
20 |
Correct |
23 ms |
12092 KB |
Output is correct |
21 |
Correct |
25 ms |
12108 KB |
Output is correct |
22 |
Correct |
45 ms |
16964 KB |
Output is correct |
23 |
Correct |
10 ms |
8864 KB |
Output is correct |
24 |
Correct |
23 ms |
11064 KB |
Output is correct |
25 |
Correct |
5 ms |
7524 KB |
Output is correct |
26 |
Correct |
11 ms |
8728 KB |
Output is correct |
27 |
Correct |
6 ms |
7892 KB |
Output is correct |
28 |
Correct |
107 ms |
30584 KB |
Output is correct |
29 |
Correct |
193 ms |
40116 KB |
Output is correct |
30 |
Correct |
219 ms |
70464 KB |
Output is correct |
31 |
Correct |
216 ms |
69860 KB |
Output is correct |
32 |
Correct |
159 ms |
37828 KB |
Output is correct |
33 |
Correct |
219 ms |
70668 KB |
Output is correct |
34 |
Correct |
219 ms |
70336 KB |
Output is correct |
35 |
Correct |
83 ms |
24908 KB |
Output is correct |
36 |
Correct |
196 ms |
54228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
13560 KB |
Output is correct |
2 |
Correct |
21 ms |
13608 KB |
Output is correct |
3 |
Correct |
62 ms |
18308 KB |
Output is correct |
4 |
Correct |
56 ms |
16972 KB |
Output is correct |
5 |
Correct |
78 ms |
22148 KB |
Output is correct |
6 |
Correct |
76 ms |
22876 KB |
Output is correct |
7 |
Correct |
84 ms |
23428 KB |
Output is correct |
8 |
Correct |
85 ms |
23512 KB |
Output is correct |
9 |
Correct |
96 ms |
29044 KB |
Output is correct |
10 |
Correct |
68 ms |
20092 KB |
Output is correct |
11 |
Correct |
139 ms |
33328 KB |
Output is correct |
12 |
Correct |
3 ms |
7252 KB |
Output is correct |
13 |
Correct |
3 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
3 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
20 ms |
13524 KB |
Output is correct |
19 |
Correct |
21 ms |
13616 KB |
Output is correct |
20 |
Correct |
20 ms |
13596 KB |
Output is correct |
21 |
Correct |
20 ms |
13560 KB |
Output is correct |
22 |
Correct |
119 ms |
32328 KB |
Output is correct |
23 |
Correct |
157 ms |
40880 KB |
Output is correct |
24 |
Correct |
171 ms |
44528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
21756 KB |
Output is correct |
2 |
Correct |
84 ms |
24380 KB |
Output is correct |
3 |
Correct |
20 ms |
13556 KB |
Output is correct |
4 |
Correct |
21 ms |
13512 KB |
Output is correct |
5 |
Correct |
265 ms |
59240 KB |
Output is correct |
6 |
Correct |
309 ms |
70680 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
127 ms |
28720 KB |
Output is correct |
9 |
Correct |
156 ms |
32512 KB |
Output is correct |
10 |
Correct |
83 ms |
21604 KB |
Output is correct |
11 |
Correct |
82 ms |
24484 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7348 KB |
Output is correct |
15 |
Correct |
3 ms |
7252 KB |
Output is correct |
16 |
Correct |
20 ms |
13524 KB |
Output is correct |
17 |
Correct |
20 ms |
13616 KB |
Output is correct |
18 |
Correct |
81 ms |
24324 KB |
Output is correct |
19 |
Correct |
108 ms |
27592 KB |
Output is correct |
20 |
Correct |
65 ms |
23016 KB |
Output is correct |
21 |
Correct |
81 ms |
22280 KB |
Output is correct |
22 |
Correct |
75 ms |
22960 KB |
Output is correct |
23 |
Correct |
83 ms |
24792 KB |
Output is correct |
24 |
Correct |
21 ms |
13560 KB |
Output is correct |
25 |
Correct |
21 ms |
13608 KB |
Output is correct |
26 |
Correct |
62 ms |
18308 KB |
Output is correct |
27 |
Correct |
56 ms |
16972 KB |
Output is correct |
28 |
Correct |
78 ms |
22148 KB |
Output is correct |
29 |
Correct |
76 ms |
22876 KB |
Output is correct |
30 |
Correct |
84 ms |
23428 KB |
Output is correct |
31 |
Correct |
85 ms |
23512 KB |
Output is correct |
32 |
Correct |
4 ms |
7252 KB |
Output is correct |
33 |
Correct |
5 ms |
7252 KB |
Output is correct |
34 |
Correct |
4 ms |
7252 KB |
Output is correct |
35 |
Correct |
5 ms |
7252 KB |
Output is correct |
36 |
Correct |
4 ms |
7252 KB |
Output is correct |
37 |
Correct |
4 ms |
7252 KB |
Output is correct |
38 |
Correct |
4 ms |
7252 KB |
Output is correct |
39 |
Correct |
4 ms |
7252 KB |
Output is correct |
40 |
Correct |
4 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7508 KB |
Output is correct |
42 |
Correct |
5 ms |
7380 KB |
Output is correct |
43 |
Correct |
5 ms |
7380 KB |
Output is correct |
44 |
Correct |
4 ms |
7252 KB |
Output is correct |
45 |
Correct |
4 ms |
7380 KB |
Output is correct |
46 |
Correct |
4 ms |
7380 KB |
Output is correct |
47 |
Correct |
5 ms |
7508 KB |
Output is correct |
48 |
Correct |
32 ms |
12744 KB |
Output is correct |
49 |
Correct |
32 ms |
12500 KB |
Output is correct |
50 |
Correct |
26 ms |
12600 KB |
Output is correct |
51 |
Correct |
23 ms |
12092 KB |
Output is correct |
52 |
Correct |
25 ms |
12108 KB |
Output is correct |
53 |
Correct |
45 ms |
16964 KB |
Output is correct |
54 |
Correct |
10 ms |
8864 KB |
Output is correct |
55 |
Correct |
23 ms |
11064 KB |
Output is correct |
56 |
Correct |
5 ms |
7524 KB |
Output is correct |
57 |
Correct |
11 ms |
8728 KB |
Output is correct |
58 |
Correct |
6 ms |
7892 KB |
Output is correct |
59 |
Correct |
107 ms |
30584 KB |
Output is correct |
60 |
Correct |
193 ms |
40116 KB |
Output is correct |
61 |
Correct |
219 ms |
70464 KB |
Output is correct |
62 |
Correct |
216 ms |
69860 KB |
Output is correct |
63 |
Correct |
159 ms |
37828 KB |
Output is correct |
64 |
Correct |
219 ms |
70668 KB |
Output is correct |
65 |
Correct |
219 ms |
70336 KB |
Output is correct |
66 |
Correct |
83 ms |
24908 KB |
Output is correct |
67 |
Correct |
196 ms |
54228 KB |
Output is correct |
68 |
Correct |
96 ms |
29044 KB |
Output is correct |
69 |
Correct |
68 ms |
20092 KB |
Output is correct |
70 |
Correct |
139 ms |
33328 KB |
Output is correct |
71 |
Correct |
3 ms |
7252 KB |
Output is correct |
72 |
Correct |
3 ms |
7252 KB |
Output is correct |
73 |
Correct |
4 ms |
7252 KB |
Output is correct |
74 |
Correct |
4 ms |
7252 KB |
Output is correct |
75 |
Correct |
3 ms |
7252 KB |
Output is correct |
76 |
Correct |
4 ms |
7252 KB |
Output is correct |
77 |
Correct |
20 ms |
13524 KB |
Output is correct |
78 |
Correct |
21 ms |
13616 KB |
Output is correct |
79 |
Correct |
20 ms |
13596 KB |
Output is correct |
80 |
Correct |
20 ms |
13560 KB |
Output is correct |
81 |
Correct |
119 ms |
32328 KB |
Output is correct |
82 |
Correct |
157 ms |
40880 KB |
Output is correct |
83 |
Correct |
171 ms |
44528 KB |
Output is correct |
84 |
Correct |
225 ms |
52232 KB |
Output is correct |
85 |
Correct |
246 ms |
51336 KB |
Output is correct |
86 |
Correct |
284 ms |
64968 KB |
Output is correct |
87 |
Correct |
291 ms |
65676 KB |
Output is correct |
88 |
Correct |
4 ms |
7272 KB |
Output is correct |
89 |
Correct |
283 ms |
66380 KB |
Output is correct |
90 |
Correct |
255 ms |
53644 KB |
Output is correct |
91 |
Correct |
181 ms |
42760 KB |
Output is correct |