Overlapping Subarray

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Author:
Problem types

Given 2 sets of numbers, your task is to determine the sum all numbers in the subarray of largest length that contains the same number at the same index for both sets. If there are 2 valid subarrays of the same length, use the one with greater sum.

Input Specification

The first line contains n (1 \le n \le 10\,000) representing the number of elements in both sets.
The next line contains n space separated positive integers no greater than 100\,000 to represent the first set.
The last line contains n space separated positive integers no greater than 100\,000 to represent the second set.

Output Specification

Print the sum of all numbers in the largest length subarray from both sets in which the numbers and indices overlap.

Sample Input

6
1 3 399 23 14 157
349 5 9 23 14 7538

Sample Output

37

Explanation for Sample

The largest overlapping subarray from both sets is (23, 14) as all elements in this subarray occur at the same consecutive index for both sets. The sum of this subarray is 37.


Comments

There are no comments at the moment.