Submission #396684

#TimeUsernameProblemLanguageResultExecution timeMemory
396684Drew_Growing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
27 ms3040 KiB
#include <iostream>
using namespace std;

#define ll long long

const ll inf = (ll) 1e18 + 69;

const int MAX = 2e5 + 7;

int n;
int a[MAX];

int main()
{
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];

	int l = 1, r = n;
	ll res = 0;

	while (l < r)
	{
		if (a[l] < a[l+1]) l++;
		else if (a[r] < a[r-1]) r--;
		else if (a[l] - a[l+1] < a[r] - a[r-1])
		{
			int x = a[l] - a[l+1] + 1;
			res += x;
			l++, a[r] -= x;
		}
		else
		{
			int x = a[r] - a[r-1] + 1;
			res += x;
			r--, a[l] -= x;
		}
	}

 	cout << res << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...