FastPrepMinimum Energy Cost

Minimum Energy Cost

Amazon logoAmazon● MediumOA
Learn

Problem statement

Janet has N bags in a row. Each bag has a weight (Wi). Janet can collect bags from either the leftmost or rightmost position, but there are energy costs:

  • The cost of collecting a bag is the bag's weight (Wi) multiplied by X (if collecting from the left) or Y (if collecting from the right).
  • If you are collecting a bag consecutively from the same side twice in a row, then there is an additional cost of El (if collected from the left side consecutively) or Er (if collected from the right side consecutively).

Find the strategy that minimizes the total energy cost Janet spends to collect all the bags and display the minimum cost Bob has to pay.

Input Format

The first line of input contains five space-separated integers: N (number of bags), X, Y, El, Er (energy costs)

The second line of input contains N space-separated integers representing the weight of each bag (Wi).

Output Format

Display the minimum energy cost expenditure for Bob.

Function

minimumEnergyCost(weights: int[], X: int, Y: int, El: int, Er: int) → int

Examples

Example 1

weights = [42, 3, 99]X = 4Y = 4El = 19Er = 1return = 576

🐡 🦚 🦤 🐌 🦦

Constraints

  • :O

More Amazon problems

See Amazon hiring insights
public int minimumEnergyCost(int[] weights, int X, int Y, int El, int Er) {
  // write your code here
}
weights[42, 3, 99]
X4
Y4
El19
Er1
expected576
Checking account…