#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 (int ii=0;ii<fishes[x - 1].size();ii++){
pair<int, int> tmp=fishes[x - 1][ii];
largest = max(largest, dp[tmp.second][kDown]);
}
long long max_up = 0;
for (int j = 0; j < (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 (int ii=0;ii<fishes[x - 1].size();ii++){
pair<int, int> tmp=fishes[x - 1][ii];
largest = max(largest, dp[tmp.second][kUp]);
}
down.clear();
for (int j = 0; j < (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 = (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;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int ii=0;ii<fishes[x - 1].size();ii++){
| ~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int ii=0;ii<fishes[x - 1].size();ii++){
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
15036 KB |
Output is correct |
2 |
Correct |
57 ms |
17456 KB |
Output is correct |
3 |
Correct |
2 ms |
2604 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
216 ms |
38908 KB |
Output is correct |
6 |
Correct |
180 ms |
36576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
111 ms |
23204 KB |
Output is correct |
3 |
Correct |
139 ms |
27704 KB |
Output is correct |
4 |
Correct |
49 ms |
14768 KB |
Output is correct |
5 |
Correct |
58 ms |
17316 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
53 ms |
14868 KB |
Output is correct |
13 |
Correct |
71 ms |
17352 KB |
Output is correct |
14 |
Correct |
49 ms |
12608 KB |
Output is correct |
15 |
Correct |
53 ms |
13948 KB |
Output is correct |
16 |
Correct |
50 ms |
12760 KB |
Output is correct |
17 |
Correct |
60 ms |
13888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
33 ms |
8964 KB |
Output is correct |
4 |
Correct |
20 ms |
6732 KB |
Output is correct |
5 |
Correct |
58 ms |
14188 KB |
Output is correct |
6 |
Correct |
62 ms |
14324 KB |
Output is correct |
7 |
Correct |
64 ms |
14824 KB |
Output is correct |
8 |
Correct |
66 ms |
15056 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 |
1 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 |
1 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
1 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 |
1 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
468 KB |
Output is correct |
17 |
Correct |
28 ms |
4840 KB |
Output is correct |
18 |
Correct |
22 ms |
5172 KB |
Output is correct |
19 |
Correct |
20 ms |
5076 KB |
Output is correct |
20 |
Correct |
20 ms |
5068 KB |
Output is correct |
21 |
Correct |
21 ms |
4940 KB |
Output is correct |
22 |
Correct |
44 ms |
9980 KB |
Output is correct |
23 |
Correct |
5 ms |
1108 KB |
Output is correct |
24 |
Correct |
14 ms |
3248 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
5 ms |
1052 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 |
1 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 |
1 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
468 KB |
Output is correct |
17 |
Correct |
28 ms |
4840 KB |
Output is correct |
18 |
Correct |
22 ms |
5172 KB |
Output is correct |
19 |
Correct |
20 ms |
5076 KB |
Output is correct |
20 |
Correct |
20 ms |
5068 KB |
Output is correct |
21 |
Correct |
21 ms |
4940 KB |
Output is correct |
22 |
Correct |
44 ms |
9980 KB |
Output is correct |
23 |
Correct |
5 ms |
1108 KB |
Output is correct |
24 |
Correct |
14 ms |
3248 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
5 ms |
1052 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
112 ms |
23776 KB |
Output is correct |
29 |
Correct |
176 ms |
32916 KB |
Output is correct |
30 |
Correct |
170 ms |
32360 KB |
Output is correct |
31 |
Correct |
169 ms |
32340 KB |
Output is correct |
32 |
Correct |
162 ms |
33096 KB |
Output is correct |
33 |
Correct |
163 ms |
32392 KB |
Output is correct |
34 |
Correct |
153 ms |
31696 KB |
Output is correct |
35 |
Correct |
56 ms |
12272 KB |
Output is correct |
36 |
Correct |
166 ms |
31068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
33 ms |
8964 KB |
Output is correct |
4 |
Correct |
20 ms |
6732 KB |
Output is correct |
5 |
Correct |
58 ms |
14188 KB |
Output is correct |
6 |
Correct |
62 ms |
14324 KB |
Output is correct |
7 |
Correct |
64 ms |
14824 KB |
Output is correct |
8 |
Correct |
66 ms |
15056 KB |
Output is correct |
9 |
Correct |
70 ms |
14308 KB |
Output is correct |
10 |
Correct |
56 ms |
11928 KB |
Output is correct |
11 |
Correct |
125 ms |
24124 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2600 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
63 ms |
14112 KB |
Output is correct |
23 |
Correct |
119 ms |
22436 KB |
Output is correct |
24 |
Correct |
130 ms |
24076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
15036 KB |
Output is correct |
2 |
Correct |
57 ms |
17456 KB |
Output is correct |
3 |
Correct |
2 ms |
2604 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
216 ms |
38908 KB |
Output is correct |
6 |
Correct |
180 ms |
36576 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
111 ms |
23204 KB |
Output is correct |
9 |
Correct |
139 ms |
27704 KB |
Output is correct |
10 |
Correct |
49 ms |
14768 KB |
Output is correct |
11 |
Correct |
58 ms |
17316 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
53 ms |
14868 KB |
Output is correct |
19 |
Correct |
71 ms |
17352 KB |
Output is correct |
20 |
Correct |
49 ms |
12608 KB |
Output is correct |
21 |
Correct |
53 ms |
13948 KB |
Output is correct |
22 |
Correct |
50 ms |
12760 KB |
Output is correct |
23 |
Correct |
60 ms |
13888 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2644 KB |
Output is correct |
26 |
Correct |
33 ms |
8964 KB |
Output is correct |
27 |
Correct |
20 ms |
6732 KB |
Output is correct |
28 |
Correct |
58 ms |
14188 KB |
Output is correct |
29 |
Correct |
62 ms |
14324 KB |
Output is correct |
30 |
Correct |
64 ms |
14824 KB |
Output is correct |
31 |
Correct |
66 ms |
15056 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 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 |
1 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 |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 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 |
468 KB |
Output is correct |
48 |
Correct |
28 ms |
4840 KB |
Output is correct |
49 |
Correct |
22 ms |
5172 KB |
Output is correct |
50 |
Correct |
20 ms |
5076 KB |
Output is correct |
51 |
Correct |
20 ms |
5068 KB |
Output is correct |
52 |
Correct |
21 ms |
4940 KB |
Output is correct |
53 |
Correct |
44 ms |
9980 KB |
Output is correct |
54 |
Correct |
5 ms |
1108 KB |
Output is correct |
55 |
Correct |
14 ms |
3248 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
5 ms |
1052 KB |
Output is correct |
58 |
Correct |
2 ms |
596 KB |
Output is correct |
59 |
Correct |
112 ms |
23776 KB |
Output is correct |
60 |
Correct |
176 ms |
32916 KB |
Output is correct |
61 |
Correct |
170 ms |
32360 KB |
Output is correct |
62 |
Correct |
169 ms |
32340 KB |
Output is correct |
63 |
Correct |
162 ms |
33096 KB |
Output is correct |
64 |
Correct |
163 ms |
32392 KB |
Output is correct |
65 |
Correct |
153 ms |
31696 KB |
Output is correct |
66 |
Correct |
56 ms |
12272 KB |
Output is correct |
67 |
Correct |
166 ms |
31068 KB |
Output is correct |
68 |
Correct |
70 ms |
14308 KB |
Output is correct |
69 |
Correct |
56 ms |
11928 KB |
Output is correct |
70 |
Correct |
125 ms |
24124 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
300 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
3 ms |
2644 KB |
Output is correct |
78 |
Correct |
2 ms |
2644 KB |
Output is correct |
79 |
Correct |
2 ms |
2600 KB |
Output is correct |
80 |
Correct |
2 ms |
2644 KB |
Output is correct |
81 |
Correct |
63 ms |
14112 KB |
Output is correct |
82 |
Correct |
119 ms |
22436 KB |
Output is correct |
83 |
Correct |
130 ms |
24076 KB |
Output is correct |
84 |
Correct |
194 ms |
34344 KB |
Output is correct |
85 |
Correct |
233 ms |
33180 KB |
Output is correct |
86 |
Correct |
184 ms |
34072 KB |
Output is correct |
87 |
Correct |
229 ms |
33540 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
210 ms |
34400 KB |
Output is correct |
90 |
Correct |
176 ms |
31820 KB |
Output is correct |
91 |
Correct |
182 ms |
31680 KB |
Output is correct |