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>
#define int int64_t
using namespace std;
constexpr static int MXSIZE = 2e5;
int n, v[MXSIZE], dp[MXSIZE+1][2]; // Increasing, decreasing
int32_t main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> v[i];
for (int i = 2; i <= n; i++)
dp[i][0] = dp[i-1][0] + max<int>(0, 1 + v[i-2] - v[i-1]);
for (int i = n-2; i >= 0; i--)
dp[i][1] = dp[i+1][1] + max<int>(0, 1 + v[i+1] - v[i]);
int mn = min(dp[n][0], dp[0][1]);
for (int i = 1; i < n-1; i++)
{
int l = v[i-1] + dp[i][0];
int r = v[i+1] + dp[i+1][1];
int c = v[i] + dp[i][0] + dp[i+1][1];
mn = min(mn, max(dp[i][0], dp[i+1][1]) + max({c-1, r, l}) - c + 1);
}
cout << mn << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |