Submission #1325874

#TimeUsernameProblemLanguageResultExecution timeMemory
1325874muhammad-ahmadGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
20 ms4920 KiB
// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
#include <numeric>
#include <stack>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

void fast_io() {
	// freopen("", "r", stdin);
	// freopen("", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie();
	cout.tie();
	cout << setprecision(9);
}

#define int long long
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fi first
#define se second
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

void solve() {
	int n; cin >> n;
	vector<int> a(n), l(n, 0), r(n, 0);
	for (auto &i : a) cin >> i;
	int cur = a[0];
	for (int i = 1; i < n; i++){
		cur = max(cur + 1, a[i] + l[i - 1]);
		l[i] = max(l[i - 1], cur - a[i]);
	}
	reverse(all(a));
	cur = a[0];
	for (int i = 1; i < n; i++){
		cur = max(cur + 1, a[i] + r[i - 1]);
		r[i] = max(r[i - 1], cur - a[i]);
	}
	reverse(all(r));
	
	int ans = 1e18;
	for (int i = 0; i < n; i++){
		ans = min(ans, max(l[i], r[i]));
	}
	cout << ans << endl;
	
}

signed main() {
	fast_io();
	srand(chrono::steady_clock::now().time_since_epoch().count());
	int tc = 1;
	// cin >> tc;
	while (tc--) solve();
	return 0;
}


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