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 int N = 302;
int n;
bool smaller;
int max_m, max_n;
long long pref[N][N];
vector<vector<long long>> a;
vector<vector<vector<long long>>> dp;
long long bt(int col, int last, int last2) {
if (col > min(n, max_m + 1)) {
return 0;
}
long long& ret = dp[col][last][last2];
if (ret != -1) {
return ret;
}
long long sum = 0;
long long ps = pref[col][last];
for (int nlast = 0; nlast <= min(n, max_n + 1); ++nlast) {
if (nlast > last2 && nlast > last) sum += a[col - 1][nlast];
if (nlast <= last) ps -= a[col][nlast];
ret = max(ret, bt(col + 1, nlast, last) + sum + ps);
}
return ret;
}
long long max_weights(int N_, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N_;
bool even = true;
bool zero_Y = true;
smaller = true;
for (int i = 0; i < M; ++i) {
max_m = max(max_m, X[i] + 1);
max_n = max(max_n, Y[i] + 1);
even &= (X[i] % 2 == 0);
zero_Y &= Y[i] == 0;
smaller &= X[i] <= 1;
}
if (even || zero_Y) {
return accumulate(W.begin(), W.end(), 0LL);
}
dp.resize(max_m + 2);
for (int i = 0; i < max_m + 2; ++i) {
dp[i].resize(max_n + 2);
for (int j = 0; j < max_n + 2; ++j) {
dp[i][j].assign(max_n + 2, -1);
}
}
a.resize(max_m + 2);
for (int i = 0; i < max_m + 2; ++i) {
a[i].resize(max_n + 2);
}
for (int i = 0; i < M; ++i) {
a[X[i] + 1][Y[i] + 1] = W[i];
}
for (int i = 1; i <= max_m; ++i) {
for (int j = 1; j <= max_n; ++j) {
pref[i][j] = pref[i][j - 1] + a[i][j];
}
}
return bt(1, 0, 0);
}
# | 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... |