Eat Pizzas!

You are given an integer array pizzas of size n, where pizzas[i] represents the weight of the i^th pizza. Every day, you eat exactly 4 pizzas. Due to your incredible metabolism, when you eat pizzas of weights W, X, Y, and Z, where W <= X <= Y <= Z, you gain the weight of only 1 pizza:

  • On odd-numbered days (1-indexed), you gain a weight of Z.
  • On even-numbered days, you gain a weight of Y.

Find the maximum total weight you can gain by eating all pizzas optimally.

Note: It is guaranteed that n is a multiple of 4, and each pizza can be eaten only once.

Example 1
Inputpizzas = [1,2,3,4,5,6,7,8]
Output14
Eating the pizzas optimally can give gains of 8 on day 1 and 6 on day 2, for a total of 14.
Example 2
Inputpizzas = [2,1,1,1,1,1,1,1]
Output3
Eating the pizzas optimally can give gains of 2 on day 1 and 1 on day 2, for a total of 3.

Constraints

  • 4 <= n == pizzas.length <= 2 * 10^5
  • 1 <= pizzas[i] <= 10^5
  • n is a multiple of 4.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate