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 38 39 40 41 42 43 44 45 46 47
| #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 2e5+10; ll mned[N],msum[N],cned[N],csum[N]; int a[N]; ll query(ll presum[],int l,int r){ return presum[l]-presum[r+1]; } int main(){ int n,k,lc,lm,pm,pc,t,d; cin>>n>>k; cin>>lc>>pc>>lm>>pm; cin>>t>>d; for(int i=0;i<n;i++)cin>>a[i]; sort(a,a+n); mned[n]=msum[n]=cned[n]=csum[n]=0; for(int i=n-1;i>=0;i--){ mned[i]=mned[i+1]+max(lm-a[i],0); msum[i]=msum[i+1]+max(a[i]-lm,0); cned[i]=cned[i+1]+max(lc-a[i],0); csum[i]=csum[i+1]+max(a[i]-lc,0); } ll sum = 0; ll ans = __LONG_LONG_MAX__; for(int c=0;c*k<n;c++){ int m = n-c*k; for(int j=max(0,(c-1)*(k-1));j<c*(k-1);j++){ sum+=min(d,a[j]-1); } if(sum+query(msum,n-m-c,n-c-1)+query(csum,n-c,n-1)>=query(mned,n-m-c,n-c-1)+query(cned,n-c,n-1)){ ll price = 1ll*(query(mned,n-m-c,n-c-1)+query(cned,n-c,n-1))*t+1ll*pc*c+1ll*pm*m; ans=min(ans,price); } } int c=(n+k-1)/k; sum = 0; for(int i=0;i<n-c;i++)sum+=min(d,a[i]-1); if(sum+query(csum,n-c,n-1)>=query(cned,n-c,n-1)){ ll price = 1ll*query(cned,n-c,n-1)*t+1ll*pc*c; ans=min(ans,price); } if(ans==__LONG_LONG_MAX__)printf("-1\n"); else printf("%lld\n",ans); }
|