cswork / OA打开练习工作台

AmazonOA #374

Get Minimum Round Trip Cost

估计简单可在线判题

考点(AI 标注)前缀和区间原地数组

The indices of the two arrays each represent time, and the values are flight costs; the return index j must be strictly greater than the departure index i. The arrays may have different lengths. Find the minimum total cost; if no valid pair exists, this site additionally outputs −1, without adding a guarantee that a solution always exists.

输入格式

Input, in order, the departure array length n, n integers, the return array length m, and m integers. The original gives no value bounds and no guarantee of non-negativity or equal lengths; this site uses 0≤n,m≤200000 and costs in −10^9..10^9.

输出格式

Output a single integer answer.

样例 1

输入:

4
1 2 3 4
4
4 3 2 1

输出:

2

样例 2

输入:

1
1
1
0

输出:

-1

样例 3

输入:

1
5
3
100 100 -2

输出:

3

样例说明

The outputs of the three public examples are, in order: 2; -1; 3. Verified by independent enumeration or direct simulation.