Show problems as
Classic problem #3857MediumMathDynamic Programming

Minimum Cost to Split into Ones

Real-world version: Cheapest Way to Break a Pile Down
Tracked in this browser only
Read Minimum Cost to Split into Ones on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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