Submission #602194

#TimeUsernameProblemLanguageResultExecution timeMemory
602194TigryonochekkGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
82 ms4196 KiB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <string>
using namespace std;
const int N = 2e5 + 2, mod = 1e9 + 7;
const long long inf = 1e18 + 49;
int n;
int a[N];
long long dp[N], rdp[N];

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dp[1] = 0;
	for (int i = 2; i <= n; i++) {
		dp[i] = dp[i - 1];
		if (a[i] <= a[i - 1])
			dp[i] += a[i - 1] + 1 - a[i];
	}
	rdp[n] = 0;
	for (int i = n - 1; i >= 1; i--) {
		rdp[i] = rdp[i + 1];
		if (a[i] <= a[i + 1])
			rdp[i] += a[i + 1] + 1 - a[i];
	}
	long long ans = inf;
	for (int i = 1; i <= n; i++) {
		ans = min(ans, max(dp[i], rdp[i]));
	}
	cout << ans << endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...