#P7791. Cafeteria Combo Optimization
Cafeteria Combo Optimization
Cafeteria Combo Optimization
You are given a list of dish prices that you have taken from the cafeteria tray. In the cafeteria, besides paying for individual dishes, a special combo is offered daily. A combo meal consists of exactly 4 dishes (for example, soup, main course, side dish, dessert) and is sold at a fixed price x. It is known that the combo price x is less than or equal to the sum of the prices of its 4 components. The cashier uses the following strategy: if you have taken dishes that belong to a combo and if the total individual cost of a set of 4 dishes is greater than x, then instead of charging you for each dish, she will charge you for the combo price (possibly using several combos) in order to reduce your total cost.
Your task is to calculate the minimum amount you have to pay given the following input format.
Problem Details:
- You are allowed to group dishes into sets of 4. For each full group of 4 dishes, if the sum of the prices is strictly greater than x, then it is beneficial to charge them as a combo. Otherwise, or if fewer than 4 dishes form a group, the individual prices must be paid.
- You may use the combo charging option multiple times if applicable.
Determine the minimum total cost for the dishes on your tray.
inputFormat
The first line contains two integers n and x (1 ≤ n ≤ 105, 1 ≤ x ≤ 109) — the number of dishes and the combo price respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the price of the i-th dish.
outputFormat
Output a single integer — the minimum total cost you have to pay.
sample
5 10
9 1 1 1 1
11