Ctrl K

Two of the First Three

Medium
Crates wait in a shipping queue; their weights are the array loads, in queue order. Crates are shipped until the queue is empty, and every trip works like this: - if three or more crates remain, pick **any two of the first three** and ship them; the trip costs the larger of the two weights; - if fewer than three remain, ship them all in one trip; it costs the largest weight among them. The remaining crates keep their order. Return the smallest total cost of emptying the queue.
Example 1
loads=[4, 9, 3, 5] → 13

First remove 4 and 3 from the front three for max(4, 3) = 4, leaving [9, 5], which go together for 9. Total 13.

Example 2
loads=[7, 2, 2, 7, 1] → 10

Remove the two 2s for 2, giving [7, 7, 1]; then the two 7s for 7, and the final 1 goes for 1. Total 10.

Example 3
loads=[6] → 6

A single element is removed in one move for 6.

Constraints
  • 1 ≤ len(loads) ≤ 1000
  • 1 ≤ loads[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
loading Python…
Input
loads=[4, 9, 3, 5]
Expected
13

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