To give you an array, you can be at most k operations, each operation can make a number +1 or -1, the smallest of the array after poor operation may ask is how much
Use map to simulate the movement can be observed that each should choose a small number of a set of numbers so that they move is optimal
int main () { int n; ll k; cin >> n >> k; vector<int> a(n); map<int,int> ls; for(int i = 0 ; i < n ; i++) cin >> a[i],ls[a[i]]++; while(ls.size() > 1) { ls.begin auto l = (); auto r = ls.end(); --r; Car ++ nl = l; --l; auto nr = --r; r++; if(l->se <= r-> se) { ll cost = l->se * 1ll * (nl->fi - l->fi); if(k <= cost) break; k -= cost; nl-> is + = l-> se; ls.erase (l); } else { ll cost = r->se * 1ll * (r->fi - nr->fi); if(k <= cost) break; k -= cost; The Nr-> + = Su from>; ls.erase (r); } } int c1 = ls.begin()->se; int c2 = ls.rbegin()->se; int mins = k / min(c1 , c2); cout << max(0 , ls.rbegin()->fi - ls.begin()->fi - mins) << endl; }