#include <bits/stdc++.h>
#include <cmath>
#define ll long long
#define fir first
#define sec second
using namespace std;
ll odp;
ll tab[1000005];
ll x,y;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>x>>y;
for(int i=0;i<x;i++){
cin>>tab[i];
}
for(int i=1;i<x;i++){
if(tab[i]-tab[i-1]>y || tab[i-1]-tab[i]>y){
if(tab[i]>tab[i-1]){
odp+=tab[i]-tab[i-1]-y;
tab[i-1]+=tab[i]-tab[i-1]-y;
}else{
odp+=tab[i-1]-tab[i]-y;
tab[i]+=tab[i-1]-tab[i]-y;
}
}
}
for(int i=x-2;i>=0;i--){
if(tab[i]-tab[i+1]>y || tab[i+1]-tab[i]>y){
if(tab[i]>tab[i+1]){
odp+=tab[i]-tab[i+1]-y;
tab[i+1]+=tab[i]-tab[i+1]-y;
}else{
odp+=tab[i+1]-tab[i]-y;
tab[i]+=tab[i+1]-tab[i]-y;
}
}
}
cout<<odp;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <bits/stdc++.h> #include <cmath> #define ll long long #define fir first #define sec second using namespace std; ll odp; ll tab[1000005]; ll x,y; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>x>>y; for(int i=0;i<x;i++){ cin>>tab[i]; } for(int i=1;i<x;i++){ if(tab[i]-tab[i-1]>y || tab[i-1]-tab[i]>y){ if(tab[i]>tab[i-1]){ odp+=tab[i]-tab[i-1]-y; tab[i-1]+=tab[i]-tab[i-1]-y; }else{ odp+=tab[i-1]-tab[i]-y; tab[i]+=tab[i-1]-tab[i]-y; } } } for(int i=x-2;i>=0;i--){ if(tab[i]-tab[i+1]>y || tab[i+1]-tab[i]>y){ if(tab[i]>tab[i+1]){ odp+=tab[i]-tab[i+1]-y; tab[i+1]+=tab[i]-tab[i+1]-y; }else{ odp+=tab[i+1]-tab[i]-y; tab[i]+=tab[i+1]-tab[i]-y; } } } cout<<odp; } |
English