#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define st first
#define nd second
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
const int maxn = 1003;
ll tab[maxn];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin>>n>>k;
ll wyn = 0;
for(int i=1;i<=n;i++){
cin>>tab[i];
}
int x;
for(int i=2;i<=n;i++){
if(tab[i]+k < tab[i-1]){
wyn += (tab[i-1]-k)-tab[i];
tab[i] = tab[i-1]-k;
}
x = i;
while(x > 1 && tab[x] > tab[x-1]+k){
wyn += (tab[x]-k)-tab[x-1];
tab[x-1] = tab[x]-k;
x--;
}
}
cout<<wyn<<"\n";
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 42 43 44 45 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define st first #define nd second #define pb push_back #define pii pair<int,int> #define pll pair<ll,ll> const int maxn = 1003; ll tab[maxn]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k; cin>>n>>k; ll wyn = 0; for(int i=1;i<=n;i++){ cin>>tab[i]; } int x; for(int i=2;i<=n;i++){ if(tab[i]+k < tab[i-1]){ wyn += (tab[i-1]-k)-tab[i]; tab[i] = tab[i-1]-k; } x = i; while(x > 1 && tab[x] > tab[x-1]+k){ wyn += (tab[x]-k)-tab[x-1]; tab[x-1] = tab[x]-k; x--; } } cout<<wyn<<"\n"; return 0; } |
English