Guest User

Untitled

a guest
Jul 30th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. long int n,m;
  6. cin>>n;
  7. while(n!=0)
  8. {
  9. long int a[n],suma[n],sum=0;
  10. sum=0;
  11.  
  12. for(int i=0;i<n;i++){
  13. cin>>a[i];
  14. sum+=a[i];
  15. suma[i]=sum;
  16. }
  17. cin>>m;
  18. long int b[m],sumb[m];
  19. sum=0;
  20. for(int i=0;i<m;i++){
  21. cin>>b[i];
  22. sum+=b[i];
  23. sumb[i]=sum;
  24. }
  25. long long ans=0,ab=0,ac=0,y=0,x;
  26. for(int i=0;i<n;i++)
  27. {
  28.  
  29. int low=0,high=n,mid,x=-1,val=a[i];
  30. while(low<high)
  31. {
  32. mid=(low+high)/2;
  33. if(b[mid]==val)
  34. {
  35. x=mid;
  36. break;
  37. }
  38. else if(b[mid]>val)
  39. {
  40. high=mid-1;
  41.  
  42. }
  43. else
  44. low=mid+1;
  45. }
  46. ;
  47. if(x>=0)
  48. {
  49. if(y>=1)
  50. {
  51. ans+=max(suma[i]-suma[ab],sumb[x]-sumb[ac]);
  52. }
  53. else
  54. {
  55. ans+=max(suma[i],sumb[x]);
  56. }
  57.  
  58. y++;
  59. ab=i;
  60. ac=x;
  61. }
  62. //cout<<ans<<endl;
  63. //cout<<ans<<endl;
  64.  
  65. }
  66. if((ab<n||ac<m)&&y)
  67. {
  68. ans+=max(suma[n-1]-suma[ab],sumb[m-1]-sumb[ac]);
  69. }
  70. else
  71. {
  72. ans+=max(suma[n-1],sumb[m-1]);
  73. }
  74. cout<<ans<<endl;
  75. cin>>n;
  76. }
  77. return 0;
  78. }
Add Comment
Please, Sign In to add comment