B2. K for the Price of One (Hard Version) Codeforces Round #610 (Div. 2)B2

19 篇文章 0 订阅

Codeforces Round #610 (Div. 2)

B2. K for the Price of One (Hard Version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is the hard version of this problem. The only difference is the constraint on kk — the number of gifts in the offer. In this version: 2≤k≤n2≤k≤n.

Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "kk of goods for the price of one" is held in store.

Using this offer, Vasya can buy exactly kk of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.

More formally, for each good, its price is determined by aiai — the number of coins it costs. Initially, Vasya has pp coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:

  • Vasya can buy one good with the index ii if he currently has enough coins (i.e p≥aip≥ai). After buying this good, the number of Vasya's coins will decrease by aiai, (i.e it becomes p:=p−aip:=p−ai).
  • Vasya can buy a good with the index ii, and also choose exactly k−1k−1 goods, the price of which does not exceed aiai, if he currently has enough coins (i.e p≥aip≥ai). Thus, he buys all these kk goods, and his number of coins decreases by aiai (i.e it becomes p:=p−aip:=p−ai).

Please note that each good can be bought no more than once.

For example, if the store now has n=5n=5 goods worth a1=2,a2=4,a3=3,a4=5,a5=7a1=2,a2=4,a3=3,a4=5,a5=7, respectively, k=2k=2, and Vasya has 66 coins, then he can buy 33 goods. A good with the index 11 will be bought by Vasya without using the offer and he will pay 22 coins. Goods with the indices 22 and 33 Vasya will buy using the offer and he will pay 44 coins. It can be proved that Vasya can not buy more goods with six coins.

Help Vasya to find out the maximum number of goods he can buy.

Input

The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.

The next lines contain a description of tt test cases.

The first line of each test case contains three integers n,p,kn,p,k (2≤n≤2⋅1052≤n≤2⋅105, 1≤p≤2⋅1091≤p≤2⋅109, 2≤k≤n2≤k≤n) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.

The second line of each test case contains nn integers aiai (1≤ai≤1041≤ai≤104) — the prices of goods.

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

For each test case in a separate line print one integer mm — the maximum number of goods that Vasya can buy.

Example

input

Copy

8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2

output

Copy

3
4
1
1
2
0
4
5

可以说是dp但是也可不dp

枚举需要单独拿的个数 可以证明单独拿的一定时前k个里的某些

#include<bits/stdc++.h>
using namespace std;
int a[200005];
int dp[200005];
int main()
{
    int _;
    scanf("%d",&_);
    while(_--)
    {
        int n,p,k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i=1;i<=n;i++)
        {
            dp[i]=0;
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
        {
            if(i<k) dp[i]=dp[i-1]+a[i];
            else    dp[i]=min(dp[i-k]+a[i],dp[i-1]+a[i]);
            //cout<<i<<" "<<dp[i]<<endl;
        }
        int ans=0;
        for(int i=n;i>=1;i--)
        {
            if(dp[i]<=p) {ans=i;break;}
        }
        printf("%d\n",ans);
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值