#include "fish.h"
#include <bits/stdc++.h>
#include <vector>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
map <pii, ll> mp;
const int nmax = 300001;
vector <vector <int> > ff(nmax);
vector <vector <int> > pos(nmax);
vector <vector <ll> > smx(nmax), pmx(nmax);
vector <vector <ll> > pref(nmax);
long long max_weights(int n, int m, vector<int> x, vector<int> y,vector<int> w) {
for(int i = 0; i < m; i++){
x[i]++;
y[i]++;
mp[{x[i], y[i]}] = w[i];
ff[x[i]].pb(y[i]);
}
for(int i= 1; i <= n; i++){
for(int j : ff[i - 1]) pos[i].pb(j);
for(int j : ff[i + 1]) pos[i].pb(j);
for(int j : ff[i]) pos[i].pb(j);
pos[i].pb(-1);pos[i].pb(n + 1);
sort(pos[i].begin(), pos[i].end());
pos[i].erase(unique(pos[i].begin(), pos[i].end()), pos[i].end());
pref[i].resize(pos[i].size());
pref[i][0] = mp[{i, pos[i][0]}];
for(int j = 1; j < pref[i].size(); ++j){
pref[i][j] = pref[i][j - 1] + mp[{i, pos[i][j]}];
}
}
ll p;
ll u;
for(int i = 1; i <= n; i++){
vector <ll> dp, DP;
dp.resize(pos[i].size());
DP.resize(pos[i].size());
smx[i].resize(pos[i].size());
pmx[i].resize(pos[i].size());
if(i == 1){
fill(dp.begin(), dp.end(), 0);
fill(DP.begin(), DP.end(), 0);
p = dp[0];
u = DP.back();
}
else{
for(int j = 0; j < pos[i].size(); j++){
int val = pos[i][j];
int u = lower_bound(pos[i - 1].begin(), pos[i - 1].end(), val) - pos[i - 1].begin();
// cout << u << ' ';
dp[j] = smx[i - 1][u] - pref[i][j];
u = upper_bound(pos[i - 1].begin(), pos[i - 1].end(), val) - pos[i - 1].begin() -1;
DP[j] = pmx[i - 1][u] + pref[i - 1][u];
}
// DP[0] = max(DP[0], pmx[i - 1].back());
dp.back() = max(dp.back(), DP.back());
DP[0] = max({DP[0], p, u});
p = dp[0];
u= DP.back();
}
if(i == n)
return max(dp[0], DP.back());
int o = upper_bound(pos[i + 1].begin(), pos[i + 1].end(), pos[i].back()) -
pos[i + 1].begin() - 1;
smx[i].back() = dp.back() + pref[i + 1][o];
for(int j = pos[i].size() - 2; j >= 0; j--){
int val = pos[i][j];
int o = upper_bound(pos[i + 1].begin(), pos[i + 1].end(), val) - pos[i + 1].begin() - 1;
smx[i][j] = max(smx[i][j + 1], dp[j] + pref[i + 1][o]);
}
pmx[i][0] = DP[0]- pref[i][0];
for(int j = 1; j < pos[i].size(); j++){
pmx[i][j] = max(pmx[i][j - 1], DP[j] - pref[i][j]);
}
}
return 0;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:38:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j = 1; j < pref[i].size(); ++j){
| ~~^~~~~~~~~~~~~~~~
fish.cpp:57:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int j = 0; j < pos[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
fish.cpp:83:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int j = 1; j < pos[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
76168 KB |
Output is correct |
2 |
Correct |
191 ms |
83060 KB |
Output is correct |
3 |
Correct |
83 ms |
60476 KB |
Output is correct |
4 |
Correct |
83 ms |
60460 KB |
Output is correct |
5 |
Correct |
615 ms |
157420 KB |
Output is correct |
6 |
Correct |
609 ms |
158020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35416 KB |
Output is correct |
2 |
Correct |
253 ms |
89208 KB |
Output is correct |
3 |
Correct |
313 ms |
97264 KB |
Output is correct |
4 |
Correct |
162 ms |
76144 KB |
Output is correct |
5 |
Correct |
188 ms |
83004 KB |
Output is correct |
6 |
Correct |
18 ms |
35412 KB |
Output is correct |
7 |
Correct |
18 ms |
35428 KB |
Output is correct |
8 |
Correct |
17 ms |
35540 KB |
Output is correct |
9 |
Correct |
16 ms |
35420 KB |
Output is correct |
10 |
Correct |
80 ms |
60492 KB |
Output is correct |
11 |
Correct |
86 ms |
60476 KB |
Output is correct |
12 |
Correct |
196 ms |
83616 KB |
Output is correct |
13 |
Correct |
242 ms |
91956 KB |
Output is correct |
14 |
Correct |
188 ms |
79872 KB |
Output is correct |
15 |
Correct |
176 ms |
77872 KB |
Output is correct |
16 |
Correct |
185 ms |
79948 KB |
Output is correct |
17 |
Correct |
204 ms |
84808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
60492 KB |
Output is correct |
2 |
Correct |
83 ms |
60568 KB |
Output is correct |
3 |
Correct |
123 ms |
66280 KB |
Output is correct |
4 |
Correct |
116 ms |
66388 KB |
Output is correct |
5 |
Correct |
171 ms |
75412 KB |
Output is correct |
6 |
Correct |
171 ms |
74748 KB |
Output is correct |
7 |
Correct |
207 ms |
75356 KB |
Output is correct |
8 |
Correct |
174 ms |
75336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35472 KB |
Output is correct |
3 |
Correct |
18 ms |
35468 KB |
Output is correct |
4 |
Correct |
18 ms |
35548 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35540 KB |
Output is correct |
7 |
Correct |
18 ms |
35488 KB |
Output is correct |
8 |
Correct |
22 ms |
35584 KB |
Output is correct |
9 |
Correct |
18 ms |
35668 KB |
Output is correct |
10 |
Correct |
19 ms |
35932 KB |
Output is correct |
11 |
Correct |
18 ms |
35676 KB |
Output is correct |
12 |
Correct |
19 ms |
35796 KB |
Output is correct |
13 |
Correct |
17 ms |
35544 KB |
Output is correct |
14 |
Correct |
18 ms |
35660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35472 KB |
Output is correct |
3 |
Correct |
18 ms |
35468 KB |
Output is correct |
4 |
Correct |
18 ms |
35548 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35540 KB |
Output is correct |
7 |
Correct |
18 ms |
35488 KB |
Output is correct |
8 |
Correct |
22 ms |
35584 KB |
Output is correct |
9 |
Correct |
18 ms |
35668 KB |
Output is correct |
10 |
Correct |
19 ms |
35932 KB |
Output is correct |
11 |
Correct |
18 ms |
35676 KB |
Output is correct |
12 |
Correct |
19 ms |
35796 KB |
Output is correct |
13 |
Correct |
17 ms |
35544 KB |
Output is correct |
14 |
Correct |
18 ms |
35660 KB |
Output is correct |
15 |
Correct |
17 ms |
35660 KB |
Output is correct |
16 |
Correct |
18 ms |
35872 KB |
Output is correct |
17 |
Correct |
63 ms |
43620 KB |
Output is correct |
18 |
Correct |
56 ms |
42924 KB |
Output is correct |
19 |
Correct |
63 ms |
42956 KB |
Output is correct |
20 |
Correct |
53 ms |
42188 KB |
Output is correct |
21 |
Correct |
53 ms |
42180 KB |
Output is correct |
22 |
Correct |
92 ms |
48848 KB |
Output is correct |
23 |
Correct |
29 ms |
38208 KB |
Output is correct |
24 |
Correct |
60 ms |
42936 KB |
Output is correct |
25 |
Correct |
19 ms |
35832 KB |
Output is correct |
26 |
Correct |
30 ms |
37888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35472 KB |
Output is correct |
3 |
Correct |
18 ms |
35468 KB |
Output is correct |
4 |
Correct |
18 ms |
35548 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35540 KB |
Output is correct |
7 |
Correct |
18 ms |
35488 KB |
Output is correct |
8 |
Correct |
22 ms |
35584 KB |
Output is correct |
9 |
Correct |
18 ms |
35668 KB |
Output is correct |
10 |
Correct |
19 ms |
35932 KB |
Output is correct |
11 |
Correct |
18 ms |
35676 KB |
Output is correct |
12 |
Correct |
19 ms |
35796 KB |
Output is correct |
13 |
Correct |
17 ms |
35544 KB |
Output is correct |
14 |
Correct |
18 ms |
35660 KB |
Output is correct |
15 |
Correct |
17 ms |
35660 KB |
Output is correct |
16 |
Correct |
18 ms |
35872 KB |
Output is correct |
17 |
Correct |
63 ms |
43620 KB |
Output is correct |
18 |
Correct |
56 ms |
42924 KB |
Output is correct |
19 |
Correct |
63 ms |
42956 KB |
Output is correct |
20 |
Correct |
53 ms |
42188 KB |
Output is correct |
21 |
Correct |
53 ms |
42180 KB |
Output is correct |
22 |
Correct |
92 ms |
48848 KB |
Output is correct |
23 |
Correct |
29 ms |
38208 KB |
Output is correct |
24 |
Correct |
60 ms |
42936 KB |
Output is correct |
25 |
Correct |
19 ms |
35832 KB |
Output is correct |
26 |
Correct |
30 ms |
37888 KB |
Output is correct |
27 |
Correct |
25 ms |
37176 KB |
Output is correct |
28 |
Correct |
252 ms |
72232 KB |
Output is correct |
29 |
Correct |
417 ms |
96756 KB |
Output is correct |
30 |
Correct |
490 ms |
129900 KB |
Output is correct |
31 |
Correct |
517 ms |
129228 KB |
Output is correct |
32 |
Correct |
350 ms |
81096 KB |
Output is correct |
33 |
Correct |
517 ms |
131736 KB |
Output is correct |
34 |
Correct |
529 ms |
131824 KB |
Output is correct |
35 |
Correct |
151 ms |
65612 KB |
Output is correct |
36 |
Correct |
483 ms |
118432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
60492 KB |
Output is correct |
2 |
Correct |
83 ms |
60568 KB |
Output is correct |
3 |
Correct |
123 ms |
66280 KB |
Output is correct |
4 |
Correct |
116 ms |
66388 KB |
Output is correct |
5 |
Correct |
171 ms |
75412 KB |
Output is correct |
6 |
Correct |
171 ms |
74748 KB |
Output is correct |
7 |
Correct |
207 ms |
75356 KB |
Output is correct |
8 |
Correct |
174 ms |
75336 KB |
Output is correct |
9 |
Correct |
214 ms |
92352 KB |
Output is correct |
10 |
Correct |
133 ms |
63064 KB |
Output is correct |
11 |
Correct |
274 ms |
90592 KB |
Output is correct |
12 |
Correct |
19 ms |
35528 KB |
Output is correct |
13 |
Correct |
19 ms |
35504 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35468 KB |
Output is correct |
16 |
Correct |
18 ms |
35512 KB |
Output is correct |
17 |
Correct |
18 ms |
35488 KB |
Output is correct |
18 |
Correct |
83 ms |
60492 KB |
Output is correct |
19 |
Correct |
81 ms |
60496 KB |
Output is correct |
20 |
Correct |
82 ms |
60528 KB |
Output is correct |
21 |
Correct |
82 ms |
60504 KB |
Output is correct |
22 |
Correct |
241 ms |
92676 KB |
Output is correct |
23 |
Correct |
332 ms |
105076 KB |
Output is correct |
24 |
Correct |
342 ms |
107224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
76168 KB |
Output is correct |
2 |
Correct |
191 ms |
83060 KB |
Output is correct |
3 |
Correct |
83 ms |
60476 KB |
Output is correct |
4 |
Correct |
83 ms |
60460 KB |
Output is correct |
5 |
Correct |
615 ms |
157420 KB |
Output is correct |
6 |
Correct |
609 ms |
158020 KB |
Output is correct |
7 |
Correct |
19 ms |
35416 KB |
Output is correct |
8 |
Correct |
253 ms |
89208 KB |
Output is correct |
9 |
Correct |
313 ms |
97264 KB |
Output is correct |
10 |
Correct |
162 ms |
76144 KB |
Output is correct |
11 |
Correct |
188 ms |
83004 KB |
Output is correct |
12 |
Correct |
18 ms |
35412 KB |
Output is correct |
13 |
Correct |
18 ms |
35428 KB |
Output is correct |
14 |
Correct |
17 ms |
35540 KB |
Output is correct |
15 |
Correct |
16 ms |
35420 KB |
Output is correct |
16 |
Correct |
80 ms |
60492 KB |
Output is correct |
17 |
Correct |
86 ms |
60476 KB |
Output is correct |
18 |
Correct |
196 ms |
83616 KB |
Output is correct |
19 |
Correct |
242 ms |
91956 KB |
Output is correct |
20 |
Correct |
188 ms |
79872 KB |
Output is correct |
21 |
Correct |
176 ms |
77872 KB |
Output is correct |
22 |
Correct |
185 ms |
79948 KB |
Output is correct |
23 |
Correct |
204 ms |
84808 KB |
Output is correct |
24 |
Correct |
82 ms |
60492 KB |
Output is correct |
25 |
Correct |
83 ms |
60568 KB |
Output is correct |
26 |
Correct |
123 ms |
66280 KB |
Output is correct |
27 |
Correct |
116 ms |
66388 KB |
Output is correct |
28 |
Correct |
171 ms |
75412 KB |
Output is correct |
29 |
Correct |
171 ms |
74748 KB |
Output is correct |
30 |
Correct |
207 ms |
75356 KB |
Output is correct |
31 |
Correct |
174 ms |
75336 KB |
Output is correct |
32 |
Correct |
17 ms |
35540 KB |
Output is correct |
33 |
Correct |
17 ms |
35472 KB |
Output is correct |
34 |
Correct |
18 ms |
35468 KB |
Output is correct |
35 |
Correct |
18 ms |
35548 KB |
Output is correct |
36 |
Correct |
18 ms |
35540 KB |
Output is correct |
37 |
Correct |
17 ms |
35540 KB |
Output is correct |
38 |
Correct |
18 ms |
35488 KB |
Output is correct |
39 |
Correct |
22 ms |
35584 KB |
Output is correct |
40 |
Correct |
18 ms |
35668 KB |
Output is correct |
41 |
Correct |
19 ms |
35932 KB |
Output is correct |
42 |
Correct |
18 ms |
35676 KB |
Output is correct |
43 |
Correct |
19 ms |
35796 KB |
Output is correct |
44 |
Correct |
17 ms |
35544 KB |
Output is correct |
45 |
Correct |
18 ms |
35660 KB |
Output is correct |
46 |
Correct |
17 ms |
35660 KB |
Output is correct |
47 |
Correct |
18 ms |
35872 KB |
Output is correct |
48 |
Correct |
63 ms |
43620 KB |
Output is correct |
49 |
Correct |
56 ms |
42924 KB |
Output is correct |
50 |
Correct |
63 ms |
42956 KB |
Output is correct |
51 |
Correct |
53 ms |
42188 KB |
Output is correct |
52 |
Correct |
53 ms |
42180 KB |
Output is correct |
53 |
Correct |
92 ms |
48848 KB |
Output is correct |
54 |
Correct |
29 ms |
38208 KB |
Output is correct |
55 |
Correct |
60 ms |
42936 KB |
Output is correct |
56 |
Correct |
19 ms |
35832 KB |
Output is correct |
57 |
Correct |
30 ms |
37888 KB |
Output is correct |
58 |
Correct |
25 ms |
37176 KB |
Output is correct |
59 |
Correct |
252 ms |
72232 KB |
Output is correct |
60 |
Correct |
417 ms |
96756 KB |
Output is correct |
61 |
Correct |
490 ms |
129900 KB |
Output is correct |
62 |
Correct |
517 ms |
129228 KB |
Output is correct |
63 |
Correct |
350 ms |
81096 KB |
Output is correct |
64 |
Correct |
517 ms |
131736 KB |
Output is correct |
65 |
Correct |
529 ms |
131824 KB |
Output is correct |
66 |
Correct |
151 ms |
65612 KB |
Output is correct |
67 |
Correct |
483 ms |
118432 KB |
Output is correct |
68 |
Correct |
214 ms |
92352 KB |
Output is correct |
69 |
Correct |
133 ms |
63064 KB |
Output is correct |
70 |
Correct |
274 ms |
90592 KB |
Output is correct |
71 |
Correct |
19 ms |
35528 KB |
Output is correct |
72 |
Correct |
19 ms |
35504 KB |
Output is correct |
73 |
Correct |
18 ms |
35540 KB |
Output is correct |
74 |
Correct |
19 ms |
35468 KB |
Output is correct |
75 |
Correct |
18 ms |
35512 KB |
Output is correct |
76 |
Correct |
18 ms |
35488 KB |
Output is correct |
77 |
Correct |
83 ms |
60492 KB |
Output is correct |
78 |
Correct |
81 ms |
60496 KB |
Output is correct |
79 |
Correct |
82 ms |
60528 KB |
Output is correct |
80 |
Correct |
82 ms |
60504 KB |
Output is correct |
81 |
Correct |
241 ms |
92676 KB |
Output is correct |
82 |
Correct |
332 ms |
105076 KB |
Output is correct |
83 |
Correct |
342 ms |
107224 KB |
Output is correct |
84 |
Correct |
604 ms |
143232 KB |
Output is correct |
85 |
Correct |
570 ms |
144636 KB |
Output is correct |
86 |
Correct |
588 ms |
154452 KB |
Output is correct |
87 |
Correct |
650 ms |
158016 KB |
Output is correct |
88 |
Correct |
18 ms |
35544 KB |
Output is correct |
89 |
Correct |
670 ms |
157936 KB |
Output is correct |
90 |
Correct |
524 ms |
136608 KB |
Output is correct |
91 |
Correct |
452 ms |
113080 KB |
Output is correct |