fork download
  1. /*
  2.   author: Tran Van Nam
  3.   Nguyen Trai High School - Quang Binh
  4. */
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. const long long INF = 1e18;
  9.  
  10. signed main(){
  11. ios_base::sync_with_stdio(0);
  12. cin.tie(0); cout.tie(0);
  13.  
  14. int n; cin >> n;
  15. vector <int> a(n);
  16. for (auto &x: a) cin >> x;
  17.  
  18. long long l = 1, r = 1e9, ans = INF;
  19. while (l <= r){
  20. long long mid = l + r >> 1;
  21. long long sum = 0;
  22. for (auto &x: a) sum += abs(x - mid);
  23. if (ans >= sum){
  24. ans = sum;
  25. r = mid - 1;
  26. }
  27. else l = mid + 1;
  28. }
  29.  
  30. cout << ans;
  31.  
  32. return 0^0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty