#K49827. Maximum Concatenated Subarray Sum

    ID: 28729 Type: Default 1000ms 256MiB

Maximum Concatenated Subarray Sum

Maximum Concatenated Subarray Sum

You are given two arrays of integers. Your task is to compute the maximum possible sum by selecting a contiguous subarray from each array and then adding the two sums together. Note that you are allowed to choose an empty subarray from an array if all its elements are negative, which is regarded as 0. Formally, let \( f(x) \) be the maximum subarray sum of array \( x \) where \( f(x)=0 \) if all numbers are negative. Then the result is \( f(a)+f(b) \).

Input Format: The input will be read from standard input (stdin) and consists of four lines. The first line contains an integer \( n \) representing the number of elements in the first array. The second line contains \( n \) space-separated integers representing the elements of the first array. The third line contains an integer \( m \) representing the number of elements in the second array. The fourth line contains \( m \) space-separated integers representing the elements of the second array.

Output Format: Output a single integer which is the maximum concatenated subarray sum.

Note: Use Kadane's algorithm (or an equivalent approach) to determine the maximum subarray sum for each array.

inputFormat

The input consists of four lines:

  1. An integer \( n \) denoting the number of elements in the first array.
  2. \( n \) space-separated integers representing the first array.
  3. An integer \( m \) denoting the number of elements in the second array.
  4. \( m \) space-separated integers representing the second array.

outputFormat

A single integer which is the maximum concatenated subarray sum.

## sample
4
1 2 3 4
3
-1 2 3
15