fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define endl "\n"
  5. #define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
  6. #define pb push_back
  7. #define yes cout<<"YES"<<endl
  8. #define no cout<<"NO"<<endl
  9. #define F first
  10. #define S second
  11. #define C continue
  12. #define R return
  13. const ll N=5e5+9;
  14. const ll mod=1e9+7;
  15. const ll oo=1e18;
  16. const ll M=1e6+10;
  17. ll T,n,a[N],x,ans;
  18. int main()
  19. {
  20. fast
  21. //freopen("input.in","r",stdin) ;
  22. //freopen("output.txt","w",stdout);
  23. T=1;
  24. //cin>>T;
  25. while(T--)
  26. {
  27. cin>>n;
  28. for(int i=1;i<=n;i++){
  29. cin>>a[i];
  30. }
  31. sort(a+1,a+n+1);
  32. x=a[(n+1)/2];
  33. ans=0;
  34. for(int i=1;i<=n;i++){
  35. ans+=abs(a[i]-x);
  36. }
  37. cout<<ans;
  38. }
  39. return 0;
  40. }
  41.  
  42. /*
  43.  
  44.  
  45. */
  46.  
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty