All problems
0931MediumMathDynamic Programming

Cheapest Way to Break a Pile Down

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3857Minimum Cost to Split into Ones

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A single pile holds n tokens.

One break takes a pile holding at least two tokens and divides it into two piles, each holding at least one token, at a cost equal to the size of the pile being broken. Breaks continue until every pile holds a single token.

Return the least total cost of getting there.

Examples

Example 1

Input
n = 7
Output
20

Breaking the seven into three and four costs 7. The three then costs 5 and the four costs 8, coming to 20. Peeling single tokens off instead would cost 7 plus 6 plus 5 plus 4 plus 3 plus 2, which is 27.

Example 2

Input
n = 1
Output
0

The pile already holds a single token, so nothing needs breaking.

Example 3

Input
n = 8
Output
24

Halving all the way down costs 8, then two piles of four at 4 each, then four piles of two at 2 each, coming to 24.

Constraints

  • 1 <= n <= 500

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_cost(n: int) -> int:
Java
public int minCost(int n)
September 7
Apply