#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
long long max_weights(int n,int m,vector<int>x,vector<int>y,vector<int>w){
int mx = *max_element(y.begin(),y.end());
if(n <= 300){
long long dp[n+2][n+2][2]{};
long long sum[n+2][n+2]{};
for(int i = 0; i < m; i++) x[i] += 1;
for(int i = 0; i < m; i++) y[i] += 1;
for(int i = 0; i < m; i++) sum[x[i]][y[i]] += w[i];
long long ans = 0;
for(int i = 0; i <= n+1; i++){
for(int j = n; j >= 0; j--){
sum[i][j] += sum[i][j+1];
}
}
for(int i = 1; i <= n+1; i++){
for(int j = 0; j < 2; j++){
dp[0][i][j] = -1e18;
}
}
dp[0][0][1] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= n+1; j++){
for(int k = 0; k <= n+1; k++){
long long now = dp[i-1][j][1];
if(k > j){
now += sum[i-1][j]-sum[i-1][k];
}
if(j > k){
now += sum[i][k]-sum[i][j];
}
ans = max(ans,now);
if(k > j) dp[i][k][1] = max(dp[i][k][1],now);
else if (k < j){
dp[i][k][0] = max(dp[i][k][0],now);
now -= dp[i-1][j][1];
now += dp[i-1][j][0];
ans = max(ans,now);
dp[i][k][0] = max(dp[i][k][0],now);
}
else{
dp[i][k][1] = max(dp[i][k][1],now);
dp[i][k][0] = max(dp[i][k][0],now);
now -= dp[i-1][j][1];
now += dp[i-1][j][0];
ans = max(ans,now);
dp[i][k][0] = max(dp[i][k][0],now);
dp[i][k][1] = max(dp[i][k][1],now);
}
}
dp[i][0][1] = max({dp[i][0][1],dp[i-1][j][1],dp[i-1][j][0]});
}
}
return ans;
}
mx = *max_element(x.begin(),x.end());
if(mx <= 1){
long long ans = 0;
long long ans2 = 0;
int cost[n][2]{};
for(int i = 0; i < m; i++){
if(x[i] == 1){cost[y[i]][1] = w[i]; ans += w[i];}
if(x[i] == 0){ans2 += w[i];cost[y[i]][0]=w[i];}
}
if(n == 2 || n == 1) return max(ans,ans2);
ans2 = max(ans,ans2);
for(int i = 1; i < n; i++){
ans -= cost[i-1][1];
ans += cost[i-1][0];
ans2 = max(ans2,ans);
}
return ans2;
}else{
bool z = 1;
for(int i = 0; i < n; i++){
z = (z && (x[i] % 2 == 0));
}
if(z){
long long sum = 0;
for(int i = 0; i < m; i++) sum += w[i];
return sum;
}
else{
long long dp[n+2][4][4]{};
vector<pair<int,int>>hv[n+2];
vector<int>to[n+2];
for(int i = 0; i < m; i++){
hv[x[i]+2].push_back({y[i]+1,w[i]});
to[x[i]+2].push_back(y[i]);
}
for(int i = 0; i <= n+1; i++){
to[i].push_back(0);
if(i > 1){
to[i].push_back(n+3);
}
}
long long ans = 0;
for(int i = 2; i <= n+1; i++){
int sz = 0;
for(int j = 0; j < to[i-2].size(); j++){
for(int k = 0; k < to[i-1].size(); k++){
for(int v = 0; v < to[i].size(); v++){
long long now = dp[i-1][k][j];
for(auto nodes : hv[i-1]){
if(nodes.first > max(to[i-2][j],to[i-1][k])){
if(nodes.first <= to[i][v]){
now += nodes.second;
}
}
}
for(auto nodes : hv[i]){
if(nodes.first > to[i][v]){
if(nodes.first <= to[i-1][k]){
now += nodes.second;
}
}
}
dp[i][v][k] = max(dp[i][v][k],now);
ans = max(ans,now);
}
}
}
}
return ans;
}
}
}
// int main(){
// cout << max_weights(5, 4, {0, 1, 4, 3}, {2, 1, 4, 3}, {5, 2, 1, 3}) << '\n';
// }
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:111:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int j = 0; j < to[i-2].size(); j++){
| ~~^~~~~~~~~~~~~~~~
fish.cpp:112:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int k = 0; k < to[i-1].size(); k++){
| ~~^~~~~~~~~~~~~~~~
fish.cpp:113:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int v = 0; v < to[i].size(); v++){
| ~~^~~~~~~~~~~~~~
fish.cpp:110:13: warning: unused variable 'sz' [-Wunused-variable]
110 | int sz = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3920 KB |
Output is correct |
2 |
Correct |
24 ms |
5204 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
74 ms |
13668 KB |
Output is correct |
6 |
Correct |
73 ms |
13908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
40 ms |
7508 KB |
Output is correct |
3 |
Correct |
48 ms |
9172 KB |
Output is correct |
4 |
Correct |
20 ms |
4312 KB |
Output is correct |
5 |
Correct |
24 ms |
5212 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
1120 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
26 ms |
4284 KB |
Output is correct |
13 |
Correct |
24 ms |
5212 KB |
Output is correct |
14 |
Correct |
20 ms |
4340 KB |
Output is correct |
15 |
Correct |
22 ms |
4832 KB |
Output is correct |
16 |
Correct |
21 ms |
4324 KB |
Output is correct |
17 |
Correct |
22 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
22 ms |
20568 KB |
Output is correct |
3 |
Correct |
42 ms |
22612 KB |
Output is correct |
4 |
Correct |
36 ms |
23292 KB |
Output is correct |
5 |
Correct |
69 ms |
27732 KB |
Output is correct |
6 |
Correct |
91 ms |
27228 KB |
Output is correct |
7 |
Correct |
72 ms |
27616 KB |
Output is correct |
8 |
Correct |
69 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
8 ms |
860 KB |
Output is correct |
10 |
Correct |
54 ms |
2392 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
54 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
56 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
8 ms |
860 KB |
Output is correct |
10 |
Correct |
54 ms |
2392 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
54 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
56 ms |
2396 KB |
Output is correct |
15 |
Correct |
54 ms |
2404 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
65 ms |
3640 KB |
Output is correct |
18 |
Correct |
67 ms |
3420 KB |
Output is correct |
19 |
Correct |
63 ms |
3416 KB |
Output is correct |
20 |
Correct |
63 ms |
3420 KB |
Output is correct |
21 |
Correct |
64 ms |
3628 KB |
Output is correct |
22 |
Correct |
74 ms |
4432 KB |
Output is correct |
23 |
Correct |
68 ms |
2764 KB |
Output is correct |
24 |
Correct |
60 ms |
3416 KB |
Output is correct |
25 |
Correct |
53 ms |
2592 KB |
Output is correct |
26 |
Correct |
55 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
8 ms |
860 KB |
Output is correct |
10 |
Correct |
54 ms |
2392 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
54 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
56 ms |
2396 KB |
Output is correct |
15 |
Correct |
54 ms |
2404 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
65 ms |
3640 KB |
Output is correct |
18 |
Correct |
67 ms |
3420 KB |
Output is correct |
19 |
Correct |
63 ms |
3416 KB |
Output is correct |
20 |
Correct |
63 ms |
3420 KB |
Output is correct |
21 |
Correct |
64 ms |
3628 KB |
Output is correct |
22 |
Correct |
74 ms |
4432 KB |
Output is correct |
23 |
Correct |
68 ms |
2764 KB |
Output is correct |
24 |
Correct |
60 ms |
3416 KB |
Output is correct |
25 |
Correct |
53 ms |
2592 KB |
Output is correct |
26 |
Correct |
55 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
1112 KB |
Output is correct |
28 |
Execution timed out |
1016 ms |
13964 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
22 ms |
20568 KB |
Output is correct |
3 |
Correct |
42 ms |
22612 KB |
Output is correct |
4 |
Correct |
36 ms |
23292 KB |
Output is correct |
5 |
Correct |
69 ms |
27732 KB |
Output is correct |
6 |
Correct |
91 ms |
27228 KB |
Output is correct |
7 |
Correct |
72 ms |
27616 KB |
Output is correct |
8 |
Correct |
69 ms |
27732 KB |
Output is correct |
9 |
Correct |
64 ms |
27476 KB |
Output is correct |
10 |
Correct |
63 ms |
16220 KB |
Output is correct |
11 |
Correct |
141 ms |
32080 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
20 ms |
20572 KB |
Output is correct |
21 |
Correct |
25 ms |
20744 KB |
Output is correct |
22 |
Correct |
78 ms |
27220 KB |
Output is correct |
23 |
Correct |
146 ms |
32080 KB |
Output is correct |
24 |
Correct |
137 ms |
32592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3920 KB |
Output is correct |
2 |
Correct |
24 ms |
5204 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
74 ms |
13668 KB |
Output is correct |
6 |
Correct |
73 ms |
13908 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
40 ms |
7508 KB |
Output is correct |
9 |
Correct |
48 ms |
9172 KB |
Output is correct |
10 |
Correct |
20 ms |
4312 KB |
Output is correct |
11 |
Correct |
24 ms |
5212 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
1120 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
26 ms |
4284 KB |
Output is correct |
19 |
Correct |
24 ms |
5212 KB |
Output is correct |
20 |
Correct |
20 ms |
4340 KB |
Output is correct |
21 |
Correct |
22 ms |
4832 KB |
Output is correct |
22 |
Correct |
21 ms |
4324 KB |
Output is correct |
23 |
Correct |
22 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
22 ms |
20568 KB |
Output is correct |
26 |
Correct |
42 ms |
22612 KB |
Output is correct |
27 |
Correct |
36 ms |
23292 KB |
Output is correct |
28 |
Correct |
69 ms |
27732 KB |
Output is correct |
29 |
Correct |
91 ms |
27228 KB |
Output is correct |
30 |
Correct |
72 ms |
27616 KB |
Output is correct |
31 |
Correct |
69 ms |
27732 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
8 ms |
860 KB |
Output is correct |
41 |
Correct |
54 ms |
2392 KB |
Output is correct |
42 |
Correct |
7 ms |
860 KB |
Output is correct |
43 |
Correct |
54 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
56 ms |
2396 KB |
Output is correct |
46 |
Correct |
54 ms |
2404 KB |
Output is correct |
47 |
Correct |
2 ms |
600 KB |
Output is correct |
48 |
Correct |
65 ms |
3640 KB |
Output is correct |
49 |
Correct |
67 ms |
3420 KB |
Output is correct |
50 |
Correct |
63 ms |
3416 KB |
Output is correct |
51 |
Correct |
63 ms |
3420 KB |
Output is correct |
52 |
Correct |
64 ms |
3628 KB |
Output is correct |
53 |
Correct |
74 ms |
4432 KB |
Output is correct |
54 |
Correct |
68 ms |
2764 KB |
Output is correct |
55 |
Correct |
60 ms |
3416 KB |
Output is correct |
56 |
Correct |
53 ms |
2592 KB |
Output is correct |
57 |
Correct |
55 ms |
2652 KB |
Output is correct |
58 |
Correct |
2 ms |
1112 KB |
Output is correct |
59 |
Execution timed out |
1016 ms |
13964 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |