fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sp " "
  5. #define el '\n'
  6. #define ll long long
  7. void Solution()
  8. {
  9. int n, m;
  10. cin >> n >> m;
  11. vector<int>k(n+1), c(m+1);
  12. deque<int>dq;
  13. for(int i = 1; i <= n; i++) cin >> k[i];
  14. for(int i = 1; i <= m; i++){
  15. c[i] = i;
  16. int x; cin >> x;
  17. dq.push_front(x);
  18. }
  19. sort(k.rbegin(),k.rend());
  20. ll ans = 0;
  21. for(int i = n; i >= 1; i--){
  22. auto lb = lower_bound(c.begin(),c.end(),k[i])- c.begin();
  23. if(lb){
  24. ans+=min(dq.front(),c[k[i]]);
  25. if(dq.front() < c[k[i]]) {
  26. dq.pop_front();
  27. }
  28. }
  29. else {
  30. ans+=c[k[i]];
  31. }
  32. cout << k[i] << " " << ans << el;
  33. }
  34. cout << ans << el;
  35.  
  36. }
  37. int main()
  38. {
  39. ios::sync_with_stdio(0);
  40. cin.tie(0); cout.tie(0);
  41. // int t;
  42. // cin >> t;
  43. // for(int tc = 1; tc <= t; tc++)
  44. Solution();
  45. return 0;
  46. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
0