In this HackerEarth Luch boxes problem solution, Alice works as a restaurant manager. The restaurant has prepared N lunch boxes and Alice plans to distribute them to some schools. Consider that there are M schools and an ith school orders Ai lunch boxes. She wants to distribute lunch boxes to as many schools as possible. Also, she has the following rule:
  1. For an ith school, she gives either zero or Ai lunch boxes
Your task is to help Alice to determine the maximum number of schools that can get lunch boxes.

hackerEarth Lunch boxes problem solution


HackerEarth Lunch boxes problem solution.

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

const int MAX_N = 1e5 + 121;
int n, m;
int a[MAX_N];

void solve(){

    cin >> n >> m;
    for(int i = 1; i <= m; ++i){
        cin >> a[i];
    }

    sort(a + 1,a + m + 1);

    int cnt = 0;
    int sum = 0;

    for(int i = 1; i <= m; ++i){
        sum += a[i];
        if(sum <= n)++cnt; else break;
    }

    cout << cnt << endl;

}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t;
    cin >> t;
    while(t-->0){
        solve();
    }

    return 0;
}

second solution

import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

t = int(input())
while t > 0:
    t -= 1
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    i = 0
    while i < m and a[i] <= n:
        n -= a[i]
        i += 1
    print(i)