AmazonOA #374
Get Minimum Round Trip Cost
Estimated EasyJudge-ready
Topics (AI-labelled)Prefix & RangesIn-place Arrays
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
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
Output a single integer answer.
Example 1
Input:
4
1 2 3 4
4
4 3 2 1
Output:
2
Example 2
Input:
1
1
1
0
Output:
-1
Example 3
Input:
1
5
3
100 100 -2
Output:
3
Example notes
The outputs of the three public examples are, in order: 2; -1; 3. Verified by independent enumeration or direct simulation.
Editorial
Loading solution…