#K60362. Minimum Trip Cost

    ID: 31070 Type: Default 1000ms 256MiB

Minimum Trip Cost

Minimum Trip Cost

You are given two lists of flight costs: one for flights from city A to city B and another for flights from city B to city C. Each flight has a cost constraint, and you can only choose flights whose cost does not exceed \(B\). Your task is to select one flight from each list such that both flights are within the given budget and the sum of their costs is minimized. If no valid pair of flights exists, output \(-1\).

Note: The flight costs are given as lists of integers. You must consider only those flights whose cost is less than or equal to \(B\), and then output the sum of the minimum cost from each list. If there is no flight from either list that meets the constraint, output \(-1\).

inputFormat

The input is given from standard input and consists of three lines:

  1. The first line contains three integers \(n\), \(m\), and \(B\), where \(n\) is the number of flights from city A to city B, \(m\) is the number of flights from city B to city C, and \(B\) is the maximum allowed cost for any individual flight.
  2. The second line contains \(n\) space-separated integers representing the costs of the flights from city A to city B.
  3. The third line contains \(m\) space-separated integers representing the costs of the flights from city B to city C.

outputFormat

Output a single integer to standard output. This integer should represent the minimum total cost of taking one flight from city A to city B and one flight from city B to city C such that each flight's cost is at most \(B\). If no such combination exists, output \(-1\).

## sample
3 4 100
80 40 60
20 10 50 90
50