Under construction

Still being built. Things may change

Hotel Rewards

Time limit2sMemory limit512 MB

Summary
Given nightly hotel prices and a K-points-per-free-night reward rule, find the minimum total cost of the whole trip.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Heap
Solved
No attempts yet

Problem

You are touring Europe for NN nights and you sleep in a different city every night. You have already picked the hotel for each city, so you know the price PiP_i of the room you take on night ii for i=1,2,…,Ni = 1, 2, \dots, N.

You book every room through a website that runs a rewards program. Each night you sleep in a hotel booked through that website you earn one point. Whenever your account holds at least KK points you may spend KK of them on one free night in any hotel. A free night earns no point.

For example, take N=6N = 6 and K=2K = 2 with prices P1=10P_1 = 10, P2=3P_2 = 3, P3=12P_3 = 12, P4=15P_4 = 15, P5=12P_5 = 12 and P6=18P_6 = 18. Paying for the first four nights leaves four points in the account, and those cover the last two nights for free, for a total of P1+P2+P3+P4=40P_1 + P_2 + P_3 + P_4 = 40. If instead you spend two of the three points earned in the first three nights on the fourth night, you can pay for the fifth night and use the last two points on the sixth. That plan costs P1+P2+P3+P5=37P_1 + P_2 + P_3 + P_5 = 37, which is cheaper.

Write a program that finds the minimum possible total cost of your accommodation. Every hotel you want always has a room free for you, and the order of the cities cannot be changed.

Input

The first line contains two integers NN and KK, the number of nights of the trip and the number of points one free night costs (1≤N,K≤1051 \le N, K \le 10^5).

The second line contains NN integers P1,P2,…,PNP_1, P_2, \dots, P_N, where PiP_i is the price of the room you take on night ii (1≤Pi≤1041 \le P_i \le 10^4).

Output

Print one integer, the minimum total cost of the accommodation.

Examples3

  1. Example 1

    Input
    6 2
    10 3 12 15 12 18
    
    Expected output
    37
    
  2. Example 2

    Input
    6 1
    10 3 12 15 12 18
    
    Expected output
    25
    
  3. Example 3

    Input
    5 5
    1 2 3 4 5
    
    Expected output
    15