#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--){
ll X = cost(i+1, 1, H[i][j]);
suf[0][i][j] = max(dp[0][i][j], dp[1][i][j]) + X;
suf[1][i][j] = dp[1][i][j] + X;
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++){
ll X1 = cost(i-1, 1, H[i][j]);
ll X2 = cost(i, 1, H[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] - (S[i-1].back() - X1));
///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] + X1);
if (idx10+1 < sz[i-2]) dp[0][i][j] = max(dp[0][i][j], suf[0][i-2][idx10+1]);
}
///0 -> 1
dp[1][i][j] = max(dp[1][i][j], dp[0][i-1][sz[i-1]-1] + (S[i].back() - X2));
///1 -> 1
int idx11 = idx00;
if (H[i-1][idx11]<H[i][j]) idx11++;
dp[1][i][j] = max(dp[1][i][j], suf[1][i-1][idx11] - X2);
}
}
return max(*max_element(dp[0][n].begin(), dp[0][n].end()), *max_element(dp[1][n].begin(), dp[1][n].end()));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
59884 KB |
Output is correct |
2 |
Correct |
211 ms |
65340 KB |
Output is correct |
3 |
Correct |
77 ms |
49996 KB |
Output is correct |
4 |
Correct |
81 ms |
50072 KB |
Output is correct |
5 |
Correct |
603 ms |
116812 KB |
Output is correct |
6 |
Correct |
673 ms |
131240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
300 ms |
68600 KB |
Output is correct |
3 |
Correct |
313 ms |
75112 KB |
Output is correct |
4 |
Correct |
181 ms |
59980 KB |
Output is correct |
5 |
Correct |
209 ms |
65248 KB |
Output is correct |
6 |
Correct |
12 ms |
21392 KB |
Output is correct |
7 |
Correct |
11 ms |
21360 KB |
Output is correct |
8 |
Correct |
11 ms |
21440 KB |
Output is correct |
9 |
Correct |
11 ms |
21348 KB |
Output is correct |
10 |
Correct |
76 ms |
50012 KB |
Output is correct |
11 |
Correct |
76 ms |
49908 KB |
Output is correct |
12 |
Correct |
221 ms |
65088 KB |
Output is correct |
13 |
Correct |
255 ms |
71628 KB |
Output is correct |
14 |
Correct |
204 ms |
62536 KB |
Output is correct |
15 |
Correct |
193 ms |
61820 KB |
Output is correct |
16 |
Correct |
215 ms |
62544 KB |
Output is correct |
17 |
Correct |
217 ms |
67140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
50012 KB |
Output is correct |
2 |
Correct |
86 ms |
49948 KB |
Output is correct |
3 |
Correct |
131 ms |
49488 KB |
Output is correct |
4 |
Correct |
112 ms |
51464 KB |
Output is correct |
5 |
Correct |
162 ms |
54448 KB |
Output is correct |
6 |
Correct |
170 ms |
54400 KB |
Output is correct |
7 |
Correct |
167 ms |
54520 KB |
Output is correct |
8 |
Correct |
161 ms |
54420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21376 KB |
Output is correct |
6 |
Correct |
12 ms |
21344 KB |
Output is correct |
7 |
Correct |
13 ms |
21464 KB |
Output is correct |
8 |
Correct |
12 ms |
21436 KB |
Output is correct |
9 |
Correct |
12 ms |
21480 KB |
Output is correct |
10 |
Correct |
13 ms |
21716 KB |
Output is correct |
11 |
Correct |
12 ms |
21600 KB |
Output is correct |
12 |
Correct |
12 ms |
21716 KB |
Output is correct |
13 |
Correct |
12 ms |
21460 KB |
Output is correct |
14 |
Correct |
13 ms |
21660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21376 KB |
Output is correct |
6 |
Correct |
12 ms |
21344 KB |
Output is correct |
7 |
Correct |
13 ms |
21464 KB |
Output is correct |
8 |
Correct |
12 ms |
21436 KB |
Output is correct |
9 |
Correct |
12 ms |
21480 KB |
Output is correct |
10 |
Correct |
13 ms |
21716 KB |
Output is correct |
11 |
Correct |
12 ms |
21600 KB |
Output is correct |
12 |
Correct |
12 ms |
21716 KB |
Output is correct |
13 |
Correct |
12 ms |
21460 KB |
Output is correct |
14 |
Correct |
13 ms |
21660 KB |
Output is correct |
15 |
Correct |
12 ms |
21592 KB |
Output is correct |
16 |
Correct |
15 ms |
21716 KB |
Output is correct |
17 |
Correct |
50 ms |
26956 KB |
Output is correct |
18 |
Correct |
47 ms |
26888 KB |
Output is correct |
19 |
Correct |
46 ms |
26936 KB |
Output is correct |
20 |
Correct |
43 ms |
26452 KB |
Output is correct |
21 |
Correct |
43 ms |
26436 KB |
Output is correct |
22 |
Correct |
79 ms |
31372 KB |
Output is correct |
23 |
Correct |
23 ms |
23764 KB |
Output is correct |
24 |
Correct |
45 ms |
26988 KB |
Output is correct |
25 |
Correct |
12 ms |
21764 KB |
Output is correct |
26 |
Correct |
21 ms |
23360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21376 KB |
Output is correct |
6 |
Correct |
12 ms |
21344 KB |
Output is correct |
7 |
Correct |
13 ms |
21464 KB |
Output is correct |
8 |
Correct |
12 ms |
21436 KB |
Output is correct |
9 |
Correct |
12 ms |
21480 KB |
Output is correct |
10 |
Correct |
13 ms |
21716 KB |
Output is correct |
11 |
Correct |
12 ms |
21600 KB |
Output is correct |
12 |
Correct |
12 ms |
21716 KB |
Output is correct |
13 |
Correct |
12 ms |
21460 KB |
Output is correct |
14 |
Correct |
13 ms |
21660 KB |
Output is correct |
15 |
Correct |
12 ms |
21592 KB |
Output is correct |
16 |
Correct |
15 ms |
21716 KB |
Output is correct |
17 |
Correct |
50 ms |
26956 KB |
Output is correct |
18 |
Correct |
47 ms |
26888 KB |
Output is correct |
19 |
Correct |
46 ms |
26936 KB |
Output is correct |
20 |
Correct |
43 ms |
26452 KB |
Output is correct |
21 |
Correct |
43 ms |
26436 KB |
Output is correct |
22 |
Correct |
79 ms |
31372 KB |
Output is correct |
23 |
Correct |
23 ms |
23764 KB |
Output is correct |
24 |
Correct |
45 ms |
26988 KB |
Output is correct |
25 |
Correct |
12 ms |
21764 KB |
Output is correct |
26 |
Correct |
21 ms |
23360 KB |
Output is correct |
27 |
Correct |
15 ms |
22772 KB |
Output is correct |
28 |
Correct |
207 ms |
47924 KB |
Output is correct |
29 |
Correct |
330 ms |
64404 KB |
Output is correct |
30 |
Correct |
491 ms |
99976 KB |
Output is correct |
31 |
Correct |
483 ms |
99316 KB |
Output is correct |
32 |
Correct |
244 ms |
54156 KB |
Output is correct |
33 |
Correct |
484 ms |
102628 KB |
Output is correct |
34 |
Correct |
492 ms |
102612 KB |
Output is correct |
35 |
Correct |
141 ms |
44976 KB |
Output is correct |
36 |
Correct |
475 ms |
88844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
50012 KB |
Output is correct |
2 |
Correct |
86 ms |
49948 KB |
Output is correct |
3 |
Correct |
131 ms |
49488 KB |
Output is correct |
4 |
Correct |
112 ms |
51464 KB |
Output is correct |
5 |
Correct |
162 ms |
54448 KB |
Output is correct |
6 |
Correct |
170 ms |
54400 KB |
Output is correct |
7 |
Correct |
167 ms |
54520 KB |
Output is correct |
8 |
Correct |
161 ms |
54420 KB |
Output is correct |
9 |
Correct |
194 ms |
65312 KB |
Output is correct |
10 |
Correct |
139 ms |
46180 KB |
Output is correct |
11 |
Correct |
255 ms |
70888 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21432 KB |
Output is correct |
14 |
Correct |
14 ms |
21460 KB |
Output is correct |
15 |
Correct |
13 ms |
21392 KB |
Output is correct |
16 |
Correct |
13 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
81 ms |
50000 KB |
Output is correct |
19 |
Correct |
83 ms |
50116 KB |
Output is correct |
20 |
Correct |
77 ms |
49956 KB |
Output is correct |
21 |
Correct |
79 ms |
49960 KB |
Output is correct |
22 |
Correct |
285 ms |
74220 KB |
Output is correct |
23 |
Correct |
341 ms |
80052 KB |
Output is correct |
24 |
Correct |
332 ms |
81036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
59884 KB |
Output is correct |
2 |
Correct |
211 ms |
65340 KB |
Output is correct |
3 |
Correct |
77 ms |
49996 KB |
Output is correct |
4 |
Correct |
81 ms |
50072 KB |
Output is correct |
5 |
Correct |
603 ms |
116812 KB |
Output is correct |
6 |
Correct |
673 ms |
131240 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
300 ms |
68600 KB |
Output is correct |
9 |
Correct |
313 ms |
75112 KB |
Output is correct |
10 |
Correct |
181 ms |
59980 KB |
Output is correct |
11 |
Correct |
209 ms |
65248 KB |
Output is correct |
12 |
Correct |
12 ms |
21392 KB |
Output is correct |
13 |
Correct |
11 ms |
21360 KB |
Output is correct |
14 |
Correct |
11 ms |
21440 KB |
Output is correct |
15 |
Correct |
11 ms |
21348 KB |
Output is correct |
16 |
Correct |
76 ms |
50012 KB |
Output is correct |
17 |
Correct |
76 ms |
49908 KB |
Output is correct |
18 |
Correct |
221 ms |
65088 KB |
Output is correct |
19 |
Correct |
255 ms |
71628 KB |
Output is correct |
20 |
Correct |
204 ms |
62536 KB |
Output is correct |
21 |
Correct |
193 ms |
61820 KB |
Output is correct |
22 |
Correct |
215 ms |
62544 KB |
Output is correct |
23 |
Correct |
217 ms |
67140 KB |
Output is correct |
24 |
Correct |
77 ms |
50012 KB |
Output is correct |
25 |
Correct |
86 ms |
49948 KB |
Output is correct |
26 |
Correct |
131 ms |
49488 KB |
Output is correct |
27 |
Correct |
112 ms |
51464 KB |
Output is correct |
28 |
Correct |
162 ms |
54448 KB |
Output is correct |
29 |
Correct |
170 ms |
54400 KB |
Output is correct |
30 |
Correct |
167 ms |
54520 KB |
Output is correct |
31 |
Correct |
161 ms |
54420 KB |
Output is correct |
32 |
Correct |
11 ms |
21460 KB |
Output is correct |
33 |
Correct |
12 ms |
21460 KB |
Output is correct |
34 |
Correct |
11 ms |
21460 KB |
Output is correct |
35 |
Correct |
12 ms |
21460 KB |
Output is correct |
36 |
Correct |
11 ms |
21376 KB |
Output is correct |
37 |
Correct |
12 ms |
21344 KB |
Output is correct |
38 |
Correct |
13 ms |
21464 KB |
Output is correct |
39 |
Correct |
12 ms |
21436 KB |
Output is correct |
40 |
Correct |
12 ms |
21480 KB |
Output is correct |
41 |
Correct |
13 ms |
21716 KB |
Output is correct |
42 |
Correct |
12 ms |
21600 KB |
Output is correct |
43 |
Correct |
12 ms |
21716 KB |
Output is correct |
44 |
Correct |
12 ms |
21460 KB |
Output is correct |
45 |
Correct |
13 ms |
21660 KB |
Output is correct |
46 |
Correct |
12 ms |
21592 KB |
Output is correct |
47 |
Correct |
15 ms |
21716 KB |
Output is correct |
48 |
Correct |
50 ms |
26956 KB |
Output is correct |
49 |
Correct |
47 ms |
26888 KB |
Output is correct |
50 |
Correct |
46 ms |
26936 KB |
Output is correct |
51 |
Correct |
43 ms |
26452 KB |
Output is correct |
52 |
Correct |
43 ms |
26436 KB |
Output is correct |
53 |
Correct |
79 ms |
31372 KB |
Output is correct |
54 |
Correct |
23 ms |
23764 KB |
Output is correct |
55 |
Correct |
45 ms |
26988 KB |
Output is correct |
56 |
Correct |
12 ms |
21764 KB |
Output is correct |
57 |
Correct |
21 ms |
23360 KB |
Output is correct |
58 |
Correct |
15 ms |
22772 KB |
Output is correct |
59 |
Correct |
207 ms |
47924 KB |
Output is correct |
60 |
Correct |
330 ms |
64404 KB |
Output is correct |
61 |
Correct |
491 ms |
99976 KB |
Output is correct |
62 |
Correct |
483 ms |
99316 KB |
Output is correct |
63 |
Correct |
244 ms |
54156 KB |
Output is correct |
64 |
Correct |
484 ms |
102628 KB |
Output is correct |
65 |
Correct |
492 ms |
102612 KB |
Output is correct |
66 |
Correct |
141 ms |
44976 KB |
Output is correct |
67 |
Correct |
475 ms |
88844 KB |
Output is correct |
68 |
Correct |
194 ms |
65312 KB |
Output is correct |
69 |
Correct |
139 ms |
46180 KB |
Output is correct |
70 |
Correct |
255 ms |
70888 KB |
Output is correct |
71 |
Correct |
11 ms |
21460 KB |
Output is correct |
72 |
Correct |
11 ms |
21432 KB |
Output is correct |
73 |
Correct |
14 ms |
21460 KB |
Output is correct |
74 |
Correct |
13 ms |
21392 KB |
Output is correct |
75 |
Correct |
13 ms |
21460 KB |
Output is correct |
76 |
Correct |
11 ms |
21460 KB |
Output is correct |
77 |
Correct |
81 ms |
50000 KB |
Output is correct |
78 |
Correct |
83 ms |
50116 KB |
Output is correct |
79 |
Correct |
77 ms |
49956 KB |
Output is correct |
80 |
Correct |
79 ms |
49960 KB |
Output is correct |
81 |
Correct |
285 ms |
74220 KB |
Output is correct |
82 |
Correct |
341 ms |
80052 KB |
Output is correct |
83 |
Correct |
332 ms |
81036 KB |
Output is correct |
84 |
Correct |
652 ms |
115632 KB |
Output is correct |
85 |
Correct |
667 ms |
117720 KB |
Output is correct |
86 |
Correct |
658 ms |
126924 KB |
Output is correct |
87 |
Correct |
680 ms |
130068 KB |
Output is correct |
88 |
Correct |
11 ms |
21460 KB |
Output is correct |
89 |
Correct |
663 ms |
130076 KB |
Output is correct |
90 |
Correct |
500 ms |
106592 KB |
Output is correct |
91 |
Correct |
344 ms |
85788 KB |
Output is correct |