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
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n;

    std::cin >> n;

    std::vector<int> occurences(n + 1, 0);

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

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

    int j = n;
    int i = 0;
    while (i != j)
    {
        if (occurences[i] < occurences[j])
        {
            occurences[j] -= occurences[i];
            i++;
        }
        else
        {
            occurences[i] -= occurences[j] - 1;
            j--;
        }
    }

    std::cout << n - j + 1 << std::endl;
}