fork download
  1.  
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. class ANARC05B
  6. {
  7. public static void main(String args[])
  8. throws IOException
  9. {
  10. String str1="";
  11. try{
  12. while(!(str1=br.readLine()).equals("0"))
  13. {
  14. String str2=br.readLine();
  15. StringTokenizer st1=new StringTokenizer(str1," +");
  16. StringTokenizer st2=new StringTokenizer(str2," +");
  17. int arr1[]=new int[Integer.parseInt(st1.nextToken())];
  18. int arr2[]=new int[Integer.parseInt(st2.nextToken())];
  19. int pos=0;
  20. ArrayList<Integer> al=new ArrayList<Integer>();
  21. ArrayList<Integer> list=new ArrayList<Integer>();
  22. while(st1.hasMoreTokens()){
  23. arr1[pos++]=Integer.parseInt(st1.nextToken());al.add(arr1[pos-1]);}
  24. pos=0;
  25. while(st2.hasMoreTokens()){
  26. arr2[pos++]=Integer.parseInt(st2.nextToken());
  27. if(al.contains(arr2[pos-1]))
  28. list.add(arr2[pos-1]);
  29. }
  30.  
  31. int sum1=0,sum2=0,beg=0, main_sum=0,p1=0,p2=0;
  32.  
  33. //if there are no intersections at all
  34. if(list.isEmpty())
  35. {
  36. if(arr1.length<arr2.length)
  37. {
  38. for(int i=0;i<arr1.length;i++)
  39. {
  40. sum1+=arr1[i];
  41. sum2+=arr2[i];
  42. }
  43. for(int i=arr1.length;i<arr2.length;i++)
  44. sum2+=arr2[i];
  45.  
  46. main_sum+=Math.max(sum1, sum2);
  47. System.out.println(main_sum);
  48. continue;
  49. }
  50. else if(arr2.length<arr1.length)
  51. {
  52. for(int i=0;i<arr2.length;i++)
  53. {
  54. sum1+=arr1[i];
  55. sum2+=arr2[i];
  56. }
  57. for(int i=arr2.length;i<arr1.length;i++)
  58. sum1+=arr1[i];
  59.  
  60. main_sum+=Math.max(sum1, sum2);
  61. System.out.println(main_sum);
  62. continue;
  63. }
  64. else//same length of both arrays
  65. {
  66. for(int i=0;i<arr2.length;i++)
  67. {
  68. sum1+=arr1[i];
  69. sum2+=arr2[i];
  70. }
  71. main_sum+=Math.max(sum1, sum2);
  72. System.out.println(main_sum);
  73. continue;
  74. }
  75. }
  76.  
  77.  
  78. Iterator<Integer> iter=list.iterator();
  79. while(iter.hasNext())
  80. {
  81. int intersect=iter.next();
  82. p1=Arrays.binarySearch(arr1, intersect);
  83. p2=Arrays.binarySearch(arr2, intersect);
  84. if(p1==p2)
  85. {
  86. for(int i=beg;i<=p1;i++)
  87. {
  88. sum1+=arr1[i];
  89. sum2+=arr2[i];
  90. }
  91. main_sum+=Math.max(sum1, sum2);
  92. beg=p1+1;
  93. }
  94. else if(p1>p2)
  95. {
  96. for(int i=beg;i<=p2;i++)
  97. {
  98. sum1+=arr1[i];
  99. sum2+=arr2[i];
  100. }
  101. for(int i=p2+1;i<=p1;i++)
  102. sum1+=arr1[i];
  103. main_sum+=Math.max(sum1, sum2);
  104. beg=p1+1;
  105. }
  106. else if(p2>p1)
  107. {
  108. for(int i=beg;i<=p1;i++)
  109. {
  110. sum1+=arr1[i];
  111. sum2+=arr2[i];
  112. }
  113. for(int i=p1+1;i<=p2;i++)
  114. sum2+=arr2[i];
  115.  
  116. main_sum+=Math.max(sum1, sum2);
  117. beg=p2+1;
  118. }
  119.  
  120. sum1=sum2=0;
  121. if(beg-p1<beg-p2)
  122. {
  123. for(int i=beg-1;i>p2;i--)
  124. sum2+=arr2[i];
  125. }
  126. else if(beg-p1>beg-p2)
  127. {
  128. for(int i=beg-1;i>p1;i--)
  129. sum1+=arr1[i];
  130. }
  131. }
  132.  
  133. //calculating from last intersection point till the end
  134. sum1=sum2=0;
  135. for(int i=p1+1;i<arr1.length;i++)
  136. sum1+=arr1[i];
  137. for(int i=p2+1;i<arr2.length;i++)
  138. sum2+=arr2[i];
  139. main_sum+=Math.max(sum1, sum2);
  140. System.out.println(main_sum);
  141. }
  142. }//try block
  143. catch(Exception e)
  144. {return;}
  145. }
  146. }
Success #stdin #stdout 0.07s 380224KB
stdin
13 3 5 7 9 20 25 30 40 55 56 57 60 62
11 1 4 7 11 14 25 44 47 55 57 100
4 -5 100 1000 1005
3 -12 1000 1001
4 1 2 3 4 
4 5 6 7 8 
4 1 2 3 4 
3 3 7 10 
4 -30 -20 -10 10 
4 -100 -80 -60 -30 
0
stdout
450
2100
26
23
-30