/*Hints:Use Dp[mx]. Use 0-1 Knapsack concept.*/

#include<bits/stdc++.h>
#define mx 100005

using namespace std;
int n,Cnt[mx],c,Big,Dp[mx];

int slove(int pos)
{
       if(pos<0) return 0;
       int &ret=Dp[pos];
       if(ret!=-1) return ret;
       return ret=max((Cnt[pos]*pos)+slove(pos-2),slove(pos-1));
}

int main()
{
       scanf("%d",&n);
       Big=-1;
       for(int i=0;i<n;i++) scanf("%d",&c),Cnt[c]++,Big=max(Big,c);
       memset(Dp,-1,sizeof(Dp));
       int res=slove(Big);
       printf("%d\n",res);
       return 0;
}


/*Hints:Use Dp[mx]. Use 0-1 Knapsack concept.*/

#include<bits/stdc++.h>
#define mx 100005

using namespace std;
typedef long long LL;
LL n,Cnt[mx],c,Big,Dp[mx];

LL slove(LL pos)
{
       if(pos>Big) return 0;
       LL &ret=Dp[pos];
       if(ret!=-1) return ret;
       return ret=max((Cnt[pos]*pos)+slove(pos+2),slove(pos+1));
}

int main()
{
       scanf("%lld",&n);
       Big=-1;
       for(LL i=0;i<n;i++) scanf("%lld",&c),Cnt[c]++,Big=max(Big,c);
       memset(Dp,-1,sizeof(Dp));
       LL res=slove(0);
       printf("%lld\n",res);
       return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	return dp[num]=max(slove(num-1),slove(num-2)+(arr[num]*num));
}

int main()
{
	cin>>n;
	int k=-1;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		++arr[x];
		k=max(x,k);
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(k);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<=0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num-1),slove(num-2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		k=max(arr[x],k);
	}

	if(k==n)
	{
		cout<<n*n<<endl;
		return 0;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(100000);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num>100000)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num+1),slove(num+2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		//k=max(arr[x],k);
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(1);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num+1),slove(num+2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		k=max(x,k);
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(k);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(ll num)
{
	//cout<<"ok"<<endl;
	if(num<0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	return dp[num]=max(slove(num-1),slove(num-2)+(arr[num]*num));
}

int main()
{
	cin>>n;
	ll k=-1;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		++arr[x];
		k=max(x,k);
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(k);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num-1),slove(num-2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		k=max(x,k);
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(k);
	cout<<res<<endl;
	return 0;
}


/*Hints:Use Dp[mx]. Use 0-1 Knapsack concept.*/

#include<bits/stdc++.h>
#define mx 100005

using namespace std;
int n,Cnt[mx],c,Big,Dp[mx];

int slove(int pos)
{
       if(pos>Big) return 0;

       int &ret=Dp[pos];
       if(ret!=-1) return ret;
       return ret=max((Cnt[pos]*pos)+slove(pos+2),slove(pos+1));
}

int main()
{
       scanf("%d",&n);
       Big=-1;
       for(int i=0;i<n;i++) scanf("%d",&c),Cnt[c]++,Big=max(Big,c);
       memset(Dp,-1,sizeof(Dp));
       int res=slove(0);
       printf("%d\n",res);
       return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num>=100001)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num+1),slove(num+2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(1);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<=0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num-1),slove(num-2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		k=max(arr[x],k);
	}

	if(k==n)
	{
		cout<<n*x<<endl;
		return 0;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(100000);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num>100000)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num+1),slove(num+2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		//k=max(arr[x],k);
	}

	memset(dp,-1,sizeof(dp));
	dp[1]=a[1];
	ll res=max(dp[1]+slove(3),slove(2));
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
ll arr[mx];
ll n;
ll x;

ll slove(ll num)
{
	//cout<<"ok"<<endl;
	if(num<0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	return dp[num]=max(slove(num-1),slove(num-2)+(arr[num]*num));
}

int main()
{
	cin>>n;
	ll k=-1;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		++arr[x];
		k=max(x,k);
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(k);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num>=mx)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num+1),slove(num+2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(0);
	cout<<res<<endl;
	return 0;
}


/*Hints:Use Dp[mx]. Use 0-1 Knapsack concept.*/

#include<bits/stdc++.h>
#define mx 100005

using namespace std;
int n,Cnt[mx],c,Big,Dp[mx];

int slove(int pos)
{
       if(pos>Big) return 0;

       int &ret=Dp[pos];
       if(ret!=-1) return ret;

       if(!Cnt[pos]) ret=slove(pos+1);
       else if(Cnt[pos])
            ret=max((Cnt[pos]*pos)+slove(pos+2),slove(pos+1));
       return ret;
}

int main()
{
       scanf("%d",&n);
       Big=-1;
       for(int i=0;i<n;i++) scanf("%d",&c),Cnt[c]++,Big=max(Big,c);
       memset(Dp,-1,sizeof(Dp));
       int res=slove(0);
       printf("%d\n",res);
       return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num==2)
		return max(arr[1],arr[2]*2);
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num-1),slove(num-2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(100000);
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num>100000)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num+1),slove(num+2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	int k=0;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
		//k=max(arr[x],k);
	}

	memset(dp,-1,sizeof(dp));
	dp[1]=arr[1];
	ll res=max(dp[1]+slove(3),slove(2));
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#define mx 100005

using namespace std;
typedef long long int ll;
typedef unsigned long long int llu;
typedef unsigned int ui;
ll dp[mx];
int arr[mx];
int n;
int x;

ll slove(int num)
{
	//cout<<"ok"<<endl;
	if(num<=0)
		return 0;
	if(dp[num]!=-1)
		return dp[num];

	ll res=max(slove(num-1),slove(num-2)+(arr[num]*num));
	return dp[num]=res;
}

int main()
{
	cin>>n;
	memset(arr,0,sizeof(arr));
	for(int i=0;i<n;i++)
	{
		cin>>x;
		arr[x]++;
	}

	memset(dp,-1,sizeof(dp));
	ll res=slove(mx);
	cout<<res<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,temp,a[N],mx;
ll memo[2][N];
vector <int> vec;
ll solve(int ind, bool allowed){
if(ind==vec.size())
    return 0;
ll num= vec[ind];
if(~memo[allowed][num])
    return memo[allowed][num];

if (allowed){
ll op1,op2;
if(ind+1 <vec.size() &&vec[ind+1]==vec[ind]+1)
op1= ((a[num]*1ll*num)+ solve(ind+1,0) );
else
 op1= ((a[num]*num*1ll)+ solve(ind+1,1) );

op2=  solve(ind+1,1);
memo[allowed][num]= max( op1,op2);
return memo[allowed][num];
}
else
 return memo[allowed][num]=solve(ind+1,1);

}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
if(!a[temp])
    vec.push_back(temp);
    a[temp]++;
}
sort(vec.begin(),vec.end());

cout<<solve(0,1)<<endl;
return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
typedef long long int ll;
const int N=1e5+3;
int n,temp,a[N],mx;
ll memo[2][N];
ll solve(int ind, bool allowed){
if(ind>mx) return 0;
if(~memo[allowed][ind])
    return memo[allowed][ind];
if(a[ind]==0)
 return solve(ind+1,1);
if (allowed){
 return memo[allowed][ind]= max(a[ind]*ind*1ll+ solve(ind+1,0), solve(ind+1,1));
}
else
 return memo[allowed][ind]=solve(ind+1,1);
}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
    a[temp]++;
    mx= max(temp,mx);
}
cout<<solve(1,1)<<endl;
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
typedef unsigned long long int ll;
const int N=1e5+5;
int n,temp,a[N],mx;
ll memo[2][N],num;
vector <int> vec;
ll solve(int ind, bool allowed){
if(ind==vec.size())
    return 0;
num= vec[ind];
if(~memo[allowed][num])
    return memo[allowed][num];

if (allowed){
ll op1,op2;
if(ind+1 <vec.size() &&vec[ind+1]==vec[ind]+1)
op1= ((a[num]*num*1ll)+ solve(ind+1,0) );
else
 op1= ((a[num]*num*1ll)+ solve(ind+1,1) );

op2=  solve(ind+1,1);
memo[allowed][num]= max( op1,op2);
return memo[allowed][num];
}
else
 return memo[allowed][num]=solve(ind+1,1);
}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
if(!a[temp])
    vec.push_back(temp);
    a[temp]++;
}
sort(vec.begin(),vec.end());
cout<<solve(0,1)<<endl;
return 0;
}


#include <iostream>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N],vis[N],vsum;
ll ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++)
    cin>>a[i];
while (vsum< n) {
vsum=0;
for(int i=1;i<=n;i++){
vsum+= vis[i];
  if(vis[i])
    continue;
if(a[i]>=a[i-1]&&a[i]>=a[i+1]){
    ans+=a[i];
 vis[i]=vis[i+1]=vis[i-1]=1;
 a[i]=a[i-1]=a[i+1]=0;
}
}
}
cout<<ans<<endl;
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
typedef unsigned long long int ll;
const int N=1e5+5;
int n,temp,a[N],mx;
ll memo[2][N],num;
vector <int> vec;
ll solve(int ind, bool allowed){
if(ind==vec.size())
    return 0;
num= vec[ind];
if(~memo[allowed][num])
    return memo[allowed][num];

if (allowed){
ll op1,op2;
if(vec[ind+1]==vec[ind]+1)
op1= ((a[num]*num*1ll)+ solve(ind+1,0) );
else
 op1= ((a[num]*num*1ll)+ solve(ind+1,1) );

op2=  solve(ind+1,1);
memo[allowed][num]= max( op1,op2);
return memo[allowed][num];
}
else
 return memo[allowed][num]=solve(ind+1,1);
}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
if(!a[temp])
    vec.push_back(temp);
    a[temp]++;
}
sort(vec.begin(),vec.end());
cout<<solve(0,1)<<endl;
return 0;
}


#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N];
map <int,int> mp;
ll ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
    cin>>a[i];
    mp[a[i]]++;
}
for(int i=1;i<N-4;i++){
   if(mp[i]*i >= mp[i-1]*(i-1) + mp[i+1]* (i+1)){
    ans+= mp[i]*i;
    mp[i]=mp[i-1]=mp[i+1]=0;
   }
}
for(int i=1;i<N-4;i++){
   if(mp[i]*i >= mp[i-1]*(i-1) + mp[i+1]* (i+1)){
    ans+= mp[i]*i;
    mp[i]=mp[i-1]=mp[i+1]=0;
   }
}
cout<<ans<<endl;
return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
typedef long long int ll;
const int N=1e5+3;
int n,temp,a[N],memo[2][N],mx;
ll solve(int ind, bool allowed){
if(ind>mx) return 0;
if(~memo[allowed][ind])
    return memo[allowed][ind];
if(a[ind]==0)
 return solve(ind+1,1);
if (allowed){
 return memo[allowed][ind]= max(a[ind]*ind+ solve(ind+1,0), solve(ind+1,1));
}
else
 return memo[allowed][ind]=solve(ind+1,1);
}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
    a[temp]++;
    mx= max(temp,mx);
}
cout<<solve(1,1)<<endl;
return 0;
}


#include <iostream>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N],vis[N];
ll ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++)
    cin>>a[i];
for(int i=1;i<=n;i++){
  if(vis[i])
    continue;
if(a[i]>=a[i-1]&&a[i]>=a[i+1]){
    ans+=a[i];
 vis[i]=vis[i+1]=vis[i-1]=1;
 a[i]=a[i-1]=a[i+1]=0;
}
}
for(int i=1;i<=n;i++){
  if(vis[i])
    continue;
if(a[i]>=a[i-1]&&a[i]>=a[i+1]){
    ans+=a[i];
 vis[i]=vis[i+1]=vis[i-1]=1;
 a[i]=a[i-1]=a[i+1]=0;
}
}
cout<<ans<<endl;
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
typedef unsigned long long int ll;
const int N=1e5+5;
int n,temp,a[N],mx;
ll memo[2][N],num;
vector <int> vec;
ll solve(int ind, bool allowed){
if(ind>vec.size()-1)
    return 0;
num= vec[ind];
if(~memo[allowed][num])
    return memo[allowed][num];

if (allowed){
ll op1,op2;
if(ind<vec.size()-1 &&vec[ind+1]==vec[ind]+1)
op1= ((a[num]*num*1ll)+ solve(ind+1,0) );
else
 op1= ((a[num]*num*1ll)+ solve(ind+1,1) );

op2=  solve(ind+1,1);
memo[allowed][num]= max( op1,op2);
return memo[allowed][num];
}
else
 return memo[allowed][num]=solve(ind+1,1);
}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
if(!a[temp])
    vec.push_back(temp);
    a[temp]++;
    mx= max(temp,mx);
}
sort(vec.begin(),vec.end());
cout<<solve(0,1)<<endl;
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
typedef unsigned long long int ll;
const int N=1e5+5;
int n,temp,a[N],mx;
ll memo[2][N];
vector <int> vec;
ll solve(int ind, bool allowed){
if(ind==vec.size())
    return 0;
int num= vec[ind];
if(~memo[allowed][num])
    return memo[allowed][num];

if (allowed){
ll op1,op2;
if(ind+1 <vec.size() &&vec[ind+1]==vec[ind]+1)
op1= ((a[num]*num*1ll)+ solve(ind+1,0) );
else
 op1= ((a[num]*num*1ll)+ solve(ind+1,1) );

op2=  solve(ind+1,1);
memo[allowed][num]= max( op1,op2);
return memo[allowed][num];
}
else
 return memo[allowed][num]=solve(ind+1,1);

}
int main(){
memset(memo,-1,sizeof memo);
cin>>n;
for(int i=0;i<n;i++){
    cin>>temp;
if(!a[temp])
    vec.push_back(temp);
    a[temp]++;
}
sort(vec.begin(),vec.end());
solve(0,1);
cout<<solve(0,1)<<endl;
return 0;
}


#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N],mx;
map <int,int> mp;
ll ans;
int main(){
cin>>n;
for(int i=0;i<n;i++){
    cin>>a[i];
    mp[a[i]]++;
mx= max(mx,a[i]);
}
for(int j=0;j<17;j++)
for(int i=1;i<=mx;i++){
   if( (mp[i]*i) >=  (mp[i-1]*(i-1) ) + (mp[i+1]* (i+1)) ){
    ans+= (mp[i]*i);
    mp[i]=mp[i-1]=mp[i+1]=0;
   }
}

cout<<ans<<endl;
return 0;
}


#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N];
map <int,int> mp;
ll ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
    cin>>a[i];
    mp[a[i]]++;
}
for(int j=0;j<17;j++)
for(int i=1;i<N-4;i++){
   if(mp[i]*i >= mp[i-1]*(i-1) + mp[i+1]* (i+1)){
    ans+= mp[i]*i;
    mp[i]=mp[i-1]=mp[i+1]=0;
   }
}

cout<<ans<<endl;
return 0;
}


#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int n,a[N],mx;
map <int,int> mp;
ll ans;
int main(){
cin>>n;
for(int i=0;i<n;i++){
    cin>>a[i];
    mp[a[i]]++;
mx= max(mx,a[i]);
}
for(int i=1;i<=mx;i++){
   if( (mp[i]*i) >=  (mp[i-1]*(i-1) ) + (mp[i+1]* (i+1)) ){
    ans+= (mp[i]*i);
    mp[i]=mp[i-1]=mp[i+1]=0;
   }
 else
 if(mp[i]*i + mp[i+2]* (i+2) + mp[i-2]* (i-2) <= mp[i+1]* (i+1) + mp[i-1]* (i-1)){
    ans+= mp[i-1] * (i-1);
    ans+= mp[i+1] * (i+1);
    mp[i]=mp[i-1]=mp[i-2]=mp[i+1]=mp[i+2]=0;
 }
}

cout<<ans<<endl;
return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<stdio.h>

using namespace std;
int main () {
    long long i,n,a[100000],b[100000],s=0;
    cin>>n;
    for(i=1;i<=n;i++){
        b[i]=0;
    }
    for(i=1;i<=n;i++){
        cin>>a[i];
        b[a[i]]++;
    }
    for(i=2;i<n;+i++){
        s=s+max(b[i]*i,b[i-1]*(i-1)+b[i+1]*(i+1));
        b[i]=0;
        b[i-1]=0;
        b[i+1]=0;
    }
    cout<<s;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<stdio.h>

using namespace std;
int main () {
    long long i,n,a[100000],b[100000],s=0;
    cin>>n;
    for(i=1;i<=n;i++){
        b[i]=0;
    }
    for(i=1;i<=n;i++){
        cin>>a[i];
        b[a[i]]++;
    }
    for(i=2;i<=n;+i++){
        s=s+max(b[i]*i,b[i-1]*(i-1)+b[i+1]*(i+1));
        b[i]=0;
        b[i-1]=0;
        b[i+1]=0;
    }
    cout<<s;
    return 0;
}


#include<stdio.h>
#include <map>
using namespace std;

int main()
{
    long int n;
    map<long int,long int> mymap;
    long int result=0,current;
    scanf("%ld",&n);
    long int a[n];
    for(long int i=0;i<n;i++){
        scanf("%ld",&a[i]);
    }
    for(long int i=0;i<n;i++){
        current=0;
        if(mymap[a[i]]==0){
          for(long int j=0;j<n;j++){
                 if(a[j]!=a[i]+1&&a[j]!=a[i]-1){
                    current+=a[j];
                 }
           }
           if(mymap[a[i]]==0){
               mymap[a[i]]=current;
           }
        }
        if(current>result){
            result=current;
        }
    }
    printf("%ld",result);
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    const int d=*max_element(a.begin(), a.end())+1;
    vector<int> cnt(d);
    for(int i=0;i<n;i++){
        cnt[a[i]]++;
    }
    vector<long long> sum(d);
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<d;i++){
        sum[i]=max(sum[i-1],sum[i-2]+static_cast<long long>(cnt[i])*static_cast<long long>(i));
    }
    cout<<sum[d-1];
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    int d=*max_element(begin(a), end(a));
    d+=1;
    vector<int> cnt(d);
    for(int i=0;i<n;i++){
        cnt[a[i]]++;
    }
    d+=1;
    vector<long long> sum(d);
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<d;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(cnt[i]*i));
    }
    cout<<sum[d-1];
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    long long cnt[10001];
    long long sum[10001];
     for(int i=0;i<=10000;i++){
        cnt[i]=0;
    }
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        cnt[x]++;
    }
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<=10000;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(cnt[i]*i));
    }
    printf("\nAnswer : %ld",sum[10000]);
    return 0;
}


#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
    long long int odd=0,even=0;
    long long int m,n;
    scanf("%I64d",&n);
    for(int i=0;i<n;i++){
        scanf("%I64d",&m);
        if(m%2==0){
            even+=m;
        }
        else{
            odd+=m;
        }
    }
    printf("%I64d",max(odd,even));
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        //a[i]=10000;
    }
    int d=*max_element(begin(a), end(a));
    d+=1;
    vector<int> cnt(d);
    for(int i=0;i<n;i++){
        cnt[a[i]]++;
    }
    vector<long long> sum(d);
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<d;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(long long)(cnt[i]*i));
    }
    cout<<sum[d-1];
    return 0;
}


#include<stdio.h>
#include <map>
using namespace std;

int main()
{
    long int n;
    map<long int,long int> mymap;
    long int result=0,current;
    scanf("%ld",&n);
    long int a[n];
    for(long int i=0;i<n;i++){
        scanf("%ld",&a[i]);
    }
    for(long int i=0;i<n;i++){
        current=0;
        if(mymap[a[i]]==0){
          for(long int j=0;j<n;j++){
                 if(a[j]!=a[i]+1&&a[j]!=a[i]-1){
                    current+=a[j];
                 }
           }
           if(mymap[a[i]]==0){
               mymap[a[i]]=current;
           }
        }
        if(current>result){
            result=current;
        }
    }
    printf("\n%ld",result);
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    int d=*max_element(begin(a), end(a));
    d+=2;
    vector<int> cnt(d);
    for(int i=0;i<n;i++){
        cnt[a[i]]++;
    }
    vector<long long> sum(d);
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<d;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(cnt[i]*i));
    }
    cout<<sum[d-1];
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    int n,x;
    //scanf("%d",&n);
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        //scanf("%d",&a[i]);
        cin>>a[i];
        //a[i]=10000;
    }
    int d=*max_element(begin(a), end(a));
    d+=1;
    vector<int> cnt(d);
    for(int i=0;i<n;i++){
        cnt[a[i]]++;
    }
    vector<long long> sum(d);
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<d;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(long long)(cnt[i]*i));
    }
    cout<<sum[d-1];
    return 0;
}


#include<stdio.h>
#include <map>
#include<set>
using namespace std;

int main()
{
    int n,x,maximum=0,prev=-3;
    map<int,int> mymap;
    scanf("%d",&n);
    long long sum[10000];
    for(long int i=0;i<n;i++){
        scanf("%d",&x);
        mymap[x]++;
    }
    sum[1]=mymap[1];
    for(int i=2;i<10000;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(mymap[i]*i));
    }
    printf("%ld",sum[9999]);
    return 0;
}


#include<stdio.h>
#include <map>
#include <algorithm>
#include<vector>
using namespace std;

int main()
{
    long int n;
    map<long int,long int> mymap;
    long int result=0,current,prev;
    scanf("%ld",&n);
    vector<long int> a;
    for(long int i=0;i<n;i++){
        scanf("%ld",&prev);
        a.push_back(prev);
    }
    sort (a.begin(), a.end());
    for(long int i=0;i<n;i++){
        current=0;
        prev=a[i];
        //cout<<"\nFor value a[i] : "<<a[i];
        if(mymap[a[i]]==0){
          //cout<<"\nSelected a[j] values : ";
          for(long int j=0;j<n;j++){
                 if(a[j]!=a[i]+1&&a[j]!=a[i]-1&&a[j]!=prev+1){
                    //cout<<a[j]<<" ";
                    current+=a[j];
                    prev=a[j];
                 }
           }
           if(mymap[a[i]]==0){
               mymap[a[i]]=current;
           }
        }
        if(current>result){
            result=current;
        }
    }
    printf("%ld",result);
    return 0;
}


#include<stdio.h>
#include <map>
#include <algorithm>
//#include<iostream>
#include<vector>
#include<set>
using namespace std;

int main()
{
    long int n;
    map<long int,long int> mymap;
    long int result=0,current,prev;
    scanf("%ld",&n);
    vector<long int> a;
    set<long int>::iterator it;
    set<long int> myset;
    for(long int i=0;i<n;i++){
        scanf("%ld",&prev);
        a.push_back(prev);
    }
    sort (a.begin(), a.end());
    for(long int i=0;i<n;i++){
        //cout<<"\na[i] : "<<a[i];
        current=0;
        prev=a[i];
        myset.clear();
        if(mymap[a[i]]==0){
           // cout<<"\na[j] :";
        for(long int j=0;j<n;j++){
            if(prev!=a[j]-1&&a[j]!=a[i]-1&&a[j]!=a[i]+1){
                //cout<<" "<<a[j];
            if(mymap[a[j]]!=0){
                current+=mymap[a[j]];
                break;
              }
            current+=a[j];
            myset.insert(a[j]);
            prev=a[j];
            }
        }
           if(mymap[a[i]]==0){
               //cout<<"\nCurrent : "<<current;
               mymap[a[i]]=current;
                for (it=myset.begin(); it!=myset.end(); ++it)
                    mymap[*it]=current;
           }
        if(current>result){
            result=current;
        }
        }
    }
    printf("%ld",result);
    return 0;
}


#include<stdio.h>
#include <map>
#include <algorithm>
#include<vector>
#include<set>
using namespace std;

int main()
{
    long int n;
    map<long int,long int> mymap;
    long int result=0,current,prev;
    scanf("%ld",&n);
    vector<long int> a;
    set<long int>::iterator it;
    set<long int> myset;
    for(long int i=0;i<n;i++){
        scanf("%ld",&prev);
        a.push_back(prev);
    }
    sort (a.begin(), a.end());
    for(long int i=0;i<n;i++){
        current=0;
        myset.clear();
        if(mymap[a[i]]==0){
        for(long int j=0;j<n;j++){
            if(prev!=a[j]-1&&a[j]!=a[i]-1&&a[j]!=a[i]+1){
            if(mymap[a[j]]!=0){
                current+=mymap[a[j]];
                break;
              }
            current+=a[j];
            myset.insert(a[j]);
            prev=a[j];
            }
        }
           if(mymap[a[i]]==0){
               mymap[a[i]]=current;
                for (it=myset.begin(); it!=myset.end(); ++it)
                    mymap[*it]=current;
           }
        if(current>result){
            result=current;
        }
        }
    }
    printf("%ld",result);
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    long long cnt[10001];
    long long sum[10001];
     for(int i=0;i<=10000;i++){
        cnt[i]=0;
    }
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        cnt[x]++;
    }
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<=10000;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(cnt[i]*i));
    }
    printf("%ld",sum[10000]);
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    int cnt[10002]={};
    long long sum[10002];
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        cnt[x]++;
    }
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<=10001;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(cnt[i]*i));
    }
    cout<<sum[10001];
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    int n,x;
    scanf("%d",&n);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    int d=*max_element(begin(a), end(a));
    d+=1;
    vector<int> cnt(d);
    for(int i=0;i<n;i++){
        cnt[a[i]]++;
    }
    vector<long long> sum(d);
    sum[0]=0;
    sum[1]=cnt[1];
    for(int i=2;i<d;i++){
        sum[i]=max(sum[i-1],sum[i-2]+(cnt[i]*i));
    }
    cout<<sum[d-1];
    return 0;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mx;
ll mydp(ll i)
{
    //cout<<i<<endl;
    if(i>mx)
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    int ret1=0,ret2=0;
    ret1=arr[i]*i+mydp(i+2);
    ret2=mydp(i+1);
    dp[i]=max(ret1,ret2);
    return dp[i];
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        mx=max(x,mx);
        arr[x]++;
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(1);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

int arr[M+10];
int mark[M+10];
vector<int>v,freq;
int n;
int mydp(int i)
{
    if(i>=v.size())
    {
        return 0;
    }
    int ret1=0,ret2=0;
    ret1=freq[i]*v[i]+mydp(i+2);
    ret2=mydp(i+1);
    return max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        arr[x]++;
    }
    for(int i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }

    int ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
       return max(ret1,ret2);
    }
    if(v[i+1]-v[i]>1)
    {
        mark[v[i]]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(v[i+1]-v[i]>1)
    {
        mark[v[i]]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
       return max(ret1,ret2);
    }
    if(v[i+1]-v[i]>1)
    {
        mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }

    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
ll  dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
       return max(ret1,ret2);
    }
    if(v[i+1]-v[i]>1)
    {
        mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
       return max(ret1,ret2);
    }
    if(v[i+1]-v[i]>1)
    {
        mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
        ret1=v[i]*freq[i];
        return ret1;
    }
    if(v[i+1]-v[i]>1)
    {
        mark[v[i]]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
      // return max(ret1,ret2);
    }
    else
    {

    if(v[i+1]-v[i]>1)
    {
        mark[v[i]]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    }
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
       return max(ret1,ret2);
    }
    if(v[i+1]-v[i]>1)
    {
        mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    return max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return 0;
    }
    int ret1=0,ret2=0;
    if(v[i+1]-v[i]>1)
    {
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else
    {
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);

    dp[i]=max(ret1,ret2);
    return dp[i];
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
   v.pb(M);
    freq.pb(0);
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
        ret1=v[i]*freq[i];
//        return ret1;
    }
    else
    {

    if(v[i+1]-v[i]>1)
    {
        mark[v[i]]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    }
    ret2=mydp(i+1);
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[100100];
ll dp[101000];
ll n;
ll mx;
ll mydp(ll i)
{
    //cout<<i<<endl;
    if(i>mx)
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
   ll  ret1=0,ret2=0;
    ret1=arr[i]*i+mydp(i+2);
    ret2=mydp(i+1);
    dp[i]=max(ret1,ret2);
    return dp[i];
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        mx=max(x,mx);
        arr[x]++;
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(1);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
int dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return 0;
    }
    ll ret1=0,ret2=0;
    if(v[i+1]-v[i]>1)
    {
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else
    {
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);

    dp[i]=max(ret1,ret2);
    return dp[i];
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
   v.pb(M);
    freq.pb(0);
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

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

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair
#define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

ll arr[M+10];
ll mark[M+10];
ll dp[M+10];
vector<ll>v,freq;
ll n;
ll mydp(ll i)
{
    if(i>=v.size())
    {
        return 0;
    }
    if(dp[i]!=-1)
    {
        return dp[i];
    }
    ll ret1=0,ret2=0;
    if(i+1==v.size())
    {
       if(mark[i-1]==true)
            ret1=0;
       else
            ret2=v[i]*freq[i];
      // return max(ret1,ret2);
    }
    else
    {

    if(v[i+1]-v[i]>1)
    {
        mark[v[i]]=true;
        ret1=v[i]*freq[i]+mydp(i+1);
    }
    else{
     //   mark[i]=true;
        ret1=v[i]*freq[i]+mydp(i+2);
    }
    ret2=mydp(i+1);
    }
    return dp[i]=max(ret1,ret2);
}
int main()
{
    fast_in_out;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        arr[x]++;
    }
    for(ll i=0;i<M;i++)
    {
        if(arr[i]!=0)
        {
            v.pb(i);
            freq.pb(arr[i]);
        }
    }
    memset(dp,-1,sizeof dp);
    ll ans=mydp(0);
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+6;
typedef long long ll;
ll dp[N], a[N];
int main()
{
    ll n, x, max1 = 0;
    scanf("%lld",&n);
    for(ll i = 1 ; i <= n ; i++)
    {
        scanf("%lld",&x);
        max1 = max(x, max1);
        a[x]++;
    }
    dp[1] = a[1];
    for(ll i = 2 ; i <= max1 ; i++)
        dp[i] = max(dp[i-1], dp[i-2] + a[i] * i);
    printf("%lld\n",dp[max1]);
    return 0;
}//2020-02-26 11:51:11.105


#include<stdio.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<time.h>
#include<map>
#include<vector>
#include<iostream>
#include<assert.h>
using namespace std;
#define sca(a) scanf("%d",&a)
#define out(a) printf("%d\n",a)
#define mes(x,a); memset(x,a,sizeof(x));
#define lowbit(x)  x & (-x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define pii pair<int, int>
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=1e6+5;
const int inf =0x3f3f3f3f;
ll mod=9973;
const double eps=1e-9;
const double pi=acos(-1);
int m,n,w;
ll a[maxn];
ll dp[maxn];
int main()
{
    int t;
    scanf("%d",&n);
    int q;
    for(int i=1;i<=n;i++)
        scanf("%d",&q),a[q]++;
    dp[1]=a[1];
    ll ans=dp[1];
    for(int i=2;i<=100000;i++)
    {
        dp[i]=max(dp[i-2]+a[i]*i,dp[i-1]);
        ans=max(ans,dp[i]);
    }
    printf("%I64d\n",ans);
    return 0;
}
//2019-08-20 20:04:51.113


/*
	ID: sixathofpain
	TASK: Lynyrd Skynyrd
	LANG: C++

*/

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                           \
    std::ios::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define endl "\n"
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m)
{
    if (n == 0)
        return 1;
    ll p = power(a, n / 2, m);
    p = (p * p) % m;
    if (n % 2)
        return (p * a) % m;
    else
        return p;
}
const ll MOD = 1000000007;
const int N = int(2e5) + 999;
#define int long long int

vector<vector<int>> up;
int lg;
int nxt[N];

int32_t main(){
    IOS;
    lg = log2(N)+1;

    int n,m,q;
    cin>>n>>m>>q;
	vector<int> pos;
	pos.resize(n+1,m+1);
    int p[n+1]={},b[m+1];
    for(int i =1;i<=n;i++)
    {
    	cin>>p[i];
    	nxt[p[i-1]] = p[i];
    }
    nxt[p[n]] = p[1];

    for(int i = 1;i<=m;i++)
    	cin>>b[i];

    up.resize(m+2,vector<int>(lg+2,m+1));
    int ans[m+2];
    ans[m+1] = m+1;

    for(int i=m;i>=1;i--){

    	up[i][0] = pos[nxt[b[i]]];

    	for(int j=1;j<=lg;j++)
    		up[i][j] = up[up[i][j-1]][j-1];

    	int tmp = n-1,u = i;
    	for(int j=lg;j>=0;j--){
    		if((1<<j)<=tmp){
    			tmp-=(1<<j);
    			u = up[u][j];
    		}
    	}
    	ans[i] = min(ans[i+1],u);
    	pos[b[i]] = i;

    }

    while(q--){
    	int l,r;
    	cin>>l>>r;
    	// cerr<<ans[l]<<endl;
    	if(ans[l]<=r){
    		cout<<"1";
    	}else{
    		cout<<"0";
    	}
    	// cout<<endl;
    }

	cout<<endl;
    return 0;
}


#include <math.h>
#include <queue>
#include<functional>
#include<algorithm>
#include<list>
#include<deque>
#include<map>
#include <unordered_map>
#include<list>
#include<set>
#include<stack>
#include<cstring>
#include<bitset>
#define ll long long int
#define pb           push_back
#define pii          pair<int,int>
#define vi           vector<int>
#define vpii         vector<pair<int,int>>
#define gph          map<int, vector<int>>
#define mp           make_pair
#define FOR(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)n; i >= 0; i--)
#define FORS(i, n) for(; i < (int)(n); i++)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }
#define FastRead     ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#include <iostream>
vi perm;
vi vals;
map<int,int> inv;
int n, m ,q;
int prev(int x){
    int ind = inv[x];
    if(ind == n - 1)return perm.front();
    return perm[ind +1];
}

int jump[(int)2e5 + 5][20];

int jumped(int start, int n){
    int val = 1;
    for(int i =0; n >=val; i++){
        if(start == -1)return -1;
        if(n & val)start = jump[start][i];
        val *= 2;
    }
    return start;
}

int main(){
    FastRead;

    cin >> n>> m >>q;
    int in;
    FOR(i, n){
        cin >> in;
        perm.pb(in);
        inv[in] = i;
    }
    FOR(i, m){
        cin >> in;
        vals.pb(in);
    }
    vi nearest(m);
    map<int,int> last;
    FORD(i, m-1){
        if(last[prev(vals[i])]){
            nearest[i] = last[prev(vals[i])];
        }else nearest[i] = -1;
        last[vals[i]] = i;
    }

    const int maxm = log2(m) + 1;
    //DEBUG(maxm);
    FOR(i,m)FOR(j, maxm)jump[i][j] = -1;

    FOR(j, maxm){
        FOR(i,m){
            if(j ==0)jump[i][j] = nearest[i];
            else if(jump[i][j-1] != -1){
                jump[i][j] = jump[jump[i][j-1]][j-1];
            }
            //cout<< jump[i][j] << " ";
        }//cout <<endl;
    }
    /*while(true){
        int dis;
        cin >> in >>dis;
        cout<< jumped(in, dis) <<endl;
    }*/
    int endd[m+3];
    FOR(i, m)endd[i] = INT_MAX;
    int prev = INT_MAX;
    //cout << "endings printing" <<endl;
    FORD(i, m-1){
        if(jumped(i, n-1) != -1){
            prev = min(prev, jumped(i, n-1));

        }
        endd[i] = prev;
       // cout << endd[i] << " ";
    }//cout <<endl;

    int l, r;
    FOR(i, q){
        cin >>l >> r;
        l--; r--;
        if(endd[l] <= r)cout << 1;
        else cout << 0;
    }

}


#include <math.h>
#include <queue>
#include<functional>
#include<algorithm>
#include<list>
#include<deque>
#include<map>
#include <unordered_map>
#include<list>
#include<set>
#include<stack>
#include<cstring>
#include<bitset>
#define ll long long int
#define pb           push_back
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vpii         vector<pair<ll,ll>>
#define gph          map<ll, vector<ll>>
#define mp           make_pair
#define FOR(i, n) for (ll i = 0; i <  (ll)(n); i++)
#define FOR1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define FORD(i, n) for (ll i = (ll)n; i >= 0; i--)
#define FORS(i, n) for(; i < (ll)(n); i++)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }
#define FastRead     ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#include <iostream>
const int ss = 2e5 + 500;
//long long
vi vals;
unordered_map<int, int> inv;
int _prev(int i){
    if(i ==0)return vals.back();
    return vals[i-1];
}
int prev(int i){
    return _prev(inv[i]);
}

int arr[ss] = {0};
struct info{
    int len;
    int ind;
    info(int l, int i){
        len = l;
        ind = i;
    }
    info(){
        len = 0;
    }
};
map<int, int> nextIn;
//info ans[ss];
int main(){
    FastRead;
    int n, m, q;
    cin >> n >> m >> q;
    int in;
    FOR(i, n){
        cin >> in;
        vals.pb(in);
        inv[in] = i;
    }
    vector<info> ans;
    ans.pb(info(0,0));
    FOR1(i, m){
        cin >> in;
        //DEBUG(arr[prev(in)]);
        if(!arr[prev(in)]){
            ans.pb(info(1, i));
        }else{
            info x = ans[arr[prev(in)]];
            nextIn[arr[prev(in)]] = i;
            //DEBUG(x.len);
           // DEBUG(x.ind);
            if(x.len == n)x.ind = nextIn[x.ind];
            if(x.len != n)x.len++;
            ans.pb(x);
        }
        arr[in] = i;
    }
    int end[ss] = {0};
    FOR1(i, m){
        if(ans[i].len == n && !end[ans[i].ind])end[ans[i].ind] = i;
    }
    //FOR1(i, m){
      //  cout << end[i] << " ";
    //}cout <<endl;
    int prev = 0;
    for(int i = m; i >= 1;i--){
        if(prev){
            if(!end[i])end[i] = prev;
            else end[i] = min(prev, end[i]);
        }
        if(end[i]){
            if(!prev)prev = end[i];
            else prev= min(prev, end[i]);
        }
    }
    //FOR1(i, m){
      //cout << end[i] << " ";
    //}cout <<endl;
    int l,r;
    FOR(i, q){
        cin >> l >> r;
        if(end[l] && end[l] <= r)cout << 1;
        else cout << 0;
    }

}


#include <math.h>
#include <queue>
#include<functional>
#include<algorithm>
#include<list>
#include<deque>
#include<map>
#include <unordered_map>
#include<list>
#include<set>
#include<stack>
#include<cstring>
#include<bitset>
#define ll long long int
#define pb           push_back
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vpii         vector<pair<ll,ll>>
#define gph          map<ll, vector<ll>>
#define mp           make_pair
#define FOR(i, n) for (ll i = 0; i <  (ll)(n); i++)
#define FOR1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define FORD(i, n) for (ll i = (ll)n; i >= 0; i--)
#define FORS(i, n) for(; i < (ll)(n); i++)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }
#define FastRead     ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#include <iostream>
const int ss = 2e5 + 500;
//long long
vi vals;
unordered_map<int, int> inv;
int _prev(int i){
    if(i ==0)return vals.back();
    return vals[i-1];
}
int prev(int i){
    return _prev(inv[i]);
}

int arr[ss] = {0};
struct info{
    int len;
    int ind;
    info(int l, int i){
        len = l;
        ind= i;
    }
    info(){
        len = 0;
        ind= 0;
    }
};

//info ans[ss];
int main(){
    FastRead;
    int n, m, q;
    cin >> n >> m >> q;
    int in;
    FOR(i, n){
        cin >> in;
        vals.pb(in);
        inv[in] = i;
    }
    vector<info> ans;
    ans.pb(info(0,0));
    FOR1(i, m){
        cin >> in;
        if(!arr[prev(in)]){
            ans.pb(info(1, i));
        }else{
            info x = ans[arr[prev(in)]];
            ans.pb(info(min(x.len+1, n), x.ind));
        }
        arr[in] = i;
    }
    int end[ss] = {0};
    FOR1(i, m){
        if(ans[i].len == n && !end[ans[i].ind])end[ans[i].ind] = i;
    }
    //FOR1(i, m){
      //  cout << end[i] << " ";
    //}cout <<endl;
    int prev = 0;
    for(int i = m; i >= 1;i--){
        if(prev){
            if(!end[i])end[i] = prev;
            else end[i] = min(prev, end[i]);
        }
        if(end[i]){
            if(!prev)prev = end[i];
            else prev= min(prev, end[i]);
        }
    }
    //FOR1(i, m){
      //cout << end[i] << " ";
    //}cout <<endl;
    int l,r;
    FOR(i, q){
        cin >> l >> r;
        if(end[l] && end[l] <= r)cout << 1;
        else cout << 0;
    }

}


#include <math.h>
#include <queue>
#include<functional>
#include<algorithm>
#include<list>
#include<deque>
#include<map>
#include <unordered_map>
#include<list>
#include<set>
#include<stack>
#include<cstring>
#include<bitset>
#define ll long long int
#define pb           push_back
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vpii         vector<pair<ll,ll>>
#define gph          map<ll, vector<ll>>
#define mp           make_pair
#define FOR(i, n) for (ll i = 0; i <  (ll)(n); i++)
#define FOR1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define FORD(i, n) for (ll i = (ll)n; i >= 0; i--)
#define FORS(i, n) for(; i < (ll)(n); i++)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }
#define FastRead     ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#include <iostream>
const int ss = 2e5 + 500;
//long long
vi vals;
unordered_map<int, int> inv;
int _prev(int i){
    if(i ==0)return vals.back();
    return vals[i-1];
}
int prev(int i){
    return _prev(inv[i]);
}

int arr[ss] = {0};
struct info{
    int len;
    int ind;
    info(int l, int i){
        len = l;
        ind= i;
    }
    info(){
        len = 0;
        ind= 0;
    }
};

//info ans[ss];
int main(){
    FastRead;
    int n, m, q;
    cin >> n >> m >> q;
    int in;
    FOR(i, n){
        cin >> in;
        vals.pb(in);
        inv[in] = i;
    }
    vector<info> ans;
    ans.pb(info(0,0));
    FOR1(i, m){
        cin >> in;
        if(!arr[prev(in)]){
            ans.pb(info(1, i));
        }else{
            info x = ans[arr[prev(in)]];
            ans.pb(info(min(x.len+1, n), x.ind));
        }
        arr[in] = i;
    }
    int end[ss] = {0};
    FOR1(i, m){
        if(ans[i].len == n && !end[ans[i].ind])end[ans[i].ind] = i;
    }
    //FOR1(i, m){
      //  cout << end[i] << " ";
    //}cout <<endl;
    int prev = 0;
    for(int i = m; i >= 1;i--){
        if(prev)end[i] = min(prev, end[i]);
        if(end[i]){
            if(!prev)prev = end[i];
            else prev= min(prev, end[i]);
        }
    }
    //FOR1(i, m){
      //  cout << end[i] << " ";
    //}cout <<endl;
    int l,r;
    FOR(i, q){
        cin >> l >> r;
        if(end[l] && end[l] <= r)cout << 1;
        else cout << 0;
    }

}


#include <math.h>
#include <queue>
#include<functional>
#include<algorithm>
#include<list>
#include<deque>
#include<map>
#include <unordered_map>
#include<list>
#include<set>
#include<stack>
#include<cstring>
#include<bitset>
#define ll long long int
#define pb           push_back
#define pii          pair<int,int>
#define vi           vector<int>
#define vpii         vector<pair<int,int>>
#define gph          map<int, vector<int>>
#define mp           make_pair
#define FOR(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)n; i >= 0; i--)
#define FORS(i, n) for(; i < (int)(n); i++)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }
#define FastRead     ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#include <iostream>
vi perm;
vi vals;
map<int,int> inv;
int n, m ,q;
int prev(int x){
    int ind = inv[x];
    if(ind == n - 1)return perm.front();
    return perm[ind +1];
}

int jump[(int)2e5 + 5][20];

int jumped(int start, int n){
    int val = 1;
    for(int i =0; n >=val; i++){
        if(start == -1)return -1;
        if(n & val)start = jump[start][i];
        val *= 2;
    }
    return start;
}

int main(){
    FastRead;

    cin >> n>> m >>q;
    int in;
    FOR(i, n){
        cin >> in;
        perm.pb(in);
        inv[in] = i;
    }
    FOR(i, m){
        cin >> in;
        vals.pb(in);
    }
    vi nearest(m);
    map<int,int> last;
    FORD(i, m-1){
        if(last[prev(vals[i])]){
            nearest[i] = last[prev(vals[i])];
        }else nearest[i] = -1;
        last[vals[i]] = i;
    }

    const int maxm = log2(m) + 1;
    //DEBUG(maxm);
    FOR(i,m)FOR(j, maxm)jump[i][j] = -1;

    FOR(j, maxm){
        FOR(i,m){
            if(j ==0)jump[i][j] = nearest[i];
            else if(jump[i][j-1] != -1){
                jump[i][j] = jump[jump[i][j-1]][j-1];
            }
            cout<< jump[i][j] << " ";
        }cout <<endl;
    }
    while(true){
        int dis;
        cin >> in >>dis;
        cout<< jumped(in, dis) <<endl;
    }
    int endd[m+3];
    FOR(i, m)endd[i] = INT_MAX;
    int prev = INT_MAX;
    FORD(i, m-1){
        if(jumped(i, n-1) != -1){
            endd[i] = min(prev, jumped(i, n-1));
            prev = min(prev, jumped(i, n-1));
        }
        //cout << endd[i] << " ";
    }//cout <<endl;

    int l, r;
    FOR(i, q){
        cin >>l >> r;
        l--; r--;
        if(endd[l] <= r)cout << 1;
        else cout << 0;
    }

}


#include <math.h>
#include <queue>
#include<functional>
#include<algorithm>
#include<list>
#include<deque>
#include<map>
#include <unordered_map>
#include<list>
#include<set>
#include<stack>
#include<cstring>
#include<bitset>
#define ll long long int
#define pb           push_back
#define pii          pair<int,int>
#define vi           vector<int>
#define vpii         vector<pair<int,int>>
#define gph          map<int, vector<int>>
#define mp           make_pair
#define FOR(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)n; i >= 0; i--)
#define FORS(i, n) for(; i < (int)(n); i++)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }
#define FastRead     ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;
#include <iostream>
vi perm;
vi vals;
map<int,int> inv;
int n, m ,q;
int prev(int x){
    int ind = inv[x];
    if(ind == n - 1)return perm.front();
    return perm[ind +1];
}

int jump[(int)2e5 + 5][20];

int jumped(int start, int n){
    int val = 1;
    for(int i =0; n >=val; i++){
        if(start == -1)return -1;
        if(n & val)start = jump[start][i];
        val *= 2;
    }
    return start;
}

int main(){
    FastRead;

    cin >> n>> m >>q;
    int in;
    FOR(i, n){
        cin >> in;
        perm.pb(in);
        inv[in] = i;
    }
    FOR(i, m){
        cin >> in;
        vals.pb(in);
    }
    vi nearest(m);
    map<int,int> last;
    FORD(i, m-1){
        if(last[prev(vals[i])]){
            nearest[i] = last[prev(vals[i])];
        }else nearest[i] = -1;
        last[vals[i]] = i;
    }

    const int maxm = log(m) + 1;

    FOR(i,m)FOR(j, maxm)jump[i][j] = -1;

    FOR(j, maxm){
        FOR(i,m){
            if(j ==0)jump[i][j] = nearest[i];
            else if(jump[i][j-1] != -1){
                jump[i][j] = jump[jump[i][j-1]][j-1];
            }
           // cout<< jump[i][j] << " ";
        }//cout <<endl;
    }
    int endd[m];
    FOR(i, m)endd[i] = INT_MAX;
    int prev = INT_MAX;
    FORD(i, m-1){
        if(jumped(i, n-1) != -1){
            endd[i] = min(prev, jumped(i, n-1));
            prev = min(prev, jumped(i, n-1));
        }
        //cout << endd[i] << " ";
    }//cout <<endl;

    int l, r;
    FOR(i, q){
        cin >>l >> r;
        l--; r--;
        if(endd[l] <= r)cout << 1;
        else cout << 0;
    }

}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = 32;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=0;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }

    int mm[m+1]={0};

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = log(2*(n+1))+1;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=-1;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }
    map<int,int> mm;

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = 32;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=0;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }

    int mm[n+1]={0};

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = log(2*(n+1))+1;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=0;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }
    map<int,int> mm;

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            if(dp[i][j-1]<=m)
                dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1]={0},lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = 32;
    int dp[m+1][val];

    for(i=0;i<m+1;++i)
    {
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }

    int mm[n+1]={0};

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    if(m<n)
    {
        while(k--)
        {
            cin>>i>>j;
            cout<<0;
        }

        return 0;
    }

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = 32;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=0;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }

    int mm[n+1]={0};

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = log(2*(n+1))+1;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=-1;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }
    map<int,int> mm;

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef && val==b[lef])
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = log(2*(n+1))+1;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=-1;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }
    map<int,int> mm;

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1],lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = log(2*(n+1))+1;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        ans[i]=-1;
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }
    map<int,int> mm;

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=n;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}


//To debug :  g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define pi acos(-1.0)/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main()
{
    IOS;

    int n,m,k,i,j,las,pos,val,dif,v;

    cin>>n>>m>>k;

    int a[n+1],pre[n+1],ans[m+1]={0},lc[n+1];

    for(i=1;i<=n;++i)
            cin>>a[i];

    int b[m+1];

    for(i=1;i<=m;++i)
            cin>>b[i];

    for(i=1;i<=n;++i)
    {
        j = i-1;
        if(j==0)
            j=n;
        pre[a[i]]=a[j];

        j = i-(n-1);

        if(j<=0)
            j+=n;

        lc[a[i]] = a[j];

    }

    val = 32;
    int dp[m+1][val];

    for(i=0;i<n+1;++i)
    {
        for(j=0;j<val;++j)
            dp[i][j]=0;
    }

    int mm[n+1]={0};

    for(i=1;i<=m;++i)
    {
        if(mm[pre[b[i]]]!=0)
            dp[i][0]=mm[pre[b[i]]];

        mm[b[i]]=i;

    }

    for(i=1;i<=m;++i)
    {
        for(j=1;j<val;++j)
        {
            dp[i][j] = dp[dp[i][j-1]][j-1];
        }

    }

    dif = n-1;

    for(i=1;i<=m;++i)
    {

        pos=i;

        for(j=31;j>=0;--j)
        {
            v = (1<<j);

            if(v & dif)
            {
                pos = dp[pos][j];
            }

        }

        ans[i] = pos;

    }

    int lef,rig;

    for(i=2;i<=m;++i)
    {
        ans[i] = max(ans[i-1],ans[i]);
    }

    while(k--)
    {
        cin>>lef>>rig;

        val = lc[b[rig]];

        if(ans[rig]>=lef)
        {
            cout<<1;
        }
        else
            cout<<0;

    }

    return 0;
}