#include<bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 1e3 + 9;
ll tab[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, k, wyn=0;
cin>>n>>k;
for(int i=0; i<n; i++)
cin >> tab[i];
if(n == 1) {
cout<<0;
return 0;
}
for(int j=0; j<n; j++) {
for(int i=0; i<n-1; i++) {
if(tab[i] < tab[i+1] - k) {
wyn += (tab[i+1] - k) - tab[i];
tab[i] += ((tab[i+1] - k) - tab[i]);
}
else if(tab[i] - k > tab[i+1]) {
wyn += (tab[i] - k - tab[i+1]);
tab[i+1] += (tab[i] - k) - tab[i+1];
}
}
}
cout << wyn;
}
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 | #include<bits/stdc++.h> using namespace std; using ll = long long; constexpr int N = 1e3 + 9; ll tab[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, k, wyn=0; cin>>n>>k; for(int i=0; i<n; i++) cin >> tab[i]; if(n == 1) { cout<<0; return 0; } for(int j=0; j<n; j++) { for(int i=0; i<n-1; i++) { if(tab[i] < tab[i+1] - k) { wyn += (tab[i+1] - k) - tab[i]; tab[i] += ((tab[i+1] - k) - tab[i]); } else if(tab[i] - k > tab[i+1]) { wyn += (tab[i] - k - tab[i+1]); tab[i+1] += (tab[i] - k) - tab[i+1]; } } } cout << wyn; } |
English