#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
int out = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[j] > a[i] + ((j-i) * k)){
int x = a[j] - (a[i] + ((j-i) * k));
out += x;
a[i] += x;
}
if(a[j] < a[i] - ((j-i) * k)){
int x = abs(a[j] - (a[i] - ((j-i) * k)));
out += x;
a[j] += x;
}
}
}
cout<<out;
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 | #include <iostream> #include <string> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int> a(n); int out = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(a[j] > a[i] + ((j-i) * k)){ int x = a[j] - (a[i] + ((j-i) * k)); out += x; a[i] += x; } if(a[j] < a[i] - ((j-i) * k)){ int x = abs(a[j] - (a[i] - ((j-i) * k))); out += x; a[j] += x; } } } cout<<out; return 0; } |
English