#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int>V(n);
for(int i = 0; i < n; i++){
int a;
cin >> a;
V[i] = a;
}
ll w = 0;
vector<int>byl(n+99);
for(int ep = 0; ep < 5*n+99;ep++){
pair<int,int>maks = {0,0};
for(int i = 0; i < n; i++){
if(byl[i] < 3)
maks = max(maks, make_pair(V[i],i));
}
int i = maks.second;
byl[i]++;
for(auto j : {i-1,i+1}){
if(j < 0 || j >= n)continue;
if(V[j] < V[i] - k){
// cout << i << ", " << j << '\n';
// cout << V[i] - k << " > " << V[j] << endl;
w += V[i] - k - V[j];
V[j] = V[i] - k;
}
}
}
cout << w << '\n';
}
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<bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int>V(n); for(int i = 0; i < n; i++){ int a; cin >> a; V[i] = a; } ll w = 0; vector<int>byl(n+99); for(int ep = 0; ep < 5*n+99;ep++){ pair<int,int>maks = {0,0}; for(int i = 0; i < n; i++){ if(byl[i] < 3) maks = max(maks, make_pair(V[i],i)); } int i = maks.second; byl[i]++; for(auto j : {i-1,i+1}){ if(j < 0 || j >= n)continue; if(V[j] < V[i] - k){ // cout << i << ", " << j << '\n'; // cout << V[i] - k << " > " << V[j] << endl; w += V[i] - k - V[j]; V[j] = V[i] - k; } } } cout << w << '\n'; } |
English