AlgoUniversity
  • Go Back
Discussion
Merge Ropes :

Author

Akash

Difficulty Level : Hard

Submissions : 232

Asked In : Amazon

Marks :10

: 9 | : 0

There are $$$n$$$ piles of ropes arranged in a row. The $$$i^{th}$$$ pile has $$$rope[i]$$$ ropes.

A move consists of merging exactly $$$k$$$ consecutive piles into one pile, and the cost of this move is equal to the total number of ropes in these $$$k$$$ piles.

Return the minimum cost to merge all piles of ropes into one pile. If it is impossible, return $$$-1.$$$

Input

The first line contains two integers, N $$$(1 \le N \le 30)$$$ and K $$$(2 \le K \le 30)$$$.

The second line contains an array of $$$N$$$ piles, $$$A_1,A_2...A_N$$$ $$$(1 \le A_i \le 100000)$$$

Output

Return the minimum cost to merge all piles of stones into one pile.

Examples

Input
4 2
3 2 4 1
Output
20
Input
4 2
6 4 4 6
Output
40

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.