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
// Fryderyk Brajer

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

using llong = long long;
using pii = pair<int, int>;
constexpr int max_n = 5e5 + 5;
constexpr int mod = 1e9 + 7;

int n;
int arr[max_n];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;

    for (int i{}; i < n; ++i)
    {
        int e;
        cin >> e;
        ++arr[e];
    }

    sort(arr + 1, arr + n + 1, greater<int>{});

    int l = 1;
    int r = find(arr + 1, arr + n + 1, 0) - arr - 1;

    int cnt{};

    while (l <= r)
    {
        int rsum{};

        while (rsum + arr[r] < arr[l])
        {
            rsum += arr[r];
            --r;
        }

        int diff = arr[l] - 1 - rsum;
        arr[r] -= diff;

        ++l;
        ++cnt;
    }

    cout << cnt;
}