This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
int n;
ll a[maxn], b[maxn];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
for (int i = 1; i < n; i ++)
b[i] = a[i + 1] - a[i];
int l = 1, r = n - 1;
ll ans = 0;
while(true)
{
while(l < n && b[l] > 0)
l ++;
while(r > 0 && b[r] < 0)
r --;
if (l > r)
break;
if (l == r)
{
if (b[l] == 0)
ans ++;
break;
}
ll s = min(-b[l] + 1, b[r] + 1);
b[l] += s;
b[r] -= s;
ans += s;
}
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |