fork download
  1. #include <iostream>
  2.  
  3. int median(int *ai, int *aj, int k) {
  4. int i = 0, j = 0;
  5. int count = 1;
  6. while (count < k) {
  7. ai[i] <= aj[j] ? i++ : j++;
  8. count++;
  9. }
  10. return ai[i] < aj[j] ? ai[i] : aj[j];
  11. }
  12.  
  13.  
  14. int main() {
  15. int n,l;
  16. std::cin >> n >> l;
  17. int arr[n][l] = {{0}};
  18. int sum = 0;
  19.  
  20. for (int i = 0; i < n; i++)
  21. for (int j = 0; j < l; j++)
  22. std::cin >> arr[i][j];
  23.  
  24. for (int i = 0; i < n - 1; i++)
  25. for (int j = i + 1; j < n; j++)
  26. sum += median(arr[i], arr[j], l);
  27.  
  28. std::cout << "\nSum is: " << sum;
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 4176KB
stdin
3 6
1 2 3 4 5 6
3 4 5 6 7 8
0 0 1 1 2 2
stdout
Sum is: 8