Submission #1125406

#TimeUsernameProblemLanguageResultExecution timeMemory
1125406Kel_MahmutGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
58 ms4272 KiB
#include <bits/stdc++.h>
#define pb push_back
#define endl ("\n")
#define all(aa) aa.begin(), aa.end()
typedef long long ll;
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0; i < n; i++) cin >> a[i];
	vector<ll> pref(n), suf(n);
	for(int i = 1; i < n; i++){
		pref[i] = pref[i - 1];
		if(a[i - 1] >= a[i]){
			pref[i] += a[i - 1] - a[i] + 1;
		}
	}

	for(int i = n - 2; i >= 0; i--){
		suf[i] = suf[i + 1];
		if(a[i + 1] >= a[i]){
			suf[i] += a[i + 1] - a[i] + 1;
		}
	}

	ll ans = LLONG_MAX;
	for(int i = 0; i < n; i++){
		ans = min(ans, max(suf[i], pref[i]));
	}

	cout << ans << endl;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...