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 <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
typedef long long llong;
const int MAXN = 5000 + 10;
const int INF = 1e9;
int a[MAXN], n;
llong prefix[MAXN];
llong suffix[MAXN];
llong prefVal[MAXN];
llong suffVal[MAXN];
void solve()
{
prefVal[1] = a[1];
suffVal[n] = a[n];
int last = a[1];
prefix[1] = suffix[n] = 0;
for (int i = 2 ; i <= n ; ++i)
{
prefix[i] = prefix[i-1] + std::max(0, last + 1 - a[i]);
last = std::max(last + 1, a[i]);
prefVal[i] = last;
}
last = a[n];
llong ans = prefix[n];
for (int i = n-1 ; i >= 0 ; --i)
{
suffix[i] = suffix[i+1] + std::max(0, last + 1 - a[i]);
last = std::max(last + 1, a[i]);
suffVal[i] = last;
}
for (int i = 0 ; i <= n ; ++i)
{
ans = std::min(ans, std::max(prefix[i], suffix[i + 1]) + (prefVal[i] == suffVal[i + 1] && prefix[i] == suffix[i + 1]));
}
std::cout << ans << '\n';
}
void read()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> a[i];
}
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |