fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int n, m;
  9. cin >> n >> m;
  10.  
  11. vector<long long> vertical(n);
  12. vector<long long> horizontal(m);
  13.  
  14. for(int i = 0; i < n; i++)
  15. cin >> vertical[i];
  16.  
  17. for(int i = 0; i < m; i++)
  18. cin >> horizontal[i];
  19.  
  20. sort(vertical.rbegin(), vertical.rend());
  21. sort(horizontal.rbegin(), horizontal.rend());
  22.  
  23. long long cost = 0;
  24. long long verticalParts = 1;
  25. long long horizontalParts = 1;
  26.  
  27. int i = 0, j = 0;
  28.  
  29. while(i < n && j < m) {
  30. if(vertical[i] > horizontal[j]) {
  31. cost += vertical[i] * horizontalParts;
  32. verticalParts++;
  33. i++;
  34. } else {
  35. cost += horizontal[j] * verticalParts;
  36. horizontalParts++;
  37. j++;
  38. }
  39. }
  40.  
  41. while(i < n) {
  42. cost += vertical[i] * horizontalParts;
  43. i++;
  44. }
  45.  
  46. while(j < m) {
  47. cost += horizontal[j] * verticalParts;
  48. j++;
  49. }
  50.  
  51. cout << cost << "\n";
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5324KB
stdin
6 4
2
1
3
1
4
4
1
2
stdout
34