#include "fish.h"
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e16;
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector<vector<ll>> p(n, vector<ll>(n, 0));
for(int i = 0; i < m; ++i) {
p[x[i]][y[i]] += w[i];
}
for(int i = 0; i < n; ++i) {
for(int j = 1; j < n; ++j) {
p[i][j] += p[i][j - 1];
}
}
vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(n + 1, vector<ll>(2, -inf)));
for(int i = 0; i <= n; ++i) {
dp[0][i][0] = dp[0][i][1] = 0;
}
//0 - increasing, 1 - decreasing
for(int i = 1; i < n; ++i) {
vector<ll> max0(n + 1, -inf), max1(n + 1, -inf);
for(int lastlen = n; lastlen >= 0; --lastlen) {
if(lastlen + 1 <= n) max1[lastlen] = max1[lastlen + 1];
max1[lastlen] = max(max1[lastlen], dp[i - 1][lastlen][0] + (lastlen ? p[i][lastlen - 1] : 0));
max1[lastlen] = max(max1[lastlen], dp[i - 1][lastlen][1] + (lastlen ? p[i][lastlen - 1] : 0));
}
for(int lastlen = 0; lastlen <= n; ++lastlen) {
dp[i][0][0] = max({dp[i][0][0], dp[i - 1][lastlen][0], dp[i - 1][lastlen][1]});
dp[i][0][1] = max({dp[i][0][1], dp[i - 1][lastlen][0], dp[i - 1][lastlen][1]});
if(lastlen) {
dp[i][0][1] = max(dp[i][0][1], dp[i - 1][lastlen][1] + p[i][lastlen - 1]);
dp[i][0][1] = max(dp[i][0][1], dp[i - 1][lastlen][0] + p[i][lastlen - 1]);
}
if(lastlen) max0[lastlen] = max0[lastlen - 1];
max0[lastlen] = max(max0[lastlen], dp[i - 1][lastlen][0] - (lastlen ? p[i - 1][lastlen - 1] : 0));
}
for(int curlen = 1; curlen <= n; ++curlen) {
dp[i][curlen][0] = max(dp[i][curlen][0], p[i - 1][curlen - 1] + max0[curlen]);
dp[i][curlen][1] = max(dp[i][curlen][1], max1[curlen] - p[i][curlen - 1]);
}
}
ll ans = 0;
for(int i = 0; i <= n; ++i) {
ans = max({ans, dp[n - 1][i][0], dp[n - 1][i][1]});
}
return ans;
}
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
708 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
754 ms |
2097152 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
710 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
1748 KB |
Output is correct |
10 |
Correct |
9 ms |
5972 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
5972 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
8 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
1748 KB |
Output is correct |
10 |
Correct |
9 ms |
5972 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
5972 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
8 ms |
5972 KB |
Output is correct |
15 |
Correct |
8 ms |
5956 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
19 ms |
7568 KB |
Output is correct |
18 |
Correct |
21 ms |
7580 KB |
Output is correct |
19 |
Correct |
20 ms |
7500 KB |
Output is correct |
20 |
Correct |
20 ms |
7492 KB |
Output is correct |
21 |
Correct |
19 ms |
7492 KB |
Output is correct |
22 |
Correct |
31 ms |
8524 KB |
Output is correct |
23 |
Correct |
10 ms |
6228 KB |
Output is correct |
24 |
Correct |
17 ms |
7036 KB |
Output is correct |
25 |
Correct |
8 ms |
5972 KB |
Output is correct |
26 |
Correct |
9 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
1748 KB |
Output is correct |
10 |
Correct |
9 ms |
5972 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
7 ms |
5972 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
8 ms |
5972 KB |
Output is correct |
15 |
Correct |
8 ms |
5956 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
19 ms |
7568 KB |
Output is correct |
18 |
Correct |
21 ms |
7580 KB |
Output is correct |
19 |
Correct |
20 ms |
7500 KB |
Output is correct |
20 |
Correct |
20 ms |
7492 KB |
Output is correct |
21 |
Correct |
19 ms |
7492 KB |
Output is correct |
22 |
Correct |
31 ms |
8524 KB |
Output is correct |
23 |
Correct |
10 ms |
6228 KB |
Output is correct |
24 |
Correct |
17 ms |
7036 KB |
Output is correct |
25 |
Correct |
8 ms |
5972 KB |
Output is correct |
26 |
Correct |
9 ms |
6100 KB |
Output is correct |
27 |
Correct |
754 ms |
564672 KB |
Output is correct |
28 |
Correct |
104 ms |
44376 KB |
Output is correct |
29 |
Correct |
818 ms |
577168 KB |
Output is correct |
30 |
Correct |
799 ms |
577228 KB |
Output is correct |
31 |
Correct |
800 ms |
577120 KB |
Output is correct |
32 |
Correct |
106 ms |
30932 KB |
Output is correct |
33 |
Correct |
804 ms |
577244 KB |
Output is correct |
34 |
Correct |
813 ms |
576716 KB |
Output is correct |
35 |
Correct |
754 ms |
568920 KB |
Output is correct |
36 |
Correct |
825 ms |
575208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
710 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
708 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |