#include "fish.h"
#include <bits/stdc++.h>
#include <vector>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const int NMAX = 1e5 + 5;
vector <vector <ll> > DP(NMAX);
vector <vector <ll> > dp(NMAX);
vector <vector <ll> > p(NMAX);
vector <vector <ll> > pmax(NMAX);
vector <vector <ll> > smax(NMAX);
vector <vector <pll> > vec(NMAX);
vector <vector <int> > vec1(NMAX);
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,std::vector<int> W) {
int n = N, m = M;
vector <int> x = X, y = Y, w = W;
for(int i = 0; i < m; i++){
vec[x[i] + 1].pb({y[i] + 1, w[i]});
vec1[x[i] + 1].pb(y[i] + 1);
}
for(int i = 1; i <= n; i++){
vec[i].pb({0, 0});
vec[i].pb({n + 1, 0});
vec1[i].pb(0);
vec1[i].pb(n + 1);
sort(vec1[i].begin(), vec1[i].end());
sort(vec[i].begin(), vec[i].end());
dp[i].resize(vec[i].size());
DP[i].resize(vec[i].size());
p[i].resize(vec[i].size());
pmax[i].resize(vec[i].size());
smax[i].resize(vec[i].size());
p[i][0] = 0;
for(int j = 1; j < vec[i].size(); j++) p[i][j] = p[i][j - 1] + vec[i][j].s;
}
ll ans = 0;
for(int i = 1; i <= n; i++){
int len = vec[i].size();
if(i != 1){
for(int j = 0; j < len; j++){
int x = lower_bound(vec1[i - 1].begin(), vec1[i - 1].end(), vec1[i][j]) - vec1[i - 1].begin();
x--;
if(x > 0)DP[i][j] = pmax[i - 1][x] + p[i - 1][x];
ans = max(ans, DP[i][j]);
}
DP[i][0] = max(DP[i][0], dp[i - 1][0]);
ans = max(ans, dp[i - 1][0]);
DP[i].back() = max(DP[i].back(), DP[i - 1].back());
ans = max(ans, DP[i - 1].back());
for(int j = 0; j < len - 1; j++){
int x = upper_bound(vec1[i - 1].begin(), vec1[i - 1].end(), vec1[i][j]) - vec1[i -1].begin();
if(j > 0)dp[i][j] = smax[i - 1][x] - p[i][j - 1];
else dp[i][j] = smax[i - 1][x];
ans = max(ans, dp[i][j]);
}
dp[i].back() = max(DP[i - 1].back(), dp[i - 1].back());
dp[i].back() = max(dp[i].back(), dp[i - 1][0]);
}
pmax[i][0] = DP[i][0];
for(int j= 1; j < len; j++)
pmax[i][j] = max(DP[i][j] - p[i][j - 1], pmax[i][j - 1]);
smax[i].back() = max(dp[i].back(), DP[i].back());
if(!vec[i + 1].empty()) {
int x = lower_bound(vec1[i + 1].begin(), vec1[i + 1].end(), vec1[i].back()) - vec1[i + 1].begin();
x--;
smax[i].back() += p[i + 1][x];
}
for(int j = len - 2; j >= 0; j--){
smax[i][j] = max(dp[i][j], DP[i][j]);
int x = lower_bound(vec1[i + 1].begin(), vec1[i + 1].end(), vec1[i][j]) - vec1[i + 1].begin();
x--;
if(x > 0){
smax[i][j] += p[i + 1][x];
}
smax[i][j] = max(smax[i][j], smax[i][j +1]);
}
}
/*for(int i = 1; i <= n; i++){
for(int to : dp[i]) cout << to << ' ';
cout << "\n";
}
for(int i= 1; i <= n; i++){
for(int to : DP[i]) cout << to << ' ';
cout << "\n";
}*/
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:44: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]
44 | for(int j = 1; j < vec[i].size(); j++) p[i][j] = p[i][j - 1] + vec[i][j].s;
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
45372 KB |
Output is correct |
2 |
Correct |
128 ms |
49620 KB |
Output is correct |
3 |
Correct |
64 ms |
40152 KB |
Output is correct |
4 |
Correct |
64 ms |
40104 KB |
Output is correct |
5 |
Correct |
212 ms |
68596 KB |
Output is correct |
6 |
Correct |
316 ms |
69992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
155 ms |
53248 KB |
Output is correct |
3 |
Correct |
186 ms |
58980 KB |
Output is correct |
4 |
Correct |
112 ms |
45372 KB |
Output is correct |
5 |
Correct |
126 ms |
49552 KB |
Output is correct |
6 |
Correct |
8 ms |
16624 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
8 ms |
16724 KB |
Output is correct |
10 |
Correct |
68 ms |
40148 KB |
Output is correct |
11 |
Correct |
67 ms |
40224 KB |
Output is correct |
12 |
Correct |
104 ms |
45396 KB |
Output is correct |
13 |
Correct |
138 ms |
49592 KB |
Output is correct |
14 |
Correct |
111 ms |
45524 KB |
Output is correct |
15 |
Correct |
122 ms |
48692 KB |
Output is correct |
16 |
Correct |
113 ms |
45496 KB |
Output is correct |
17 |
Correct |
116 ms |
48616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
40120 KB |
Output is correct |
2 |
Correct |
69 ms |
40204 KB |
Output is correct |
3 |
Correct |
110 ms |
41324 KB |
Output is correct |
4 |
Correct |
89 ms |
42656 KB |
Output is correct |
5 |
Correct |
127 ms |
46836 KB |
Output is correct |
6 |
Correct |
124 ms |
47552 KB |
Output is correct |
7 |
Correct |
130 ms |
48152 KB |
Output is correct |
8 |
Correct |
133 ms |
48224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16652 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16656 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
8 ms |
16852 KB |
Output is correct |
10 |
Correct |
9 ms |
16980 KB |
Output is correct |
11 |
Correct |
8 ms |
16852 KB |
Output is correct |
12 |
Correct |
10 ms |
16852 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16652 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16656 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
8 ms |
16852 KB |
Output is correct |
10 |
Correct |
9 ms |
16980 KB |
Output is correct |
11 |
Correct |
8 ms |
16852 KB |
Output is correct |
12 |
Correct |
10 ms |
16852 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16852 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16980 KB |
Output is correct |
17 |
Correct |
29 ms |
21860 KB |
Output is correct |
18 |
Correct |
29 ms |
22512 KB |
Output is correct |
19 |
Correct |
28 ms |
22376 KB |
Output is correct |
20 |
Correct |
32 ms |
22360 KB |
Output is correct |
21 |
Correct |
27 ms |
22360 KB |
Output is correct |
22 |
Correct |
50 ms |
28120 KB |
Output is correct |
23 |
Correct |
13 ms |
17772 KB |
Output is correct |
24 |
Correct |
28 ms |
20168 KB |
Output is correct |
25 |
Correct |
9 ms |
16852 KB |
Output is correct |
26 |
Correct |
13 ms |
17624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16652 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16656 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
8 ms |
16852 KB |
Output is correct |
10 |
Correct |
9 ms |
16980 KB |
Output is correct |
11 |
Correct |
8 ms |
16852 KB |
Output is correct |
12 |
Correct |
10 ms |
16852 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16852 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16980 KB |
Output is correct |
17 |
Correct |
29 ms |
21860 KB |
Output is correct |
18 |
Correct |
29 ms |
22512 KB |
Output is correct |
19 |
Correct |
28 ms |
22376 KB |
Output is correct |
20 |
Correct |
32 ms |
22360 KB |
Output is correct |
21 |
Correct |
27 ms |
22360 KB |
Output is correct |
22 |
Correct |
50 ms |
28120 KB |
Output is correct |
23 |
Correct |
13 ms |
17772 KB |
Output is correct |
24 |
Correct |
28 ms |
20168 KB |
Output is correct |
25 |
Correct |
9 ms |
16852 KB |
Output is correct |
26 |
Correct |
13 ms |
17624 KB |
Output is correct |
27 |
Correct |
11 ms |
17644 KB |
Output is correct |
28 |
Correct |
111 ms |
43508 KB |
Output is correct |
29 |
Correct |
159 ms |
53044 KB |
Output is correct |
30 |
Correct |
171 ms |
52952 KB |
Output is correct |
31 |
Correct |
153 ms |
53020 KB |
Output is correct |
32 |
Correct |
149 ms |
54532 KB |
Output is correct |
33 |
Correct |
149 ms |
52968 KB |
Output is correct |
34 |
Correct |
150 ms |
52356 KB |
Output is correct |
35 |
Correct |
66 ms |
30916 KB |
Output is correct |
36 |
Correct |
156 ms |
52488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
40120 KB |
Output is correct |
2 |
Correct |
69 ms |
40204 KB |
Output is correct |
3 |
Correct |
110 ms |
41324 KB |
Output is correct |
4 |
Correct |
89 ms |
42656 KB |
Output is correct |
5 |
Correct |
127 ms |
46836 KB |
Output is correct |
6 |
Correct |
124 ms |
47552 KB |
Output is correct |
7 |
Correct |
130 ms |
48152 KB |
Output is correct |
8 |
Correct |
133 ms |
48224 KB |
Output is correct |
9 |
Correct |
131 ms |
47436 KB |
Output is correct |
10 |
Correct |
84 ms |
38860 KB |
Output is correct |
11 |
Correct |
202 ms |
61652 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
10 ms |
16752 KB |
Output is correct |
14 |
Correct |
9 ms |
16712 KB |
Output is correct |
15 |
Correct |
11 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16636 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
75 ms |
40196 KB |
Output is correct |
19 |
Correct |
68 ms |
40140 KB |
Output is correct |
20 |
Correct |
65 ms |
40196 KB |
Output is correct |
21 |
Correct |
63 ms |
40196 KB |
Output is correct |
22 |
Correct |
146 ms |
49860 KB |
Output is correct |
23 |
Correct |
180 ms |
60044 KB |
Output is correct |
24 |
Correct |
194 ms |
61796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
45372 KB |
Output is correct |
2 |
Correct |
128 ms |
49620 KB |
Output is correct |
3 |
Correct |
64 ms |
40152 KB |
Output is correct |
4 |
Correct |
64 ms |
40104 KB |
Output is correct |
5 |
Correct |
212 ms |
68596 KB |
Output is correct |
6 |
Correct |
316 ms |
69992 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
155 ms |
53248 KB |
Output is correct |
9 |
Correct |
186 ms |
58980 KB |
Output is correct |
10 |
Correct |
112 ms |
45372 KB |
Output is correct |
11 |
Correct |
126 ms |
49552 KB |
Output is correct |
12 |
Correct |
8 ms |
16624 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16724 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
68 ms |
40148 KB |
Output is correct |
17 |
Correct |
67 ms |
40224 KB |
Output is correct |
18 |
Correct |
104 ms |
45396 KB |
Output is correct |
19 |
Correct |
138 ms |
49592 KB |
Output is correct |
20 |
Correct |
111 ms |
45524 KB |
Output is correct |
21 |
Correct |
122 ms |
48692 KB |
Output is correct |
22 |
Correct |
113 ms |
45496 KB |
Output is correct |
23 |
Correct |
116 ms |
48616 KB |
Output is correct |
24 |
Correct |
75 ms |
40120 KB |
Output is correct |
25 |
Correct |
69 ms |
40204 KB |
Output is correct |
26 |
Correct |
110 ms |
41324 KB |
Output is correct |
27 |
Correct |
89 ms |
42656 KB |
Output is correct |
28 |
Correct |
127 ms |
46836 KB |
Output is correct |
29 |
Correct |
124 ms |
47552 KB |
Output is correct |
30 |
Correct |
130 ms |
48152 KB |
Output is correct |
31 |
Correct |
133 ms |
48224 KB |
Output is correct |
32 |
Correct |
8 ms |
16724 KB |
Output is correct |
33 |
Correct |
11 ms |
16724 KB |
Output is correct |
34 |
Correct |
8 ms |
16724 KB |
Output is correct |
35 |
Correct |
8 ms |
16652 KB |
Output is correct |
36 |
Correct |
8 ms |
16724 KB |
Output is correct |
37 |
Correct |
8 ms |
16724 KB |
Output is correct |
38 |
Correct |
9 ms |
16656 KB |
Output is correct |
39 |
Correct |
8 ms |
16724 KB |
Output is correct |
40 |
Correct |
8 ms |
16852 KB |
Output is correct |
41 |
Correct |
9 ms |
16980 KB |
Output is correct |
42 |
Correct |
8 ms |
16852 KB |
Output is correct |
43 |
Correct |
10 ms |
16852 KB |
Output is correct |
44 |
Correct |
8 ms |
16724 KB |
Output is correct |
45 |
Correct |
9 ms |
16852 KB |
Output is correct |
46 |
Correct |
8 ms |
16724 KB |
Output is correct |
47 |
Correct |
9 ms |
16980 KB |
Output is correct |
48 |
Correct |
29 ms |
21860 KB |
Output is correct |
49 |
Correct |
29 ms |
22512 KB |
Output is correct |
50 |
Correct |
28 ms |
22376 KB |
Output is correct |
51 |
Correct |
32 ms |
22360 KB |
Output is correct |
52 |
Correct |
27 ms |
22360 KB |
Output is correct |
53 |
Correct |
50 ms |
28120 KB |
Output is correct |
54 |
Correct |
13 ms |
17772 KB |
Output is correct |
55 |
Correct |
28 ms |
20168 KB |
Output is correct |
56 |
Correct |
9 ms |
16852 KB |
Output is correct |
57 |
Correct |
13 ms |
17624 KB |
Output is correct |
58 |
Correct |
11 ms |
17644 KB |
Output is correct |
59 |
Correct |
111 ms |
43508 KB |
Output is correct |
60 |
Correct |
159 ms |
53044 KB |
Output is correct |
61 |
Correct |
171 ms |
52952 KB |
Output is correct |
62 |
Correct |
153 ms |
53020 KB |
Output is correct |
63 |
Correct |
149 ms |
54532 KB |
Output is correct |
64 |
Correct |
149 ms |
52968 KB |
Output is correct |
65 |
Correct |
150 ms |
52356 KB |
Output is correct |
66 |
Correct |
66 ms |
30916 KB |
Output is correct |
67 |
Correct |
156 ms |
52488 KB |
Output is correct |
68 |
Correct |
131 ms |
47436 KB |
Output is correct |
69 |
Correct |
84 ms |
38860 KB |
Output is correct |
70 |
Correct |
202 ms |
61652 KB |
Output is correct |
71 |
Correct |
8 ms |
16724 KB |
Output is correct |
72 |
Correct |
10 ms |
16752 KB |
Output is correct |
73 |
Correct |
9 ms |
16712 KB |
Output is correct |
74 |
Correct |
11 ms |
16724 KB |
Output is correct |
75 |
Correct |
9 ms |
16636 KB |
Output is correct |
76 |
Correct |
8 ms |
16724 KB |
Output is correct |
77 |
Correct |
75 ms |
40196 KB |
Output is correct |
78 |
Correct |
68 ms |
40140 KB |
Output is correct |
79 |
Correct |
65 ms |
40196 KB |
Output is correct |
80 |
Correct |
63 ms |
40196 KB |
Output is correct |
81 |
Correct |
146 ms |
49860 KB |
Output is correct |
82 |
Correct |
180 ms |
60044 KB |
Output is correct |
83 |
Correct |
194 ms |
61796 KB |
Output is correct |
84 |
Correct |
239 ms |
71192 KB |
Output is correct |
85 |
Correct |
258 ms |
72084 KB |
Output is correct |
86 |
Correct |
264 ms |
70096 KB |
Output is correct |
87 |
Correct |
273 ms |
71456 KB |
Output is correct |
88 |
Correct |
8 ms |
16724 KB |
Output is correct |
89 |
Correct |
286 ms |
72308 KB |
Output is correct |
90 |
Correct |
280 ms |
71596 KB |
Output is correct |
91 |
Correct |
217 ms |
71144 KB |
Output is correct |