#include "bits/stdc++.h"
#define all(v) (v).begin(), (v).end()
#define st first
#define nd second
#define printv(a) { for(auto u : a) cout<<u<<" "; cout<<"\n"; }
#define debug(x) cerr << #x << " = " << x << '\n';
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using vi = vector<int>;
using si = set<int>;
using mii = map<int,int>;
int tab[1005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin>>n>>k;
int wyn = 0;
for(int i=0;i<n;i++){
cin>>tab[i];
if(i==0) continue;
if(tab[i] < tab[i-1] - k){
wyn += tab[i-1] - k - tab[i];
tab[i] = tab[i-1] - k;
}
}
for(int i=n-1;i>=0;i--){
if(i==n-1) continue;
if(tab[i] < tab[i+1] - k){
wyn += tab[i+1] - k - tab[i];
tab[i] = tab[i+1] - k;
}
}
cout<<wyn<<endl;
return 0;
}
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 | #include "bits/stdc++.h" #define all(v) (v).begin(), (v).end() #define st first #define nd second #define printv(a) { for(auto u : a) cout<<u<<" "; cout<<"\n"; } #define debug(x) cerr << #x << " = " << x << '\n'; using namespace std; using ll = long long; using pii = pair<int,int>; using vi = vector<int>; using si = set<int>; using mii = map<int,int>; int tab[1005]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k; cin>>n>>k; int wyn = 0; for(int i=0;i<n;i++){ cin>>tab[i]; if(i==0) continue; if(tab[i] < tab[i-1] - k){ wyn += tab[i-1] - k - tab[i]; tab[i] = tab[i-1] - k; } } for(int i=n-1;i>=0;i--){ if(i==n-1) continue; if(tab[i] < tab[i+1] - k){ wyn += tab[i+1] - k - tab[i]; tab[i] = tab[i+1] - k; } } cout<<wyn<<endl; return 0; } |
English