#include "fish.h"
#include <bits/stdc++.h>
#define MAX 500005
#define INF (ll)(1e16)
using namespace std;
typedef long long ll;
int N, M;
ll up[MAX], down[MAX], sum[MAX], in[MAX], out[MAX];
vector<ll> X, Y, W;
vector<pair<int, ll> > pos[MAX];
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y, std::vector<int> w) {
N = n, M = m; X.resize(M); Y.resize(N + M + 1); W.resize(M);
for(ll i = 0; i < M; ++i) X[i] = x[i] + 1, Y[i] = y[i] + 1, W[i] = w[i];
for(ll i = 0; i < M; ++i) pos[X[i]].push_back({Y[i], W[i]});
for(ll i = 1; i <= N; ++i) pos[i].push_back({N + 1, 0});
for(ll i = 1; i <= N; ++i) sort(pos[i].begin(), pos[i].end());
ll cnt = 0;
for(ll i = 1; i <= N; ++i) {
sum[++cnt] = pos[i][0].second; in[i] = cnt; Y[cnt] = pos[i][0].first;
for(ll j = 1; j < (ll)pos[i].size(); ++j) {
++cnt;
sum[cnt] = sum[cnt - 1] + pos[i][j].second;
Y[cnt] = pos[i][j].first;
}
out[i] = cnt;
}
for(ll i = 0; i <= N; ++i) up[i] = down[i] = -INF;
up[0] = down[0] = 0; Y[0] = -1;
for(ll x = 1; x <= N; ++x) {
ll k = in[x - 1], mx = -INF, p = -1;
for(ll i = in[x]; i <= out[x]; ++i) {
while(k <= out[x - 1] && Y[k] < Y[i]) {
mx = max(mx, up[k] - (k == in[x - 1] ? 0 : sum[k - 1]));
p = k; ++k;
}
up[i] = max(up[i], mx + (p == -1 ? 0 : sum[p]));
}
k = out[x - 1], mx = -INF, p = out[x];
for(ll i = out[x]; i >= in[x]; --i) {
while(k >= in[x - 1] && Y[k] > Y[i]) {
while(p >= in[x] && Y[k] <= Y[p]) --p;
mx = max(mx, max(down[k], up[k]) + sum[p]);
--k;
}
down[i] = max(down[i], mx - (i == in[x] ? 0 : sum[i - 1]));
if(k >= in[x - 1] && Y[k] == Y[i]) down[i] = max(down[i], max(down[k], up[k]));
}
if(x < 2) continue;
k = in[x - 2], mx = -INF, p = in[x - 1]; ll p2 = -1;
for(ll i = in[x]; i <= out[x]; ++i) {
while(k <= out[x - 2] && Y[k] <= Y[i]) {
mx = max(mx, max(up[k], down[k]));
++k;
}
while(p <= out[x - 1] && Y[p] < Y[i]) p2 = p++;
up[i] = max(up[i], mx + (p2 == -1 ? 0 : sum[p2]));
}
k = out[x - 2], mx = -INF, p = out[x - 1];
for(ll i = out[x]; i >= in[x]; --i) {
while(k >= in[x - 2] && Y[k] > Y[i]) {
while(p >= in[x - 1] && Y[k] <= Y[p]) --p;
mx = max(mx, max(up[k], down[k]) + (p == in[x - 1] - 1 ? 0 : sum[p]));
--k;
}
up[i] = max(up[i], mx);
}
}
ll ret = 0;
for(ll i = 0; i <= N + M; ++i) ret = max(ret, max(up[i], down[i]));
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
27412 KB |
Output is correct |
2 |
Correct |
58 ms |
30260 KB |
Output is correct |
3 |
Correct |
16 ms |
19856 KB |
Output is correct |
4 |
Correct |
16 ms |
19924 KB |
Output is correct |
5 |
Correct |
146 ms |
50524 KB |
Output is correct |
6 |
Correct |
205 ms |
54468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12052 KB |
Output is correct |
2 |
Correct |
79 ms |
35864 KB |
Output is correct |
3 |
Correct |
100 ms |
40628 KB |
Output is correct |
4 |
Correct |
47 ms |
27436 KB |
Output is correct |
5 |
Correct |
56 ms |
30344 KB |
Output is correct |
6 |
Correct |
8 ms |
12000 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
8 ms |
12052 KB |
Output is correct |
9 |
Correct |
6 ms |
12052 KB |
Output is correct |
10 |
Correct |
15 ms |
19924 KB |
Output is correct |
11 |
Correct |
16 ms |
19920 KB |
Output is correct |
12 |
Correct |
46 ms |
27532 KB |
Output is correct |
13 |
Correct |
59 ms |
30188 KB |
Output is correct |
14 |
Correct |
52 ms |
27476 KB |
Output is correct |
15 |
Correct |
55 ms |
29212 KB |
Output is correct |
16 |
Correct |
65 ms |
27448 KB |
Output is correct |
17 |
Correct |
55 ms |
29212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
19924 KB |
Output is correct |
2 |
Correct |
16 ms |
19892 KB |
Output is correct |
3 |
Correct |
38 ms |
24836 KB |
Output is correct |
4 |
Correct |
34 ms |
23640 KB |
Output is correct |
5 |
Correct |
64 ms |
31008 KB |
Output is correct |
6 |
Correct |
60 ms |
30284 KB |
Output is correct |
7 |
Correct |
61 ms |
30880 KB |
Output is correct |
8 |
Correct |
72 ms |
30996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12052 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
10 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
9 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
12052 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
10 ms |
11988 KB |
Output is correct |
9 |
Correct |
9 ms |
12116 KB |
Output is correct |
10 |
Correct |
10 ms |
12372 KB |
Output is correct |
11 |
Correct |
9 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
11988 KB |
Output is correct |
14 |
Correct |
10 ms |
12212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12052 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
10 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
9 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
12052 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
10 ms |
11988 KB |
Output is correct |
9 |
Correct |
9 ms |
12116 KB |
Output is correct |
10 |
Correct |
10 ms |
12372 KB |
Output is correct |
11 |
Correct |
9 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
11988 KB |
Output is correct |
14 |
Correct |
10 ms |
12212 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12328 KB |
Output is correct |
17 |
Correct |
24 ms |
16976 KB |
Output is correct |
18 |
Correct |
24 ms |
17436 KB |
Output is correct |
19 |
Correct |
26 ms |
17356 KB |
Output is correct |
20 |
Correct |
24 ms |
17368 KB |
Output is correct |
21 |
Correct |
25 ms |
17364 KB |
Output is correct |
22 |
Correct |
43 ms |
22684 KB |
Output is correct |
23 |
Correct |
11 ms |
13088 KB |
Output is correct |
24 |
Correct |
21 ms |
15392 KB |
Output is correct |
25 |
Correct |
8 ms |
12208 KB |
Output is correct |
26 |
Correct |
10 ms |
13012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12052 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
10 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
9 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
12052 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
10 ms |
11988 KB |
Output is correct |
9 |
Correct |
9 ms |
12116 KB |
Output is correct |
10 |
Correct |
10 ms |
12372 KB |
Output is correct |
11 |
Correct |
9 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
11988 KB |
Output is correct |
14 |
Correct |
10 ms |
12212 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12328 KB |
Output is correct |
17 |
Correct |
24 ms |
16976 KB |
Output is correct |
18 |
Correct |
24 ms |
17436 KB |
Output is correct |
19 |
Correct |
26 ms |
17356 KB |
Output is correct |
20 |
Correct |
24 ms |
17368 KB |
Output is correct |
21 |
Correct |
25 ms |
17364 KB |
Output is correct |
22 |
Correct |
43 ms |
22684 KB |
Output is correct |
23 |
Correct |
11 ms |
13088 KB |
Output is correct |
24 |
Correct |
21 ms |
15392 KB |
Output is correct |
25 |
Correct |
8 ms |
12208 KB |
Output is correct |
26 |
Correct |
10 ms |
13012 KB |
Output is correct |
27 |
Correct |
7 ms |
12632 KB |
Output is correct |
28 |
Correct |
93 ms |
36964 KB |
Output is correct |
29 |
Correct |
135 ms |
46140 KB |
Output is correct |
30 |
Correct |
136 ms |
45512 KB |
Output is correct |
31 |
Correct |
142 ms |
45640 KB |
Output is correct |
32 |
Correct |
125 ms |
46912 KB |
Output is correct |
33 |
Correct |
141 ms |
45628 KB |
Output is correct |
34 |
Correct |
144 ms |
45632 KB |
Output is correct |
35 |
Correct |
56 ms |
25440 KB |
Output is correct |
36 |
Correct |
142 ms |
47420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
19924 KB |
Output is correct |
2 |
Correct |
16 ms |
19892 KB |
Output is correct |
3 |
Correct |
38 ms |
24836 KB |
Output is correct |
4 |
Correct |
34 ms |
23640 KB |
Output is correct |
5 |
Correct |
64 ms |
31008 KB |
Output is correct |
6 |
Correct |
60 ms |
30284 KB |
Output is correct |
7 |
Correct |
61 ms |
30880 KB |
Output is correct |
8 |
Correct |
72 ms |
30996 KB |
Output is correct |
9 |
Correct |
63 ms |
30672 KB |
Output is correct |
10 |
Correct |
70 ms |
27684 KB |
Output is correct |
11 |
Correct |
122 ms |
43328 KB |
Output is correct |
12 |
Correct |
7 ms |
12032 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
16 ms |
19924 KB |
Output is correct |
19 |
Correct |
20 ms |
19852 KB |
Output is correct |
20 |
Correct |
15 ms |
19796 KB |
Output is correct |
21 |
Correct |
16 ms |
19864 KB |
Output is correct |
22 |
Correct |
64 ms |
31136 KB |
Output is correct |
23 |
Correct |
108 ms |
43308 KB |
Output is correct |
24 |
Correct |
122 ms |
43828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
27412 KB |
Output is correct |
2 |
Correct |
58 ms |
30260 KB |
Output is correct |
3 |
Correct |
16 ms |
19856 KB |
Output is correct |
4 |
Correct |
16 ms |
19924 KB |
Output is correct |
5 |
Correct |
146 ms |
50524 KB |
Output is correct |
6 |
Correct |
205 ms |
54468 KB |
Output is correct |
7 |
Correct |
6 ms |
12052 KB |
Output is correct |
8 |
Correct |
79 ms |
35864 KB |
Output is correct |
9 |
Correct |
100 ms |
40628 KB |
Output is correct |
10 |
Correct |
47 ms |
27436 KB |
Output is correct |
11 |
Correct |
56 ms |
30344 KB |
Output is correct |
12 |
Correct |
8 ms |
12000 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
8 ms |
12052 KB |
Output is correct |
15 |
Correct |
6 ms |
12052 KB |
Output is correct |
16 |
Correct |
15 ms |
19924 KB |
Output is correct |
17 |
Correct |
16 ms |
19920 KB |
Output is correct |
18 |
Correct |
46 ms |
27532 KB |
Output is correct |
19 |
Correct |
59 ms |
30188 KB |
Output is correct |
20 |
Correct |
52 ms |
27476 KB |
Output is correct |
21 |
Correct |
55 ms |
29212 KB |
Output is correct |
22 |
Correct |
65 ms |
27448 KB |
Output is correct |
23 |
Correct |
55 ms |
29212 KB |
Output is correct |
24 |
Correct |
16 ms |
19924 KB |
Output is correct |
25 |
Correct |
16 ms |
19892 KB |
Output is correct |
26 |
Correct |
38 ms |
24836 KB |
Output is correct |
27 |
Correct |
34 ms |
23640 KB |
Output is correct |
28 |
Correct |
64 ms |
31008 KB |
Output is correct |
29 |
Correct |
60 ms |
30284 KB |
Output is correct |
30 |
Correct |
61 ms |
30880 KB |
Output is correct |
31 |
Correct |
72 ms |
30996 KB |
Output is correct |
32 |
Correct |
9 ms |
12052 KB |
Output is correct |
33 |
Correct |
8 ms |
11988 KB |
Output is correct |
34 |
Correct |
10 ms |
12056 KB |
Output is correct |
35 |
Correct |
8 ms |
11988 KB |
Output is correct |
36 |
Correct |
9 ms |
11988 KB |
Output is correct |
37 |
Correct |
8 ms |
12052 KB |
Output is correct |
38 |
Correct |
8 ms |
11988 KB |
Output is correct |
39 |
Correct |
10 ms |
11988 KB |
Output is correct |
40 |
Correct |
9 ms |
12116 KB |
Output is correct |
41 |
Correct |
10 ms |
12372 KB |
Output is correct |
42 |
Correct |
9 ms |
12116 KB |
Output is correct |
43 |
Correct |
8 ms |
12244 KB |
Output is correct |
44 |
Correct |
8 ms |
11988 KB |
Output is correct |
45 |
Correct |
10 ms |
12212 KB |
Output is correct |
46 |
Correct |
6 ms |
12116 KB |
Output is correct |
47 |
Correct |
7 ms |
12328 KB |
Output is correct |
48 |
Correct |
24 ms |
16976 KB |
Output is correct |
49 |
Correct |
24 ms |
17436 KB |
Output is correct |
50 |
Correct |
26 ms |
17356 KB |
Output is correct |
51 |
Correct |
24 ms |
17368 KB |
Output is correct |
52 |
Correct |
25 ms |
17364 KB |
Output is correct |
53 |
Correct |
43 ms |
22684 KB |
Output is correct |
54 |
Correct |
11 ms |
13088 KB |
Output is correct |
55 |
Correct |
21 ms |
15392 KB |
Output is correct |
56 |
Correct |
8 ms |
12208 KB |
Output is correct |
57 |
Correct |
10 ms |
13012 KB |
Output is correct |
58 |
Correct |
7 ms |
12632 KB |
Output is correct |
59 |
Correct |
93 ms |
36964 KB |
Output is correct |
60 |
Correct |
135 ms |
46140 KB |
Output is correct |
61 |
Correct |
136 ms |
45512 KB |
Output is correct |
62 |
Correct |
142 ms |
45640 KB |
Output is correct |
63 |
Correct |
125 ms |
46912 KB |
Output is correct |
64 |
Correct |
141 ms |
45628 KB |
Output is correct |
65 |
Correct |
144 ms |
45632 KB |
Output is correct |
66 |
Correct |
56 ms |
25440 KB |
Output is correct |
67 |
Correct |
142 ms |
47420 KB |
Output is correct |
68 |
Correct |
63 ms |
30672 KB |
Output is correct |
69 |
Correct |
70 ms |
27684 KB |
Output is correct |
70 |
Correct |
122 ms |
43328 KB |
Output is correct |
71 |
Correct |
7 ms |
12032 KB |
Output is correct |
72 |
Correct |
6 ms |
11988 KB |
Output is correct |
73 |
Correct |
6 ms |
11988 KB |
Output is correct |
74 |
Correct |
6 ms |
11988 KB |
Output is correct |
75 |
Correct |
6 ms |
11988 KB |
Output is correct |
76 |
Correct |
7 ms |
11988 KB |
Output is correct |
77 |
Correct |
16 ms |
19924 KB |
Output is correct |
78 |
Correct |
20 ms |
19852 KB |
Output is correct |
79 |
Correct |
15 ms |
19796 KB |
Output is correct |
80 |
Correct |
16 ms |
19864 KB |
Output is correct |
81 |
Correct |
64 ms |
31136 KB |
Output is correct |
82 |
Correct |
108 ms |
43308 KB |
Output is correct |
83 |
Correct |
122 ms |
43828 KB |
Output is correct |
84 |
Correct |
154 ms |
51056 KB |
Output is correct |
85 |
Correct |
143 ms |
51320 KB |
Output is correct |
86 |
Correct |
188 ms |
53416 KB |
Output is correct |
87 |
Correct |
213 ms |
54460 KB |
Output is correct |
88 |
Correct |
7 ms |
11988 KB |
Output is correct |
89 |
Correct |
177 ms |
54332 KB |
Output is correct |
90 |
Correct |
169 ms |
52940 KB |
Output is correct |
91 |
Correct |
145 ms |
52940 KB |
Output is correct |