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
2012-09-26 17:26:44 hot
working gd on all test cases i hve checked yet giving wa..
plz any one give some tricky test cases...soon
2011-09-11 19:36:23 Code Maestro
Final EDIT: Just be careful with negative numbers if your methods tend to be peculiar :P

Last edit: 2011-09-11 21:07:55
2010-10-08 21:30:11 kapil
4 1 2 3 4
3 3 7 10

output will be 23
2010-06-25 09:28:19 hendrik
TXT solution?!!? Shouldnt be there.
2010-05-23 13:05:14 The Bartender
@Roman Gonzalez
thanks / muchas gracias!
2010-03-27 10:55:02 Alexander Tolstov
there is strange problem with Java... Many java solutions got Runtime Error, but the same c++ solution got ac...
2010-01-20 14:16:02 Jacob
this one gave me loads of jip using stringstreams because of extra whitespace in the input and different behaviour on the judge machine. need more input verification than most problems
2009-11-20 06:25:03 Roman
Careful with the white spaces on the input... it costed me 3 RE
2009-11-13 20:25:47 Richard Cocks
Problem states that sequences are strictly increasing. This prevents any cycling.
2009-10-30 16:14:57 Dhruv M
Is this a valid example?
5 1 2 3 4 5
6 4 1 2 3 5
So, I can go from 1 in the 1st sequence to 1 in the 2nd one, followed by 2 in the 1st one and then follow the 4 in the 1st to the 4 in the 2nd one, and continue indefinitely?
Is the input guaranteed to now have any cycles?

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.