#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const int P = 1000000007;
const ll LLINF = (ll)1e18+1;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef struct prefix_sum {
vector<vector<pll>> B;
ll operator()(int x, int s, int e) {
assert(s <= e);
return this->operator()(x, e)-this->operator()(x, s);
}
ll operator()(int x, int y) {
if(x < 0 || x >= (int)B.size()) return 0LL;
auto iter = lower_bound(B[x].begin(), B[x].end(), pll(y, 0LL));
return (iter == B[x].begin() ? 0LL : (--iter)->se);
}
prefix_sum(vector<vector<pii>> &A) {
for(auto &i : A) {
ll su = 0LL;
B.push_back(vector<pll>());
for(auto &j : i) {
su += j.se;
B.back().push_back({j.fi, su});
}
}
}
} prefix_sum;
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vector<vector<pii>> brd(N);
for(int i = 0; i < M; i++) brd[X[i]].push_back({Y[i], W[i]});
for(int i = 0; i < N; i++) {
brd[i].push_back({N, 0});
sort(brd[i].begin(), brd[i].end());
}
prefix_sum pre(brd);
vector<vector<pll>> down(N), dp(N);
down[0].resize(brd[0].size()); dp[0].resize(brd[0].size());
for(int j = 0; j < (int)brd[0].size(); j++) {
down[0][j] = dp[0][j] = pll(brd[0][j].fi, pre(1, brd[0][j].fi));
}
down[1].resize(brd[1].size()); dp[1].resize(brd[1].size());
for(int j = 0; j < (int)brd[1].size(); j++) {
down[1][j] = dp[1][j] = pll(brd[1][j].fi, pre(0, brd[1][j].fi)+pre(2, brd[1][j].fi));
dp[1][j].se = max(dp[1][j].se, pre(1, brd[1][j].fi, N)+pre(2, brd[1][j].fi));
}
for(int i = 2; i < N; i++) {
vector<pll> A(down[i-1]), B(dp[i-2]), C(dp[i-2]), D(dp[i-1]);
for(auto &j : A) j.se -= pre(i, j.fi)+pre(i-1, j.fi);
for(auto &j : B) j.se -= pre(i-1, j.fi);
for(int j = 1; j < (int)A.size(); j++) A[j].se = max(A[j].se, A[j-1].se);
for(int j = 1; j < (int)B.size(); j++) B[j].se = max(B[j].se, B[j-1].se);
for(int j = (int)C.size()-2; j >= 0; j--) C[j].se = max(C[j].se, C[j+1].se);
for(int j = (int)D.size()-2; j >= 0; j--) D[j].se = max(D[j].se, D[j+1].se);
for(auto &j : brd[i]) {
pll h = {j.fi, LLINF};
ll downtmp = 0LL, dptmp = 0LL;
auto itera = upper_bound(A.begin(), A.end(), h);
if(itera != A.begin()) downtmp = max(downtmp, (--itera)->se+pre(i-1, j.fi)+pre(i+1, j.fi));
auto iterb = upper_bound(B.begin(), B.end(), h);
if(iterb != B.begin()) downtmp = max(downtmp, (--iterb)->se+pre(i-1, j.fi)+pre(i+1, j.fi));
auto iterc = upper_bound(C.begin(), C.end(), h);
if(iterc != C.end()) downtmp = max(downtmp, iterc->se+pre(i+1, j.fi));
auto iterd = upper_bound(D.begin(), D.end(), h);
if(iterd != D.end()) dptmp = max(dptmp, iterd->se-pre(i, j.fi)+pre(i+1, j.fi));
dptmp = max(dptmp, downtmp);
down[i].push_back({h.fi, downtmp});
dp[i].push_back({h.fi, dptmp});
}
}
//cout << " " << down << " " << dp << "\n";
ll ans = 0LL;
for(auto &j : dp.back()) ans = max(ans, j.se);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
27692 KB |
Output is correct |
2 |
Correct |
92 ms |
31692 KB |
Output is correct |
3 |
Correct |
49 ms |
22188 KB |
Output is correct |
4 |
Correct |
53 ms |
22116 KB |
Output is correct |
5 |
Correct |
272 ms |
56512 KB |
Output is correct |
6 |
Correct |
306 ms |
58776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
183 ms |
35564 KB |
Output is correct |
3 |
Correct |
201 ms |
41388 KB |
Output is correct |
4 |
Correct |
99 ms |
27732 KB |
Output is correct |
5 |
Correct |
152 ms |
31808 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
49 ms |
22132 KB |
Output is correct |
11 |
Correct |
71 ms |
22148 KB |
Output is correct |
12 |
Correct |
95 ms |
27720 KB |
Output is correct |
13 |
Correct |
133 ms |
31748 KB |
Output is correct |
14 |
Correct |
89 ms |
27728 KB |
Output is correct |
15 |
Correct |
107 ms |
30884 KB |
Output is correct |
16 |
Correct |
92 ms |
27720 KB |
Output is correct |
17 |
Correct |
107 ms |
30720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
22188 KB |
Output is correct |
2 |
Correct |
81 ms |
22136 KB |
Output is correct |
3 |
Correct |
87 ms |
24664 KB |
Output is correct |
4 |
Correct |
91 ms |
25336 KB |
Output is correct |
5 |
Correct |
178 ms |
30852 KB |
Output is correct |
6 |
Correct |
128 ms |
30184 KB |
Output is correct |
7 |
Correct |
126 ms |
30764 KB |
Output is correct |
8 |
Correct |
134 ms |
30792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
468 KB |
Output is correct |
17 |
Correct |
50 ms |
4128 KB |
Output is correct |
18 |
Correct |
54 ms |
6120 KB |
Output is correct |
19 |
Correct |
50 ms |
5656 KB |
Output is correct |
20 |
Correct |
51 ms |
5692 KB |
Output is correct |
21 |
Correct |
74 ms |
5580 KB |
Output is correct |
22 |
Correct |
87 ms |
11072 KB |
Output is correct |
23 |
Correct |
8 ms |
1236 KB |
Output is correct |
24 |
Correct |
30 ms |
3128 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
8 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
468 KB |
Output is correct |
17 |
Correct |
50 ms |
4128 KB |
Output is correct |
18 |
Correct |
54 ms |
6120 KB |
Output is correct |
19 |
Correct |
50 ms |
5656 KB |
Output is correct |
20 |
Correct |
51 ms |
5692 KB |
Output is correct |
21 |
Correct |
74 ms |
5580 KB |
Output is correct |
22 |
Correct |
87 ms |
11072 KB |
Output is correct |
23 |
Correct |
8 ms |
1236 KB |
Output is correct |
24 |
Correct |
30 ms |
3128 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
8 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
237 ms |
23392 KB |
Output is correct |
29 |
Correct |
310 ms |
27224 KB |
Output is correct |
30 |
Correct |
281 ms |
29152 KB |
Output is correct |
31 |
Correct |
299 ms |
31200 KB |
Output is correct |
32 |
Correct |
300 ms |
34956 KB |
Output is correct |
33 |
Correct |
293 ms |
31172 KB |
Output is correct |
34 |
Correct |
290 ms |
31164 KB |
Output is correct |
35 |
Correct |
106 ms |
15272 KB |
Output is correct |
36 |
Correct |
328 ms |
36440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
22188 KB |
Output is correct |
2 |
Correct |
81 ms |
22136 KB |
Output is correct |
3 |
Correct |
87 ms |
24664 KB |
Output is correct |
4 |
Correct |
91 ms |
25336 KB |
Output is correct |
5 |
Correct |
178 ms |
30852 KB |
Output is correct |
6 |
Correct |
128 ms |
30184 KB |
Output is correct |
7 |
Correct |
126 ms |
30764 KB |
Output is correct |
8 |
Correct |
134 ms |
30792 KB |
Output is correct |
9 |
Correct |
137 ms |
30584 KB |
Output is correct |
10 |
Correct |
95 ms |
23560 KB |
Output is correct |
11 |
Correct |
185 ms |
46840 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
49 ms |
22108 KB |
Output is correct |
19 |
Correct |
47 ms |
22132 KB |
Output is correct |
20 |
Correct |
47 ms |
22164 KB |
Output is correct |
21 |
Correct |
48 ms |
22124 KB |
Output is correct |
22 |
Correct |
157 ms |
33332 KB |
Output is correct |
23 |
Correct |
215 ms |
46948 KB |
Output is correct |
24 |
Correct |
187 ms |
47396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
27692 KB |
Output is correct |
2 |
Correct |
92 ms |
31692 KB |
Output is correct |
3 |
Correct |
49 ms |
22188 KB |
Output is correct |
4 |
Correct |
53 ms |
22116 KB |
Output is correct |
5 |
Correct |
272 ms |
56512 KB |
Output is correct |
6 |
Correct |
306 ms |
58776 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
183 ms |
35564 KB |
Output is correct |
9 |
Correct |
201 ms |
41388 KB |
Output is correct |
10 |
Correct |
99 ms |
27732 KB |
Output is correct |
11 |
Correct |
152 ms |
31808 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
49 ms |
22132 KB |
Output is correct |
17 |
Correct |
71 ms |
22148 KB |
Output is correct |
18 |
Correct |
95 ms |
27720 KB |
Output is correct |
19 |
Correct |
133 ms |
31748 KB |
Output is correct |
20 |
Correct |
89 ms |
27728 KB |
Output is correct |
21 |
Correct |
107 ms |
30884 KB |
Output is correct |
22 |
Correct |
92 ms |
27720 KB |
Output is correct |
23 |
Correct |
107 ms |
30720 KB |
Output is correct |
24 |
Correct |
53 ms |
22188 KB |
Output is correct |
25 |
Correct |
81 ms |
22136 KB |
Output is correct |
26 |
Correct |
87 ms |
24664 KB |
Output is correct |
27 |
Correct |
91 ms |
25336 KB |
Output is correct |
28 |
Correct |
178 ms |
30852 KB |
Output is correct |
29 |
Correct |
128 ms |
30184 KB |
Output is correct |
30 |
Correct |
126 ms |
30764 KB |
Output is correct |
31 |
Correct |
134 ms |
30792 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
2 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
5 ms |
468 KB |
Output is correct |
48 |
Correct |
50 ms |
4128 KB |
Output is correct |
49 |
Correct |
54 ms |
6120 KB |
Output is correct |
50 |
Correct |
50 ms |
5656 KB |
Output is correct |
51 |
Correct |
51 ms |
5692 KB |
Output is correct |
52 |
Correct |
74 ms |
5580 KB |
Output is correct |
53 |
Correct |
87 ms |
11072 KB |
Output is correct |
54 |
Correct |
8 ms |
1236 KB |
Output is correct |
55 |
Correct |
30 ms |
3128 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
8 ms |
1236 KB |
Output is correct |
58 |
Correct |
3 ms |
1108 KB |
Output is correct |
59 |
Correct |
237 ms |
23392 KB |
Output is correct |
60 |
Correct |
310 ms |
27224 KB |
Output is correct |
61 |
Correct |
281 ms |
29152 KB |
Output is correct |
62 |
Correct |
299 ms |
31200 KB |
Output is correct |
63 |
Correct |
300 ms |
34956 KB |
Output is correct |
64 |
Correct |
293 ms |
31172 KB |
Output is correct |
65 |
Correct |
290 ms |
31164 KB |
Output is correct |
66 |
Correct |
106 ms |
15272 KB |
Output is correct |
67 |
Correct |
328 ms |
36440 KB |
Output is correct |
68 |
Correct |
137 ms |
30584 KB |
Output is correct |
69 |
Correct |
95 ms |
23560 KB |
Output is correct |
70 |
Correct |
185 ms |
46840 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
296 KB |
Output is correct |
73 |
Correct |
1 ms |
296 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
49 ms |
22108 KB |
Output is correct |
78 |
Correct |
47 ms |
22132 KB |
Output is correct |
79 |
Correct |
47 ms |
22164 KB |
Output is correct |
80 |
Correct |
48 ms |
22124 KB |
Output is correct |
81 |
Correct |
157 ms |
33332 KB |
Output is correct |
82 |
Correct |
215 ms |
46948 KB |
Output is correct |
83 |
Correct |
187 ms |
47396 KB |
Output is correct |
84 |
Correct |
387 ms |
51548 KB |
Output is correct |
85 |
Correct |
408 ms |
52424 KB |
Output is correct |
86 |
Correct |
343 ms |
54948 KB |
Output is correct |
87 |
Correct |
314 ms |
57272 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
319 ms |
57204 KB |
Output is correct |
90 |
Correct |
385 ms |
57036 KB |
Output is correct |
91 |
Correct |
362 ms |
57844 KB |
Output is correct |