Ctrl K

The Middle Bag

Medium
You, Anya and Boris are splitting 3n bags of coins; bag i holds bags[i] coins. The split goes in rounds: - you put any three of the remaining bags on the table; - Anya takes the richest of them; - you take the richer of the two that are left; - Boris takes the last one. Rounds go on until no bags remain. Return the largest number of coins you can end up with.
Example 1
bags=[5, 1, 3, 8, 2, 6] → 9

First put out 8, 6 and 1: Anya takes 8, you take 6, Boris takes 1. Then 5, 3 and 2: you get 3. That is 6 + 3 = 9.

Example 2
bags=[4, 4, 4] → 4

There are only three bags and they are all equal, so you get 4.

Example 3
bags=[10, 1, 1, 1, 9, 9, 2, 3, 7] → 18
Constraints
  • 3 ≤ len(bags) ≤ 10⁵
  • len(bags) is divisible by 3
  • 1 ≤ bags[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSame Prizes, Different Tastes MediumUp or Down for Every Post MediumRound Prices to a Total MediumPrices That Fade With Each Sale MediumNever a Multiple of Three Medium
loading Python…
Input
bags=[5, 1, 3, 8, 2, 6]
Expected
9

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