#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 4e18;
vector<int> H[100100], A[100100];
vector<ll> dp[2][100100], S[100100], prf[2][100100], suf[2][100100];
int n, sz[100100];
ll cost(int i, int s, int e){
if (s>e || i==0) return 0;
int j1 = upper_bound(H[i].begin(), H[i].end(), s-1) - H[i].begin() - 1;
int j2 = upper_bound(H[i].begin(), H[i].end(), e) - H[i].begin() - 1;
return S[i][j2] - S[i][j1];
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
n = N;
for (int i=0;i<=N;i++) {H[i].push_back(0); H[i].push_back(N);}
for (int i=0;i<M;i++){
++X[i], ++Y[i];
H[X[i]].push_back(Y[i]);
H[X[i]].push_back(Y[i]-1);
if (X[i]>1) H[X[i]-1].push_back(Y[i]);
if (X[i]<N) H[X[i]+1].push_back(Y[i]);
}
for (int i=0;i<=N;i++){
sort(H[i].begin(), H[i].end());
H[i].erase(unique(H[i].begin(), H[i].end()), H[i].end());
sz[i] = H[i].size();
A[i].resize(sz[i], 0);
S[i].resize(sz[i], 0);
dp[0][i].resize(sz[i], 0);
dp[1][i].resize(sz[i], 0);
prf[0][i].resize(sz[i]);
prf[1][i].resize(sz[i]);
suf[0][i].resize(sz[i]);
suf[1][i].resize(sz[i]);
}
for (int i=0;i<M;i++){
int idx = lower_bound(H[X[i]].begin(), H[X[i]].end(), Y[i]) - H[X[i]].begin();
assert(idx < sz[X[i]]);
A[X[i]][idx] = W[i];
}
for (int i=1;i<=n;i++){
for (int j=1;j<sz[i];j++) S[i][j] = S[i][j-1] + A[i][j];
}
dp[0][0][0] = 0;
dp[1][0][0] = 0;
assert(sz[0]==2);
dp[0][0][1] = -INF;
dp[1][0][1] = -INF;
for (int i=1;i<=n;i++){
--i;
for (int j=0;j<sz[i];j++){
prf[0][i][j] = dp[0][i][j] + cost(i, H[i][j]+1, n);
prf[1][i][j] = max(dp[0][i][j], dp[1][i][j]);
if (j==0) continue;
prf[0][i][j] = max(prf[0][i][j], prf[0][i][j-1]);
prf[1][i][j] = max(prf[1][i][j], prf[1][i][j-1]);
}
for (int j=sz[i]-1;j>=0;j--){
suf[0][i][j] = max(dp[0][i][j], dp[1][i][j]) + cost(i+1, 1, H[i][j]);
suf[1][i][j] = dp[1][i][j] + cost(i+1, 1, H[i][j]);
if (j==sz[i]-1) continue;
suf[0][i][j] = max(suf[0][i][j], suf[0][i][j+1]);
suf[1][i][j] = max(suf[1][i][j], suf[1][i][j+1]);
}
++i;
for (int j=0;j<sz[i];j++){
///0 -> 0
int idx00 = upper_bound(H[i-1].begin(), H[i-1].end(), H[i][j]) - H[i-1].begin() - 1;
dp[0][i][j] = max(dp[0][i][j], prf[0][i-1][idx00] - cost(i-1, H[i][j]+1, n));
/*for (int k=0;k<sz[i-1] && H[i-1][k]<=H[i][j];k++){
dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][k] + cost(i-1, H[i-1][k]+1, H[i][j]));
}*/
///1 -> 0
if (i>=2){
int idx10 = upper_bound(H[i-2].begin(), H[i-2].end(), H[i][j]) - H[i-2].begin() - 1;
dp[0][i][j] = max(dp[0][i][j], prf[1][i-2][idx10] + cost(i-1, 1, H[i][j]));
if (idx10+1 < sz[i-2]) dp[0][i][j] = max(dp[0][i][j], suf[0][i-2][idx10+1]);
/*for (int k=0;k<sz[i-2];k++){
dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-2][k], dp[1][i-2][k]) + cost(i-1, 1, max(H[i-2][k], H[i][j])));
}*/
}
///0 -> 1
dp[1][i][j] = max(dp[1][i][j], dp[0][i-1][sz[i-1]-1] + cost(i, H[i][j]+1, n));
///1 -> 1
int idx11 = lower_bound(H[i-1].begin(), H[i-1].end(), H[i][j]) - H[i-1].begin();
dp[1][i][j] = max(dp[1][i][j], suf[1][i-1][idx11] - cost(i, 1, H[i][j]));
//for (int k=0;k<sz[i-1];k++) if (H[i-1][k] >= H[i][j]) dp[1][i][j] = max(dp[1][i][j], dp[1][i-1][k] + cost(i, H[i][j]+1, H[i-1][k]));
}
}
//printf(" %lld %lld %lld\n", H[1][0], H[1][1], H[1][2]);
//printf(" %lld -> %lld(%d)\n", cost(1, 1, 1), dp[0][2][1], H[2][1]);
return max(*max_element(dp[0][n].begin(), dp[0][n].end()), *max_element(dp[1][n].begin(), dp[1][n].end()));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
60004 KB |
Output is correct |
2 |
Correct |
245 ms |
65252 KB |
Output is correct |
3 |
Correct |
87 ms |
49996 KB |
Output is correct |
4 |
Correct |
79 ms |
49996 KB |
Output is correct |
5 |
Correct |
786 ms |
116852 KB |
Output is correct |
6 |
Correct |
784 ms |
131348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
334 ms |
68664 KB |
Output is correct |
3 |
Correct |
379 ms |
75104 KB |
Output is correct |
4 |
Correct |
207 ms |
60076 KB |
Output is correct |
5 |
Correct |
236 ms |
65336 KB |
Output is correct |
6 |
Correct |
12 ms |
21520 KB |
Output is correct |
7 |
Correct |
12 ms |
21460 KB |
Output is correct |
8 |
Correct |
12 ms |
21360 KB |
Output is correct |
9 |
Correct |
11 ms |
21440 KB |
Output is correct |
10 |
Correct |
89 ms |
50008 KB |
Output is correct |
11 |
Correct |
80 ms |
49996 KB |
Output is correct |
12 |
Correct |
270 ms |
65164 KB |
Output is correct |
13 |
Correct |
345 ms |
71632 KB |
Output is correct |
14 |
Correct |
240 ms |
62524 KB |
Output is correct |
15 |
Correct |
244 ms |
61892 KB |
Output is correct |
16 |
Correct |
255 ms |
62656 KB |
Output is correct |
17 |
Correct |
281 ms |
67136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
49992 KB |
Output is correct |
2 |
Correct |
80 ms |
49996 KB |
Output is correct |
3 |
Correct |
120 ms |
49496 KB |
Output is correct |
4 |
Correct |
123 ms |
51472 KB |
Output is correct |
5 |
Correct |
189 ms |
54460 KB |
Output is correct |
6 |
Correct |
178 ms |
54468 KB |
Output is correct |
7 |
Correct |
201 ms |
54456 KB |
Output is correct |
8 |
Correct |
199 ms |
54360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21472 KB |
Output is correct |
3 |
Correct |
11 ms |
21344 KB |
Output is correct |
4 |
Correct |
15 ms |
21388 KB |
Output is correct |
5 |
Correct |
14 ms |
21336 KB |
Output is correct |
6 |
Correct |
14 ms |
21464 KB |
Output is correct |
7 |
Correct |
13 ms |
21408 KB |
Output is correct |
8 |
Correct |
13 ms |
21436 KB |
Output is correct |
9 |
Correct |
12 ms |
21572 KB |
Output is correct |
10 |
Correct |
14 ms |
21812 KB |
Output is correct |
11 |
Correct |
12 ms |
21588 KB |
Output is correct |
12 |
Correct |
16 ms |
21708 KB |
Output is correct |
13 |
Correct |
12 ms |
21504 KB |
Output is correct |
14 |
Correct |
15 ms |
21660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21472 KB |
Output is correct |
3 |
Correct |
11 ms |
21344 KB |
Output is correct |
4 |
Correct |
15 ms |
21388 KB |
Output is correct |
5 |
Correct |
14 ms |
21336 KB |
Output is correct |
6 |
Correct |
14 ms |
21464 KB |
Output is correct |
7 |
Correct |
13 ms |
21408 KB |
Output is correct |
8 |
Correct |
13 ms |
21436 KB |
Output is correct |
9 |
Correct |
12 ms |
21572 KB |
Output is correct |
10 |
Correct |
14 ms |
21812 KB |
Output is correct |
11 |
Correct |
12 ms |
21588 KB |
Output is correct |
12 |
Correct |
16 ms |
21708 KB |
Output is correct |
13 |
Correct |
12 ms |
21504 KB |
Output is correct |
14 |
Correct |
15 ms |
21660 KB |
Output is correct |
15 |
Correct |
15 ms |
21588 KB |
Output is correct |
16 |
Correct |
12 ms |
21716 KB |
Output is correct |
17 |
Correct |
66 ms |
27008 KB |
Output is correct |
18 |
Correct |
61 ms |
26916 KB |
Output is correct |
19 |
Correct |
62 ms |
26988 KB |
Output is correct |
20 |
Correct |
60 ms |
26444 KB |
Output is correct |
21 |
Correct |
63 ms |
26380 KB |
Output is correct |
22 |
Correct |
107 ms |
31284 KB |
Output is correct |
23 |
Correct |
32 ms |
23764 KB |
Output is correct |
24 |
Correct |
65 ms |
27052 KB |
Output is correct |
25 |
Correct |
16 ms |
21724 KB |
Output is correct |
26 |
Correct |
30 ms |
23252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21472 KB |
Output is correct |
3 |
Correct |
11 ms |
21344 KB |
Output is correct |
4 |
Correct |
15 ms |
21388 KB |
Output is correct |
5 |
Correct |
14 ms |
21336 KB |
Output is correct |
6 |
Correct |
14 ms |
21464 KB |
Output is correct |
7 |
Correct |
13 ms |
21408 KB |
Output is correct |
8 |
Correct |
13 ms |
21436 KB |
Output is correct |
9 |
Correct |
12 ms |
21572 KB |
Output is correct |
10 |
Correct |
14 ms |
21812 KB |
Output is correct |
11 |
Correct |
12 ms |
21588 KB |
Output is correct |
12 |
Correct |
16 ms |
21708 KB |
Output is correct |
13 |
Correct |
12 ms |
21504 KB |
Output is correct |
14 |
Correct |
15 ms |
21660 KB |
Output is correct |
15 |
Correct |
15 ms |
21588 KB |
Output is correct |
16 |
Correct |
12 ms |
21716 KB |
Output is correct |
17 |
Correct |
66 ms |
27008 KB |
Output is correct |
18 |
Correct |
61 ms |
26916 KB |
Output is correct |
19 |
Correct |
62 ms |
26988 KB |
Output is correct |
20 |
Correct |
60 ms |
26444 KB |
Output is correct |
21 |
Correct |
63 ms |
26380 KB |
Output is correct |
22 |
Correct |
107 ms |
31284 KB |
Output is correct |
23 |
Correct |
32 ms |
23764 KB |
Output is correct |
24 |
Correct |
65 ms |
27052 KB |
Output is correct |
25 |
Correct |
16 ms |
21724 KB |
Output is correct |
26 |
Correct |
30 ms |
23252 KB |
Output is correct |
27 |
Correct |
17 ms |
22740 KB |
Output is correct |
28 |
Correct |
282 ms |
47964 KB |
Output is correct |
29 |
Correct |
476 ms |
64520 KB |
Output is correct |
30 |
Correct |
711 ms |
99968 KB |
Output is correct |
31 |
Correct |
728 ms |
99308 KB |
Output is correct |
32 |
Correct |
340 ms |
54044 KB |
Output is correct |
33 |
Correct |
728 ms |
102628 KB |
Output is correct |
34 |
Correct |
721 ms |
102572 KB |
Output is correct |
35 |
Correct |
208 ms |
44976 KB |
Output is correct |
36 |
Correct |
672 ms |
88972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
49992 KB |
Output is correct |
2 |
Correct |
80 ms |
49996 KB |
Output is correct |
3 |
Correct |
120 ms |
49496 KB |
Output is correct |
4 |
Correct |
123 ms |
51472 KB |
Output is correct |
5 |
Correct |
189 ms |
54460 KB |
Output is correct |
6 |
Correct |
178 ms |
54468 KB |
Output is correct |
7 |
Correct |
201 ms |
54456 KB |
Output is correct |
8 |
Correct |
199 ms |
54360 KB |
Output is correct |
9 |
Correct |
203 ms |
65424 KB |
Output is correct |
10 |
Correct |
144 ms |
46092 KB |
Output is correct |
11 |
Correct |
316 ms |
70940 KB |
Output is correct |
12 |
Correct |
13 ms |
21436 KB |
Output is correct |
13 |
Correct |
13 ms |
21392 KB |
Output is correct |
14 |
Correct |
12 ms |
21344 KB |
Output is correct |
15 |
Correct |
13 ms |
21376 KB |
Output is correct |
16 |
Correct |
13 ms |
21460 KB |
Output is correct |
17 |
Correct |
13 ms |
21460 KB |
Output is correct |
18 |
Correct |
98 ms |
50076 KB |
Output is correct |
19 |
Correct |
111 ms |
49952 KB |
Output is correct |
20 |
Correct |
112 ms |
50008 KB |
Output is correct |
21 |
Correct |
86 ms |
49992 KB |
Output is correct |
22 |
Correct |
327 ms |
74344 KB |
Output is correct |
23 |
Correct |
406 ms |
80212 KB |
Output is correct |
24 |
Correct |
432 ms |
81260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
60004 KB |
Output is correct |
2 |
Correct |
245 ms |
65252 KB |
Output is correct |
3 |
Correct |
87 ms |
49996 KB |
Output is correct |
4 |
Correct |
79 ms |
49996 KB |
Output is correct |
5 |
Correct |
786 ms |
116852 KB |
Output is correct |
6 |
Correct |
784 ms |
131348 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
334 ms |
68664 KB |
Output is correct |
9 |
Correct |
379 ms |
75104 KB |
Output is correct |
10 |
Correct |
207 ms |
60076 KB |
Output is correct |
11 |
Correct |
236 ms |
65336 KB |
Output is correct |
12 |
Correct |
12 ms |
21520 KB |
Output is correct |
13 |
Correct |
12 ms |
21460 KB |
Output is correct |
14 |
Correct |
12 ms |
21360 KB |
Output is correct |
15 |
Correct |
11 ms |
21440 KB |
Output is correct |
16 |
Correct |
89 ms |
50008 KB |
Output is correct |
17 |
Correct |
80 ms |
49996 KB |
Output is correct |
18 |
Correct |
270 ms |
65164 KB |
Output is correct |
19 |
Correct |
345 ms |
71632 KB |
Output is correct |
20 |
Correct |
240 ms |
62524 KB |
Output is correct |
21 |
Correct |
244 ms |
61892 KB |
Output is correct |
22 |
Correct |
255 ms |
62656 KB |
Output is correct |
23 |
Correct |
281 ms |
67136 KB |
Output is correct |
24 |
Correct |
89 ms |
49992 KB |
Output is correct |
25 |
Correct |
80 ms |
49996 KB |
Output is correct |
26 |
Correct |
120 ms |
49496 KB |
Output is correct |
27 |
Correct |
123 ms |
51472 KB |
Output is correct |
28 |
Correct |
189 ms |
54460 KB |
Output is correct |
29 |
Correct |
178 ms |
54468 KB |
Output is correct |
30 |
Correct |
201 ms |
54456 KB |
Output is correct |
31 |
Correct |
199 ms |
54360 KB |
Output is correct |
32 |
Correct |
12 ms |
21460 KB |
Output is correct |
33 |
Correct |
12 ms |
21472 KB |
Output is correct |
34 |
Correct |
11 ms |
21344 KB |
Output is correct |
35 |
Correct |
15 ms |
21388 KB |
Output is correct |
36 |
Correct |
14 ms |
21336 KB |
Output is correct |
37 |
Correct |
14 ms |
21464 KB |
Output is correct |
38 |
Correct |
13 ms |
21408 KB |
Output is correct |
39 |
Correct |
13 ms |
21436 KB |
Output is correct |
40 |
Correct |
12 ms |
21572 KB |
Output is correct |
41 |
Correct |
14 ms |
21812 KB |
Output is correct |
42 |
Correct |
12 ms |
21588 KB |
Output is correct |
43 |
Correct |
16 ms |
21708 KB |
Output is correct |
44 |
Correct |
12 ms |
21504 KB |
Output is correct |
45 |
Correct |
15 ms |
21660 KB |
Output is correct |
46 |
Correct |
15 ms |
21588 KB |
Output is correct |
47 |
Correct |
12 ms |
21716 KB |
Output is correct |
48 |
Correct |
66 ms |
27008 KB |
Output is correct |
49 |
Correct |
61 ms |
26916 KB |
Output is correct |
50 |
Correct |
62 ms |
26988 KB |
Output is correct |
51 |
Correct |
60 ms |
26444 KB |
Output is correct |
52 |
Correct |
63 ms |
26380 KB |
Output is correct |
53 |
Correct |
107 ms |
31284 KB |
Output is correct |
54 |
Correct |
32 ms |
23764 KB |
Output is correct |
55 |
Correct |
65 ms |
27052 KB |
Output is correct |
56 |
Correct |
16 ms |
21724 KB |
Output is correct |
57 |
Correct |
30 ms |
23252 KB |
Output is correct |
58 |
Correct |
17 ms |
22740 KB |
Output is correct |
59 |
Correct |
282 ms |
47964 KB |
Output is correct |
60 |
Correct |
476 ms |
64520 KB |
Output is correct |
61 |
Correct |
711 ms |
99968 KB |
Output is correct |
62 |
Correct |
728 ms |
99308 KB |
Output is correct |
63 |
Correct |
340 ms |
54044 KB |
Output is correct |
64 |
Correct |
728 ms |
102628 KB |
Output is correct |
65 |
Correct |
721 ms |
102572 KB |
Output is correct |
66 |
Correct |
208 ms |
44976 KB |
Output is correct |
67 |
Correct |
672 ms |
88972 KB |
Output is correct |
68 |
Correct |
203 ms |
65424 KB |
Output is correct |
69 |
Correct |
144 ms |
46092 KB |
Output is correct |
70 |
Correct |
316 ms |
70940 KB |
Output is correct |
71 |
Correct |
13 ms |
21436 KB |
Output is correct |
72 |
Correct |
13 ms |
21392 KB |
Output is correct |
73 |
Correct |
12 ms |
21344 KB |
Output is correct |
74 |
Correct |
13 ms |
21376 KB |
Output is correct |
75 |
Correct |
13 ms |
21460 KB |
Output is correct |
76 |
Correct |
13 ms |
21460 KB |
Output is correct |
77 |
Correct |
98 ms |
50076 KB |
Output is correct |
78 |
Correct |
111 ms |
49952 KB |
Output is correct |
79 |
Correct |
112 ms |
50008 KB |
Output is correct |
80 |
Correct |
86 ms |
49992 KB |
Output is correct |
81 |
Correct |
327 ms |
74344 KB |
Output is correct |
82 |
Correct |
406 ms |
80212 KB |
Output is correct |
83 |
Correct |
432 ms |
81260 KB |
Output is correct |
84 |
Correct |
982 ms |
116016 KB |
Output is correct |
85 |
Correct |
952 ms |
118084 KB |
Output is correct |
86 |
Correct |
812 ms |
127356 KB |
Output is correct |
87 |
Correct |
834 ms |
130612 KB |
Output is correct |
88 |
Correct |
13 ms |
21460 KB |
Output is correct |
89 |
Correct |
876 ms |
130556 KB |
Output is correct |
90 |
Correct |
611 ms |
106812 KB |
Output is correct |
91 |
Correct |
429 ms |
86244 KB |
Output is correct |