ANARC05B  The Double HeLiX
Two ﬁnite, strictly increasing, integer sequences are given. Any common integer between the two sequences constitute an intersection point. Take for example the following two sequences where intersection points are
printed in bold:
 First= 3 5 7 9 20 25 30 40 55 56 57 60 62
 Second= 1 4 7 11 14 25 44 47 55 57 100
You can ‘walk” over these two sequences in the following way:
 You may start at the beginning of any of the two sequences. Now start moving forward.
 At each intersection point, you have the choice of either continuing with the same sequence you’re currently on, or switching to the other sequence.
The objective is ﬁnding a path that produces the maximum sum of data you walked over. In the above example, the largest possible sum is 450, which is the result of adding 3, 5, 7, 9, 20, 25, 44, 47, 55, 56, 57, 60, and 62
Input
Your program will be tested on a number of test cases. Each test case will be speciﬁed on two separate lines. Each line denotes a sequence and is speciﬁed using the following format:
n v1 v2 ... vn
Where n is the length of the sequence and vi is the ith element in that sequence. Each sequence will have at least one element but no more than 10,000. All elements are between 10,000 and 10,000 (inclusive).
The last line of the input includes a single zero, which is not part of the test cases.
Output
For each test case, write on a separate line, the largest possible sum that can be produced.
Sample
Input: 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 0 Output: 450 2100
hide comments
sophozaar:
20181209 17:18:12
@adityad1998, perfect hint :P


lamia2658:
20181127 16:27:35
can be done easily with greedy and set_intersection()


masterchef2209:
20180823 11:43:43
AC in 1 go :D


shrikant_7:
20180820 08:15:21
problem with BufferedReader in java, try with some other fast i/o class. 

mit_gundigara:
20180814 19:55:43
is test case is in strictly increasing order? 

adityad1998:
20180704 12:06:41
No DP required.


foool:
20180617 21:44:27
greedy.. no dp reqiured 

soham_12345:
20180616 08:21:28
greedy 

anurag_tangri:
20180421 10:49:53
Used binary_search() and lower_bound() function . O(n+mlogn) 

shivam_2296:
20180407 18:45:34
Hashing + 2 pointers = AC 
Added by:  ~!(*(@*!@^& 
Date:  20090705 
Time limit:  0.130s0.686s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ERL JSRHINO NODEJS PERL6 VB.NET 
Resource:  ANARC 2005 