# Find the minimum cost to cross the River

Given an integer **N** which is the number of villagers who need to cross a river but there is only one boat on which a maximum of **2** person can travel. Each person **i** has to pay some specific price **P _{i}** to travel alone in the boat. If two person

**i, j**travel in the boat then they have to pay

**max(P**. The task is to find the minimum amount all the villagers have to pay to cross the river.

_{i}, P_{j})**Examples:**

Input:Price[] = {30, 40, 60, 70}

Output:220

P_{1}and P_{2}go together (which costs 40)

and P_{1}comes back (total cost 70 now).

Now P_{3}and P_{4}go (total cost 140) and

P_{2}comes back (total cost 180) and

finally P_{1}and P_{2}go together (total cost 220).

Input:Price[] = {892, 124}

Output:892

**Approach:** There are two ways for the two most costly person to cross the river:

- Both of them cross the river with the cheapest person turn by turn. So, the total cost will be the cost of the two costly persons + 2 * (cost of the cheapest person) (due to coming back).
- The two cheapest person cross the river and the cheapest person comes back. Now, the 2 most costly person cross the river and 2nd cheapest person comes back. So, the total cost will be the cost of the cheapest and costliest person plus 2 * cost of the second cheapest person.
- Total cost will be the minimum of the above two ways.

Let’s consider the example we used above to understand the approach:

P_{1}= 30, P_{2}= 40, P_{3}= 60, P_{4}= 70According to first method, P

_{4}goes with P_{1}and P_{1}comes back (cost is P_{4}+P_{1}).

Now, P_{3}goes with P_{1}and P_{1}comes back (cost for this ride will be P_{4}+P_{1}).

So, total cost for sending two most costly person according to method 1 is P_{4}+2*P_{1}+P_{3}= 190According to second method, P

_{2}goes with P_{1}and P_{1}comes back (cost is P_{2}+P_{1}).

Now, P_{3}goes with P_{4}and P_{2}comes back (cost for this ride will be P_{4}+P_{2}).

So, total cost for sending two most costly person according to method 2 is P_{4}+2*P_{2}+P_{1}= 180Hence, cost for sending P

_{3}and P_{4}will be minimum of 2 methods, i.e., 180.Now, we are left with P

_{1}and P_{2}whom we have to send together and cost will

be P_{2}= 40.So, total cost for travelling is 180 + 40 = 220.

Below is the implementation of the above approach:

## CPP

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define ll long long int ` ` ` `// Function to return the minimum cost ` `int` `minimumCost(ll price[], ` `int` `n) ` `{ ` ` ` ` ` `// Sort the price array ` ` ` `sort(price, price + n); ` ` ` `ll totalCost = 0; ` ` ` ` ` `// Calcualte minimum price ` ` ` `// of n-2 most costly person ` ` ` `for` `(` `int` `i = n - 1; i > 1; i -= 2) { ` ` ` `if` `(i == 2) { ` ` ` `totalCost += price[2] + price[0]; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// Both the ways as discussed above ` ` ` `ll price_first = price[i] + price[0] + 2 * price[1]; ` ` ` `ll price_second = price[i] + price[i - 1] + 2 * price[0]; ` ` ` `totalCost += min(price_first, price_second); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Calculate the minimum price ` ` ` `// of the two cheapest person ` ` ` `if` `(n == 1) { ` ` ` `totalCost += price[0]; ` ` ` `} ` ` ` `else` `{ ` ` ` `totalCost += price[1]; ` ` ` `} ` ` ` ` ` `return` `totalCost; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll price[] = { 30, 40, 60, 70 }; ` ` ` `int` `n = ` `sizeof` `(price) / ` `sizeof` `(price[0]); ` ` ` ` ` `cout << minimumCost(price, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum cost ` ` ` `static` `long` `minimumCost(` `long` `price[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Sort the price array ` ` ` `Arrays.sort(price); ` ` ` ` ` `long` `totalCost = ` `0` `; ` ` ` ` ` `// Calcualte minimum price ` ` ` `// of n-2 most costly person ` ` ` `for` `(` `int` `i = n - ` `1` `; i > ` `1` `; i -= ` `2` `) ` ` ` `{ ` ` ` `if` `(i == ` `2` `) ` ` ` `{ ` ` ` `totalCost += price[` `2` `] + price[` `0` `]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Both the ways as discussed above ` ` ` `long` `price_first = price[i] + price[` `0` `] + ` `2` `* price[` `1` `]; ` ` ` `long` `price_second = price[i] + price[i - ` `1` `] + ` `2` `* price[` `0` `]; ` ` ` `totalCost += Math.min(price_first, price_second); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Calculate the minimum price ` ` ` `// of the two cheapest person ` ` ` `if` `(n == ` `1` `) ` ` ` `{ ` ` ` `totalCost += price[` `0` `]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `totalCost += price[` `1` `]; ` ` ` `} ` ` ` ` ` `return` `totalCost; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `long` `price[] = { ` `30` `, ` `40` `, ` `60` `, ` `70` `}; ` ` ` `int` `n = price.length; ` ` ` ` ` `System.out.println(minimumCost(price, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum cost ` `def` `minimumCost(price, n): ` ` ` ` ` `# Sort the price array ` ` ` `price ` `=` `sorted` `(price) ` ` ` `totalCost ` `=` `0` ` ` ` ` `# Calcualte minimum price ` ` ` `# of n-2 most costly person ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `1` `, ` `-` `2` `): ` ` ` `if` `(i ` `=` `=` `2` `): ` ` ` `totalCost ` `+` `=` `price[` `2` `] ` `+` `price[` `0` `] ` ` ` ` ` `else` `: ` ` ` ` ` `# Both the ways as discussed above ` ` ` `price_first ` `=` `price[i] ` `+` `price[` `0` `] ` `+` `2` `*` `price[` `1` `] ` ` ` `price_second ` `=` `price[i] ` `+` `price[i ` `-` `1` `] ` `+` `2` `*` `price[` `0` `] ` ` ` `totalCost ` `+` `=` `min` `(price_first, price_second) ` ` ` ` ` `# Calculate the minimum price ` ` ` `# of the two cheapest person ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `totalCost ` `+` `=` `price[` `0` `] ` ` ` ` ` `else` `: ` ` ` `totalCost ` `+` `=` `price[` `1` `] ` ` ` ` ` `return` `totalCost ` ` ` `# Driver code ` ` ` `price ` `=` `[` `30` `, ` `40` `, ` `60` `, ` `70` `] ` `n ` `=` `len` `(price) ` ` ` `print` `(minimumCost(price, n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum cost ` ` ` `static` `long` `minimumCost(` `long` `[]price, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Sort the price array ` ` ` `Array.Sort(price); ` ` ` ` ` `long` `totalCost = 0; ` ` ` ` ` `// Calcualte minimum price ` ` ` `// of n-2 most costly person ` ` ` `for` `(` `int` `i = n - 1; i > 1; i -= 2) ` ` ` `{ ` ` ` `if` `(i == 2) ` ` ` `{ ` ` ` `totalCost += price[2] + price[0]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Both the ways as discussed above ` ` ` `long` `price_first = price[i] + price[0] + 2 * price[1]; ` ` ` `long` `price_second = price[i] + price[i - 1] + 2 * price[0]; ` ` ` `totalCost += Math.Min(price_first, price_second); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Calculate the minimum price ` ` ` `// of the two cheapest person ` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `totalCost += price[0]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `totalCost += price[1]; ` ` ` `} ` ` ` ` ` `return` `totalCost; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `long` `[]price = { 30, 40, 60, 70 }; ` ` ` `int` `n = price.Length; ` ` ` ` ` `Console.WriteLine(minimumCost(price, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

220

## Recommended Posts:

- Minimum Cost Graph
- Minimum cost to make two strings same
- Minimum possible travel cost among N cities
- Minimum odd cost path in a matrix
- Minimum cost to buy N kilograms of sweet for M persons
- Minimum cost to partition the given binary string
- Minimum cost to traverse from one index to another in the String
- Minimum cost to sort a matrix of numbers from 0 to n^2 - 1
- Minimum cost to cover the given positions in a N*M grid
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Minimum cost to form a number X by adding up powers of 2
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum cost to make all array elements equal
- Minimum total cost incurred to reach the last station
- Minimum cost to reach the top of the floor by climbing stairs

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.