Editorial for Sales


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

tr

Mustafa'nın en fazla sayıda ürün alabilmesi için, alacaklarını en ucuz ürünlerden seçmesi gerek. Önce ürünleri ucuzdan pahalıya sıralar. Sonra baştan itibaren parasının yettiği (\(\mathbf{N \geq a_i}\)) ürünleri tek tek alır ve parası azalmış olur. (\(\mathbf{N -= a_i}\))

en

In order Mustafa to buy the maximum number of items, he needs to choose what he buys among the cheapest items. He first sorts the items from the cheapest to the most expensive. Then he buys the items he can afford (\(\mathbf{N \geq a_i}\)) one by one and his money is decreased by the price of the item he recently bought. (\(\mathbf{N -= a_i}\))

#include <bits/stdc++.h>
using namespace std;

int arr[300005], n, m, res;

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>arr[i]; 
    sort(arr, arr+n);
    for(int i=0;i<n;i++){
        if(arr[i]>m) break;
        m-=arr[i];
        res++;
    }
    cout<<res;    
}