Ctrl K

Lots of Four

Medium
You are clearing out a storeroom: item prices are given in the array prices, and the number of items is a multiple of 4. Each day you put together a lot of exactly four items that have not been handed out yet, and you keep one of them: - on odd days (1st, 3rd, …), the most expensive item of the lot; - on even days, the second most expensive one (if the lot's prices sorted are a ≤ b ≤ c ≤ d, you get c). This goes on until no items remain. Return the largest total value you can keep.
Example 1
prices=[3, 9, 1, 4, 8, 2, 6, 5] → 15

Day 1: lot [1, 2, 3, 9], you take 9. Day 2: lot [4, 5, 6, 8], you take 6. Total 15.

Example 2
prices=[7, 7, 7, 7] → 7

There is a single day, and it is odd: you take the priciest item, 7.

Example 3
prices=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] → 32
Constraints
  • 4 ≤ len(prices) ≤ 2 · 10⁵
  • len(prices) is a multiple of 4
  • 1 ≤ prices[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
prices=[3, 9, 1, 4, 8, 2, 6, 5]
Expected
15

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.