#include <bits/stdc++.h>
using namespace std;
constexpr int kUp = 0;
constexpr int kDown = 1;
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<pair<int, int>>> fishes(N);
for (int i = 0; i < M; ++i) {
fishes[X[i]].push_back(make_pair(Y[i], i));
}
long long largest = 0;
vector<pair<int, long long>> up, down;
vector<vector<long long>> dp(M, vector<long long>(2, LLONG_MIN / 2));
for (int x = 0; x < N; ++x) {
sort(fishes[x].begin(), fishes[x].end());
if (x > 0) {
long long max_down = 0;
for (int j = (int)fishes[x].size() - 1; j >= 0; --j) {
int y = fishes[x][j].first;
int i = fishes[x][j].second;
while (down.size() > 0 && down.back().first > y) {
max_down = max(max_down, down.back().second);
down.pop_back();
}
long long &ret = dp[i][kDown];
ret = largest + W[i];
ret = max(ret, max_down + W[i]);
if (j + 1 < static_cast<int>(fishes[x].size())) {
ret = max(ret, dp[fishes[x][j + 1].second][kDown] + W[i]);
}
}
}
if (x >= 1) {
for (auto [y, i] : fishes[x - 1]) {
largest = max(largest, dp[i][kDown]);
}
}
long long max_up = 0;
for (int j = 0; j < static_cast<int>(fishes[x].size()); ++j) {
int y = fishes[x][j].first;
int i = fishes[x][j].second;
while (up.size() > 0 && up.back().first < y) {
max_up = max(max_up, up.back().second);
up.pop_back();
}
long long &ret = dp[i][kUp];
ret = largest + W[i];
ret = max(ret, max_up + W[i]);
if (j > 0) {
ret = max(ret, dp[fishes[x][j - 1].second][kUp] + W[i]);
}
}
if (x >= 1) {
for (auto [y, i] : fishes[x - 1]) {
largest = max(largest, dp[i][kUp]);
}
}
down.clear();
for (int j = 0; j < static_cast<int>(fishes[x].size()); ++j) {
down.push_back(make_pair(fishes[x][j].first, dp[fishes[x][j].second][kDown]));
}
up.clear();
for (int j = static_cast<int>(fishes[x].size()) - 1; j >= 0; --j) {
up.push_back(make_pair(fishes[x][j].first, dp[fishes[x][j].second][kUp]));
}
}
long long answer = 0;
for (int i = 0; i < M; ++i) {
answer = max(answer, dp[i][kDown]);
if (X[i] + 1 < N) {
answer = max(answer, dp[i][kUp]);
}
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
15060 KB |
Output is correct |
2 |
Correct |
42 ms |
17776 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
143 ms |
39268 KB |
Output is correct |
6 |
Correct |
131 ms |
36952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
78 ms |
23656 KB |
Output is correct |
3 |
Correct |
86 ms |
27968 KB |
Output is correct |
4 |
Correct |
36 ms |
15168 KB |
Output is correct |
5 |
Correct |
42 ms |
17680 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
42 ms |
15688 KB |
Output is correct |
13 |
Correct |
43 ms |
17644 KB |
Output is correct |
14 |
Correct |
33 ms |
13028 KB |
Output is correct |
15 |
Correct |
37 ms |
14400 KB |
Output is correct |
16 |
Correct |
34 ms |
13136 KB |
Output is correct |
17 |
Correct |
37 ms |
14152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
22 ms |
9308 KB |
Output is correct |
4 |
Correct |
15 ms |
7260 KB |
Output is correct |
5 |
Correct |
44 ms |
15196 KB |
Output is correct |
6 |
Correct |
39 ms |
14676 KB |
Output is correct |
7 |
Correct |
42 ms |
15188 KB |
Output is correct |
8 |
Correct |
41 ms |
15184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
17 ms |
5188 KB |
Output is correct |
18 |
Correct |
16 ms |
5212 KB |
Output is correct |
19 |
Correct |
17 ms |
5212 KB |
Output is correct |
20 |
Correct |
16 ms |
5176 KB |
Output is correct |
21 |
Correct |
16 ms |
5212 KB |
Output is correct |
22 |
Correct |
33 ms |
10168 KB |
Output is correct |
23 |
Correct |
4 ms |
1372 KB |
Output is correct |
24 |
Correct |
11 ms |
3420 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
17 ms |
5188 KB |
Output is correct |
18 |
Correct |
16 ms |
5212 KB |
Output is correct |
19 |
Correct |
17 ms |
5212 KB |
Output is correct |
20 |
Correct |
16 ms |
5176 KB |
Output is correct |
21 |
Correct |
16 ms |
5212 KB |
Output is correct |
22 |
Correct |
33 ms |
10168 KB |
Output is correct |
23 |
Correct |
4 ms |
1372 KB |
Output is correct |
24 |
Correct |
11 ms |
3420 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
83 ms |
24040 KB |
Output is correct |
29 |
Correct |
128 ms |
33132 KB |
Output is correct |
30 |
Correct |
117 ms |
32596 KB |
Output is correct |
31 |
Correct |
118 ms |
32600 KB |
Output is correct |
32 |
Correct |
136 ms |
33364 KB |
Output is correct |
33 |
Correct |
116 ms |
32604 KB |
Output is correct |
34 |
Correct |
116 ms |
32612 KB |
Output is correct |
35 |
Correct |
43 ms |
12876 KB |
Output is correct |
36 |
Correct |
116 ms |
33120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
22 ms |
9308 KB |
Output is correct |
4 |
Correct |
15 ms |
7260 KB |
Output is correct |
5 |
Correct |
44 ms |
15196 KB |
Output is correct |
6 |
Correct |
39 ms |
14676 KB |
Output is correct |
7 |
Correct |
42 ms |
15188 KB |
Output is correct |
8 |
Correct |
41 ms |
15184 KB |
Output is correct |
9 |
Correct |
38 ms |
14928 KB |
Output is correct |
10 |
Correct |
38 ms |
12472 KB |
Output is correct |
11 |
Correct |
85 ms |
24788 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
41 ms |
14656 KB |
Output is correct |
23 |
Correct |
85 ms |
25048 KB |
Output is correct |
24 |
Correct |
92 ms |
25424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
15060 KB |
Output is correct |
2 |
Correct |
42 ms |
17776 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
143 ms |
39268 KB |
Output is correct |
6 |
Correct |
131 ms |
36952 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
78 ms |
23656 KB |
Output is correct |
9 |
Correct |
86 ms |
27968 KB |
Output is correct |
10 |
Correct |
36 ms |
15168 KB |
Output is correct |
11 |
Correct |
42 ms |
17680 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
42 ms |
15688 KB |
Output is correct |
19 |
Correct |
43 ms |
17644 KB |
Output is correct |
20 |
Correct |
33 ms |
13028 KB |
Output is correct |
21 |
Correct |
37 ms |
14400 KB |
Output is correct |
22 |
Correct |
34 ms |
13136 KB |
Output is correct |
23 |
Correct |
37 ms |
14152 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
22 ms |
9308 KB |
Output is correct |
27 |
Correct |
15 ms |
7260 KB |
Output is correct |
28 |
Correct |
44 ms |
15196 KB |
Output is correct |
29 |
Correct |
39 ms |
14676 KB |
Output is correct |
30 |
Correct |
42 ms |
15188 KB |
Output is correct |
31 |
Correct |
41 ms |
15184 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
448 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
17 ms |
5188 KB |
Output is correct |
49 |
Correct |
16 ms |
5212 KB |
Output is correct |
50 |
Correct |
17 ms |
5212 KB |
Output is correct |
51 |
Correct |
16 ms |
5176 KB |
Output is correct |
52 |
Correct |
16 ms |
5212 KB |
Output is correct |
53 |
Correct |
33 ms |
10168 KB |
Output is correct |
54 |
Correct |
4 ms |
1372 KB |
Output is correct |
55 |
Correct |
11 ms |
3420 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
1116 KB |
Output is correct |
58 |
Correct |
1 ms |
860 KB |
Output is correct |
59 |
Correct |
83 ms |
24040 KB |
Output is correct |
60 |
Correct |
128 ms |
33132 KB |
Output is correct |
61 |
Correct |
117 ms |
32596 KB |
Output is correct |
62 |
Correct |
118 ms |
32600 KB |
Output is correct |
63 |
Correct |
136 ms |
33364 KB |
Output is correct |
64 |
Correct |
116 ms |
32604 KB |
Output is correct |
65 |
Correct |
116 ms |
32612 KB |
Output is correct |
66 |
Correct |
43 ms |
12876 KB |
Output is correct |
67 |
Correct |
116 ms |
33120 KB |
Output is correct |
68 |
Correct |
38 ms |
14928 KB |
Output is correct |
69 |
Correct |
38 ms |
12472 KB |
Output is correct |
70 |
Correct |
85 ms |
24788 KB |
Output is correct |
71 |
Correct |
1 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
436 KB |
Output is correct |
77 |
Correct |
2 ms |
2652 KB |
Output is correct |
78 |
Correct |
2 ms |
2652 KB |
Output is correct |
79 |
Correct |
2 ms |
2652 KB |
Output is correct |
80 |
Correct |
2 ms |
2652 KB |
Output is correct |
81 |
Correct |
41 ms |
14656 KB |
Output is correct |
82 |
Correct |
85 ms |
25048 KB |
Output is correct |
83 |
Correct |
92 ms |
25424 KB |
Output is correct |
84 |
Correct |
137 ms |
35764 KB |
Output is correct |
85 |
Correct |
128 ms |
35312 KB |
Output is correct |
86 |
Correct |
138 ms |
36432 KB |
Output is correct |
87 |
Correct |
137 ms |
36944 KB |
Output is correct |
88 |
Correct |
0 ms |
344 KB |
Output is correct |
89 |
Correct |
142 ms |
37080 KB |
Output is correct |
90 |
Correct |
128 ms |
35412 KB |
Output is correct |
91 |
Correct |
123 ms |
35152 KB |
Output is correct |