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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <cstdio>

void quicksort(int tab[], int left, int right){
    int i=left;
    int j=right;
    int x=tab[(left+right)>>1];
    do{
        while(tab[i]<x) i++;
        while(tab[j]>x) j--;
        if(i<=j){
            int temp=tab[i];
            tab[i]=tab[j];
            tab[j]=temp;
            i++;
            j--;
        }
    }while(i<=j);
    if(left<j) quicksort(tab,left,j);
    if(right>i) quicksort(tab,i,right);
}

int main()
{
    int n;
    scanf("%d", &n);
    int tab[n+1] = {0};
    for (int i = 0; i < n; i++){
        int temp;
        scanf("%d", &temp);
        tab[temp-1]++;
    }

    quicksort(tab, 0, n-1);



    int count = 0;
    for (int i = n-1; i >= 0; i--){
        /*for (int j = n-1; j >= 0; j--){
            printf("%d ", tab[j]);
        }
        printf("\n");
        printf("%d; %d; %d\n", tab[i], count, i);*/
        if (tab[i] == 0 && tab[i-1] == 0){
            break;
        }
        if (tab[i] == 0){
            continue;
        }
        if (tab[i] == 1){
            count++;
            continue;
        }
        if (i-2 >= 0 && tab[i] > 1 && tab[i-1] == 0 && tab[i-2] == 0){
            count++;
            continue;
        }
        int lastUsed = 0;
        int sum = 0;
        for (int j = lastUsed; j < i; j++){
            if (tab[j] == 0){
                continue;
            }
            lastUsed = j;
            sum += tab[j];
            if (sum < tab[i]){
                if (j == i-1){
                    tab[j] = 0;
                    count++;
                    break;
                }
                tab[j] = 0;
                continue;
            }
            if (sum == tab[j]){
                sum = 0;
                break;
            }
            tab[i]+=sum-tab[j];
            sum = 0;
            count++;
            break;
        }
        if (tab[i] == tab[i-1]){
            if (i-4 >= 0 && tab[i-2] == tab[i] && tab[i-4] == tab[i] && tab[i-3] == tab[i] && tab[i] % 3 == 0){
                int temp = (tab[i-4] + tab[i-3])/3;
                if (temp < tab[i]){
                    tab[i]+=temp;
                    tab[i-1]+=temp;
                    tab[i-2]+=temp;
                    tab[i-4] = 0;
                    tab[i-3] = 0;
                    count+=3;
                    i-=2;
                    continue;
                }
            }
            if (i-2 >= 0 && tab[i-2] == tab[i] && tab[i] % 3 == 0){
                int temp = tab[i-2]/3;
                if (temp*2 < tab[i]){
                    tab[i]+=temp*2;
                    tab[i-1]+=temp;
                    tab[i-2] = 0;
                    count+=2;
                    i--;
                    continue;
                }
            }
            if (i-2 >= 0 && tab[i-2] == tab[i] && tab[i] % 2 == 0){
                int temp = tab[i-2]/2;
                if (temp < tab[i]){
                    tab[i]+=temp;
                    tab[i-1]+=temp;
                    tab[i-2] = 0;
                    count+=2;
                    i--;
                    continue;
                }
            }
            count+=2;
            i--;
            continue;
        }
    }

    printf("%d", count);

    return 0;
}