题目链接:https://codeforces.com/problemset/problem/658/E
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution — an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution t.
Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments.
- Limak can spend b minutes to read one blog and upvote it. Author's contribution will be increased by 5.
- Limak can spend c minutes to read one comment and upvote it. Author's contribution will be increased by 1.
Note that it's possible that Limak reads blogs faster than comments.
Limak likes ties. He thinks it would be awesome to see a tie between at least k registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value x that at least k registered users have contribution exactly x.
How much time does Limak need to achieve his goal?
Input
The first line contains four integers n, k, b and c (2 ≤ k ≤ n ≤ 200 000, 1 ≤ b, c ≤ 1000) — the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.
The second line contains n integers t, t, ..., t (|t| ≤ 10) where t denotes contribution of the i-th registered user.
Output
Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.
Examples
4 3 100 30
12 2 6 1
220
4 3 30 100
12 2 6 1
190
6 2 987 789
-8 42 -4 -65 -8 -8
0
Note
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows.
- He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6.
- Then he spends 4·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1).
In the given scenario, Limak spends 100 + 4·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6.
In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3·30 = 190 minutes:
- Spend 2·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12.
- Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.
题目大意:有n(2e5)个注册用户,每个人的贡献为t[i](±1e9范围)
我们可以花费b的时间成本,使得一个人的贡献+5
同样可以划分c的时间成本,使得一个人的贡献+1
问你至少需要花费的时间,使得至少k(2<=k<=n)个注册用户拥有同样的贡献值。
b,c∈[1,1000]
(可能存在b<c)
思路:
这道题的总体思路是枚举答案,请记住这句话!!!
仔细想想 ,答案会是哪些数呢? 我们要得到至少有K个数的值刚好等于x,我们总不可能从-1e9枚举到1e9吧,然后选取一个最小的时间花费
想想x的值是不是一定是n个数中某个数+0 +1 +2 +3 +4的后的值。 仔细想想,发现一定是的,如果+5的话,那还不如是本身呢 ,这一点自己仔细想想,只可意会,不可言传
再多说一句,为什么要+1 +2 +3 +4 因为题目说会出现+5所需的花费比+1所需的花费更小的情况
到此时 我们的复杂度是 5*N 因为我们每个数都得枚举
那么我们怎么快速求出对应每个ans下的总花费呢? 如果对于每个ans再跑一遍for 很显然是不行的
这里使用一种贪心的思想,保证在log的复杂度内求出答案
我们按照每个数的初始贡献度从小往大排序,那么如果当前选中的数还不到K个的时候直接加进去就好了,当满了K个的时候,我们去掉一个怎样的数才是最优的呢?
直接说吧,去掉一个到它本身的贡献度所需时间最少的那个。下面解释这个数:
比如+5所需的时间是1 +1所需的时间是2
我们有三个数 6 10 11
我们按照从小往大遍历 选了6和10 此时有两种方案:
6+1+1+1+1 得到10 那么花费是8
另一种6+5 10+1 花费才3 所以此时算出来解是3 (代码怎么实现,自己下面的代码)