#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
ll k=0,n=0,ans=0; vector<pair<ll,ll>> A,B; vector<ll> T;
cin>>n>>k;
for(int i=0; i<n; ++i){ll KWA; cin>>KWA; A.push_back({KWA,i}); T.push_back(KWA);}
for(int i=0; i<n; ++i)
{
ll mx=-1,dx=-1;
for(int j=0; j<n; ++j)
{
if(A[j].second==-1)continue;
if(A[j].first>mx){mx=A[j].first; dx=A[j].second;}
}
A[dx].second=-1;
if(dx!=0 and A[dx].first-A[dx-1].first>k){ans+=A[dx].first-k-A[dx-1].first; A[dx-1].first+=A[dx].first-k-A[dx-1].first;}
if(dx!=n-1 and A[dx].first-A[dx+1].first>k){ans+=A[dx].first-k-A[dx+1].first; A[dx+1].first+=A[dx].first-k-A[dx+1].first;}
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int MAKUMBA=1;
//cin>>MAKUMBA;
for(int KOKOJAMBO=0; KOKOJAMBO<MAKUMBA; ++KOKOJAMBO){solve();}
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; void solve() { ll k=0,n=0,ans=0; vector<pair<ll,ll>> A,B; vector<ll> T; cin>>n>>k; for(int i=0; i<n; ++i){ll KWA; cin>>KWA; A.push_back({KWA,i}); T.push_back(KWA);} for(int i=0; i<n; ++i) { ll mx=-1,dx=-1; for(int j=0; j<n; ++j) { if(A[j].second==-1)continue; if(A[j].first>mx){mx=A[j].first; dx=A[j].second;} } A[dx].second=-1; if(dx!=0 and A[dx].first-A[dx-1].first>k){ans+=A[dx].first-k-A[dx-1].first; A[dx-1].first+=A[dx].first-k-A[dx-1].first;} if(dx!=n-1 and A[dx].first-A[dx+1].first>k){ans+=A[dx].first-k-A[dx+1].first; A[dx+1].first+=A[dx].first-k-A[dx+1].first;} } cout<<ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int MAKUMBA=1; //cin>>MAKUMBA; for(int KOKOJAMBO=0; KOKOJAMBO<MAKUMBA; ++KOKOJAMBO){solve();} return 0; } |
English