제출 #883413

#제출 시각아이디문제언어결과실행 시간메모리
883413elisaipateGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
67 ms4068 KiB
#include <iostream>

using namespace std;

#define nmax 200003

long long v[nmax];
int main()
{
    long long n, i, x, y, rez = 0;
    cin >> n;
    for( i = 0; i < n; i++ )
        cin >> v[i];

    x = 0;
    y = n - 1;
    while( y - x > 0 ) {
        if( v[x] < v[x+1] )
            x++;
        else if( v[y-1] > v[y] )
            y--;
        else {
            if( v[x] - v[x+1] + 1 < v[y] - v[y-1] + 1 ) {
                rez += v[x] - v[x+1] + 1;
                v[y] -= v[x] - v[x+1] + 1;
                x++;
            } else {
                rez += v[y] - v[y-1] + 1;
                v[x] -= v[y] - v[y-1] + 1;
                y--;
            }
        }
        /*cout << rez << " ";
        for( i = 0; i < n; i++ )
            cout << v[i] << " ";
        cout << "\n";*/
    }
    cout << rez;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...