#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 |
36 ms |
27592 KB |
Output is correct |
2 |
Correct |
44 ms |
30404 KB |
Output is correct |
3 |
Correct |
13 ms |
20056 KB |
Output is correct |
4 |
Correct |
12 ms |
20060 KB |
Output is correct |
5 |
Correct |
99 ms |
52260 KB |
Output is correct |
6 |
Correct |
118 ms |
54604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
107 ms |
35956 KB |
Output is correct |
3 |
Correct |
69 ms |
40632 KB |
Output is correct |
4 |
Correct |
34 ms |
27588 KB |
Output is correct |
5 |
Correct |
40 ms |
30404 KB |
Output is correct |
6 |
Correct |
5 ms |
12120 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
13 ms |
20060 KB |
Output is correct |
11 |
Correct |
14 ms |
20056 KB |
Output is correct |
12 |
Correct |
37 ms |
27532 KB |
Output is correct |
13 |
Correct |
41 ms |
30368 KB |
Output is correct |
14 |
Correct |
36 ms |
27620 KB |
Output is correct |
15 |
Correct |
42 ms |
29372 KB |
Output is correct |
16 |
Correct |
34 ms |
27580 KB |
Output is correct |
17 |
Correct |
38 ms |
29116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
20060 KB |
Output is correct |
2 |
Correct |
12 ms |
20056 KB |
Output is correct |
3 |
Correct |
27 ms |
24916 KB |
Output is correct |
4 |
Correct |
23 ms |
23776 KB |
Output is correct |
5 |
Correct |
44 ms |
31060 KB |
Output is correct |
6 |
Correct |
41 ms |
30456 KB |
Output is correct |
7 |
Correct |
51 ms |
30888 KB |
Output is correct |
8 |
Correct |
44 ms |
31056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12008 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12172 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
6 ms |
12124 KB |
Output is correct |
10 |
Correct |
6 ms |
12380 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
7 ms |
12476 KB |
Output is correct |
13 |
Correct |
6 ms |
12120 KB |
Output is correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12008 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12172 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
6 ms |
12124 KB |
Output is correct |
10 |
Correct |
6 ms |
12380 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
7 ms |
12476 KB |
Output is correct |
13 |
Correct |
6 ms |
12120 KB |
Output is correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
6 ms |
12384 KB |
Output is correct |
17 |
Correct |
17 ms |
16988 KB |
Output is correct |
18 |
Correct |
18 ms |
17496 KB |
Output is correct |
19 |
Correct |
17 ms |
17500 KB |
Output is correct |
20 |
Correct |
17 ms |
17448 KB |
Output is correct |
21 |
Correct |
17 ms |
17500 KB |
Output is correct |
22 |
Correct |
28 ms |
22780 KB |
Output is correct |
23 |
Correct |
8 ms |
13148 KB |
Output is correct |
24 |
Correct |
14 ms |
15472 KB |
Output is correct |
25 |
Correct |
5 ms |
12328 KB |
Output is correct |
26 |
Correct |
8 ms |
13148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12008 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12172 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
6 ms |
12124 KB |
Output is correct |
10 |
Correct |
6 ms |
12380 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
7 ms |
12476 KB |
Output is correct |
13 |
Correct |
6 ms |
12120 KB |
Output is correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
6 ms |
12384 KB |
Output is correct |
17 |
Correct |
17 ms |
16988 KB |
Output is correct |
18 |
Correct |
18 ms |
17496 KB |
Output is correct |
19 |
Correct |
17 ms |
17500 KB |
Output is correct |
20 |
Correct |
17 ms |
17448 KB |
Output is correct |
21 |
Correct |
17 ms |
17500 KB |
Output is correct |
22 |
Correct |
28 ms |
22780 KB |
Output is correct |
23 |
Correct |
8 ms |
13148 KB |
Output is correct |
24 |
Correct |
14 ms |
15472 KB |
Output is correct |
25 |
Correct |
5 ms |
12328 KB |
Output is correct |
26 |
Correct |
8 ms |
13148 KB |
Output is correct |
27 |
Correct |
6 ms |
12632 KB |
Output is correct |
28 |
Correct |
62 ms |
36948 KB |
Output is correct |
29 |
Correct |
94 ms |
46156 KB |
Output is correct |
30 |
Correct |
103 ms |
45652 KB |
Output is correct |
31 |
Correct |
102 ms |
45776 KB |
Output is correct |
32 |
Correct |
85 ms |
46936 KB |
Output is correct |
33 |
Correct |
94 ms |
45648 KB |
Output is correct |
34 |
Correct |
97 ms |
45752 KB |
Output is correct |
35 |
Correct |
42 ms |
25428 KB |
Output is correct |
36 |
Correct |
104 ms |
47700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
20060 KB |
Output is correct |
2 |
Correct |
12 ms |
20056 KB |
Output is correct |
3 |
Correct |
27 ms |
24916 KB |
Output is correct |
4 |
Correct |
23 ms |
23776 KB |
Output is correct |
5 |
Correct |
44 ms |
31060 KB |
Output is correct |
6 |
Correct |
41 ms |
30456 KB |
Output is correct |
7 |
Correct |
51 ms |
30888 KB |
Output is correct |
8 |
Correct |
44 ms |
31056 KB |
Output is correct |
9 |
Correct |
40 ms |
30700 KB |
Output is correct |
10 |
Correct |
39 ms |
27728 KB |
Output is correct |
11 |
Correct |
78 ms |
43348 KB |
Output is correct |
12 |
Correct |
5 ms |
12188 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12120 KB |
Output is correct |
17 |
Correct |
5 ms |
12124 KB |
Output is correct |
18 |
Correct |
12 ms |
19852 KB |
Output is correct |
19 |
Correct |
12 ms |
19804 KB |
Output is correct |
20 |
Correct |
11 ms |
20060 KB |
Output is correct |
21 |
Correct |
12 ms |
20060 KB |
Output is correct |
22 |
Correct |
48 ms |
31272 KB |
Output is correct |
23 |
Correct |
83 ms |
43344 KB |
Output is correct |
24 |
Correct |
80 ms |
43856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
27592 KB |
Output is correct |
2 |
Correct |
44 ms |
30404 KB |
Output is correct |
3 |
Correct |
13 ms |
20056 KB |
Output is correct |
4 |
Correct |
12 ms |
20060 KB |
Output is correct |
5 |
Correct |
99 ms |
52260 KB |
Output is correct |
6 |
Correct |
118 ms |
54604 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
107 ms |
35956 KB |
Output is correct |
9 |
Correct |
69 ms |
40632 KB |
Output is correct |
10 |
Correct |
34 ms |
27588 KB |
Output is correct |
11 |
Correct |
40 ms |
30404 KB |
Output is correct |
12 |
Correct |
5 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
13 ms |
20060 KB |
Output is correct |
17 |
Correct |
14 ms |
20056 KB |
Output is correct |
18 |
Correct |
37 ms |
27532 KB |
Output is correct |
19 |
Correct |
41 ms |
30368 KB |
Output is correct |
20 |
Correct |
36 ms |
27620 KB |
Output is correct |
21 |
Correct |
42 ms |
29372 KB |
Output is correct |
22 |
Correct |
34 ms |
27580 KB |
Output is correct |
23 |
Correct |
38 ms |
29116 KB |
Output is correct |
24 |
Correct |
16 ms |
20060 KB |
Output is correct |
25 |
Correct |
12 ms |
20056 KB |
Output is correct |
26 |
Correct |
27 ms |
24916 KB |
Output is correct |
27 |
Correct |
23 ms |
23776 KB |
Output is correct |
28 |
Correct |
44 ms |
31060 KB |
Output is correct |
29 |
Correct |
41 ms |
30456 KB |
Output is correct |
30 |
Correct |
51 ms |
30888 KB |
Output is correct |
31 |
Correct |
44 ms |
31056 KB |
Output is correct |
32 |
Correct |
5 ms |
12124 KB |
Output is correct |
33 |
Correct |
5 ms |
12008 KB |
Output is correct |
34 |
Correct |
5 ms |
12124 KB |
Output is correct |
35 |
Correct |
5 ms |
12124 KB |
Output is correct |
36 |
Correct |
5 ms |
12124 KB |
Output is correct |
37 |
Correct |
5 ms |
12124 KB |
Output is correct |
38 |
Correct |
5 ms |
12172 KB |
Output is correct |
39 |
Correct |
5 ms |
12124 KB |
Output is correct |
40 |
Correct |
6 ms |
12124 KB |
Output is correct |
41 |
Correct |
6 ms |
12380 KB |
Output is correct |
42 |
Correct |
5 ms |
12124 KB |
Output is correct |
43 |
Correct |
7 ms |
12476 KB |
Output is correct |
44 |
Correct |
6 ms |
12120 KB |
Output is correct |
45 |
Correct |
5 ms |
12124 KB |
Output is correct |
46 |
Correct |
5 ms |
12124 KB |
Output is correct |
47 |
Correct |
6 ms |
12384 KB |
Output is correct |
48 |
Correct |
17 ms |
16988 KB |
Output is correct |
49 |
Correct |
18 ms |
17496 KB |
Output is correct |
50 |
Correct |
17 ms |
17500 KB |
Output is correct |
51 |
Correct |
17 ms |
17448 KB |
Output is correct |
52 |
Correct |
17 ms |
17500 KB |
Output is correct |
53 |
Correct |
28 ms |
22780 KB |
Output is correct |
54 |
Correct |
8 ms |
13148 KB |
Output is correct |
55 |
Correct |
14 ms |
15472 KB |
Output is correct |
56 |
Correct |
5 ms |
12328 KB |
Output is correct |
57 |
Correct |
8 ms |
13148 KB |
Output is correct |
58 |
Correct |
6 ms |
12632 KB |
Output is correct |
59 |
Correct |
62 ms |
36948 KB |
Output is correct |
60 |
Correct |
94 ms |
46156 KB |
Output is correct |
61 |
Correct |
103 ms |
45652 KB |
Output is correct |
62 |
Correct |
102 ms |
45776 KB |
Output is correct |
63 |
Correct |
85 ms |
46936 KB |
Output is correct |
64 |
Correct |
94 ms |
45648 KB |
Output is correct |
65 |
Correct |
97 ms |
45752 KB |
Output is correct |
66 |
Correct |
42 ms |
25428 KB |
Output is correct |
67 |
Correct |
104 ms |
47700 KB |
Output is correct |
68 |
Correct |
40 ms |
30700 KB |
Output is correct |
69 |
Correct |
39 ms |
27728 KB |
Output is correct |
70 |
Correct |
78 ms |
43348 KB |
Output is correct |
71 |
Correct |
5 ms |
12188 KB |
Output is correct |
72 |
Correct |
5 ms |
12124 KB |
Output is correct |
73 |
Correct |
5 ms |
12124 KB |
Output is correct |
74 |
Correct |
5 ms |
12124 KB |
Output is correct |
75 |
Correct |
5 ms |
12120 KB |
Output is correct |
76 |
Correct |
5 ms |
12124 KB |
Output is correct |
77 |
Correct |
12 ms |
19852 KB |
Output is correct |
78 |
Correct |
12 ms |
19804 KB |
Output is correct |
79 |
Correct |
11 ms |
20060 KB |
Output is correct |
80 |
Correct |
12 ms |
20060 KB |
Output is correct |
81 |
Correct |
48 ms |
31272 KB |
Output is correct |
82 |
Correct |
83 ms |
43344 KB |
Output is correct |
83 |
Correct |
80 ms |
43856 KB |
Output is correct |
84 |
Correct |
108 ms |
51144 KB |
Output is correct |
85 |
Correct |
103 ms |
51460 KB |
Output is correct |
86 |
Correct |
115 ms |
53332 KB |
Output is correct |
87 |
Correct |
120 ms |
54352 KB |
Output is correct |
88 |
Correct |
5 ms |
12124 KB |
Output is correct |
89 |
Correct |
119 ms |
54356 KB |
Output is correct |
90 |
Correct |
109 ms |
53056 KB |
Output is correct |
91 |
Correct |
106 ms |
53148 KB |
Output is correct |