Christmas shopping

4.3

3 votes
Algorithms, Easy-Medium, Sorting
Problem

x-mas tree

The Lalalandia's Christmas is coming! Only N days are left and Jambo wants to buy all his friends a present. There are N presents that his friends want and Jambo, because he is a very small elephant, cannot buy and carry more than K presents at any day. Every present in jth day costs aij for every 1jN. Can you help Jambo determine the minimum amount of money needed to buy all presents and make his friends happy?

Input format

The first line consists of integers N - the number of days and the number of presents Jambo wants to buy, and K - the number of presents Jambo can carry. The second line contains n integers a1,a2,...,aN — the cost of the ith present.

Output format

The first line should contain the single integer - answer to the problem.

Constraints:

1N105

1KN

1ai109

Sample Input
3 1
1 2 3
Sample Output
10
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Since K equals to 1, the optimal solution is 31+22+13=10.

Editor Image

?