1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <algorithm>
#include <array>
#include <iostream>

int main(int argc, char **argv) {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    std::array<int, 500001> N{};
    int n{0};

    std::cin >> n;

    int half = n / 2;

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

    std::sort(N.begin(), N.end(), std::greater<int>());

    auto end = N.begin();    
    while(*end != 0) {
        end++;
    }
    end--;

    int sum = 0;

    if(end-N.begin()== 0) {
        std::cout << "1\n";
        return 0;
    }

    auto cur = N.begin();

    while(cur != end) {
        auto partSum{0}; 
        while(*cur > partSum) {
            if (partSum + *end < *cur) {
                partSum += *end;
                end--;
                if(cur == end) {
                    sum++;
                    break;
                }
            } else {
                sum++;
                cur++;
                if(cur == end) sum++;
                break;
            }
        }
    }

    std::cout << sum << "\n";
    return 0;
}