Surprise Party
Totos and Annoula's friends are impressed by their hard work and decide to relax them by organizing a surprise party. They start writing a list of items needed to make the party as fun as possible, along with a value indicating the importance of each item for the party, i.e., the cost incurred if that item is missing from the party.
The friends decide to divide the items from the list among themselves in sequence, so that each friend is responsible for bringing a continuous segment of items. However, recognizing that there's always a chance someone might forget to bring an item-and the more items someone is responsible for, the more likely they are to forget something-the friends want to ensure that the total estimated cost of potential missing items is minimized.
More formally:
You are given positive integers:
- , the number of items.
- , the number of friends.
You are also given a sequence consisting of positive integers , which represent the cost of missing each item.
We need to divide the sequence into segments such that the sum of the estimated costs of each segment is minimized. The estimated cost of a segment of , consisting of the (consecutive) positions where , is defined as:
That is, we aim to find the minimum value of the expression:
where .
Input Data (STDIN
)
The st line contains two integers and , separated by a space: the number of items and the number of friends, respectively.
The nd line contains integers , the cost of missing each item.
Output Data (STDOUT
)
The output consists of a single line with one integer: the minimum total estimated cost.
Example
Input (STDIN)
6 3
11 11 11 24 26 100
Output (STDOUT)
299
Example Explanation:
The first friend is responsible for the segment with the first three items, with an estimated cost of .
The second friend is responsible for the next two items, with an estimated cost of .
The third friend is responsible for the last item, with an estimated cost of .
Thus, the total estimated cost is , which is the minimum.
Constraints:
Subtasks:
- of the points:
- of the points:
- of the points: , i.e., the elements of are sorted in ascending order.
- of the points: no additional constraints.
Comments