Splitting into Ones

You start with a single block of size n.

Your objective is to completely break this block down until you are left with exactly n individual blocks of size 1.

Each time you break any block x into two smaller pieces a and b (where a + b = x), you incur a penalty equal to a * b.

Find the lowest possible total penalty you can accumulate to finish breaking down the entire block.

Example 1:

Input: n = 5

Output: 10

Explanation: We start by splitting 5 into 3 and 2, which costs 6. Next, we split the 3 into 2 and 1, costing 2. This leaves us with two 2s and a 1. Finally, we split both 2s into 1s, which costs 1 for each split. The total accumulated cost is 6 + 2 + 1 + 1 = 10.

Example 2:

Input: n = 7

Output: 21

Explanation: We can split 7 into 4 and 3, which costs 12. We then split the 4 into two 2s (costing 4) and the 3 into 2 and 1 (costing 2). We now have three 2s. Splitting all three of these 2s into 1s costs 1 each. The total cost is 12 + 4 + 2 + 3 = 21.

Now Your Turn!

Pick the correct output for the given input

Input: n = 11

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 5000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
// --- IDE Code ---
class Solution {
public:
long long minCost(long long n) {
// Write your code here
return 0;
}
};
Test Case

Input:

N