OIG XVI - aga

// https://szkopul.edu.pl/problemset/problem/QgFenN44XX_a8nX7RPmBNph4/site/?key=statement
// https://oij.edu.pl/oij16/etap3/zadania/aga/agazad.pdf

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define __int128 long long
#define _syst_ "WINDOWS"
#else
#define _syst_ "UNIX"
#endif

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

constexpr long long INF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000 + 17;

void print(long long x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int n;
    std::cin >> n;

    std::vector<long long> v(n);

    for (int i = 0; i < n; i++) {
        std::cin >> v[i];
    }

    long long mass = 2;
    int ans = 0;
    int prev = 0;

    std::sort(v.begin(), v.end());

    long long maxi = v[v.size() - 1];

    while (mass < maxi) {
        int p = 0;
        int _size = v.size();
        int k = _size - 1;

        while (p < k) {
            int s = (p + k) / 2;

            if (v[s] <= mass - 1) {
                p = s + 1;
            } else {
                k = s;
            }
        }

        if (v[p] >= mass) {
            p--;
        }

        if (p < 0 /*|| v[p] == INF*/) {
            std::cout << "NIE\n";
            return 0;
        } else {
            ans++;
            mass += v[p];

            v.erase(v.begin() + p);
        }
    }

    std::cout << ans << '\n';

    return 0;
}