#include "fish.h"
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e16;
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector<vector<pair<ll, ll>>> p(n);
vector<int> len[n];
for(int i = 0; i < m; ++i) {
p[x[i]].push_back({y[i], w[i]});
len[x[i]].push_back(y[i]);
}
for(int i = 0; i < n; ++i) {
sort(p[i].begin(), p[i].end());
ll sum = 0;
for(int j = 0; j < p[i].size(); ++j) {
sum += p[i][j].second;
p[i][j].second = sum;
}
p[i].insert(p[i].begin(), make_pair(-1, 0));
}
for(int i = 0; i < n; ++i) {
len[i].push_back(n);
len[i].push_back(0);
sort(len[i].begin(), len[i].end());
len[i].erase(unique(len[i].begin(), len[i].end()), len[i].end());
}
map<ll, ll> dp[n + 1][2];
for(int i = 0; i <= n; ++i) {
dp[0][0][i] = dp[0][1][i] = 0;
}
//0 - increasing, 1 - decreasing
for(int i = 1; i < n; ++i) {
vector<pair<ll, ll>> mx0, mx1;
for(ll lastlen: len[i - 1]) {
dp[i][0][0] = max({dp[i][0][0], dp[i - 1][0][lastlen], dp[i - 1][1][lastlen]});
dp[i][1][0] = max({dp[i][1][0], dp[i - 1][0][lastlen], dp[i - 1][1][lastlen]});
ll cur = 0;
if(!mx0.empty()) cur = mx0.back().second;
auto paiu = upper_bound(p[i - 1].begin(), p[i - 1].end(), make_pair(lastlen - 1, LLONG_MAX)); --paiu;
cur = max(cur, dp[i - 1][0][lastlen] - paiu->second);
mx0.push_back({lastlen, cur});
}
reverse(len[i - 1].begin(), len[i - 1].end());
for(ll lastlen: len[i - 1]) {
ll cur = 0;
if(!mx1.empty()) cur = mx1.back().second;
auto paiu = upper_bound(p[i].begin(), p[i].end(), make_pair(lastlen - 1, LLONG_MAX)); --paiu;
cur = max(cur, dp[i - 1][0][lastlen] + paiu->second);
cur = max(cur, dp[i - 1][1][lastlen] + paiu->second);
mx1.push_back({lastlen, cur});
}
reverse(mx1.begin(), mx1.end());
for(ll curlen: len[i]) {
auto it1 = upper_bound(mx0.begin(), mx0.end(), make_pair(curlen, LLONG_MAX));
assert(it1 != mx0.begin()); --it1;
auto it2 = lower_bound(mx1.begin(), mx1.end(), make_pair(curlen, LLONG_MIN));
assert(it2 != mx1.end());
auto paiu = upper_bound(p[i - 1].begin(), p[i - 1].end(), make_pair(curlen - 1, LLONG_MAX)); --paiu;
dp[i][0][curlen] = max(dp[i][0][curlen], paiu->second + it1->second);
auto paiu2 = upper_bound(p[i].begin(), p[i].end(), make_pair(curlen - 1, LLONG_MAX)); --paiu2;
dp[i][1][curlen] = max(dp[i][1][curlen], it2->second - paiu2->second);
}
}
ll ans = 0;
for(int i = 0; i <= n; ++i) {
ans = max({ans, dp[n - 1][0][i], dp[n - 1][1][i]});
}
return ans;
}
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:17:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int j = 0; j < p[i].size(); ++j) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
68440 KB |
Output is correct |
2 |
Correct |
299 ms |
76464 KB |
Output is correct |
3 |
Correct |
187 ms |
70728 KB |
Output is correct |
4 |
Correct |
176 ms |
70620 KB |
Output is correct |
5 |
Correct |
810 ms |
127552 KB |
Output is correct |
6 |
Correct |
529 ms |
129968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
491 ms |
83552 KB |
Output is correct |
3 |
Correct |
523 ms |
95108 KB |
Output is correct |
4 |
Correct |
258 ms |
68300 KB |
Output is correct |
5 |
Correct |
333 ms |
76588 KB |
Output is correct |
6 |
Correct |
1 ms |
236 KB |
Output is correct |
7 |
Correct |
1 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 |
196 ms |
70640 KB |
Output is correct |
11 |
Correct |
192 ms |
70728 KB |
Output is correct |
12 |
Correct |
362 ms |
78460 KB |
Output is correct |
13 |
Correct |
414 ms |
88952 KB |
Output is correct |
14 |
Correct |
325 ms |
73436 KB |
Output is correct |
15 |
Correct |
346 ms |
81608 KB |
Output is correct |
16 |
Correct |
313 ms |
73444 KB |
Output is correct |
17 |
Correct |
332 ms |
81508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
70684 KB |
Output is correct |
2 |
Correct |
205 ms |
70688 KB |
Output is correct |
3 |
Correct |
270 ms |
66416 KB |
Output is correct |
4 |
Correct |
218 ms |
72416 KB |
Output is correct |
5 |
Correct |
296 ms |
77728 KB |
Output is correct |
6 |
Correct |
280 ms |
78592 KB |
Output is correct |
7 |
Correct |
286 ms |
79088 KB |
Output is correct |
8 |
Correct |
272 ms |
79092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
564 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
56 ms |
8120 KB |
Output is correct |
18 |
Correct |
59 ms |
8772 KB |
Output is correct |
19 |
Correct |
55 ms |
8608 KB |
Output is correct |
20 |
Correct |
51 ms |
8572 KB |
Output is correct |
21 |
Correct |
46 ms |
8504 KB |
Output is correct |
22 |
Correct |
101 ms |
16740 KB |
Output is correct |
23 |
Correct |
9 ms |
2004 KB |
Output is correct |
24 |
Correct |
35 ms |
5588 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
9 ms |
1928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
564 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
56 ms |
8120 KB |
Output is correct |
18 |
Correct |
59 ms |
8772 KB |
Output is correct |
19 |
Correct |
55 ms |
8608 KB |
Output is correct |
20 |
Correct |
51 ms |
8572 KB |
Output is correct |
21 |
Correct |
46 ms |
8504 KB |
Output is correct |
22 |
Correct |
101 ms |
16740 KB |
Output is correct |
23 |
Correct |
9 ms |
2004 KB |
Output is correct |
24 |
Correct |
35 ms |
5588 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
9 ms |
1928 KB |
Output is correct |
27 |
Correct |
6 ms |
2900 KB |
Output is correct |
28 |
Correct |
320 ms |
39356 KB |
Output is correct |
29 |
Correct |
429 ms |
55128 KB |
Output is correct |
30 |
Correct |
342 ms |
54484 KB |
Output is correct |
31 |
Correct |
330 ms |
54552 KB |
Output is correct |
32 |
Correct |
383 ms |
54500 KB |
Output is correct |
33 |
Correct |
333 ms |
54492 KB |
Output is correct |
34 |
Correct |
354 ms |
54512 KB |
Output is correct |
35 |
Correct |
124 ms |
22820 KB |
Output is correct |
36 |
Correct |
390 ms |
56396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
70684 KB |
Output is correct |
2 |
Correct |
205 ms |
70688 KB |
Output is correct |
3 |
Correct |
270 ms |
66416 KB |
Output is correct |
4 |
Correct |
218 ms |
72416 KB |
Output is correct |
5 |
Correct |
296 ms |
77728 KB |
Output is correct |
6 |
Correct |
280 ms |
78592 KB |
Output is correct |
7 |
Correct |
286 ms |
79088 KB |
Output is correct |
8 |
Correct |
272 ms |
79092 KB |
Output is correct |
9 |
Correct |
277 ms |
90976 KB |
Output is correct |
10 |
Correct |
166 ms |
48416 KB |
Output is correct |
11 |
Correct |
346 ms |
97084 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
177 ms |
70632 KB |
Output is correct |
19 |
Correct |
170 ms |
70728 KB |
Output is correct |
20 |
Correct |
185 ms |
70680 KB |
Output is correct |
21 |
Correct |
185 ms |
70716 KB |
Output is correct |
22 |
Correct |
288 ms |
89084 KB |
Output is correct |
23 |
Correct |
375 ms |
107132 KB |
Output is correct |
24 |
Correct |
387 ms |
109820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
68440 KB |
Output is correct |
2 |
Correct |
299 ms |
76464 KB |
Output is correct |
3 |
Correct |
187 ms |
70728 KB |
Output is correct |
4 |
Correct |
176 ms |
70620 KB |
Output is correct |
5 |
Correct |
810 ms |
127552 KB |
Output is correct |
6 |
Correct |
529 ms |
129968 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
491 ms |
83552 KB |
Output is correct |
9 |
Correct |
523 ms |
95108 KB |
Output is correct |
10 |
Correct |
258 ms |
68300 KB |
Output is correct |
11 |
Correct |
333 ms |
76588 KB |
Output is correct |
12 |
Correct |
1 ms |
236 KB |
Output is correct |
13 |
Correct |
1 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 |
196 ms |
70640 KB |
Output is correct |
17 |
Correct |
192 ms |
70728 KB |
Output is correct |
18 |
Correct |
362 ms |
78460 KB |
Output is correct |
19 |
Correct |
414 ms |
88952 KB |
Output is correct |
20 |
Correct |
325 ms |
73436 KB |
Output is correct |
21 |
Correct |
346 ms |
81608 KB |
Output is correct |
22 |
Correct |
313 ms |
73444 KB |
Output is correct |
23 |
Correct |
332 ms |
81508 KB |
Output is correct |
24 |
Correct |
243 ms |
70684 KB |
Output is correct |
25 |
Correct |
205 ms |
70688 KB |
Output is correct |
26 |
Correct |
270 ms |
66416 KB |
Output is correct |
27 |
Correct |
218 ms |
72416 KB |
Output is correct |
28 |
Correct |
296 ms |
77728 KB |
Output is correct |
29 |
Correct |
280 ms |
78592 KB |
Output is correct |
30 |
Correct |
286 ms |
79088 KB |
Output is correct |
31 |
Correct |
272 ms |
79092 KB |
Output is correct |
32 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
564 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
724 KB |
Output is correct |
48 |
Correct |
56 ms |
8120 KB |
Output is correct |
49 |
Correct |
59 ms |
8772 KB |
Output is correct |
50 |
Correct |
55 ms |
8608 KB |
Output is correct |
51 |
Correct |
51 ms |
8572 KB |
Output is correct |
52 |
Correct |
46 ms |
8504 KB |
Output is correct |
53 |
Correct |
101 ms |
16740 KB |
Output is correct |
54 |
Correct |
9 ms |
2004 KB |
Output is correct |
55 |
Correct |
35 ms |
5588 KB |
Output is correct |
56 |
Correct |
2 ms |
596 KB |
Output is correct |
57 |
Correct |
9 ms |
1928 KB |
Output is correct |
58 |
Correct |
6 ms |
2900 KB |
Output is correct |
59 |
Correct |
320 ms |
39356 KB |
Output is correct |
60 |
Correct |
429 ms |
55128 KB |
Output is correct |
61 |
Correct |
342 ms |
54484 KB |
Output is correct |
62 |
Correct |
330 ms |
54552 KB |
Output is correct |
63 |
Correct |
383 ms |
54500 KB |
Output is correct |
64 |
Correct |
333 ms |
54492 KB |
Output is correct |
65 |
Correct |
354 ms |
54512 KB |
Output is correct |
66 |
Correct |
124 ms |
22820 KB |
Output is correct |
67 |
Correct |
390 ms |
56396 KB |
Output is correct |
68 |
Correct |
277 ms |
90976 KB |
Output is correct |
69 |
Correct |
166 ms |
48416 KB |
Output is correct |
70 |
Correct |
346 ms |
97084 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
304 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
177 ms |
70632 KB |
Output is correct |
78 |
Correct |
170 ms |
70728 KB |
Output is correct |
79 |
Correct |
185 ms |
70680 KB |
Output is correct |
80 |
Correct |
185 ms |
70716 KB |
Output is correct |
81 |
Correct |
288 ms |
89084 KB |
Output is correct |
82 |
Correct |
375 ms |
107132 KB |
Output is correct |
83 |
Correct |
387 ms |
109820 KB |
Output is correct |
84 |
Correct |
714 ms |
119456 KB |
Output is correct |
85 |
Correct |
737 ms |
125012 KB |
Output is correct |
86 |
Correct |
464 ms |
118160 KB |
Output is correct |
87 |
Correct |
545 ms |
124108 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
534 ms |
125028 KB |
Output is correct |
90 |
Correct |
454 ms |
121756 KB |
Output is correct |
91 |
Correct |
477 ms |
121676 KB |
Output is correct |