#include "fish.h"
#include <climits>
#include <algorithm>
#include <numeric>
#include <utility>
#include <vector>
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
std::vector<std::vector<std::pair<int, long long>>> fish(N + 2);
for (int i = 0; i < M; ++i) {
fish[X[i] + 1].emplace_back(Y[i], W[i]);
}
fish[0] = {{-1, 0LL}};
for (int i = 1; i <= N; ++i) {
fish[i].emplace_back(-1, 0LL);
std::sort(fish[i].begin(), fish[i].end());
for (int j = 1; j < static_cast<int>(fish[i].size()); ++j) {
fish[i][j].second += fish[i][j - 1].second;
}
}
fish[N + 1] = {{-1, 0LL}};
auto getRowWeight = [&](int row, int col) {
return (upper_bound(fish[row].begin(), fish[row].end(), std::make_pair(col, LLONG_MAX)) - 1)->second;
};
std::vector<std::vector<int>> indices(N + 2);
indices[0] = {-1};
for (int i = 1; i <= N; ++i) {
for (std::pair<int, int> f : fish[i - 1]) {
indices[i].push_back(f.first);
}
for (std::pair<int, int> f : fish[i + 1]) {
indices[i].push_back(f.first);
}
std::sort(indices[i].begin(), indices[i].end());
indices[i].erase(std::unique(indices[i].begin(), indices[i].end()), indices[i].end());
}
indices[N + 1] = {-1};
std::vector<long long> dpInc = {0LL}, dpDec = {0LL};
for (int i = 1; i <= N + 1; ++i) {
std::vector<long long> ndpInc(indices[i].size());
long long pref = 0LL;
int pidx = 0;
for (int j = 0; j < static_cast<int>(indices[i].size()); ++j) {
int col = indices[i][j];
while (pidx < static_cast<int>(indices[i - 1].size()) && indices[i - 1][pidx] <= col) {
pref = std::max(pref, dpInc[pidx] - getRowWeight(i - 1, indices[i - 1][pidx]));
++pidx;
}
ndpInc[j] = std::max(pref + getRowWeight(i - 1, col), dpDec[0]);
}
std::vector<long long> ndpDec(indices[i].size());
long long suff = 0;
int sidx = indices[i - 1].size();
for (int j = static_cast<int>(indices[i].size()) - 1; j >= 0; --j) {
int col = indices[i][j];
while (sidx > 0 && indices[i - 1][sidx - 1] >= col) {
--sidx;
suff = std::max(suff, std::max(dpInc[sidx], dpDec[sidx]) + getRowWeight(i, indices[i - 1][sidx]));
}
ndpDec[j] = suff - getRowWeight(i, col);
}
std::swap(dpInc, ndpInc);
std::swap(dpDec, ndpDec);
}
return std::max(dpInc[0], dpDec[0]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
15944 KB |
Output is correct |
2 |
Correct |
78 ms |
18580 KB |
Output is correct |
3 |
Correct |
25 ms |
11216 KB |
Output is correct |
4 |
Correct |
27 ms |
11220 KB |
Output is correct |
5 |
Correct |
210 ms |
31928 KB |
Output is correct |
6 |
Correct |
207 ms |
31500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
128 ms |
22420 KB |
Output is correct |
3 |
Correct |
157 ms |
25952 KB |
Output is correct |
4 |
Correct |
76 ms |
16032 KB |
Output is correct |
5 |
Correct |
76 ms |
18540 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
27 ms |
11220 KB |
Output is correct |
11 |
Correct |
29 ms |
11156 KB |
Output is correct |
12 |
Correct |
69 ms |
16376 KB |
Output is correct |
13 |
Correct |
84 ms |
19080 KB |
Output is correct |
14 |
Correct |
79 ms |
16216 KB |
Output is correct |
15 |
Correct |
84 ms |
18044 KB |
Output is correct |
16 |
Correct |
69 ms |
16188 KB |
Output is correct |
17 |
Correct |
80 ms |
17976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11244 KB |
Output is correct |
2 |
Correct |
25 ms |
11152 KB |
Output is correct |
3 |
Correct |
48 ms |
12896 KB |
Output is correct |
4 |
Correct |
44 ms |
12860 KB |
Output is correct |
5 |
Correct |
77 ms |
15720 KB |
Output is correct |
6 |
Correct |
66 ms |
16396 KB |
Output is correct |
7 |
Correct |
85 ms |
17080 KB |
Output is correct |
8 |
Correct |
82 ms |
16956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
1 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
26 ms |
3148 KB |
Output is correct |
18 |
Correct |
23 ms |
4020 KB |
Output is correct |
19 |
Correct |
26 ms |
3788 KB |
Output is correct |
20 |
Correct |
25 ms |
3860 KB |
Output is correct |
21 |
Correct |
19 ms |
3872 KB |
Output is correct |
22 |
Correct |
40 ms |
7324 KB |
Output is correct |
23 |
Correct |
5 ms |
852 KB |
Output is correct |
24 |
Correct |
19 ms |
2284 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
6 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
26 ms |
3148 KB |
Output is correct |
18 |
Correct |
23 ms |
4020 KB |
Output is correct |
19 |
Correct |
26 ms |
3788 KB |
Output is correct |
20 |
Correct |
25 ms |
3860 KB |
Output is correct |
21 |
Correct |
19 ms |
3872 KB |
Output is correct |
22 |
Correct |
40 ms |
7324 KB |
Output is correct |
23 |
Correct |
5 ms |
852 KB |
Output is correct |
24 |
Correct |
19 ms |
2284 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
6 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
126 ms |
16044 KB |
Output is correct |
29 |
Correct |
198 ms |
19976 KB |
Output is correct |
30 |
Correct |
194 ms |
18976 KB |
Output is correct |
31 |
Correct |
191 ms |
19020 KB |
Output is correct |
32 |
Correct |
161 ms |
21976 KB |
Output is correct |
33 |
Correct |
195 ms |
19184 KB |
Output is correct |
34 |
Correct |
191 ms |
18876 KB |
Output is correct |
35 |
Correct |
67 ms |
8908 KB |
Output is correct |
36 |
Correct |
198 ms |
21140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11244 KB |
Output is correct |
2 |
Correct |
25 ms |
11152 KB |
Output is correct |
3 |
Correct |
48 ms |
12896 KB |
Output is correct |
4 |
Correct |
44 ms |
12860 KB |
Output is correct |
5 |
Correct |
77 ms |
15720 KB |
Output is correct |
6 |
Correct |
66 ms |
16396 KB |
Output is correct |
7 |
Correct |
85 ms |
17080 KB |
Output is correct |
8 |
Correct |
82 ms |
16956 KB |
Output is correct |
9 |
Correct |
73 ms |
16332 KB |
Output is correct |
10 |
Correct |
59 ms |
12532 KB |
Output is correct |
11 |
Correct |
128 ms |
24856 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 |
27 ms |
11256 KB |
Output is correct |
19 |
Correct |
25 ms |
11224 KB |
Output is correct |
20 |
Correct |
24 ms |
11220 KB |
Output is correct |
21 |
Correct |
25 ms |
11176 KB |
Output is correct |
22 |
Correct |
88 ms |
16276 KB |
Output is correct |
23 |
Correct |
136 ms |
22984 KB |
Output is correct |
24 |
Correct |
149 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
15944 KB |
Output is correct |
2 |
Correct |
78 ms |
18580 KB |
Output is correct |
3 |
Correct |
25 ms |
11216 KB |
Output is correct |
4 |
Correct |
27 ms |
11220 KB |
Output is correct |
5 |
Correct |
210 ms |
31928 KB |
Output is correct |
6 |
Correct |
207 ms |
31500 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
128 ms |
22420 KB |
Output is correct |
9 |
Correct |
157 ms |
25952 KB |
Output is correct |
10 |
Correct |
76 ms |
16032 KB |
Output is correct |
11 |
Correct |
76 ms |
18540 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 |
27 ms |
11220 KB |
Output is correct |
17 |
Correct |
29 ms |
11156 KB |
Output is correct |
18 |
Correct |
69 ms |
16376 KB |
Output is correct |
19 |
Correct |
84 ms |
19080 KB |
Output is correct |
20 |
Correct |
79 ms |
16216 KB |
Output is correct |
21 |
Correct |
84 ms |
18044 KB |
Output is correct |
22 |
Correct |
69 ms |
16188 KB |
Output is correct |
23 |
Correct |
80 ms |
17976 KB |
Output is correct |
24 |
Correct |
26 ms |
11244 KB |
Output is correct |
25 |
Correct |
25 ms |
11152 KB |
Output is correct |
26 |
Correct |
48 ms |
12896 KB |
Output is correct |
27 |
Correct |
44 ms |
12860 KB |
Output is correct |
28 |
Correct |
77 ms |
15720 KB |
Output is correct |
29 |
Correct |
66 ms |
16396 KB |
Output is correct |
30 |
Correct |
85 ms |
17080 KB |
Output is correct |
31 |
Correct |
82 ms |
16956 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
26 ms |
3148 KB |
Output is correct |
49 |
Correct |
23 ms |
4020 KB |
Output is correct |
50 |
Correct |
26 ms |
3788 KB |
Output is correct |
51 |
Correct |
25 ms |
3860 KB |
Output is correct |
52 |
Correct |
19 ms |
3872 KB |
Output is correct |
53 |
Correct |
40 ms |
7324 KB |
Output is correct |
54 |
Correct |
5 ms |
852 KB |
Output is correct |
55 |
Correct |
19 ms |
2284 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
6 ms |
724 KB |
Output is correct |
58 |
Correct |
2 ms |
724 KB |
Output is correct |
59 |
Correct |
126 ms |
16044 KB |
Output is correct |
60 |
Correct |
198 ms |
19976 KB |
Output is correct |
61 |
Correct |
194 ms |
18976 KB |
Output is correct |
62 |
Correct |
191 ms |
19020 KB |
Output is correct |
63 |
Correct |
161 ms |
21976 KB |
Output is correct |
64 |
Correct |
195 ms |
19184 KB |
Output is correct |
65 |
Correct |
191 ms |
18876 KB |
Output is correct |
66 |
Correct |
67 ms |
8908 KB |
Output is correct |
67 |
Correct |
198 ms |
21140 KB |
Output is correct |
68 |
Correct |
73 ms |
16332 KB |
Output is correct |
69 |
Correct |
59 ms |
12532 KB |
Output is correct |
70 |
Correct |
128 ms |
24856 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 |
27 ms |
11256 KB |
Output is correct |
78 |
Correct |
25 ms |
11224 KB |
Output is correct |
79 |
Correct |
24 ms |
11220 KB |
Output is correct |
80 |
Correct |
25 ms |
11176 KB |
Output is correct |
81 |
Correct |
88 ms |
16276 KB |
Output is correct |
82 |
Correct |
136 ms |
22984 KB |
Output is correct |
83 |
Correct |
149 ms |
23800 KB |
Output is correct |
84 |
Correct |
247 ms |
27192 KB |
Output is correct |
85 |
Correct |
260 ms |
28304 KB |
Output is correct |
86 |
Correct |
222 ms |
27340 KB |
Output is correct |
87 |
Correct |
208 ms |
28000 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
214 ms |
28416 KB |
Output is correct |
90 |
Correct |
190 ms |
28180 KB |
Output is correct |
91 |
Correct |
165 ms |
28624 KB |
Output is correct |