ANARC05B - The Double HeLiX


Two finite, 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:

  1. You may start at the beginning of any of the two sequences. Now start moving forward.
  2. 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 finding 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 specified on two separate lines. Each line denotes a sequence and is specified 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
deepika bagaria: 2013-03-27 07:46:37

getting WA....plz put up some cases..

LeBron: 2013-02-10 17:29:42

My first accepted solution gets RE on original tests from ANARC05, but here it got AC)

Ouditchya Sinha: 2013-01-25 10:10:55

@v_m thanks for your suggestion, finally got AC :)

hemalatha: 2013-01-19 16:38:27

@v_m thanks for the test case....
it helped a lot...:)

Abhishek Tiwari: 2013-01-08 17:33:11

nice question.....it gives me the memory of assembly line scheduling....

avinash kumar: 2012-12-16 13:26:26

all the numbers in a list are unique or repeated can be there?
no repeation

Last edit: 2013-03-24 15:01:39
mallipeddi hardhik: 2012-12-14 06:30:24

thanks for the link to ACM in the comments it helped :)

Last edit: 2012-12-14 06:55:30
#vaidy_MIT#: 2012-12-09 09:47:25

Be careful with this case
4 1 2 3 4
4 5 6 7 8
ANS:26
There can be NO intersection as well :) GL:)
Got 3 WA because of ds :(

Last edit: 2012-12-09 09:48:12
Akhil Rao: 2012-10-23 15:30:34

can it happen that there is no intersection element between the two??

hot: 2012-09-26 17:35:26

working well on all cases i have checked but still giving WA, any one plz provide some tricky test cases ..soon


Added by:psetter
Date:2009-07-05
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ANARC 2005