Destination cost
Practice
2.9 (21 votes)
Algorithms
Greedy algorithms
Sorting
Problem
28% Success 2891 Attempts 20 Points 5s Time Limit 256MB Memory 1024 KB Max Code

You are given \(N\) destinations that must be covered in \(N\) days in any order. Any \(i^{th}\) (\(1\le i\le N\)) destination can be traveled on any \(j^{th}\ (1\le j\le N)\) day. There are only two ways to reach any destination and that is by cars or buses. The cost of reaching to the \(i^{th}\) destination by cars \((C_1,C_2,.....C_n)\) and buses \((B_1, B_2,.....B_n)\) 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

\(1\le N \le10^5\\ 1\le C_i,\ B_i \le10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
74 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:20
7 votes
Tags:
Greedy AlgorithmsAlgorithmsBasics of Greedy AlgorithmsGreedy algorithm
Points:20
12 votes
Tags:
AlgorithmsEasyGreedy AlgorithmsString Manipulationsorting