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

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

hide comments
2014-04-14 07:47:30 Tarun Garg
DP rox
2014-03-12 12:00:43 Sanchit Manchanda
Nice question. AC in first attempt. O(n) solution. :)
2014-01-27 18:09:52 JordanBelfort
good problem . ..:)
little bit of thinking..:)
2014-01-27 15:15:22 Vipul Pandey
this problem can be solved by atleast 3 different ways. But it is an easy one. Got AC in first attempt.
2013-11-30 12:29:42 _maverick
classic DP problem !! thanks for the question , enjoyed solving it :D
2013-11-05 22:07:54 dunnohyet
keep getting wrong ans....can sum1 put up more test cases plzz
2013-07-17 20:02:35 P_Quantum
Nice one..!! O(n) will pass.. :)
take care that series is strictly increasing..!!
2013-07-09 18:14:46 Vishal Sharma
Can we skip a negative value to get the max value of sum

Last edit: 2013-07-09 18:15:10
2013-05-18 07:51:55 Aradhya
almost same problem... aleins2... just made 2 changes :P
2013-04-29 15:30:53 sanborn
I am getting correct answer for all the test cases given here (comments), but still getting WA. Please somebody help me out with more test cases. Simple logic I have been through, I don't know why why it's WA.. :(
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.