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;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll int, ll int>
#define ff first
#define ss second
#define pb push_back
vector<int> x, y, w;
int n, m;
ll int st1() {
ll int ans = 0;
for (int &cur : w) ans += cur;
return ans;
}
ll int st2() {
ll int fin[n][2], ps[n][2];
for (int i = 0; i < n; i++) {
fin[i][0] = 0;
fin[i][1] = 0;
}
for (int i = 0; i < m; i++) {
fin[y[i]][x[i]] = w[i];
}
if (n == 2) {
return max({fin[0][0] + fin[1][0], fin[0][1] + fin[1][1]});
}
ps[0][0] = fin[0][0];
ps[0][1] = fin[0][1];
for (int i = 1; i < n; i++) {
ps[i][0] = ps[i - 1][0] + fin[i][0];
ps[i][1] = ps[i - 1][1] + fin[i][1];
}
ll int ans = max(ps[n - 1][0], ps[n - 1][1]);
for (int stop = 0; stop < n; stop++) {
ans = max(ans, ps[stop][0] + (ps[n - 1][1] - ps[stop][1]));
}
return ans;
}
ll int st3() {
ll int a[n + 1];
for (int i = 0; i <= n; i++) a[i] = 0;
for (int i = 0; i < m; i++) a[x[i] + 1] = w[i];
ll int dp[n + 1][4];
dp[2][0] = 0;
dp[2][1] = a[1];
dp[2][2] = a[2];
dp[2][3] = 0;
for (int i = 3; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][2]);
dp[i][1] = max(dp[i - 1][0] + a[i - 1], dp[i - 1][2]);
dp[i][2] = max(dp[i - 1][1] + a[i], dp[i - 1][3] + a[i]);
dp[i][3] = max(dp[i - 1][1], dp[i - 1][3]);
}
ll int ans = 0;
ans = max(max(dp[n][0], dp[n][1]), max(dp[n][2], dp[n][3]));
return ans;
}
const ll int MIN = -1e16;
ll int st4() {
ll int a[n + 1][n + 1], ps[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++) {
a[x[i] + 1][y[i] + 1] = w[i];
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << a[i][j] << ' ';
// }
// cout << endl;
// }
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];
}
}
ll int dp[n + 1][11][11];
ll int cur;
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
if (i == j) dp[1][i][j] = 0;
else dp[1][i][j] = MIN;
}
}
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= 10; j++) {
for (int k = 0; k <= 10; k++) {
dp[i][j][k] = MIN;
}
}
for (int j = 0; j <= 10; j++) {
for (int k = j; k <= 10; k++) {
for (int l = 0; l <= 10; l++) {
int cover = max(j, l);
cur = dp[i - 1][j][k];
if (l > k) {
cur += ps[i - 1][l] - ps[i - 1][k];
} else if (j > l) {
cur += ps[i][j] - ps[i][l];
}
dp[i][l][cover] = max(dp[i][l][cover], cur);
}
}
}
}
ll int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 10; j++) {
for (int k = 0; k <= 10; k++) {
ans = max(ans, dp[i][j][k]);
}
}
}
return ans;
}
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N; m = M;
x = X; y = Y; w = W;
bool isst4 = (n <= 300);
bool isst3 = true;
for (int &cur : Y) {
isst4 &= (cur <= 8);
isst3 &= (cur == 0);
}
if (isst3) return st3();
bool isst1 = true, isst2 = true;
for (int &cur : X) {
if ((cur & 1) != (X[0] & 1)) {
isst1 = false;
}
if (cur > 1) {
isst2 = false;
}
}
if (isst2) return st2();
if (isst1) return st1();
if (isst4) return st4();
throw runtime_error("wrong");
}
# | 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... |