12662 - I got a perfect body
raw download clone
CPP
views 8
,
size 904 b
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;


using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int n, p, k;
        cin>>n>>p>>k;
        vector<int> num(n);
        for(int &i:num){
            scanf("%d", &i);
        }
        num.push_back(0);
        sort(num.begin(), num.end());
        vector<ll> sum(n+1);
        int i;
        for(i = 1; i <=  n; i++){
            if(i < k){
                sum[i] = num[i] + sum[i-1];
            }
            else{
                sum[i] = num[i] + sum[i-k];
            }
        }

        for(int i = sum.size()-1; i >= 0; i--){
            if(sum[i] <= p){
//                cout<<"i: "<<i<<" sum[i]:"<<sum[i]<<" p:"<<p<<"\n";
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.