Language: C++

#define NOMINMAX

#include <vector>
#include <iostream>
#include <algorithm>
#include <cstddef>

struct child {
    int a;
    bool b;
    bool operator <(child const& arg) {
        return a < arg.a;
    }
};

int main() {
    auto n = int{};
    auto x = int{};
    std::cin >> n;
    std::cin >> x;
    auto children = std::vector<child>(n);
    for (auto& e : children) {
        std::cin >> e.a;
        e.b = false;
    }

    static auto const is_b = [](child const& a) {
        return a.b;
    };

    std::sort(children.begin(), children.end());
    auto next = [&children]() {
        auto it = std::find_if_not(children.begin(), children.end(), is_b);
        if (it == children.end())
            return;
        it->b = true;
        std::for_each(children.begin(), it, [](child& e) {
            e.b = false;
        });
    };

    auto sum = [&children]() {
        int result = 0;
        for (auto const& e : children) {
            if (e.b)
                result += e.a;
        }
        return result;
    };
    auto count_enabled = [&children]() {
        return std::count_if(
            children.begin(),
            children.end(), [](child const& e) {
                return e.b;
            });
    };

    std::ptrdiff_t min = 0u;
    while (true) {
        next();
        auto const summed = sum();
        auto const counted_enabled = count_enabled();
        if (summed == x) {
            std::cout << counted_enabled << std::endl;
            return 0;
        }
        if (summed <= x)
            min = std::max(min, counted_enabled);
        if (count_enabled() == children.size()) {
            std::cout << std::max(0, min - 1) << std::endl;
            return 0;
        }
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:70:34: error: no matching function for call to 'max(int, std::ptrdiff_t)'
   70 |             std::cout << std::max(0, min - 1) << std::endl;
      |                          ~~~~~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:62,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:70:34: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::ptrdiff_t' {aka 'long int'})
   70 |             std::cout << std::max(0, min - 1) << std::endl;
      |                          ~~~~~~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:70:34: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   70 |             std::cout << std::max(0, min - 1) << std::endl;
      |                          ~~~~~~~~^~~~~~~~~~~~