Submission #1311478

#TimeUsernameProblemLanguageResultExecution timeMemory
1311478madamadam3Growing Vegetables is Fun 4 (JOI21_ho_t1)C++20
0 / 100
13 ms572 KiB
#include <bits/stdc++.h>

using namespace std;
#define int long long int

signed main() {
    cin.tie(0)->sync_with_stdio(0);

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

    vector<int> f(n), r(n);
    f[0] = r[n-1] = 0;

    for (int i = 1; i < n; i++) {
        int dx = (a[i-1] + f[i-1] + 1) - (a[i]);
        f[i] = max(0LL, dx);

        int dy = (a[n-i] + r[n-i] + 1) - (a[n-i-1]);
        r[n-i-1] = max(0LL, dy);
    }

    vector<int> DP1(n+1, 0);
    for (int i = 0; i <= n; i++) {
        vector<int> f2(n); for (int j = 0; j < n; j++) f2[j] = (j <= i ? f[j] : r[j]);
        if (i<n && a[i] + f2[i] <= a[i+1] + f2[i+1]) f2[i] += (a[i+1] + f2[i+1] + 1) - (a[i] + f2[i]);
        deque<int> pos; for (int i = 0; i < n; i++) pos.push_back(i);

        while (!pos.empty()) {
            if (pos.front() < i && pos.back() > i) {
                DP1[i] += min(f2[pos.front()], f2[pos.back()]);
                pos.pop_front(); pos.pop_back();
            } else {
                DP1[i] += f2[pos.front()];
                pos.pop_front();
            }
        }
    }

    cout << *min_element(DP1.begin(), DP1.end());
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...