#include <bits/stdc++.h>   using namespace std;   int c[100001], s[100001];   int mx, ans, n, nc, ns;   int i, j;   void checkAns(int k) {     if (ns <= 2) {       if ((mx - 1) * nc == (i - 1) || mx * (nc - 1) == (i - 1)) {         if (s[1] == 1 || s[1] == i || 1 == nc || s[mx] == 1) {           ans = i;         }       }     }   }   int main() {     int k;     cin >> n;     for (i = 1; i <= n; ++i) {       cin >> j;       k = c[j];       if (k) {         s[k]--;         if (!s[k]) {           ns--;         }       } else {         nc++;       }       c[j]++;       k++;       if (!s[k]) {         ns++;       }       s[k]++;       if (k > mx) {         mx = k;       }       checkAns(k);     }     cout << ans << endl;     return 0;   }   