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
Hot-Shot: 2017-02-17 18:23:26

Nice problem applied almost everything I had.Evolution curve was from simple Recursion ,augmented merge and finally to simple dp. LOL

Last edit: 2017-02-17 18:23:52
scorpion_ajay: 2017-02-17 14:30:08

great question.....!!

cake_is_a_lie: 2017-02-15 01:44:11

This is much simpler than the tags might suggest.

rohit659: 2017-02-09 18:50:25

Nice problem AC in one go :)

holmesherlock: 2017-02-07 15:18:08

loved doing this problem,,gr8 problem

manas0008: 2017-02-05 19:25:53

Loved this problem.Very nice problem.You too loved it?.....To those who loved this problem...here you go simpler version of this problem--------https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/practice-problems/algorithm/bike-trip/
.
.
Hint: think of maintaining two different dp arrays for each sequence and think of possible cases of updating each array at those intersection points.If you still have doubts,the above link helps

Last edit: 2017-02-05 19:28:30
vidyut_1: 2017-01-25 06:22:44

nice problem

nessaa_05: 2017-01-19 14:51:08

can we have little more test cases for this problem please

flyingduchman_: 2017-01-08 18:39:19

No need for dp and binary search.
Use the algorithm for merging two sorted arrays(used in merge-sort) then modify slightly. You might learn a very good thing !

yash_22: 2016-12-27 20:33:46

Shouldn't this be tagged with greedy rather than DP?


Added by:~!(*(@*!@^&
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