#include <bits/stdc++.h>
#include "fish.h"
// #include "grader.cpp"
using namespace std;
typedef long long ll;
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
ll mat[n + 5][n + 5] = {}, pref[n + 5][n + 5] = {};
for (int i = 0; i < m; i ++)
mat[x[i] + 1][y[i] + 1] = w[i];
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
pref[i][j] = pref[i][j - 1] + mat[i][j];
ll dp[n + 1][n + 1][2] = {}, pmx[n + 1][n + 1][2] = {}, smx[n + 1][n + 1] = {};
for (int k = 1; k <= n; k ++)
dp[1][k][0] = dp[1][k][1] = pref[2][k];
for (int k = 1; k <= n; k ++){
pmx[1][k][0] = max(pmx[1][k - 1][0], max(dp[1][k][0], dp[1][k][1]) - pref[2][k]);
pmx[1][k][1] = max(pmx[1][k - 1][1], dp[1][k][0] - pref[1][k] - pref[2][k]);
}
for (int k = n; k >= 0; k --)
smx[1][k] = max(smx[1][k + 1], max(dp[1][k][0], dp[1][k][1]));
for (int i = 2; i <= n; i ++){
for (int k = 0; k <= n; k ++){
dp[i][k][0] = max(dp[i][k][0], pmx[i - 1][k][1] + pref[i - 1][k] + pref[i + 1][k]); // pmx[i - 1][k][1] is dp[i - 1][pk][0] - pref[i][pk] - pref[i - 1][pk]
dp[i][k][1] = max(dp[i][k][1], smx[i - 1][k + 1] - pref[i][k] + pref[i + 1][k]);
dp[i][k][0] = max(dp[i][k][0], pmx[i - 2][k][0] + pref[i - 1][k] + pref[i + 1][k]); // pmx[i - 2][k][0] is max overall max(dp[i-2][pk][0,1]) - pref[i - 1][pk] , pk <= k
dp[i][k][0] = max(dp[i][k][0], smx[i - 2][k + 1] + pref[i + 1][k]);
// cout << i << " " << k << " : " << dp[i][k][0] << ", " << dp[i][k][1] << endl;
}
for (int k = 1; k <= n; k ++){
pmx[i][k][0] = max(pmx[i][k - 1][0], max(dp[i][k][0], dp[i][k][1]) - pref[i + 1][k]);
pmx[i][k][1] = max(pmx[i][k - 1][1], dp[i][k][0] - pref[i][k] - pref[i + 1][k]);
}
for (int k = n; k >= 0; k --)
smx[i][k] = max(smx[i][k + 1], max(dp[i][k][0], dp[i][k][1]));
}
ll ans = 0;
for (int k = 0; k <= n; k ++)
ans = max(ans, max(dp[n][k][0], dp[n][k][1]));
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |