fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FAST std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
  4. #define int long long
  5. int32_t main (){
  6. FAST
  7. int tc; cin >> tc;
  8. while (tc--) {
  9. int n; cin >> n;
  10. int a[n], sum = 0;
  11. for (int i = 0; i < n; ++i) {
  12. cin >> a[i];
  13. sum += a[i];
  14. }
  15.  
  16. int Y = sum / n + (sum % n != 0);
  17. int ans = 1e17;
  18. //cout << Y << "\n";
  19. for (int i = Y - 10; i < Y + 10; ++i) {
  20. int tmp = 0;
  21. for (auto &ele : a) {
  22. tmp += ( (i - ele) * (i - ele) );
  23. }
  24.  
  25. ans = min(ans, tmp);
  26. }
  27.  
  28. cout << ans << "\n";
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5452KB
stdin
3
3
1 1 3
3
4 2 5
4
5 5 7 7
stdout
3
5
4