#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e18;
struct fish{
int y=0;
ll cost,f0=0,f1=0;
friend bool operator<(fish a,fish b){
return a.y<b.y;
}
};
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<fish>> dp(N+1);
vector<int> check(N+1,0);
for(int i=0;i<M;i++){
X[i]++;Y[i]++;
dp[X[i]].push_back({Y[i]-1,W[i],0,0});
check[X[i]]|=(Y[i]==1);
}
for(int i=0;i<=N;i++){
if(!check[i]) dp[i].push_back({0,0,0,0});
dp[i].push_back({N,0,0,0});
sort(dp[i].begin(),dp[i].end());
}
dp[0].front().f0=dp[0].front().f1=0;
dp[0].back().f0=dp[0].back().f1=-inf;
for(int i=1;i<=N;i++){
int cur=0;
ll sum=0,csum=0,Max=dp[i-1][0].f0;
//cout << i << '\n';
for(auto &a:dp[i]){
csum+=a.cost;
while(cur+1<(int)dp[i-1].size() && dp[i-1][cur+1].y<=a.y){
sum+=dp[i-1][cur].cost;cur++;
Max=max(Max,dp[i-1][cur].f0-sum);
}
a.f0=Max+sum+(a.y>dp[i-1][cur].y)*dp[i-1][cur].cost;
//cout << "*f0 " << Max << ' ' << sum+dp[i-1][cur].cost << '\n';
}
Max=dp[i-1].back().f1+csum;cur++;
for(int j=(int)dp[i].size()-1;j>=0;j--){
auto &a=dp[i][j];
while(cur && dp[i-1][cur-1].y>=a.y){
cur--,Max=max(Max,dp[i-1][cur].f1+csum-(dp[i-1][cur].y==a.y)*a.cost);
}
csum-=a.cost;
a.f1=Max-csum;
//cout << "*f1 " << Max << ' ' << csum << '\n';
}
dp[i].front().f0=max(dp[i].front().f0,dp[i-1].front().f1);
dp[i].back().f1=max({dp[i].back().f1,dp[i].back().f0,dp[i-1].front().f1});
/*
for(auto a:dp[i]){
cout << a.f0 << ' ' << a.f1 << '\n';
}
*/
}
return max(dp[N].front().f1,dp[N].back().f0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
14132 KB |
Output is correct |
2 |
Correct |
56 ms |
16300 KB |
Output is correct |
3 |
Correct |
15 ms |
10836 KB |
Output is correct |
4 |
Correct |
14 ms |
10836 KB |
Output is correct |
5 |
Correct |
133 ms |
27312 KB |
Output is correct |
6 |
Correct |
174 ms |
32460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
78 ms |
18688 KB |
Output is correct |
3 |
Correct |
96 ms |
21808 KB |
Output is correct |
4 |
Correct |
46 ms |
14160 KB |
Output is correct |
5 |
Correct |
52 ms |
16304 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
14 ms |
10836 KB |
Output is correct |
11 |
Correct |
16 ms |
10848 KB |
Output is correct |
12 |
Correct |
43 ms |
14260 KB |
Output is correct |
13 |
Correct |
50 ms |
16320 KB |
Output is correct |
14 |
Correct |
44 ms |
14232 KB |
Output is correct |
15 |
Correct |
49 ms |
15816 KB |
Output is correct |
16 |
Correct |
43 ms |
14260 KB |
Output is correct |
17 |
Correct |
47 ms |
15792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
10864 KB |
Output is correct |
2 |
Correct |
14 ms |
10816 KB |
Output is correct |
3 |
Correct |
35 ms |
11556 KB |
Output is correct |
4 |
Correct |
30 ms |
12692 KB |
Output is correct |
5 |
Correct |
54 ms |
16056 KB |
Output is correct |
6 |
Correct |
52 ms |
15324 KB |
Output is correct |
7 |
Correct |
56 ms |
15916 KB |
Output is correct |
8 |
Correct |
53 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
216 KB |
Output is correct |
7 |
Correct |
1 ms |
248 KB |
Output is correct |
8 |
Correct |
0 ms |
224 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
224 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
216 KB |
Output is correct |
7 |
Correct |
1 ms |
248 KB |
Output is correct |
8 |
Correct |
0 ms |
224 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
224 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
2 ms |
452 KB |
Output is correct |
17 |
Correct |
17 ms |
3800 KB |
Output is correct |
18 |
Correct |
18 ms |
4696 KB |
Output is correct |
19 |
Correct |
17 ms |
4852 KB |
Output is correct |
20 |
Correct |
19 ms |
4804 KB |
Output is correct |
21 |
Correct |
17 ms |
4760 KB |
Output is correct |
22 |
Correct |
34 ms |
8896 KB |
Output is correct |
23 |
Correct |
5 ms |
1224 KB |
Output is correct |
24 |
Correct |
12 ms |
2820 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
4 ms |
1120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
216 KB |
Output is correct |
7 |
Correct |
1 ms |
248 KB |
Output is correct |
8 |
Correct |
0 ms |
224 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
224 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
2 ms |
452 KB |
Output is correct |
17 |
Correct |
17 ms |
3800 KB |
Output is correct |
18 |
Correct |
18 ms |
4696 KB |
Output is correct |
19 |
Correct |
17 ms |
4852 KB |
Output is correct |
20 |
Correct |
19 ms |
4804 KB |
Output is correct |
21 |
Correct |
17 ms |
4760 KB |
Output is correct |
22 |
Correct |
34 ms |
8896 KB |
Output is correct |
23 |
Correct |
5 ms |
1224 KB |
Output is correct |
24 |
Correct |
12 ms |
2820 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
4 ms |
1120 KB |
Output is correct |
27 |
Correct |
2 ms |
864 KB |
Output is correct |
28 |
Correct |
78 ms |
19988 KB |
Output is correct |
29 |
Correct |
113 ms |
27224 KB |
Output is correct |
30 |
Correct |
124 ms |
26328 KB |
Output is correct |
31 |
Correct |
123 ms |
26200 KB |
Output is correct |
32 |
Correct |
108 ms |
28404 KB |
Output is correct |
33 |
Correct |
120 ms |
26336 KB |
Output is correct |
34 |
Correct |
119 ms |
26208 KB |
Output is correct |
35 |
Correct |
44 ms |
11252 KB |
Output is correct |
36 |
Correct |
118 ms |
28748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
10864 KB |
Output is correct |
2 |
Correct |
14 ms |
10816 KB |
Output is correct |
3 |
Correct |
35 ms |
11556 KB |
Output is correct |
4 |
Correct |
30 ms |
12692 KB |
Output is correct |
5 |
Correct |
54 ms |
16056 KB |
Output is correct |
6 |
Correct |
52 ms |
15324 KB |
Output is correct |
7 |
Correct |
56 ms |
15916 KB |
Output is correct |
8 |
Correct |
53 ms |
15948 KB |
Output is correct |
9 |
Correct |
59 ms |
21600 KB |
Output is correct |
10 |
Correct |
48 ms |
13616 KB |
Output is correct |
11 |
Correct |
133 ms |
27076 KB |
Output is correct |
12 |
Correct |
1 ms |
224 KB |
Output is correct |
13 |
Correct |
1 ms |
224 KB |
Output is correct |
14 |
Correct |
0 ms |
216 KB |
Output is correct |
15 |
Correct |
0 ms |
216 KB |
Output is correct |
16 |
Correct |
1 ms |
216 KB |
Output is correct |
17 |
Correct |
1 ms |
216 KB |
Output is correct |
18 |
Correct |
14 ms |
10840 KB |
Output is correct |
19 |
Correct |
15 ms |
10884 KB |
Output is correct |
20 |
Correct |
14 ms |
10796 KB |
Output is correct |
21 |
Correct |
15 ms |
10800 KB |
Output is correct |
22 |
Correct |
68 ms |
19964 KB |
Output is correct |
23 |
Correct |
119 ms |
26952 KB |
Output is correct |
24 |
Correct |
118 ms |
27460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
14132 KB |
Output is correct |
2 |
Correct |
56 ms |
16300 KB |
Output is correct |
3 |
Correct |
15 ms |
10836 KB |
Output is correct |
4 |
Correct |
14 ms |
10836 KB |
Output is correct |
5 |
Correct |
133 ms |
27312 KB |
Output is correct |
6 |
Correct |
174 ms |
32460 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
78 ms |
18688 KB |
Output is correct |
9 |
Correct |
96 ms |
21808 KB |
Output is correct |
10 |
Correct |
46 ms |
14160 KB |
Output is correct |
11 |
Correct |
52 ms |
16304 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
14 ms |
10836 KB |
Output is correct |
17 |
Correct |
16 ms |
10848 KB |
Output is correct |
18 |
Correct |
43 ms |
14260 KB |
Output is correct |
19 |
Correct |
50 ms |
16320 KB |
Output is correct |
20 |
Correct |
44 ms |
14232 KB |
Output is correct |
21 |
Correct |
49 ms |
15816 KB |
Output is correct |
22 |
Correct |
43 ms |
14260 KB |
Output is correct |
23 |
Correct |
47 ms |
15792 KB |
Output is correct |
24 |
Correct |
16 ms |
10864 KB |
Output is correct |
25 |
Correct |
14 ms |
10816 KB |
Output is correct |
26 |
Correct |
35 ms |
11556 KB |
Output is correct |
27 |
Correct |
30 ms |
12692 KB |
Output is correct |
28 |
Correct |
54 ms |
16056 KB |
Output is correct |
29 |
Correct |
52 ms |
15324 KB |
Output is correct |
30 |
Correct |
56 ms |
15916 KB |
Output is correct |
31 |
Correct |
53 ms |
15948 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 |
216 KB |
Output is correct |
38 |
Correct |
1 ms |
248 KB |
Output is correct |
39 |
Correct |
0 ms |
224 KB |
Output is correct |
40 |
Correct |
1 ms |
352 KB |
Output is correct |
41 |
Correct |
2 ms |
580 KB |
Output is correct |
42 |
Correct |
1 ms |
352 KB |
Output is correct |
43 |
Correct |
1 ms |
352 KB |
Output is correct |
44 |
Correct |
1 ms |
224 KB |
Output is correct |
45 |
Correct |
1 ms |
352 KB |
Output is correct |
46 |
Correct |
1 ms |
352 KB |
Output is correct |
47 |
Correct |
2 ms |
452 KB |
Output is correct |
48 |
Correct |
17 ms |
3800 KB |
Output is correct |
49 |
Correct |
18 ms |
4696 KB |
Output is correct |
50 |
Correct |
17 ms |
4852 KB |
Output is correct |
51 |
Correct |
19 ms |
4804 KB |
Output is correct |
52 |
Correct |
17 ms |
4760 KB |
Output is correct |
53 |
Correct |
34 ms |
8896 KB |
Output is correct |
54 |
Correct |
5 ms |
1224 KB |
Output is correct |
55 |
Correct |
12 ms |
2820 KB |
Output is correct |
56 |
Correct |
1 ms |
448 KB |
Output is correct |
57 |
Correct |
4 ms |
1120 KB |
Output is correct |
58 |
Correct |
2 ms |
864 KB |
Output is correct |
59 |
Correct |
78 ms |
19988 KB |
Output is correct |
60 |
Correct |
113 ms |
27224 KB |
Output is correct |
61 |
Correct |
124 ms |
26328 KB |
Output is correct |
62 |
Correct |
123 ms |
26200 KB |
Output is correct |
63 |
Correct |
108 ms |
28404 KB |
Output is correct |
64 |
Correct |
120 ms |
26336 KB |
Output is correct |
65 |
Correct |
119 ms |
26208 KB |
Output is correct |
66 |
Correct |
44 ms |
11252 KB |
Output is correct |
67 |
Correct |
118 ms |
28748 KB |
Output is correct |
68 |
Correct |
59 ms |
21600 KB |
Output is correct |
69 |
Correct |
48 ms |
13616 KB |
Output is correct |
70 |
Correct |
133 ms |
27076 KB |
Output is correct |
71 |
Correct |
1 ms |
224 KB |
Output is correct |
72 |
Correct |
1 ms |
224 KB |
Output is correct |
73 |
Correct |
0 ms |
216 KB |
Output is correct |
74 |
Correct |
0 ms |
216 KB |
Output is correct |
75 |
Correct |
1 ms |
216 KB |
Output is correct |
76 |
Correct |
1 ms |
216 KB |
Output is correct |
77 |
Correct |
14 ms |
10840 KB |
Output is correct |
78 |
Correct |
15 ms |
10884 KB |
Output is correct |
79 |
Correct |
14 ms |
10796 KB |
Output is correct |
80 |
Correct |
15 ms |
10800 KB |
Output is correct |
81 |
Correct |
68 ms |
19964 KB |
Output is correct |
82 |
Correct |
119 ms |
26952 KB |
Output is correct |
83 |
Correct |
118 ms |
27460 KB |
Output is correct |
84 |
Correct |
138 ms |
32060 KB |
Output is correct |
85 |
Correct |
137 ms |
32776 KB |
Output is correct |
86 |
Correct |
193 ms |
38848 KB |
Output is correct |
87 |
Correct |
185 ms |
39884 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
204 ms |
39912 KB |
Output is correct |
90 |
Correct |
186 ms |
36384 KB |
Output is correct |
91 |
Correct |
149 ms |
36196 KB |
Output is correct |