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 i64 = long long;
void chmax(i64 &a, i64 b) {
if (a < b)
a = b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n + 1), b, c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > 0) {
for (int j = 1; j <= min(a[i], n); j++) {
b.push_back(i);
}
} else if (a[i] == -1) {
c.push_back(i);
}
}
vector<int> l(n + 1), r(n + 1);
int lst = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == -1)
lst = i;
l[i] = lst;
}
lst = n + 1;
for (int i = n; i >= 1; i--) {
if (a[i] == -1)
lst = i;
r[i] = lst;
}
vector dp(2 * n + 1, vector(n + 1, vector<i64>(n + 1, -1)));
dp[0][1][0] = 0;
for (int i = 0; i < 2 * n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k < int(b.size()); k++) {
if (dp[i][j][k] == -1)
continue;
for (int p = k; p < int(b.size()); p++) {
int x = b[k];
if (l[x] != 0) {
int y = l[x];
if (2 + i + abs(x - j) <= 2 * x) {
chmax(dp[min(2 * n, i + abs(x - j) + 2 * abs(x - y))][x][p + 1], dp[i][j][k] + 1);
}
}
if (r[x] != n + 1) {
int y = r[x];
if (2 + i + abs(x - j) <= 2 * x) {
chmax(dp[min(2 * n, i + abs(x - j) + abs(x - y))][y][p + 1], dp[i][j][k] + 1);
}
}
}
}
}
}
i64 ans = 0;
for (int i = 0; i <= 2 * n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= int(b.size()); k++) {
ans = max(ans, dp[i][j][k]);
}
}
}
i64 sum = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > 0) {
sum += a[i];
}
}
cout << sum - ans << "\n";
return 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... |