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 <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
vector<int> L(N, -1), R(N, N);
for (int i = 0; i < N; i++) {
if (A[i] == -1) {
L[i] = i;
} else if (i) {
L[i] = L[i - 1];
}
int j = N - i - 1;
if (A[j] == -1) {
R[j] = j;
} else if (i) {
R[j] = R[j + 1];
}
}
int ans = 0;
vector dp(2 * N + 1, vector<int>(N, -1e9));
dp[0][0] = 0;
for (int i = 0; i < N; i++) {
if (A[i] == -1) {
continue;
}
auto pd = dp;
for (int j = 0; j <= 2 * N; j++) {
for (int k = 0; k < N; k++) {
if (pd[j][k] < 0) {
// continue;
}
for (int c = 1; c <= A[i]; c++) {
int t = 2 * (c - 1) * min(L[i] == -1 ? N : i - L[i], R[i] == N ? N : R[i] - i);
if (j + abs(i - k) + t > 2 * i) {
break;
}
ans = max(ans, pd[j][k] + c);
if (L[i] != -1) {
int p = j + abs(i - k) + t + i - L[i];
if (p <= 2 * N) {
dp[p][L[i]] = max(dp[p][L[i]], pd[j][k] + c);
}
}
if (R[i] != N) {
int p = j + abs(i - k) + t + R[i] - i;
if (p <= 2 * N) {
dp[p][R[i]] = max(dp[p][R[i]], pd[j][k] + c);
}
}
}
}
}
}
ans = -ans;
for (int i = 0; i < N; i++) {
if (A[i] != -1) {
ans += A[i];
}
}
cout << ans << "\n";
return 6/22;
}
# | 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... |