fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define N 600005
  6.  
  7. int tc = 1, n, m, a[N], b[N], vis[N], c[N], k[N];
  8. priority_queue <pair<int,int>> s;
  9.  
  10. int32_t main() {
  11. ios::sync_with_stdio(0);cin.tie(0);
  12.  
  13. cin >> tc;
  14. while(tc--) {
  15. cin >> n >> m;
  16. for(int i = 1; i <= n; i++) {
  17. cin >> a[i];
  18. }
  19. for(int i = 1; i <= n; i++) {
  20. cin >> b[i];
  21. }
  22. int ans = 0;
  23. bool tr = 0;
  24. priority_queue <int> q, s;
  25. for(int i = n; i >= 1; i--) {
  26. q.push(a[i]);
  27. while(b[i] > 0 && (!q.empty() || !s.empty())) {
  28. while(!s.empty() && b[i] > 0) {
  29. int t = s.top();
  30. int pot = min(b[i],t);
  31. s.pop();
  32. b[i] -= pot;
  33. t -= pot;
  34. if(t) s.push(t);
  35. }
  36. if(s.empty() && b[i]) {
  37. while(!q.empty() && b[i] > 0) {
  38. ans++;
  39. int t = q.top();
  40. int pot = min(b[i],t);
  41. q.pop();
  42. b[i] -= pot;
  43. t -= pot;
  44. if(t) s.push(t);
  45. }
  46. }
  47. }
  48. if(b[i]) {
  49. ans = -1;
  50. break;
  51. }
  52. }
  53. cout << ans << '\n';
  54. }
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
0