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 <stdio.h>
#include <assert.h>
#define N 5000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
void is_it_convex(int *dp, int n) {
int i;
for (i = 1; i < n && dp[i + 1] != -INF; i++)
assert(dp[i - 1] - dp[i] <= dp[i] - dp[i + 1]);
}
int main() {
static int aa[N], dp[N * 2 + 1];
int n, h, i, j, k, r, ans;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (k = 0; k <= n * 2; k++)
dp[k] = -INF;
dp[0] = 0;
for (i = -1, j = 0; j <= n; j++)
if (j == n || aa[j] == -1) {
int d_, h_, i_, j_;
d_ = -1, h_ = -1;
for (h = i + 1; h <= j; h++)
if (aa[h] > 0) {
int d;
d = INF;
if (i != -1)
d = min(d, h - i);
if (j != n)
d = min(d, j - h);
if (d_ < d)
d_ = d, h_ = h;
}
i_ = max(i, 0), j_ = min(j, n - 1);
if (d_ == -1) {
for (k = 0; k <= n * 2; k++)
if (dp[k] != -INF)
dp[k] += j_ - i_;
} else {
for (h = i + 1; h < h_; h++)
for (r = 0; r < aa[h]; r++)
for (k = n * 2; k >= 0; k--)
if (dp[k] != -INF && dp[k] + h - i >= 0)
dp[k + 1] = max(dp[k + 1], dp[k] - (h - i) * 2);
is_it_convex(dp, n * 2 + 1);
if (j == n || i >= 0 && h_ - i <= j - h_) {
for (r = 0; r < aa[h_] - 1; r++)
for (k = n * 2; k >= 0; k--)
if (dp[k] != -INF && dp[k] + h_ - i >= 0)
dp[k + 1] = max(dp[k + 1], dp[k] - (h_ - i) * 2);
is_it_convex(dp, n * 2 + 1);
for (k = n * 2; k >= 0; k--)
if (dp[k] != -INF) {
if (dp[k] + h_ - i_ >= 0)
dp[k + 1] = max(dp[k + 1], dp[k] + j_ - i_);
dp[k] += j_ - i_;
}
is_it_convex(dp, n * 2 + 1);
} else {
for (k = n * 2; k >= 0; k--)
if (dp[k] != -INF) {
if (dp[k] + h_ - i_ >= 0)
dp[k + 1] = max(dp[k + 1], dp[k] + j_ - i_);
dp[k] += j_ - i_;
}
is_it_convex(dp, n * 2 + 1);
for (r = 0; r < aa[h_] - 1; r++)
for (k = n * 2; k >= 0; k--)
if (dp[k] != -INF && dp[k] - (j - h_) * 3 >= 0)
dp[k + 1] = max(dp[k + 1], dp[k] - (j - h_) * 2);
is_it_convex(dp, n * 2 + 1);
}
for (h = h_ + 1; h < j; h++)
for (r = 0; r < aa[h]; r++)
for (k = n * 2; k >= 0; k--)
if (dp[k] != -INF && dp[k] - (j - h) * 3 >= 0)
dp[k + 1] = max(dp[k + 1], dp[k] - (j - h) * 2);
is_it_convex(dp, n * 2 + 1);
}
is_it_convex(dp, n * 2 + 1);
i = j;
}
ans = 0;
for (i = 0; i < n; i++)
if (aa[i] > 0)
ans += aa[i];
k = 0;
while (k < n * 2 && dp[k + 1] != -INF)
k++;
ans -= k;
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
tortoise.c: In function 'main':
tortoise.c:56:26: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
56 | if (j == n || i >= 0 && h_ - i <= j - h_) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
tortoise.c:21:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
tortoise.c:23:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# | 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... |