# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
758937 | ridiche | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++17 | 0 ms | 0 KiB |
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 <iostream>
using namespace std;
int a[200005];
int cr[200005], cn[200005], ce[200005];
int dr[200005], dn[200005], de[200005];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cn[0] = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] > cn[i - 1])
{
cr[i] = 0;
cn[i] = a[i];
ce[i] = 0;
}
else
{
cn[i] = cn[i - 1] + 1;
ce[i] = cn[i] - a[i];
cr[i] = cr[i - 1] + max(0, ce[i] - ce[i - 1]);
}
}
dn[n - 1] = a[n - 1];
for (int i = n-2; i >= 0; i--)
{
if (a[i] > dn[i + 1])
{
dr[i] = 0;
dn[i] = a[i];
de[i] = 0;
}
else
{
dn[i] = dn[i + 1] + 1;
de[i] = dn[i] + a[i];
dr[i] = dr[i + 1] + max(0, de[i] - de[i + 1]);
}
}
int r = INT_MAX;
for (int i = 0; i < n; i++)
{
r = min(r, cr[i] + dr[i] - min(ce[i], de[i]));
}
cout << r;
}