This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,k,a[200001],h[200001],mmax,mm;
long long ans,sum;
map<int,int> cnt;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>k;
for (int i=1; i<=n; ++i) cin>>h[i],sum+=h[i];
sum/=n;
if (k==0) {
for (int i=1; i<=n; ++i) ans+=abs(sum-h[i]);
cout<<ans;
return 0;
}
a[n]=h[n];
for (int i=n; i>1; --i) {
if (a[i]-h[i-1]>k) a[i-1]=a[i]-k;
else if (h[i-1]-a[i]>k) a[i-1]=a[i]+k;
else a[i-1]=h[i-1];
ans+=abs(a[i-1]-h[i-1]);
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |