Task DescriptionDiscussion (0)

The input contains on the first line two positive integers, N and

The output will contain a single line on which the minimum cost for gathering all the leaves in exactly K piles will be written.

* 0 <

* 0 <

* 0 < the weight of any leaf < 1 001

Input:

Output:

It would be best to form the 2 piles in points 1 and 4. Leaves 1, 2 and 3 are taken to coordinate 1, and 4 and 5 are taken to coordinate 4. The total cost is:

1 * 0 + 2 * 1 + 3 * 2 + 4 * 0 + 5 * 1 = 13

Task :: NKLEAVES

One beautiful autumn day Radu and Mars noticed that the garden alley where they usually spend time is rather full of leaves. They decided to gather all the leaves in exactly K piles. The alley is a straight line and they established a coordinate system on the alley, with the origine at the beginning of the alley.

There are

The leaf gathering operation takes place as Radu and Mars leave the garden, so the leaves can only be moved to the left. The cost of moving a leaf is equal to its weight times the distance it is moved. Obviously one of the K piles will be at coordinate 1, but the rest can be anywhere.

Find out the total minimum cost of gathering the

There are

**N**leaves of various weights on the alley, all lined up, and the distance between each consecutive leaves is 1. More precisely the first leaf is at coordinate 1, the second at coordinate 2, ... , the**N**-th at coordinate**N**. Initially the two guys are at coordinate**N**.The leaf gathering operation takes place as Radu and Mars leave the garden, so the leaves can only be moved to the left. The cost of moving a leaf is equal to its weight times the distance it is moved. Obviously one of the K piles will be at coordinate 1, but the rest can be anywhere.

Find out the total minimum cost of gathering the

**N**leaves in exactly**K**piles.**INPUT:**

The input contains on the first line two positive integers, N and

**K**, separated by a space, having the significance given above. The following

**N**lines contain

**N**positive integers representing the weights of the leaves (line i+1 contains the weight of the leaf located at coordinate i).

**OUTPUT:**

The output will contain a single line on which the minimum cost for gathering all the leaves in exactly K piles will be written.

**Constraint**

* 0 <

**N**< 100 001

* 0 <

**K**< 11,

**K**<

**N**

* 0 < the weight of any leaf < 1 001

Input:

**5 2**

1

2

3

4

5

1

2

3

4

5

Output:

**13**

**Explanation:**

It would be best to form the 2 piles in points 1 and 4. Leaves 1, 2 and 3 are taken to coordinate 1, and 4 and 5 are taken to coordinate 4. The total cost is:

1 * 0 + 2 * 1 + 3 * 2 + 4 * 0 + 5 * 1 = 13

Submit Solution

Available Languages

Task info

Name: | NKLEAVES |

Time: | 0.5 sec. |

Memory: | 32 MB |

#Tests: | 10 |

AddedBy: | harta01 |

Source: | Campion 2005 |

Task Ratings

Difficulty: | 4.7 (7 votes) |

Quality: | 4.7 (6 votes) |

Acceptance Rate

Recent Submissions

Fastest Solutions

User | Time |
---|---|

sokokaleb | 0.112 s. |

oa12gb | 0.145 s. |

salzhs | 0.154 s. |

cakcups | 0.154 s. |

1010011010 | 0.165 s. |

dayatbflash | 0.165 s. |

nigo92 | 0.166 s. |

mateuscd11 | 0.167 s. |

logine7 | 0.17 s. |

toppykung | 0.171 s. |

Solved By