1282B2 - K For Price One

Click to show code.


using namespace std;
const int NMAX = 2e5 + 11;
int n, p, k, a[NMAX], dp[NMAX];
int solve(void)
{
    int ans = 0;
    dp[0] = 0;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i)
    {
        dp[i] = a[i];
        if (i - k >= 0)
            dp[i] += dp[i - k];
        else
            dp[i] += dp[i - 1];
    }
    for (int i = 1; i <= n; ++i)
    {
        if (dp[i] <= p)
            ans = max(ans, i);
    }
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> p >> k;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        cout << solve() << endl;
    }
    return 0;
}