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
Dhruv: 2015-11-02 06:47:48

Always getting runtime error, but working perfectly in ideone- <snip> . Please tell the reason if anybody would like to see it.

Last edit: 2023-05-28 23:10:29
prakash_reddy: 2015-09-18 21:31:28

declare array as a[n],b[m] don't declare as a[10005],b[10005] got several WA due to this finally accepted... :)

sarthak_007d: 2015-09-18 12:59:03

<snip>
can anybody tell me the problem with why i m getting all WA

Last edit: 2023-05-28 23:05:54
Harsh Vardhan Ladha: 2015-09-05 19:46:03

just a linear traversal with few if else if :D

satya_jha123: 2015-08-31 22:30:06

good question to learn a new concept simple binary+good greedy

Abhishek jaiswal: 2015-08-18 14:44:19

Did it in O(n) time complexity and O(1) space complexity.

Last edit: 2015-08-18 14:44:55
Shashank Tiwari: 2015-07-30 02:12:19

Okay sum can be int . No need of long long .
It's a simple question of implementation. Yeah !! No need of dp if u r thinking in that direction. Solution can be in o(n).

Avinash Kumar: 2015-07-18 10:24:25

int is just fine, no need for long long.

shallow: 2015-06-30 21:47:24

No need of long or long long.It's perfectly fine with int.

যোবায়ের: 2015-06-27 11:43:56

Edit: Click on English link if statement is not visible.

Last edit: 2015-06-27 11:50:45

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