#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin>>N; vector<int>A(N); for(int &i:A)cin>>i; sort(A.begin(),A.end()); A.push_back(1e9); for(int i=0,f=0,t=0;i<=N;i++){ if(A[i]!=t){ cout<<t<<'\n'; return 0; } if(A[i+1]==t){ int c=0; while(i<N&&A[i]==t)++i,++c; --i; t+=1; if(f==0&&c>2)f=-1; }else if(f==0&&i<N-1||f==-1){ f=1; A[N-1]=1e9; t+=1; }else{ cout<<t<<'\n'; return 0; } } }