fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int n;
  7. cin >> n;
  8.  
  9. vector<int> arr(n);
  10.  
  11. for(int i=0; i<n; i++) {
  12. cin >> arr[i];
  13. }
  14.  
  15. int q;
  16. cin >> q;
  17.  
  18. for(int i=0; i<q; i++) {
  19. int target;
  20. cin >> target;
  21.  
  22. int min_operation = 0;
  23.  
  24. for(int i=0; i<n; i++) {
  25. min_operation += abs(arr[i] - target);
  26. }
  27. cout << "Minimum operations for target " << target << " is : " << min_operation << endl;
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 5312KB
stdin
5
1
2
3
4
5
2
2
3
stdout
Minimum operations for target 2 is : 7
Minimum operations for target 3 is : 6