fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n,l;
  7. cin >> n >> l;
  8.  
  9. long long arr [n+1][l+1];
  10.  
  11. for(int i=1; i<=n; i++)
  12. {
  13. for(int j=1; j<=l; j++)
  14. {
  15. cin >> arr[i][j];
  16. }
  17. }
  18.  
  19. long long sum = 0;
  20.  
  21. for(int i=1; i<n; i++)
  22. {
  23. for(int j=i+1; j<=n; j++)
  24. {
  25. int ai,aj;
  26. ai = aj = 0;
  27. while(true)
  28. {
  29. int temp;
  30. if(arr[i][ai+1] < arr[j][aj+1])
  31. {
  32. ai ++;
  33. temp = arr[i][ai];
  34. }
  35. else
  36. {
  37. aj ++;
  38. temp = arr[j][aj];
  39. }
  40. if(ai + aj == l)
  41. {
  42. sum += temp;
  43. break;
  44. }
  45. }
  46. }
  47. }
  48.  
  49. cout << sum;
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 15232KB
stdin
3 6
1 2 3 4 5 6
3 4 5 6 7 8
0 0 1 1 2 2
stdout
8