fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void operations(vector<long long> queries, vector<long long> arr){
  5. for (int i = 0; i < queries.size(); i++){
  6. long long op = 0;
  7.  
  8. for (int j = 0; j < arr.size(); j++){
  9. op += abs(queries[i]-arr[j]);
  10. }
  11.  
  12. cout << op << endl;
  13. }
  14.  
  15. }
  16.  
  17. int main() {
  18. // your code goes here
  19. int n, q;
  20. cin >> n >> q;
  21. vector<long long> arr(n);
  22. vector<long long> queries(q);
  23. for (int i = 0; i < n; i ++){
  24. cin >> arr[i];
  25. }
  26. for (int i = 0; i < q; i ++){
  27. cin >> queries[i];
  28. }
  29.  
  30. operations(queries, arr);
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5320KB
stdin
5 2
1 2 3 4 5
3 2
stdout
6
7