Advertisement
Ahmed_Negm

Untitled

Mar 7th, 2023
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. ll n; cin>>n;
  2. vector<ll>v(n);
  3. for(int i=0;i<n;i++)cin>>v[i];
  4. sort(all(v));
  5. ll mini = 0,maxi = 0;
  6. for(int i=0;i<n-1;i++){
  7.     mini += abs(v[i]-v[i+1]);
  8. }
  9. vector<ll>m(n),m2(n);
  10. ll l = 0,r = n-1;
  11. for(int i=0;i<n;i++){
  12.     if(i&1)m[i] = v[l++];
  13.     else m[i] = v[r--];
  14. }
  15. l=0,r=n-1;
  16. for(int i=0;i<n;i++){
  17.     if(i&1)m2[i] = v[r--];
  18.     else m2[i] = v[l++];
  19. }
  20. ll tmp = 0;
  21. for(int i=0;i<n-1;i++){
  22.     tmp += abs(m2[i]-m2[i+1]);
  23. }
  24. for(int i=0;i<n-1;i++){
  25.     maxi += abs(m[i]-m[i+1]);
  26. }
  27. maxi = max(maxi,tmp);
  28. cout<<mini<<" "<<maxi<<nl;
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement