#include <bits/stdc++.h>
using namespace std;
#define pass (void)0
//#define int long long
//signed main(){
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
if(n==1){
cout << '0';
return 0;
}
vector<int> sciezka;
for(int i=0; i<n; i++){
int a;
cin >> a;
sciezka.push_back(a);
}
int res=0;
bool solved=false;
while(!solved){
solved=true;
for(int i=1; i<n; i++){
int r = abs(sciezka[i]-sciezka[i-1]);
if(r-k>0){
solved=false;
if(sciezka[i]>sciezka[i-1]){
sciezka[i-1] += r-k;
res += r-k;
}
else{
sciezka[i] += r-k;
res += r-k;
}
}
}
}
//for(int i=0; i<n; i++){
// cout << sciezka[i] << " ";
//}
//cout << endl;
cout << res;
}
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 46 47 48 49 50 | #include <bits/stdc++.h> using namespace std; #define pass (void)0 //#define int long long //signed main(){ int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; if(n==1){ cout << '0'; return 0; } vector<int> sciezka; for(int i=0; i<n; i++){ int a; cin >> a; sciezka.push_back(a); } int res=0; bool solved=false; while(!solved){ solved=true; for(int i=1; i<n; i++){ int r = abs(sciezka[i]-sciezka[i-1]); if(r-k>0){ solved=false; if(sciezka[i]>sciezka[i-1]){ sciezka[i-1] += r-k; res += r-k; } else{ sciezka[i] += r-k; res += r-k; } } } } //for(int i=0; i<n; i++){ // cout << sciezka[i] << " "; //} //cout << endl; cout << res; } |
English