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;
constexpr size_t N = 300;
int32_t f[N << 1][N], a[N], x[N], u[N], v[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
for (size_t i = 0; i < N; ++i)
fill(f[i], f[i] + N, INT32_MIN);
size_t n, m = 0, last_playground = -1;
cin >> n;
int32_t sum_a = 0;
for (size_t i = 0; i < n; ++i)
{
cin >> a[i];
if (a[i] >= 0)
x[m] = i, u[m] = last_playground, sum_a += a[i], ++m;
else
last_playground = i;
}
last_playground = n;
for (size_t i = n - 1, j = m - 1; i < n; --i)
{
if (a[i] >= 0)
v[j] = last_playground, --j;
else
last_playground = i;
}
for (size_t i = 0; i < m; ++i)
f[x[i]][i] = 0;
// don't take candies already bought by the turtle
int32_t ans = 0;
for (size_t i = 0; i < n << 1; ++i)
for (size_t j = 0; j < m; ++j)
{
ans = max(ans, f[i][j]);
for (size_t k = j + 1; k < m; ++k)
{
// Don't pick up the candy at the j-th shop.
if (i + x[k] - x[j] < n << 1)
f[i + x[k] - x[j]][k] = max(f[i + x[k] - x[j]][k], f[i][j]);
// Bring the candy of the j-th shop to the preceding playground.
if (i <= x[j] << 1 && u[j] != -1 && i + x[j] + x[k] - (u[j] << 1) < (n << 1))
f[i + x[j] + x[k] - (u[j] << 1)][k] = max(f[i + x[j] + x[k] - (u[j] << 1)][k], f[i][j] + 1);
// Bring the candy of the j-th shop to the next playground.
if (i <= x[j] << 1 && v[j] != n && i + v[j] - x[j] + abs(x[k] - v[j]) < (n << 1))
f[i + v[j] - x[j] + abs(x[k] - v[j])][k] = max(f[i + v[j] - x[j] + abs(x[k] - v[j])][k], f[i][j] + 1);
}
}
cout << sum_a - ans << '\n';
}
Compilation message (stderr)
tortoise.cpp: In function 'int main()':
tortoise.cpp:53:23: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int32_t' {aka 'int'} [-Wsign-compare]
53 | if (i <= x[j] << 1 && u[j] != -1 && i + x[j] + x[k] - (u[j] << 1) < (n << 1))
| ~~^~~~~~~~~~~~
tortoise.cpp:56:23: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int32_t' {aka 'int'} [-Wsign-compare]
56 | if (i <= x[j] << 1 && v[j] != n && i + v[j] - x[j] + abs(x[k] - v[j]) < (n << 1))
| ~~^~~~~~~~~~~~
tortoise.cpp:56:44: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
56 | if (i <= x[j] << 1 && v[j] != n && i + v[j] - x[j] + abs(x[k] - v[j]) < (n << 1))
| ~~~~~^~~~
# | 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... |