fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. const int N=2e5+5;
  5. ll n,m,a[N],b[N];
  6. bool can(ll mid)
  7. {
  8. ll cur=a[0],o=0;
  9. for(int i=1;i<n;i++)
  10. {
  11. if(cur+a[i]<=mid)
  12. {
  13. cur+=a[i];
  14. }
  15. else
  16. {
  17. while(o<m&&cur+a[i]>mid)
  18. {
  19. cur+=b[o];
  20. o++;
  21. }
  22. cur=max(0LL,cur);
  23. cur+=a[i];
  24. if(cur>mid) return 0;
  25. }
  26. }
  27. return cur<=mid;
  28. }
  29. signed main()
  30. {
  31. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  32. cin>>n>>m;
  33. for(int i=0;i<n;i++)
  34. {
  35. cin>>a[i];
  36. }
  37. for(int o=0;o<m;o++)
  38. {
  39. cin>>b[o];
  40. }
  41. ll l=*max_element(a,a+n),r=1e17,ans=l;
  42. while(l<=r)
  43. {
  44. ll mid=(l+r)/2;
  45. if(can(mid))
  46. {
  47. ans=mid;
  48. r=mid-1;
  49. }
  50. else
  51. {
  52. l=mid+1;
  53. }
  54. }
  55. cout<<ans;
  56. return 0;
  57. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty