fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, m;
  6. cin >> n >> m;
  7.  
  8. priority_queue<int> tab;
  9. for(int i = 0; i < n-1; i++){
  10. int a;
  11. cin >> a;
  12. tab.push(a);
  13. }
  14.  
  15. priority_queue<int> bajtek;
  16. for(int i = 0; i < m-1; i++){
  17. int a;
  18. cin >> a;
  19. bajtek.push(a);
  20.  
  21. }
  22.  
  23. long long s = 0;
  24. int x = 1, y = 1;
  25.  
  26. for(int i=0; i<n+m-2; i++) {
  27. if(bajtek.top() >= tab.top()) {
  28. s += bajtek.top() * x;
  29. bajtek.pop();
  30. y++;
  31. }
  32. else {
  33. s += tab.top() * y;
  34. tab.pop();
  35. x++;
  36. }
  37. }
  38.  
  39. cout << s;
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5308KB
stdin
6 4
2
1
3
1
4
4
1
4
stdout
47