Submission #1032108

#TimeUsernameProblemLanguageResultExecution timeMemory
1032108juicyGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
0 / 100
1 ms2396 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5;
const long long inf = 1e18;

int n;
int a[N], d[N];
long long pf[N], sf[N];

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	for (int i = 1; i < n; ++i) {
		d[i] = a[i] - a[i + 1];
	}
	for (int i = 1; i < n; ++i) {
		pf[i] = pf[i - 1] +  max(0, d[i] + 1);
	}
	for (int i = n - 1; i >= 1; --i) {
		sf[i] = sf[i + 1] + max(0, -d[i] + 1);
	}
	long long res = inf;
	for (int i = 1; i <= n; ++i) {
		res = min(res, pf[i - 1] + sf[i]);
	}
	cout << res;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...