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
davidgalehouse: 2016-10-06 04:12:13

Note to C# solvers: input is formatted incorrectly, you should RemoveEmptyEntries if splitting.

Last edit: 2016-10-06 04:12:27
Anonomous: 2016-10-02 15:53:23

Be careful, some of the test cases are not strictly increasing.

shubham2305: 2016-09-29 18:21:00

no binary search required in fact no dp too.
simple ad-hoc
be careful for the boundary cases cost me 2WA

alaa_alrayes96: 2016-09-27 16:09:27

binary search + 2 array check TC of Krishna AC 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
6 1 2 4 6 8 20
6 22 24 26 28 30 40
6 1 4 8 20 100 500
1 1
6 1 4 8 20 100 500
1 1000
6 1 4 8 20 100 500
4 2 3 8 15
6 1 4 8 20 100 500
4 2 6 400 500
6 1 4 8 20 100 500
6 1 4 8 20 100 500
6 1 4 8 20 100 500
4 2 8 20 200
6 1 4 8 20 100 500
4 2 3 80 500
6 100 200 300 400 500 600
4 40 81 99 100
26
23
-30
170
633
1000
633
908
633
633
633
2320

Last edit: 2016-09-27 16:11:41
ajeyo: 2016-09-08 13:28:34

No dp! :P

sonu: 2016-08-31 13:29:07

seriously why is this question even tagged dp??

Sumit : 2016-08-31 08:49:30

I've tried all the test cases present in the comments till Page9 and java code is giving correct output. I am Still getting WA.

love11mishra: 2016-08-29 11:42:11

ya :) very simple conceptually but a little stuff needed to implement !
Little Hint:- using HashSet in java makes the search operation too easier.

Last edit: 2016-08-29 11:45:35
manish3749: 2016-08-19 20:51:43

accepted in one go ☻☻☻☺
binary search + two vectors

tebnine: 2016-08-18 22:20:01

what happens if there is more than one intersection with the same value like this :
6 9 9 3 -9 -9 5
6 1 1 3 -9 -9 3
The best way to go is 9 9 3 right ?


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