#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define bigint __int128
#define emb emplace_back
#define pb push_back
#define pii pair <int, int>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define Task ""
#define MASK(k) (1ull << k)
#define bitcnt(k) __builtin_popcount(k)
#define testBit(n, k) ((n >> k) & 1)
#define flipBit(n, k) (n ^ (1ll << k))
#define offBit(n, k) (n & ~MASK(k))
#define onBit(n, k) (n | (1ll << k))
template <class T> bool minimize(T &a, T b) {if (a > b) {a = b; return true;} return false;}
template <class T> bool maximize(T &a, T b) {if (a < b) {a = b; return true;} return false;}
const int N = 2e5 + 5, lim = 60, mod = 1e9 + 7;
const ll INF = 1e18;
int n, a[N];
int le[N], ri[N];
ll dpL[N], dpR[N];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
le[1] = 1; ri[n] = n;
for (int i = 2; i <= n; i++) le[i] = (a[i - 1] < a[i] ? le[i - 1] : i);
for (int i = n - 1; i > 0; i--) ri[i] = (a[i + 1] < a[i] ? ri[i + 1] : i);
for (int i = 1; i <= n; i++) {
int p = le[i], inc = max(0, a[p - 1] - a[p] + 1);
dpL[i] = dpL[p - 1] + inc;
}
for (int i = n; i > 0; i--) {
int p = ri[i], inc = max(0, a[p + 1] - a[p] + 1);
dpR[i] = dpR[p + 1] + inc;
}
ll ans = INF;
for (int i = 1; i <= n; i++) {
int u = le[i], v = ri[i];
int needInc = max({0, a[u - 1] - a[u] + 1, a[v + 1] - a[v] + 1});
minimize(ans, dpL[u - 1] + dpR[v + 1] + needInc);
}
cout << ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:37:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:38:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(Task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |