This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = -(1LL << 60);
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector p(n, vector<int>(n + 1));
for (int i = 0; i < m; i++) {
p[x[i]][y[i] + 1] = w[i];
}
vector s(n, vector<long long>(n + 2));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
s[i][j + 1] = s[i][j] + p[i][j];
}
}
auto range_sum = [&](int i, int l, int r) -> long long {
if (i == -1) {
return 0;
}
return s[i][r + 1] - s[i][l + 1];
};
vector dp(n + 1, array<long long, 2>{inf, inf});
dp[0] = {0, 0};
for (int i = 0; i < n; i++) {
auto pd = dp;
vector<long long> pmax(n + 2, inf);
for (int j = 0; j <= n; j++) {
pmax[j + 1] = max(pmax[j], pd[j][0] + range_sum(i - 1, j, n));
}
vector<long long> smax(n + 2, inf);
for (int j = n; j >= 0; j--) {
smax[j] = max(smax[j + 1], max(pd[j][0], pd[j][1]) - range_sum(i, j, n));
}
dp = vector(n + 1, array<long long, 2>{inf, inf});
for (int j = 0; j <= n; j++) {
dp[0][0] = max(dp[0][0], max(pd[j][0], pd[j][1]));
dp[0][1] = max(dp[0][1], max(pd[j][0], pd[j][1]) + range_sum(i, 0, j));
}
for (int j = 1; j <= n; j++) {
dp[j][0] = max(dp[j][0], max(pd[0][1], pd[0][0] + range_sum(i - 1, 0, j)));
dp[j][1] = max(dp[j][1], max(pd[0][1], pd[0][0] + range_sum(i - 1, 0, j)));
dp[j][0] = max(dp[j][0], pmax[j + 1] - range_sum(i - 1, j, n));
dp[j][1] = max(dp[j][1], smax[j] + range_sum(i, j, n));
// for (int k = 1; k <= j; k++) {
// dp[j][0] = max(dp[j][0], pd[k][0] + range_sum(i - 1, k, j));
// }
// for (int k = j; k <= n; k++) {
// dp[j][1] = max(dp[j][1], max(pd[k][0], pd[k][1]) + range_sum(i, j, k));
// }
}
}
long long ans = inf;
for (int i = 0; i <= n; i++) {
ans = max(ans, max(dp[i][0], dp[i][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... |