#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
#define ll long long
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define pll pair<ll, ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll a[n-1];
ll pv = -1, t;
iloop(0, n) {
cin >> t;
if (i) a[i-1] = t - pv;
pv = t;
}
ll pre[n-1], suf[n-1], c[n-1], d[n-1];
iloop(0, n-1) {
c[i] = max(1 - a[i], 0LL);
pre[i] = c[i] + (i ? pre[i-1] : 0);
}
iloop(n-1, -1) {
d[i] = max(a[i] + 1, 0LL);
suf[i] = d[i] + (i != n-2 ? suf[i+1] : 0);
}
ll cans = INF;
iloop(0, n-2) cans = min(cans, max(pre[i], suf[i+1]) - c[i] - d[i] + max(c[i], d[i]));
cout << min({cans, suf[0], pre[n-2]});
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |