#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define mp make_pair
using ll = long long;
using ii = pair<int, int>;
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
auto tmp = vector<ll>(N + M), tmp2 = vector<ll>(N + M);
auto dp = vector<vector<ll> >(N + M, vector<ll>(4, 0));
auto col = vector<vector<ii> >(N);
for (int i = 0; i < M; i++) {
col[X[i]].eb(Y[i], i);
}
for (int i = 0; i < N; i++) {
sort(col[i].begin(), col[i].end());
}
for (int i = 0; i <= N; i++) {
{
// ty == 0
if (i > 0) {
dp[i][0] = max({0ll, dp[i - 1][0], dp[i - 1][1]});
} else {
dp[i][0] = 0;
}
if (i && !col[i - 1].empty()) {
dp[i][0] = max(dp[i][0], dp[col[i - 1][0].second][2]);
}
}
{
// ty == 1
if (i > 0) {
dp[i][1] = max({0ll, dp[i - 1][0], dp[i - 1][1]});
} else {
dp[i][1] = 0;
}
if (i && !col[i - 1].empty()) {
dp[i][1] = max(dp[i][1], dp[col[i - 1][0].second][2]);
dp[i][1] = max(dp[i][1], dp[col[i - 1].back().second][3]);
}
}
if (i < N) {
// ty == 2
{
ll sum_all = 0;
for (int _ = (int)col[i].size() - 1; _ >= 0; _--) {
int p = col[i][_].second, x = X[p];
sum_all += W[p];
if (x - 1 >= 0) {
dp[p][2] = max(dp[p][2], dp[x - 1][1] + sum_all);
}
}
if (i && !col[i - 1].empty()) {
int ptr = 0;
ll pf_sum = 0;
for (int _ = 0; _ < (int)col[i - 1].size(); _++) {
while (ptr < (int)col[i].size() && col[i - 1][_].first > col[i][ptr].first) {
pf_sum += W[col[i][ptr].second];
ptr++;
}
tmp[_] = pf_sum;
}
pf_sum = 0;
for (int _ = 0; _ < (int)col[i].size(); _++) {
tmp2[_] = pf_sum;
pf_sum += W[col[i][_].second];
}
ptr = (int)col[i - 1].size() - 1;
ll rs = -(ll)1e16;
for (int _ = (int)col[i].size() - 1; _ >= 0; _--) {
while (ptr >= 0 && col[i - 1][ptr].first > col[i][_].first) {
rs = max(rs, dp[col[i - 1][ptr].second][2] + tmp[ptr]);
ptr--;
}
dp[col[i][_].second][2] = max(dp[col[i][_].second][2], rs - tmp2[_]);
}
}
}
// ty == 3
{
ll sum_all = 0;
for (int _ = 0; _ < (int)col[i].size(); _++) {
int p = col[i][_].second, x = X[p];
sum_all += W[p];
dp[p][3] = max(dp[p][3], dp[x][0] + sum_all);
}
if (i && !col[i - 1].empty()) {
int ptr = (int)col[i].size() - 1;
ll pf_sum = 0;
for (int _ = (int)col[i - 1].size() - 1; _ >= 0; _--) {
while (ptr >= 0 && col[i - 1][_].first < col[i][ptr].first) {
pf_sum += W[col[i][ptr].second];
ptr--;
}
tmp[_] = pf_sum;
}
pf_sum = 0;
for (int _ = (int)col[i].size() - 1; _ >= 0; _--) {
tmp2[_] = pf_sum;
pf_sum += W[col[i][_].second];
}
ptr = 0;
ll rs = -(ll)1e16;
for (int _ = 0; _ < (int)col[i].size(); _++) {
while (ptr < (int)col[i - 1].size() && col[i - 1][ptr].first < col[i][_].first) {
rs = max(rs, dp[col[i - 1][ptr].second][3] + tmp[ptr]);
ptr++;
}
dp[col[i][_].second][3] = max(dp[col[i][_].second][3], rs - tmp2[_]);
}
}
}
}
}
return dp[N][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
20052 KB |
Output is correct |
2 |
Correct |
46 ms |
23372 KB |
Output is correct |
3 |
Correct |
8 ms |
11220 KB |
Output is correct |
4 |
Correct |
8 ms |
11220 KB |
Output is correct |
5 |
Correct |
132 ms |
46664 KB |
Output is correct |
6 |
Correct |
135 ms |
48512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
76 ms |
29484 KB |
Output is correct |
3 |
Correct |
95 ms |
34900 KB |
Output is correct |
4 |
Correct |
38 ms |
20180 KB |
Output is correct |
5 |
Correct |
46 ms |
23364 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
8 ms |
11220 KB |
Output is correct |
11 |
Correct |
8 ms |
11220 KB |
Output is correct |
12 |
Correct |
37 ms |
20172 KB |
Output is correct |
13 |
Correct |
52 ms |
23364 KB |
Output is correct |
14 |
Correct |
41 ms |
20048 KB |
Output is correct |
15 |
Correct |
44 ms |
22092 KB |
Output is correct |
16 |
Correct |
40 ms |
20048 KB |
Output is correct |
17 |
Correct |
45 ms |
22084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11220 KB |
Output is correct |
2 |
Correct |
8 ms |
11220 KB |
Output is correct |
3 |
Correct |
31 ms |
17620 KB |
Output is correct |
4 |
Correct |
24 ms |
16464 KB |
Output is correct |
5 |
Correct |
58 ms |
25300 KB |
Output is correct |
6 |
Correct |
62 ms |
25272 KB |
Output is correct |
7 |
Correct |
56 ms |
25292 KB |
Output is correct |
8 |
Correct |
56 ms |
25284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
16 ms |
5716 KB |
Output is correct |
18 |
Correct |
15 ms |
5972 KB |
Output is correct |
19 |
Correct |
15 ms |
5972 KB |
Output is correct |
20 |
Correct |
15 ms |
5972 KB |
Output is correct |
21 |
Correct |
15 ms |
5952 KB |
Output is correct |
22 |
Correct |
33 ms |
11604 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
11 ms |
3924 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
16 ms |
5716 KB |
Output is correct |
18 |
Correct |
15 ms |
5972 KB |
Output is correct |
19 |
Correct |
15 ms |
5972 KB |
Output is correct |
20 |
Correct |
15 ms |
5972 KB |
Output is correct |
21 |
Correct |
15 ms |
5952 KB |
Output is correct |
22 |
Correct |
33 ms |
11604 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
11 ms |
3924 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
84 ms |
27340 KB |
Output is correct |
29 |
Correct |
130 ms |
42800 KB |
Output is correct |
30 |
Correct |
123 ms |
42404 KB |
Output is correct |
31 |
Correct |
120 ms |
42492 KB |
Output is correct |
32 |
Correct |
120 ms |
43212 KB |
Output is correct |
33 |
Correct |
124 ms |
42460 KB |
Output is correct |
34 |
Correct |
122 ms |
42456 KB |
Output is correct |
35 |
Correct |
44 ms |
16848 KB |
Output is correct |
36 |
Correct |
121 ms |
43336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11220 KB |
Output is correct |
2 |
Correct |
8 ms |
11220 KB |
Output is correct |
3 |
Correct |
31 ms |
17620 KB |
Output is correct |
4 |
Correct |
24 ms |
16464 KB |
Output is correct |
5 |
Correct |
58 ms |
25300 KB |
Output is correct |
6 |
Correct |
62 ms |
25272 KB |
Output is correct |
7 |
Correct |
56 ms |
25292 KB |
Output is correct |
8 |
Correct |
56 ms |
25284 KB |
Output is correct |
9 |
Correct |
61 ms |
25292 KB |
Output is correct |
10 |
Correct |
46 ms |
18252 KB |
Output is correct |
11 |
Correct |
107 ms |
36300 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
8 ms |
11220 KB |
Output is correct |
19 |
Correct |
8 ms |
11148 KB |
Output is correct |
20 |
Correct |
8 ms |
11220 KB |
Output is correct |
21 |
Correct |
8 ms |
11220 KB |
Output is correct |
22 |
Correct |
53 ms |
24268 KB |
Output is correct |
23 |
Correct |
104 ms |
36300 KB |
Output is correct |
24 |
Correct |
106 ms |
36276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
20052 KB |
Output is correct |
2 |
Correct |
46 ms |
23372 KB |
Output is correct |
3 |
Correct |
8 ms |
11220 KB |
Output is correct |
4 |
Correct |
8 ms |
11220 KB |
Output is correct |
5 |
Correct |
132 ms |
46664 KB |
Output is correct |
6 |
Correct |
135 ms |
48512 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
76 ms |
29484 KB |
Output is correct |
9 |
Correct |
95 ms |
34900 KB |
Output is correct |
10 |
Correct |
38 ms |
20180 KB |
Output is correct |
11 |
Correct |
46 ms |
23364 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
8 ms |
11220 KB |
Output is correct |
17 |
Correct |
8 ms |
11220 KB |
Output is correct |
18 |
Correct |
37 ms |
20172 KB |
Output is correct |
19 |
Correct |
52 ms |
23364 KB |
Output is correct |
20 |
Correct |
41 ms |
20048 KB |
Output is correct |
21 |
Correct |
44 ms |
22092 KB |
Output is correct |
22 |
Correct |
40 ms |
20048 KB |
Output is correct |
23 |
Correct |
45 ms |
22084 KB |
Output is correct |
24 |
Correct |
8 ms |
11220 KB |
Output is correct |
25 |
Correct |
8 ms |
11220 KB |
Output is correct |
26 |
Correct |
31 ms |
17620 KB |
Output is correct |
27 |
Correct |
24 ms |
16464 KB |
Output is correct |
28 |
Correct |
58 ms |
25300 KB |
Output is correct |
29 |
Correct |
62 ms |
25272 KB |
Output is correct |
30 |
Correct |
56 ms |
25292 KB |
Output is correct |
31 |
Correct |
56 ms |
25284 KB |
Output is correct |
32 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
16 ms |
5716 KB |
Output is correct |
49 |
Correct |
15 ms |
5972 KB |
Output is correct |
50 |
Correct |
15 ms |
5972 KB |
Output is correct |
51 |
Correct |
15 ms |
5972 KB |
Output is correct |
52 |
Correct |
15 ms |
5952 KB |
Output is correct |
53 |
Correct |
33 ms |
11604 KB |
Output is correct |
54 |
Correct |
4 ms |
1364 KB |
Output is correct |
55 |
Correct |
11 ms |
3924 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
3 ms |
1364 KB |
Output is correct |
58 |
Correct |
2 ms |
980 KB |
Output is correct |
59 |
Correct |
84 ms |
27340 KB |
Output is correct |
60 |
Correct |
130 ms |
42800 KB |
Output is correct |
61 |
Correct |
123 ms |
42404 KB |
Output is correct |
62 |
Correct |
120 ms |
42492 KB |
Output is correct |
63 |
Correct |
120 ms |
43212 KB |
Output is correct |
64 |
Correct |
124 ms |
42460 KB |
Output is correct |
65 |
Correct |
122 ms |
42456 KB |
Output is correct |
66 |
Correct |
44 ms |
16848 KB |
Output is correct |
67 |
Correct |
121 ms |
43336 KB |
Output is correct |
68 |
Correct |
61 ms |
25292 KB |
Output is correct |
69 |
Correct |
46 ms |
18252 KB |
Output is correct |
70 |
Correct |
107 ms |
36300 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
8 ms |
11220 KB |
Output is correct |
78 |
Correct |
8 ms |
11148 KB |
Output is correct |
79 |
Correct |
8 ms |
11220 KB |
Output is correct |
80 |
Correct |
8 ms |
11220 KB |
Output is correct |
81 |
Correct |
53 ms |
24268 KB |
Output is correct |
82 |
Correct |
104 ms |
36300 KB |
Output is correct |
83 |
Correct |
106 ms |
36276 KB |
Output is correct |
84 |
Correct |
141 ms |
52044 KB |
Output is correct |
85 |
Correct |
158 ms |
52464 KB |
Output is correct |
86 |
Correct |
161 ms |
53844 KB |
Output is correct |
87 |
Correct |
161 ms |
55404 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
163 ms |
55552 KB |
Output is correct |
90 |
Correct |
141 ms |
53852 KB |
Output is correct |
91 |
Correct |
130 ms |
53452 KB |
Output is correct |