#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
long long max_weights(int32_t n, int32_t m, vector<int32_t> X, vector<int32_t> Y,
vector<int32_t> W) {
int a[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
a[i][j] = 0;
}
}
for (int i = 0; i < m; i++) {
X[i]++; Y[i]++;
a[X[i]][Y[i]] = W[i];
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cerr << a[i][j] << ' ';
// }
// cerr << endl;
// }
// cerr << endl;
int ps[n + 1][n + 1]; // ps[i][j] = a[i][1] + ... + a[i][j]
for (int i = 1; i <= n; i++) {
ps[i][0] = 0;
for (int j = 1; j <= n; j++) {
ps[i][j] = ps[i][j - 1] + a[i][j];
}
}
int dp[2][n + 1][n + 1];
// dp[increasing][pos][height] = max ans from 1 to pos with pos getting height, height must be increasing
for (int i = 0; i <= n; i++) {
dp[0][0][i] = dp[1][0][i] = (i == 0 ? 0 : -1e18);
dp[0][1][i] = dp[1][1][i] = 0;
}
int cur[n + 1];
function<void(void)> resetcur = [&]() {
for (int i = 0; i <= n; i++) cur[i] = 0;
};
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= n; j++) {
dp[0][i][j] = dp[1][i][j] = 0;
}
// Case 0: height of i is 0
resetcur();
dp[0][i][0] = max(dp[0][i - 1][0], dp[1][i - 1][0]);
for (int j = 1; j <= n; j++) {
dp[0][i][0] = max(dp[0][i][0], max(dp[0][i - 1][j], dp[1][i - 1][j]) + ps[i][j]);
}
dp[1][i][0] = dp[0][i][0];
// Case 1: height of i-1 is 0
// Case 1.1: height of i-2 <= height of i
resetcur();
cur[0] = dp[0][i - 2][0];
for (int j = 1; j <= n; j++) {
cur[j] = max(cur[j - 1], max(dp[0][i - 2][j], dp[1][i - 2][j]));
}
for (int j = 0; j <= n; j++) {
dp[0][i][j] = max(dp[0][i][j], cur[j] + ps[i - 1][j]);
dp[1][i][j] = max(dp[1][i][j], cur[j] + ps[i - 1][j]);
}
// Case 1.2: height of i-2 >= height of i
resetcur();
cur[n] = dp[0][i - 2][n] + ps[i - 1][n];
for (int j = n - 1; j >= 0; j--) {
cur[j] = max(cur[j + 1], max(dp[0][i - 2][j], dp[1][i - 2][j]) + ps[i - 1][j]);
}
for (int j = 0; j <= n; j++) {
dp[0][i][j] = max(dp[0][i][j], cur[j]);
dp[1][i][j] = max(dp[1][i][j], cur[j]);
}
// now height of i-1 > 0
// Case 2: height of i-1 <= height of i
resetcur();
cur[1] = dp[0][i - 1][1];
for (int j = 2; j <= n; j++) {
cur[j] = max(cur[j - 1] + a[i - 1][j], dp[0][i - 1][j]);
}
for (int j = 1; j <= n; j++) {
dp[0][i][j] = max(dp[0][i][j], cur[j]);
}
// Case 3: height of i-1 >= height of i
resetcur();
cur[n] = max(dp[0][i - 1][n], dp[1][i - 1][n]);
for (int j = n - 1; j >= 1; j--) {
cur[j] = max(cur[j + 1] + a[i][j + 1], max(dp[0][i - 1][j], dp[1][i - 1][j]));
}
for (int j = 1; j <= n; j++) {
dp[1][i][j] = max(dp[1][i][j], cur[j]);
}
}
int ans = 0;
for (int i = 0; i <= n; i++) {
// cerr << dp[0][n][i] << ' ' << dp[1][n][i] << endl;
ans = max(ans, max(dp[0][n][i], dp[1][n][i]));
}
return ans;
}
#undef int
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
755 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Runtime error |
760 ms |
2097152 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
743 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
4 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
4 ms |
3156 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
3028 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
4 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
4 ms |
3156 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
3028 KB |
Output is correct |
15 |
Correct |
4 ms |
3028 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
17 ms |
4240 KB |
Output is correct |
18 |
Correct |
15 ms |
4196 KB |
Output is correct |
19 |
Correct |
18 ms |
4180 KB |
Output is correct |
20 |
Correct |
15 ms |
4216 KB |
Output is correct |
21 |
Correct |
17 ms |
4168 KB |
Output is correct |
22 |
Correct |
30 ms |
5224 KB |
Output is correct |
23 |
Correct |
7 ms |
3284 KB |
Output is correct |
24 |
Correct |
12 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
6 ms |
3260 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
4 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
4 ms |
3156 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
3028 KB |
Output is correct |
15 |
Correct |
4 ms |
3028 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
17 ms |
4240 KB |
Output is correct |
18 |
Correct |
15 ms |
4196 KB |
Output is correct |
19 |
Correct |
18 ms |
4180 KB |
Output is correct |
20 |
Correct |
15 ms |
4216 KB |
Output is correct |
21 |
Correct |
17 ms |
4168 KB |
Output is correct |
22 |
Correct |
30 ms |
5224 KB |
Output is correct |
23 |
Correct |
7 ms |
3284 KB |
Output is correct |
24 |
Correct |
12 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
6 ms |
3260 KB |
Output is correct |
27 |
Correct |
325 ms |
282488 KB |
Output is correct |
28 |
Correct |
87 ms |
22984 KB |
Output is correct |
29 |
Correct |
401 ms |
289316 KB |
Output is correct |
30 |
Correct |
440 ms |
289352 KB |
Output is correct |
31 |
Correct |
444 ms |
294864 KB |
Output is correct |
32 |
Correct |
98 ms |
21848 KB |
Output is correct |
33 |
Correct |
446 ms |
295052 KB |
Output is correct |
34 |
Correct |
436 ms |
295008 KB |
Output is correct |
35 |
Correct |
346 ms |
286924 KB |
Output is correct |
36 |
Correct |
424 ms |
294864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
743 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
755 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |