Destination cost

2.9

21 votes
Algorithms, Greedy Algorithms, Sorting
Problem

You are given N destinations that must be covered in N days in any order. Any ith (1iN) destination can be traveled on any jth (1jN) day. There are only two ways to reach any destination and that is by cars or buses. The cost of reaching to the ith destination by cars (C1,C2,.....Cn) and buses (B1,B2,.....Bn) are given.

The only constraint is that you cannot use the same way of travel on two consecutive days. Determine the minimum total cost that you need to pay in reaching all N destinations.

Input format

  • First line: A single integer N
  • Second line: N space-separated integers denoting the cost of reaching the destination by cars
  • Third line: N space-separated integers denoting the cost of reaching the destination by buses

Output format

Print the minimum total cost that you need to pay in reaching all N destinations.

Constraints

1N1051Ci, Bi109

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

We can go on two days by car i.e on first day and last day, and on the other three days by bus to get the minimum cost which is 29.

  

Editor Image

?