Submission #386129

#TimeUsernameProblemLanguageResultExecution timeMemory
386129sahil_kGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
2 ms492 KiB
#include <iostream> using namespace std; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n]; for (int i=0; i<n; i++) { cin >> a[i]; } long long pre[n]; pre[0] = 0; for (int i=1; i<n; i++) { pre[i] = pre[i-1]+max(0, a[i-1]-a[i]+1); } long long suf[n]; suf[n-1] = 0; for (int i=n-2; i>=0; i--) { suf[i] = suf[i+1]+max(0, a[i+1]-a[i]+1); } long long o = 1e16; for (int i=0; i<n; i++) { long long cost = 0; if (i > 0) cost += pre[i-1]; if (i < n-1) cost += suf[i+1]; long long extra = 0; if (i > 0) extra = max(extra, a[i-1]-(a[i]+(i+1<n?suf[i+1]:0))+1); if (i < n-1) extra = max(extra, a[i+1]-(a[i]+(i-1>=0?pre[i-1]:0))+1); o = min(o, cost+extra); } cout << o << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...