#include<iostream>
#include <vector>
#define int long long
using namespace std;
int tab[1007];
int zap[1007];
main()
{
vector<int> p;
int n,k, fl, odp=0, ost, ret=(1LL<<60);
cin>>n>>k;
for(int a=0; a<n; a++)
cin>>tab[a];
for(int a=0; a<n; a++)
{
p={-1,0};
for(int i=0; i<n; i++){
if(zap[i]==0 && p[0]<tab[i])
p={tab[i],i};
}
zap[p[1]]=1;
if(p[1]>0 && zap[p[1]-1]==0){
odp+=max(0LL,tab[p[1]]-tab[p[1]-1]-k);
tab[p[1]-1]=max(tab[p[1]-1],tab[p[1]]-k);
}
if(p[1]<n-1 && zap[p[1]+1]==0){
odp+=max(0LL,tab[p[1]]-tab[p[1]+1]-k);
tab[p[1]+1]=max(tab[p[1]+1],tab[p[1]]-k);
}
}
cout<<odp;
}
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 | #include<iostream> #include <vector> #define int long long using namespace std; int tab[1007]; int zap[1007]; main() { vector<int> p; int n,k, fl, odp=0, ost, ret=(1LL<<60); cin>>n>>k; for(int a=0; a<n; a++) cin>>tab[a]; for(int a=0; a<n; a++) { p={-1,0}; for(int i=0; i<n; i++){ if(zap[i]==0 && p[0]<tab[i]) p={tab[i],i}; } zap[p[1]]=1; if(p[1]>0 && zap[p[1]-1]==0){ odp+=max(0LL,tab[p[1]]-tab[p[1]-1]-k); tab[p[1]-1]=max(tab[p[1]-1],tab[p[1]]-k); } if(p[1]<n-1 && zap[p[1]+1]==0){ odp+=max(0LL,tab[p[1]]-tab[p[1]+1]-k); tab[p[1]+1]=max(tab[p[1]+1],tab[p[1]]-k); } } cout<<odp; } |
English