#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
	int x,k,c;
}a[101];
int n,m,b,t,M,mn=1e18,dp[2000001];
inline int read()
{
    int sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
    {
        if (ch=='-')
	      x=-1;
	    ch=getchar();
    }
    while (ch>='0'&&ch<='9')
    {
   	    sum=sum*10+ch-'0';
	    ch=getchar();
    }
    return sum*x;
}
inline void write(int x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');
    return;
}
inline bool cmp(node a,node b)
{
	return a.k<b.k;
}
signed main()
{
    n=read();m=read();b=read();
    M=(1<<m)-1;
    for (register int i=1;i<=n;++i)
    {
        a[i].x=read();a[i].k=read();t=read();
        for (register int j=1;j<=t;++j)
          a[i].c|=1<<(read())-1;
    }
    sort(a+1,a+1+n,cmp);
    for (register int i=0;i<=M;++i)
      dp[i]=1e18;
    dp[0]=0;
    for (register int i=1;i<=n;++i)
	{
		for (register int j=0;j<=M;++j)
       	  if(dp[j]+a[i].x<dp[j|a[i].c])
	        dp[j|a[i].c]=dp[j]+a[i].x;
    	mn=min(mn,dp[M]+b*a[i].k);
	}
    if (mn!=1e18&&mn>-1)
	  write(mn);else
	  write(-1);
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
	int x,k,c;
}a[101];
int n,m,b,t,M,mn=1e19,dp[2000001];
inline int read()
{
    int sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
    {
        if (ch=='-')
	      x=-1;
	    ch=getchar();
    }
    while (ch>='0'&&ch<='9')
    {
   	    sum=sum*10+ch-'0';
	    ch=getchar();
    }
    return sum*x;
}
inline void write(int x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');
    return;
}
inline bool cmp(node a,node b)
{
	return a.k<b.k;
}
signed main()
{
    n=read();m=read();b=read();
    M=(1<<m)-1;
    for (register int i=1;i<=n;++i)
    {
        a[i].x=read();a[i].k=read();t=read();
        for (register int j=1;j<=t;++j)
          a[i].c|=1<<(read())-1;
    }
    sort(a+1,a+1+n,cmp);
    memset(dp,127,sizeof(dp));
    dp[0]=0;
    for (register int i=1;i<=n;++i)
	{
		for (register int j=0;j<=M;++j)
       	  if(dp[j]+a[i].x<dp[j|a[i].c])
	        dp[j|a[i].c]=dp[j]+a[i].x;
    	mn=min(mn,dp[M]+b*a[i].k);
	}
    if (mn!=1e19&&mn>-1)
	  write(mn);else
	  write(-1);
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
	int x,k,c;
}a[101];
int n,m,b,t,M,mn=1e19,dp[2000001];
inline int read()
{
    int sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
    {
        if (ch=='-')
	      x=-1;
	    ch=getchar();
    }
    while (ch>='0'&&ch<='9')
    {
   	    sum=sum*10+ch-'0';
	    ch=getchar();
    }
    return sum*x;
}
inline void write(int x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');
    return;
}
inline bool cmp(node a,node b)
{
	return a.k<b.k;
}
signed main()
{
    n=read();m=read();b=read();
    M=(1<<m)-1;
    for (register int i=1;i<=n;++i)
    {
        a[i].x=read();a[i].k=read();t=read();
        for (register int j=1;j<=t;++j)
          a[i].c|=1<<(read())-1;
    }
    sort(a+1,a+1+n,cmp);
    memset(dp,0x7f,sizeof(dp));
    dp[0]=0;
    for (register int i=1;i<=n;++i)
	{
		for (register int j=0;j<=M;++j)
       	  if(dp[j]+a[i].x<dp[j|a[i].c])
	        dp[j|a[i].c]=dp[j]+a[i].x;
    	mn=min(mn,dp[M]+b*a[i].k);
	}
    if (mn!=1e19&&mn>-1)
	  write(mn);else
	  write(-1);
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
	int x,k,c;
}a[101];
int n,m,b,t,M,mn=553295301492712182,dp[2000001];
inline int read()
{
    int sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
    {
        if (ch=='-')
	      x=-1;
	    ch=getchar();
    }
    while (ch>='0'&&ch<='9')
    {
   	    sum=sum*10+ch-'0';
	    ch=getchar();
    }
    return sum*x;
}
inline void write(int x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');
    return;
}
inline bool cmp(node a,node b)
{
	return a.k<b.k;
}
signed main()
{
    n=read();m=read();b=read();
    M=(1<<m)-1;
    for (register int i=1;i<=n;++i)
    {
        a[i].x=read();a[i].k=read();t=read();
        for (register int j=1;j<=t;++j)
          a[i].c|=1<<(read())-1;
    }
    sort(a+1,a+1+n,cmp);
    memset(dp,127,sizeof(dp));
    dp[0]=0;
    for (register int i=1;i<=n;++i)
	{
		for (register int j=0;j<=M;++j)
       	  if(dp[j]+a[i].x<dp[j|a[i].c])
	        dp[j|a[i].c]=dp[j]+a[i].x;
    	mn=min(mn,dp[M]+b*a[i].k);
	}
    if (mn!=553295301492712182)
	  write(mn);else
	  write(-1);
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
	int x,k,c;
}a[101];
int n,m,b,t,M,mn=1e18,dp[2000001];
inline int read()
{
    int sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
    {
        if (ch=='-')
	      x=-1;
	    ch=getchar();
    }
    while (ch>='0'&&ch<='9')
    {
   	    sum=sum*10+ch-'0';
	    ch=getchar();
    }
    return sum*x;
}
inline void write(int x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');
    return;
}
inline bool cmp(node a,node b)
{
	return a.k<b.k;
}
signed main()
{
    n=read();m=read();b=read();
    M=(1<<m)-1;
    for (register int i=1;i<=n;++i)
    {
        a[i].x=read();a[i].k=read();t=read();
        for (register int j=1;j<=t;++j)
          a[i].c|=1<<(read())-1;
    }
    sort(a+1,a+1+n,cmp);
    memset(dp,0x7f,sizeof(dp));
    dp[0]=0;
    for (register int i=1;i<=n;++i)
	{
		for (register int j=0;j<=M;++j)
       	  if(dp[j]+a[i].x<dp[j|a[i].c])
	        dp[j|a[i].c]=dp[j]+a[i].x;
    	mn=min(mn,dp[M]+b*a[i].k);
	}
    if (mn!=1e18&&mn>-1)
	  write(mn);else
	  write(-1);
    return 0;
}


#include<iostream>
using namespace std;
long long int arr[100005];
int main()
{   int n,x,maxim=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {   cin>>x;
        if(x>maxim) maxim = x;
        arr[x]+=x;
    }
    for(int i=maxim-2;i>=0;i--)
    {   arr[i] += max(arr[i+2],arr[i+3]);
    }
    long long int res = arr[0]>arr[1]?arr[0]:arr[1];
    cout<<res;
    return 0;
}


#include<iostream>
using namespace std;
long long int counter[200005],dp[200005],maxim[200005],num[20005],first,last;
int main()
{   int n;
    cin>>n;
    last=0;
    for(int i=0;i<n;i++)
    {   int x;  cin>>x;
        if(x>last)  last=x;
        if(x<first) first=x;
        counter[x]+=x;
    }
    int k=0;
    for(int i=first;i<=last;i++)
    {   if(counter[i]!=0){
            dp[k]=counter[i];
            num[k++]=i;
        }
    }
    maxim[k-1]=dp[k-1];
    maxim[k-2]=max(dp[k-1],dp[k-2]);
    for(int i=k-3;i>=0;i--)
    {   if(num[i+1]==num[i]+1)
            dp[i]=dp[i]+maxim[i+2];
        else
            dp[i]=dp[i]+maxim[i+1];
        maxim[i]=max(dp[i]+maxim[i],maxim[i+1]);

    }
    cout<<maxim[0];
    return 0;
}


#include<iostream>
#include<set>
using namespace std;
set<int> ss;
long long int arr[100005],total[100005];
int main()
{   int n,x;
    cin>>n;
    for(int i=0;i<n;i++)
    {   cin>>x;
        ss.insert(x);
        total[x]+=x;
    }
    set<int>::iterator it;
    int k=0,maxim,start=0;
    for(it=ss.begin();it!=ss.end();it++)
        arr[k++]=*it;
    for(int i=k-3;i>=0;i--)
    {   maxim=0;
        if(arr[i+1]!=arr[i]+1)  start=i+1;
        else    start=i+2;
        total[arr[i]]+=max(total[arr[start]],total[arr[start+1]]);
    }
    cout<<max(total[arr[0]],total[arr[1]]);
    return 0;
}


#include<iostream>
using namespace std;
long long int counter[200005],dp[200005],maxim[200005],num[20005],first,last;
int main()
{   int n;
    cin>>n;
    last=0;
    for(int i=0;i<n;i++)
    {   int x;  cin>>x;
        if(x>last)  last=x;
        if(x<first) first=x;
        counter[x]+=x;
    }
    int k=0;
    for(int i=first;i<=last;i++)
    {   if(counter[i]!=0){
            dp[k]=counter[i];
            num[k++]=i;
        }
    }
    maxim[k-1]=dp[k-1];
    maxim[k-2]=max(dp[k-1],dp[k-2]);
    for(int i=k-3;i>=0;i--)
    {   if(num[i+1]==num[i]+1)
            dp[i]=dp[i]+maxim[i+2];
        else
            dp[i]=dp[i]+maxim[i+1];
        maxim[i]=max(dp[i]+maxim[i],maxim[i+1]);

    }
    cout<<max(dp[0],dp[1]);
    return 0;
}


#include<iostream>
using namespace std;
int counter[20005],first,last;
int func(int counter[],int index)
{   if(index-1>=first)  counter[index-1]=0;
    if(index+1<=last)   counter[index+1]=0;
    int val=index;
    counter[index]--;
    if(counter[index]==-1)   return val;
    int maxim=0;
    for(int i=1;i<=last;i++)
    {   if(counter[i]!=0)
            maxim=max(maxim,func(counter,i));
    }
    return val+maxim;
}
int main()
{   int n;
    cin>>n;
    last=0;
    for(int i=0;i<n;i++)
    {   int x;  cin>>x;
        if(x>last)  last=x;
        if(x<first) first=x;
        counter[x]++;
    }
    int maxim=0;
    int temp[200005];
    for(int i=first;i<=last;i++){
        for(int i=first;i<=last;i++)    temp[i]=counter[i];
        if(counter[i]!=0)
            maxim=max(maxim,func(temp,i));
    }
    cout<<maxim;
    return 0;
}


#include<iostream>
using namespace std;
int arr[200005],first,last;
int func(int index)
{   if(index>last) return 0;
    int val=arr[index];
    int maxim=0;
    for(int i=index+2;i<=last;i++)
        maxim=max(maxim,func(i));
    return val+maxim;
}
int main()
{   int n;
    cin>>n;
    first=999999;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(x<first) first=x;
        if(x>last)  last=x;
        arr[x]+=x;
    }
    int maxim=0;
    for(int i=first;i<=last;i++)
    {   if(arr[i]!=0)
            maxim=max(maxim,func(i));
    }
    cout<<maxim;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int first,last,counts[200005];
int func(vector<int> counter,int index)
{   if(index-1>=first)  counter[index-1]=0;
    if(index+1<=last)   counter[index+1]=0;
    int val=index;
    counter[index]--;
    int maxim=0;
    for(int i=first;i<=last;i++)
    {   if(counter[i]!=0)
            maxim=max(maxim,func(counter,i));
    }
    return val+maxim;
}
int main()
{   int n;
    cin>>n;
    last=0;
    first=999999;
    for(int i=0;i<n;i++)
    {   int x;  cin>>x;
        if(x>last)  last=x;
        if(x<first) first=x;
        counts[x]++;
    }
    vector<int> cunt;
    cunt.push_back(-1);
    for(int i=1;i<=last;i++)
        cunt.push_back(counts[i]);
    int maxim=0;
    for(int i=first;i<=last;i++){
        if(counts[i]!=0)
            maxim=max(maxim,func(cunt,i));
    }
    cout<<maxim;
    return 0;
}


#include<iostream>
#include<set>
using namespace std;
set<int> ss;
long long int arr[100005],total[100005],maxim;
int main()
{   int n,x;
    cin>>n;
    for(int i=0;i<n;i++)
    {   cin>>x;
        ss.insert(x);
        total[x]+=x;
    }
    set<int>::iterator it;
    int k=0,start=0;
    for(it=ss.begin();it!=ss.end();it++)
        arr[k++]=*it;
    for(int i=k-3;i>=0;i--)
    {   maxim=0;
        if(arr[i+1]!=arr[i]+1)  start=i+1;
        else    start=i+2;
        for(int j=start;j<min(100+start,n);j++)
            if(total[arr[j]]>maxim) maxim=total[arr[j]];
        //total[arr[i]]+=max(total[arr[start]],total[arr[start+1]]);
        total[arr[i]]+=maxim;
    }
    maxim=0;
    for(int i=0;i<k;i++)
    {   if(total[arr[i]]>maxim)
            maxim=total[arr[i]];
    }
    cout<<maxim;
    return 0;
}


#include<iostream>
using namespace std;
long long int arr[100005];
int main()
{   int n,maxim=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {   cin>>x;
        if(x>maxim) maxim = x;
        arr[x]+=x;
    }
    for(int i=maxim-2;i>=0;i--)
    {   arr[i] += max(arr[i+2],arr[i+3]);
    }
    long long int res = arr[0]>arr[1]?arr[0]:arr[1];
    cout<<res;
    return 0;
}


#include<iostream>
using namespace std;
long long int counter[200005],dp[200005],maxim[200005],num[200005],first,last;
int main()
{   int n;
    cin>>n;
    last=0;
    first=999999;
    for(int i=0;i<n;i++)
    {   long long int x;  cin>>x;
        if(x>last)  last=x;
        if(x<first) first=x;
        counter[x]+=x;
    }
    int k=0;
    for(int i=first;i<=last;i++)
    {   if(counter[i]!=0){
            dp[k]=counter[i];
            num[k]=i;
            k++;
        }
    }
    maxim[k-1]=dp[k-1];
    if(num[k-1]!=num[k-2]+1)
        dp[k-2]=dp[k-2]+maxim[k-1];
    maxim[k-2]=max(dp[k-2],maxim[k-1]);

    for(int i=k-3;i>=0;i--)
    {   if(num[i+1]==num[i]+1)
            dp[i]=dp[i]+maxim[i+2];
        else
            dp[i]=dp[i]+maxim[i+1];
        maxim[i]=max(dp[i],maxim[i+1]);

    }
    cout<<maxim[0];
    return 0;
}


#include<iostream>
using namespace std;
int counter[200005],dp[200005],maxim[200005],num[20005],first,last;
int main()
{   int n;
    cin>>n;
    last=0;
    for(int i=0;i<n;i++)
    {   int x;  cin>>x;
        if(x>last)  last=x;
        if(x<first) first=x;
        counter[x]+=x;
    }
    int k=0;
    for(int i=first;i<=last;i++)
    {   if(counter[i]!=0){
            dp[k]=counter[i];
            num[k++]=i;
        }
    }
    maxim[k-1]=dp[k-1];
    maxim[k-2]=max(dp[k-1],dp[k-2]);
    for(int i=k-3;i>=0;i--)
    {   if(num[i+1]==num[i]+1)
            dp[i]=dp[i]+maxim[i+2];
        else
            dp[i]=dp[i]+maxim[i+1];
        maxim[i]=max(dp[i]+maxim[i],maxim[i+1]);

    }
    cout<<maxim[0];
    return 0;
}


#include<iostream>
using namespace std;
int counter[20005],last;
int func(int counter[],int index)
{   if(index-1>=0)  counter[index-1]=0;
    if(index+1<=last)   counter[index+1]=0;
    int val=index;
    counter[index]--;
    if(counter[index]==-1)   return val;
    for(int i=1;i<=last;i++)
    {   if(counter[i]!=0)
            val+=func(counter,i);
    }
    return val;
}
int main()
{   int n;
    cin>>n;
    last=0;
    for(int i=0;i<n;i++)
    {   int x;  cin>>x;
        if(x>last)  last=x;
        counter[x]++;
    }
    int maxim=0;
    int temp[200005];
    for(int i=1;i<=last;i++){
        for(int i=1;i<=last;i++)    temp[i]=counter[i];
        maxim=max(maxim,func(temp,i));
    }
    cout<<maxim;
    return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100005];
	long long int res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100005; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}
	cout<<res[n]<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n; cin>>n;
	int numbers[n];
	int quantities[100000];
	int res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = 2*quantities[2];
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			int case1 = res[i-1];
			int case2 = res[i-2] + quantities[i]*i;
			int left = 0;

			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + quantities[i]*i;
			}else{
				if(case1>case2){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+quantities[i]*i;
				}
			}
		}
	}
	int ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	long long left = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		long long mult = quantities[i]*i;
		if(quantities[i] != 0){
			if(res[i-1] == 0 && res[i-2] == 0){
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
			left = res[i];
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
  	cout<<ans<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long int res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	long long int left = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		long long int mult = quantities[i]*i;
		if(quantities[i] != 0){
			if(res[i-1] == 0){
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
			left = res[i];
		}
	}
	long long int ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
  	cout<<ans<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100005];
	long long int res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	long long int left = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100005; i++){
		long long int mult = quantities[i]*i;
		if(quantities[i] != 0){
			if(res[i-1] == 0){
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
			left = res[i];
		}
	}
	cout<<left<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	long long n; cin>>n;
	long long numbers[n];
	long long quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		long long x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(res[1],2*quantities[2]);
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			long long case1 = res[i-1];
			long long case2 = res[i-2] + quantities[i]*i;
			long long left = 0;

			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + quantities[i]*i;
			}else{
				if(case1>case2){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+quantities[i]*i;
				}
			}
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


#include <iostream>
using namespace std;

int main(){
	long long n; cin>>n;
	long long quantities[100005];
	long long res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		long long x; cin>>x;
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100005; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}
	cout<<res[n+2]<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n; cin>>n;
	int numbers[n];
	int quantities[100000];
	int res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = 2*quantities[2];
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			int case1 = quantities[i-1]*(i-1);
			int case2 = quantities[i-2]*(i-2) + quantities[i]*i;
			res[i] = res[i-3]+max(case1,case2);
		}
	}
	int ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		long long int mult = quantities[i]*i;
		if(quantities[i] != 0){
			long long left = 0;
			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	 clock_t begin = clock();
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		long long mult = quantities[i]*i;
		if(quantities[i] != 0){
			long long left = 0;
			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	clock_t end = clock();
  	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  	if(n >90000){
	cout<<elapsed_secs<<endl;
	}else{
		cout<<ans<<endl;
	}
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	long long left = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		long long mult = quantities[i]*i;
		if(quantities[i] != 0){
			if(res[i-1] == 0){
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
			left = res[i];
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
  	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n; cin>>n;
	int numbers[n];
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			long long left = 0;
			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + quantities[i]*i;
			}else{
				if(res[i-1]>res[i-2]+quantities[i]*i){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+quantities[i]*i;
				}
			}
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n; cin>>n;
	int numbers[n];
	int quantities[100000];
	int res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(res[1],2*quantities[2]);
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			int case1 = res[i-1];
			int case2 = res[i-2] + quantities[i]*i;
			int left = 0;

			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + quantities[i]*i;
			}else{
				if(case1>case2){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+quantities[i]*i;
				}
			}
		}
	}
	int ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	 clock_t begin = clock();
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		long long mult = quantities[i]*i;
		if(quantities[i] != 0){
			long long left = 0;
			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	clock_t end = clock();
  	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


#include <iostream>
using namespace std;

int main(){
	long long n; cin>>n;
	long long quantities[100005];
	long long res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		long long x; cin>>x;
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100005; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}
	cout<<res[n]<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100000];
	long long res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			long long left = 0;
			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + quantities[i]*i;
			}else{
				if(res[i-1]>res[i-2]+quantities[i]*i){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+quantities[i]*i;
				}
			}
		}
	}
	long long ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100005];
	long long int res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100005; i++){
		res[i] = max(res[i-1],res[i-2]+quantities[i]*i);
	}
	cout<<res[n]<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100005];
	long long int res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100000; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}
	cout<<res[n]<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n; cin>>n;
	int numbers[n];
	int quantities[100000];
	int res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = 2*quantities[2];
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			int case1 = res[i-1];
			int case2 = res[i-2] + quantities[i]*i;
			int left = 0;

			if(res[i-1] == 0 && res[i-2] == 0){
				for(int j=i-1;j>=0;j--){
					if(res[j] != 0)
					left = res[j];
				}
				res[i] = left + quantities[i]*i;
				cout<<left<<endl;
			}else{
				if(case1>case2){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+quantities[i]*i;
				}
			}
		}
	}
	int ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main(){
	int n; cin>>n;
	int numbers[n];
	int quantities[100000];
	int res[100000];
	for(int i=0;i<100000;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; cin>>x;
		numbers[i] = x;
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = 2*quantities[2];
	for(int i=3; i<100000; i++){
		if(quantities[i] != 0){
			int case1 = quantities[i-1]*(i-1);
			int case2 = quantities[i-2]*(i-2) + quantities[i]*i;
			if(case1>case2){
				res[i] = res[i-3]+case1;
			}else{
				res[i] = res[i-2]+quantities[i]*i;
			}
		}
	}
	int ans = res[0];
	for(int i=0;i<100000;i++){
		if(res[i]>ans)
			ans = res[i];
	}
	cout<<ans<<endl;
	// for(int i=0;i<100000;i++){
	// 	if(res[i]!=0)
	// 		cout<<res[i]<<" ";
	// }
	// cout<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100005];
	long long int res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[1] = quantities[1];
	res[2] = max(quantities[1],quantities[2]*2);
	long long int left = max(quantities[1],quantities[2]*2);
	for(int i=3; i<100005; i++){
		long long int mult = quantities[i]*i;
		if(quantities[i] != 0){
			if(res[i-1] == 0){
				res[i] = left + mult;
			}else{
				if(res[i-1]>res[i-2]+mult){
					res[i] = res[i-1];
				}else{
					res[i] = res[i-2]+mult;
				}
			}
			left = res[i];
		}
	}
	long long int ans = res[0];
	for(int i=0;i<100005;i++){
		if(res[i]>ans)
			ans = res[i];
	}
  	cout<<ans<<endl;
	return 0;
}


#include <iostream>
using namespace std;

int main(){
	long long n; cin>>n;
	long long quantities[100005];
	long long res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		long long x; cin>>x;
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100005; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}

	cout<<res[100000]<<endl;
	return 0;
}


#include <iostream>
using namespace std;

int main(){
	long long n; cin>>n;
	long long quantities[100005];
	long long res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		long long x; cin>>x;
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<100005; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}
	cout<<res[n+1]<<endl;
	return 0;
}


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

int main(){
	int n; cin>>n;
	int quantities[100005];
	long long int res[100005];
	for(int i=0;i<100005;i++){
		quantities[i] = 0;
		res[i] = 0;
	}
	for(int i=0;i<n;i++){
		int x; scanf("%d", &x);
		quantities[x]++;
	}
	res[0] = 0;
	res[1] = quantities[1];
	for(int i=2; i<=100000; i++){
		if(res[i-1]>res[i-2]+quantities[i]*i){
			res[i] = res[i-1];
		}else{
			res[i] = res[i-2]+quantities[i]*i;
		}
	}
	cout<<res[n]<<endl;
	return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, idx, c[maxn], t;
SI state;
MII f;
map<SI, int> states;

int ID() {
	if (states.count(state)) return states[state];
	states.insert(mp(state, idx));
	return idx++;
}

int dp() {
	int id = ID();
	if (f[id]) return f[id];
	int l, r, k;
	for(SI::iterator i = state.begin(); i != state.end(); i++) {
		k = *i;
		l = state.erase(k-1), r = state.erase(k+1);
		state.erase(k);
		f[id] = max(f[id], dp() + c[k]);
		if (l) state.insert(k-1);
		if (r) state.insert(k+1);
		i = state.insert(k).fi;
	}
	return f[id];
}

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    	state.insert(t);
    }
    printf("%d", dp());
    return 0;
}


#define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    }
    f[1] = c[1];
    FOR(i, 2, maxn) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    int minT ＝ INF;
    F(i, n) {
    	s(t);
    	minT = min(minT, t);
    	c[t] += t;
    }
    f[1] = c[minT];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += (LL)t;
    }
    f[1] = c[1];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    }
    f[1] = c[1];
    FOR(i, 2, maxn) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    }
    f[1] = c[1];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


#define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, c[maxn], f[maxn], t;

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    }
    f[1] = c[1];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf("%d", f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    }
    f[1] = c[1];
    FOR(i, 2, maxn-1) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[maxn-1]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, a[maxn], idx, c[maxn];
VI state;
MII f;
map<VI, int> states;

// 1:2
// 2:5
// 3:2
int ID() {
	int &t = states[state];
	if (t) return t;
	else return t = idx++;
}

int dp() {
	int id = ID();
	if (f[id]) return f[id];
	int l, r, k;
	FOR(i, 1, n) {
		if (!state[i]) continue;
		l = state[i-1], r = state[i+1], k = state[i];
		state[i+1] = state[i-1] = state[i] = 0;
		f[id] = max(f[id], dp() + k);
		state[i-1] = l, state[i+1] = r, state[i] = k;
	}
	return f[id];
}

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    int t;
    F(i, n) state.pb(0);
    F(i, n) {
    	s(t);
    	//c[t] += t;
    	state[t] += t;
    }
    printf("%d\n", dp());
    // for(auto &i: init) {cout << i.fi << ":" << i.se << endl;}
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    int minT ＝ inf;
    F(i, n) {
    	s(t);
    	minT = min(minT, t);
    	c[t] += t;
    }
    f[1] = c[minT];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, a[maxn], idx, c[maxn];
VI state;
MII f;
map<VI, int> states;

// 1:2
// 2:5
// 3:2
int ID() {
	if (states.count(state)) return states[state];
	states.insert(mp(state, idx));
	return idx++;
}

int dp() {
	int id = ID();
	if (f[id]) return f[id];
	int l, r, k;
	FOR(i, 1, n) {
		if (!state[i]) continue;
		l = state[i-1], r = state[i+1], k = state[i];
		state[i+1] = state[i-1] = state[i] = 0;
		f[id] = max(f[id], dp() + k);
		state[i-1] = l, state[i+1] = r, state[i] = k;
	}
	return f[id];
}

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    int t;
    F(i, n+1) state.pb(0);
    F(i, n) {
    	s(t);
    	//c[t] += t;
    	state[t] += t;
    }
    printf("%d\n", dp());
    // for(auto &i: init) {cout << i.fi << ":" << i.se << endl;}
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, t;
LL f[maxn], c[maxn];

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    int minT = INF;
    F(i, n) {
    	s(t);
    	minT = min(minT, t);
    	c[t] += t;
    }
    f[1] = c[minT];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf(LLD, f[n]);
    return 0;
}


// #define          		          	LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
typedef set<int> SI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
int n, c[maxn], f[maxn], t;

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    F(i, n) {
    	s(t);
    	c[t] += t;
    }
    f[1] = c[1];
    FOR(i, 2, n) f[i] = max(f[i-2]+c[i], f[i-1]);
    printf("%d", f[n]);
    return 0;
}


// #define                             LOCAL
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <climits>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <functional>
using namespace std;
#ifdef LOCAL
#define LLD                         "%lld"
#else
#define LLD                         "%I64d"
#endif
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define F(i,n)                      for(int i=0;i<(n);++i)
#define FOR(i,x,y)                  for(int i=(x);i<=(y);++i)
#define FD(i,n)                     for(int i=(n-1);i>=0;--i)
#define FORD(i,y,x)                 for(int i=(y);i>=(x);--i)
#define MEM(x,i)                    memset(x,i,sizeof(x))
#define mp                          make_pair
#define DB(a)                       cout<<(a)<<endl
#define WHATIS(a)                   cout<<#a<<" is "<<a<<endl
#define BUG                         puts("here!!!")
#define fi                          first
#define se                          second
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define s(n)                        scanf("%d",&(n))
#define sc(n)                       scanf("%c",&(n))
#define sl(n)                       scanf(LLD,&(n))
#define sf(n)                       scanf("%lf",&(n))
#define ss(n)                       scanf("%s",&(n))
typedef vector<int> VI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 1e5+10;
map<MII, int> f;
int n, a[maxn];

int dp(MII &state) {
	if (f[state]) return f[state];
	int t = 0;
	for (auto &i: state) {
		if (!i.se) continue;
		int l = state[i.fi-1], r = state[i.fi+1], k = i.fi;
		state[k+1] = state[k-1] = 0;
		i.se--;
    	// for(auto &j: state) {cout << j.fi << ":" << j.se << endl;}
		t = max(t, dp(state) + k);
		i.se++;
		state[k-1] = l;
		state[k+1] = r;
	}
	f[state] = t;
	return f[state];
}

int main() { _
#ifdef LOCAL
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    s(n);
    MII init;
    F(i, n) {
    	s(a[i]);
    	init[a[i]]++;
    }
    // for(auto &i: init) {cout << i.fi << ":" << i.se << endl;}
    printf("%d\n", dp(init));
    // for(auto &i: init) {cout << i.fi << ":" << i.se << endl;}
    return 0;
}


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

long long int A[100001],DP[100001];

long long int f(long long int x){
	if(DP[x]!=0)
		return DP[x];
	if(x==0)
		return 0;
	if(x==1)
		return A[1];
	DP[x]= max(f(x-1),f(x-2)+A[x]*x);
	return DP[x];
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(nullptr);
	long long int N,s;
	cin >> N;
	for(long long int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	cout << f(N) << '\n';
}


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

#define inf 0x3f3f3f3f
#define pi 3.14159265358979323846264338327950288
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define FORI(i,a,n) for(int i=a;i>=n;i--)
#define REP(i,n) FOR(i,0,n)
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef unsigned long long  ull;
typedef vector<int> vi;
typedef vector< ll > vll;
typedef pair<int,int> ii;
typedef pair<int, ii> iii;
typedef vector< ii > vii;
typedef vector< iii > viii;

int N;
int main(){
	cin >> N;
	vi A(N);
	ll saida=0;
	bool V[N];
	int i=0;
	for(auto &a:A){
		cin >> a;
		V[i++]=false;
	}
	if(N==1){
		cout << A[0] << '\n';
		return 0;
	}
	FOR(i,1,N){
		if(A[i]>=(A[i-1]+A[i+1])){
			saida+=A[i];
			V[i]=V[i-1]=V[i+1]=true;
			i+=2;
		}else{
			saida+=A[i-1];
			V[i]=V[i-1]=true;
			i++;
		}
	}
	REP(i,N)
		if(!V[i]){
			V[i]=V[i-1]=V[i+1]=true;
			saida+=A[i];
		}
	cout << saida << '\n';
}


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

int A[100001],DP[100001];

int f(int x){
	if(DP[x]!=0)
		return DP[x];
	if(x==0)
		return 0;
	if(x==1)
		return A[1];
	DP[x]= max(f(x-1),f(x-2)+A[x]*x);
	return DP[x];
}

int main(){
	int N,s;
	cin >> N;
	for(int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	cout << f(N) << '\n';
}


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

long long int A[100001],DP[100001];

int f(int x){
	if(DP[x]!=0)
		return DP[x];
	if(x==0)
		return 0;
	if(x==1)
		return A[1];
	DP[x]= max((long long int)f(x-1),(long long int)f(x-2)+A[x]*x);
	return DP[x];
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(nullptr);
	int N,s;
	cin >> N;
	for(int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	cout << f(N) << '\n';
}


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

long long int A[100001],DP[100001];

int f(long long int x){
	if(DP[x]!=0)
		return DP[x];
	if(x==0)
		return 0;
	if(x==1)
		return A[1];
	DP[x]= max(f(x-1),f(x-2)+A[x]*x);
	return DP[x];
}

int main(){
	long long int N,s;
	cin >> N;
	for(int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	cout << f(N) << '\n';
}


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

#define inf 0x3f3f3f3f
#define pi 3.14159265358979323846264338327950288
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define FORI(i,a,n) for(int i=a;i>=n;i--)
#define REP(i,n) FOR(i,0,n)
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef unsigned long long  ull;
typedef vector<int> vi;
typedef vector< ll > vll;
typedef pair<int,int> ii;
typedef pair<int, ii> iii;
typedef vector< ii > vii;
typedef vector< iii > viii;

int N;
int main(){
	cin >> N;
	vi A(N);
	ll saida=0;
	bool V[N];
	int i=0;
	for(auto &a:A){
		cin >> a;
		V[i++]=false;
	}
	//if(N==1){
		//cout << A[0] << '\n';
		//return 0;
	//}
	FOR(i,1,N){
		if(A[i]>=(A[i-1]+A[i+1])){
			saida+=A[i];
			V[i]=V[i-1]=V[i+1]=true;
			i+=2;
		}else{
			saida+=A[i-1];
			V[i]=V[i-1]=true;
			i++;
		}
	}
	REP(i,N)
		if(!V[i]){
			V[i]=V[i-1]=V[i+1]=true;
			saida+=A[i];
		}
	cout << saida << '\n';
}


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

#define inf 0x3f3f3f3f
#define pi 3.14159265358979323846264338327950288
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define FORI(i,a,n) for(int i=a;i>=n;i--)
#define REP(i,n) FOR(i,0,n)
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef unsigned long long  ull;
typedef vector<int> vi;
typedef vector< ll > vll;
typedef pair<int,int> ii;
typedef pair<int, ii> iii;
typedef vector< ii > vii;
typedef vector< iii > viii;

ll N,V[100002],dp[100002];
int main(){
	cin >> N;
	vi A(N);
	int i=0;
	for(auto &a:A){
		cin >> a,V[a]++;
		if(V[a]==N){
			cout << a*N << '\n';
			return 0;
		}
	}
	dp[1]=V[1];
	FOR(i,2,N+1)
		dp[i]=max(dp[i-1],dp[i-2]+V[i]*i);
	cout << dp[N] << '\n';
}


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

int A[100001];

int f(int x){
	if(x==0)
		return 0;
	if(x==1)
		return A[1];
	return max(f(x-1),f(x-2)+A[x]*x);
}

int main(){
	int N,s;
	cin >> N;
	for(int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	cout << f(N) << '\n';
}


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

#define mmax(X,Y) ((X>Y)?X:Y)

long long int A[100001],DP[100001];

int main(){
	long long int N,s;
	cin >> N;
	for(long long int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	DP[0]=0;
	DP[1]=A[1];
	for(long long int n=2;n<=N;n++)
		DP[n] = mmax(DP[n-1],DP[n-2]+A[n]*n);
	cout << DP[N] << '\n';
}


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

long long int A[100001],DP[100001];

long long int f(long long int x){
	if(DP[x]!=0)
		return DP[x];
	if(x==0)
		return 0;
	if(x==1)
		return A[1];
	DP[x]= max(f(x-1),f(x-2)+A[x]*x);
	return DP[x];
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(nullptr);
	long long int N,s;
	cin >> N;
	for(long long int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	for(long long int n=2;n<N;n++)
		f(n);
	cout << f(N) << '\n';
}


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

#define inf 0x3f3f3f3f
#define pi 3.14159265358979323846264338327950288
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define FORI(i,a,n) for(int i=a;i>=n;i--)
#define REP(i,n) FOR(i,0,n)
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef unsigned long long  ull;
typedef vector<int> vi;
typedef vector< ll > vll;
typedef pair<int,int> ii;
typedef pair<int, ii> iii;
typedef vector< ii > vii;
typedef vector< iii > viii;

int N,V[100002],dp[100002];
int main(){
	cin >> N;
	vi A(N);
	int i=0;
	for(auto &a:A){
		cin >> a,V[a]++;
		if(V[a]==N){
			cout << a*N << '\n';
			return 0;
		}
	}
	dp[1]=V[1];
	FOR(i,2,N+1)
		dp[i]=max(dp[i-1],dp[i-2]+V[i]*i);
	cout << dp[N] << '\n';
}


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

#define mmax(X,Y) ((X>Y)?X:Y)

long long int A[100010],DP[100010];

int main(){
	long long int N,s;
	bool k=false;
	cin >> N;
	for(long long int n=0;n<N;n++){
		cin >> s;
		A[s]++;
		if(A[s]==N){
			cout << s*N << '\n';
			k=true;
		}
	}
	if(!k){
	DP[0]=0;
	DP[1]=A[1];
	for(long long int n=2;n<=N;n++)
		DP[n] = mmax(DP[n-1],DP[n-2]+A[n]*n);
	cout << DP[N] << '\n';
	}
}


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

#define mmax(X,Y) ((X>Y)?X:Y)

long long int A[100001],DP[100001];

int main(){
	long long int N,s;
	cin >> N;
	for(long long int n=0;n<N;n++){
		cin >> s;
		A[s]++;
	}
	DP[0]=0;
	DP[1]=1;
	for(long long int n=2;n<=N;n++)
		DP[n] = mmax(DP[n-1],DP[n-2]+A[n]*n);
	cout << DP[N] << '\n';
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
int msum(vector<pair<int, int> > v, int i, int j){
    if (i == j - 1){
        if (v[i].first > v[j].first) return v[i].first;
        return v[j].first;
    }
    else if (i == j){
        return v[i].first;
    }
    else{
        int sum1 = msum(v, i, j - 1);
        int sum2 = msum(v, i, j - 2) + v[j].first;
        if (sum1 > sum2) return sum1;
        return sum2;
    }
}
int main(){
    int n;
    cin >> n;
    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        a[b]++;
    }
    for (int i = 0; i < 100001; i++){
        if (a[i]) {
            pair<int, int> p = make_pair(a[i] * i, i);
            v.push_back(p);
        }
    }
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        int j = i;
        if (i < v.size() - 1){
            while (v[j].second == v[j + 1].second - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(v, i, j);
            i = j;
        }
        else sum += v[i].first;
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
int msum(vector<int> v, int i, int j){
    if (i == j - 1){
        if (v[i] * a[v[i]] > v[j] * a[v[j]]) return v[i] * a[v[i]];
        return v[j] * a[v[j]];
    }
    else if (i == j){
        return v[i] * a[v[i]];
    }
    else{
        int sum1 = msum(v, i, j - 1);
        int sum2 = msum(v, i, j - 2) + v[j] * a[v[j]];
        if (sum1 > sum2) return sum1;
        return sum2;
    }
}
int main(){
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        a[b]++;
    }
    for (int i = 0; i < 100001; i++){
        if (a[i]) {
            v.push_back(i);
        }
    }
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        int j = i;
        if (i < v.size() - 1){
            while (v[j] == v[j + 1] - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(v, i, j);
            i = j;
        }
        else sum += v[i]*a[v[i]];
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
ll r[100001],maxs[100001];
int main(){
    ll n;
    cin>>n;
    vector<ll> v;
    for(int i=0; i<n; i++){
        ll a;
        cin>>a;
        if(!r[a]) v.push_back(a);
        r[a]++;
    }
    sort(v.begin(),v.end());
    maxs[v[0]]=v[0]*r[v[0]];
    if(v.size()>1){
    if(v[1]-1==v[0]){
        if(maxs[v[0]]>v[1]*r[v[1]]) maxs[v[1]]=maxs[v[0]];
        else maxs[v[1]]=v[1]*r[v[1]];
    } else maxs[v[1]]=maxs[v[0]]+v[1]*r[v[1]];
    for(int i=2; i<v.size(); i++){
        if(v[i]-1==v[i-1]){
            if(maxs[v[i-1]]>maxs[v[i-2]]+r[v[i]]*v[i]) maxs[v[i]]=maxs[v[i-1]];
            else maxs[v[i]]=maxs[v[i-2]]+r[v[i]]*v[i];
        } else maxs[v[i]]=maxs[v[i-1]]+r[v[i]]*v[i];
    }
    cout<<maxs[v[v.size()-1]];
    }
    else cout<<maxs[v[0]];
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
bool t[100002];
int main(){
    int n;
    cin >> n;
    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        a[b]++;
    }
    for (int i = 0; i < 100001; i++){
        if (a[i]) {
            pair<int, int> p = make_pair(a[i] * i, i);
            v.push_back(p);
        }
    }
    sort(v.rbegin(), v.rend());
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        if (!t[v[i].second]){
            sum += v[i].first;
            t[v[i].second + 1] = true;
            if(v[i].second) t[v[i].second - 1] = true;
        }
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
ll r[100001],maxs[100001];
int main(){
    ll n;
    cin>>n;
    vector<ll> v;
    for(int i=0; i<n; i++){
        ll a;
        cin>>a;
        if(!r[a]) v.push_back(a);
        r[a]++;
    }
    sort(v.begin(),v.end());
    maxs[v[0]]=v[0]*r[v[0]];
    if(v[1]-1==v[0]){
        if(maxs[v[0]]>v[1]*r[v[1]]) maxs[v[1]]=maxs[v[0]];
        else maxs[v[1]]=v[1]*r[v[1]];
    } else maxs[v[1]]=maxs[v[0]]+v[1]*r[v[1]];
    for(int i=2; i<v.size(); i++){
        if(v[i]-1==v[i-1]){
            if(maxs[v[i-1]]>maxs[v[i-2]]+r[v[i]]*v[i]) maxs[v[i]]=maxs[v[i-1]];
            else maxs[v[i]]=maxs[v[i-2]]+r[v[i]]*v[i];
        } else maxs[v[i]]=maxs[v[i-1]]+r[v[i]]*v[i];
    }
    cout<<maxs[v[v.size()-1]];
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
int r[100001],maxs[100001];
int main(){
    int n;
    cin>>n;
    vector<int> v;
    for(int i=0; i<n; i++){
        int a;
        cin>>a;
        if(!r[a]) v.push_back(a);
        r[a]++;
    }
    sort(v.begin(),v.end());
    maxs[v[0]]=v[0]*r[v[0]];
    if(maxs[v[0]]>v[1]*r[v[1]]) maxs[v[1]]=maxs[v[0]];
    else maxs[v[1]]=v[1]*r[v[1]];
    for(int i=2; i<v.size(); i++){
        if(maxs[v[i-1]]>maxs[v[i-2]]+r[v[i]]*v[i]) maxs[v[i]]=maxs[v[i-1]];
        else maxs[v[i]]=maxs[v[i-2]]+r[v[i]]*v[i];
    }
    cout<<maxs[v[v.size()-1]];
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
int r[100001],maxs[100001];
int main(){
    int n;
    cin>>n;
    vector<int> v;
    for(int i=0; i<n; i++){
        int a;
        cin>>a;
        if(!r[a]) v.push_back(a);
        r[a]++;
    }
    sort(v.begin(),v.end());
    maxs[v[0]]=v[0]*r[v[0]];
    if(maxs[v[0]]>v[1]*r[v[1]]) maxs[v[1]]=maxs[v[0]];
    else maxs[v[1]]=v[1]*r[v[1]];
    for(int i=2; i<v.size(); i++){
        if(maxs[v[i-1]]>maxs[v[i-2]]+r[v[i]]*v[i]) maxs[v[i]]=maxs[v[i-1]];
        else maxs[v[i]]=maxs[v[i-2]]+r[v[i]]*v[i];
    }
    cout<<maxs[v[v.size()-1]];
    return 0;
}#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
int r[100001],maxs[100001];
int main(){
    int n;
    cin>>n;
    vector<int> v;
    for(int i=0; i<n; i++){
        int a;
        cin>>a;
        if(!r[a]) v.push_back(a);
        r[a]++;
    }
    sort(v.begin(),v.end());
    maxs[v[0]]=v[0]*r[v[0]];
    if(maxs[v[0]]>v[1]*r[v[1]]) maxs[v[1]]=maxs[v[0]];
    else maxs[v[1]]=v[1]*r[v[1]];
    for(int i=2; i<v.size(); i++){
        if(maxs[v[i-1]]>maxs[v[i-2]]+r[v[i]]*v[i]) maxs[v[i]]=maxs[v[i-1]];
        else maxs[v[i]]=maxs[v[i-2]]+r[v[i]]*v[i];
    }
    cout<<maxs[v[v.size()-1]];
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
pair<int,bool> msum(vector<int> v, int i, int j){
    if (i == j - 1){
        if (v[i] * a[v[i]] > v[j] * a[v[j]]) return make_pair(v[i] * a[v[i]],false);
        return make_pair(v[j] * a[v[j]],true);
    }
    else if (i == j){
        return make_pair(v[i] * a[v[i]],true);
    }
    else{
        pair<int, bool> p = msum(v, i, j - 1);
        int sum1 = p.first;
        if (p.second){
            int sum2 = msum(v, i, j - 2).first + v[j] * a[v[j]];
            if (sum1 > sum2) return make_pair(sum1, false);
            return make_pair(sum2, true);
        }
        return make_pair(sum1 + v[j] * a[v[j]], true);
    }
}
int main(){
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        a[b]++;
    }
    for (int i = 0; i < 100001; i++){
        if (a[i]) {
            v.push_back(i);
        }
    }
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        int j = i;
        if (i < v.size() - 1){
            while (v[j] == v[j + 1] - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(v, i, j).first;
            i = j;
        }
        else sum += v[i]*a[v[i]];
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
int msum(vector<pair<int, int> > v, int i, int j){
    if (i == j - 1){
        if (v[i].first > v[j].first) return v[i].first;
        return v[j].first;
    }
    else if (i == j){
        return v[i].first;
    }
    else{
        int sum1 = msum(v, i, j - 1);
        int sum2 = msum(v, i, j - 2) + v[j].first;
        if (sum1 > sum2) return sum1;
        return sum2;
    }
}
int main(){
    int n;
    cin >> n;
    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        a[b]++;
    }
    for (int i = 0; i < 100001; i++){
        if (a[i]) {
            pair<int, int> p = make_pair(a[i] * i, i);
            v.push_back(p);
        }
    }
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        int j = i;
        if (i < v.size() - 1){
            while (v[j].second == v[j + 1].second - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(v, i, j);
            i = j + 1;
        }
        else sum += v[i].first;
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
long long a[100001];
vector<long long> v;
long long msum(long long i, long long j){
    if (i == j - 1){
        if (v[i] * a[v[i]] > v[j] * a[v[j]]) return v[i] * a[v[i]];
        return v[j] * a[v[j]];
    }
    else if (i == j){
        return v[i] * a[v[i]];
    }
    else{
        long long sum1 = msum(i, j - 1);
        long long sum2 = msum(i, j - 2) + v[j] * a[v[j]];
        if (sum1 > sum2) return sum1;
        return sum2;
    }
}
int main(){
    long long n;
    scanf("%I64d",&n);
    for (long long i = 0; i < n; i++){
        long long b;
        scanf("%I64d",&b);
        if(!a[b]) v.push_back(b);
        a[b]++;
    }
    sort(v.begin(), v.end());
    long long sum = 0;
    for (long long i = 0; i < v.size(); i++){
        long long j = i;
        if (i < v.size() - 1){
            while (v[j] == v[j + 1] - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(i, j);
            i = j;
        }
        else sum += v[i]*a[v[i]];
    }
    printf("%d",sum);
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
int r[100001],maxs[100001];
int main(){
    int n;
    cin>>n;
    vector<int> v;
    for(int i=0; i<n; i++){
        int a;
        cin>>a;
        if(!r[a]) v.push_back(a);
        r[a]++;
    }
    sort(v.begin(),v.end());
    maxs[v[0]]=v[0]*r[v[0]];
    if(v[1]-1==v[0]){
        if(maxs[v[0]]>v[1]*r[v[1]]) maxs[v[1]]=maxs[v[0]];
        else maxs[v[1]]=v[1]*r[v[1]];
    } else maxs[v[1]]=maxs[v[0]]+v[1]*r[v[1]];
    for(int i=2; i<v.size(); i++){
        if(v[i]-1==v[i-1]){
            if(maxs[v[i-1]]>maxs[v[i-2]]+r[v[i]]*v[i]) maxs[v[i]]=maxs[v[i-1]];
            else maxs[v[i]]=maxs[v[i-2]]+r[v[i]]*v[i];
        } else maxs[v[i]]=maxs[v[i-1]]+r[v[i]]*v[i];
    }
    cout<<maxs[v[v.size()-1]];
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long a[100001];
vector<long long> v;
long long msum(long long i, long long j){
    if (i == j - 1){
        if (v[i] * a[v[i]] > v[j] * a[v[j]]) return v[i] * a[v[i]];
        return v[j] * a[v[j]];
    }
    else if (i == j){
        return v[i] * a[v[i]];
    }
    else{
        long long sum1 = msum(i, j - 1);
        long long sum2 = msum(i, j - 2) + v[j] * a[v[j]];
        if (sum1 > sum2) return sum1;
        return sum2;
    }
}
int main(){
    long long n;
    cin >> n;
    for (long long i = 0; i < n; i++){
        long long b;
        cin >> b;
        if(!a[b]) v.push_back(b);
        a[b]++;
    }
    sort(v.begin(), v.end());
    long long sum = 0;
    for (long long i = 0; i < v.size(); i++){
        long long j = i;
        if (i < v.size() - 1){
            while (v[j] == v[j + 1] - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(i, j);
            i = j;
        }
        else sum += v[i]*a[v[i]];
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
int msum(vector<pair<int, int> > v, int i, int j){
    int sum1 = 0, sum2 = 0;
    for (int k = i; k <= j; k++){
        if (k % 2) sum1 += v[k].first;
        else sum2 += v[k].first;
    }
    if (sum1 < sum2) return sum2;
    return sum1;
}
int main(){
    int n;
    cin >> n;
    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        a[b]++;
    }
    for (int i = 0; i < 100001; i++){
        if (a[i]) {
            pair<int, int> p = make_pair(a[i] * i, i);
            v.push_back(p);
        }
    }
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        int j = i;
        if (i < v.size() - 1){
            while (v[j].second == v[j + 1].second - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(v, i, j);
            i = j + 1;
        }
        else sum += v[i].first;
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[100001];
vector<int> v;
int msum(int i, int j){
    if (i == j - 1){
        if (v[i] * a[v[i]] > v[j] * a[v[j]]) return v[i] * a[v[i]];
        return v[j] * a[v[j]];
    }
    else if (i == j){
        return v[i] * a[v[i]];
    }
    else{
        int sum1 = msum(i, j - 1);
        int sum2 = msum(i, j - 2) + v[j] * a[v[j]];
        if (sum1 > sum2) return sum1;
        return sum2;
    }
}
int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        int b;
        cin >> b;
        if(!a[b]) v.push_back(b);
        a[b]++;
    }
    sort(v.begin(), v.end());
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        int j = i;
        if (i < v.size() - 1){
            while (v[j] == v[j + 1] - 1){
                if (j < v.size() - 2) j++;
                else{
                    j++;
                    break;
                }
            }
            sum += msum(i, j);
            i = j;
        }
        else sum += v[i]*a[v[i]];
    }
    cout << sum;
    //system("pause");
    return 0;
}


#include <iostream>
using namespace std;
const int maxn = 100005;
int n, dp[maxn], a[maxn], cnt[maxn], ans;
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        cnt[a[i]]++;
    }
    dp[0] = 0;
    dp[1] = cnt[1];
    for (int i = 2; i <= n; i++) {
        dp[i] = max(dp[i - 1], dp[i - 2] + i * cnt[i]);
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
long long cnt[100005];
long long dp[100005][2];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int ai;
		cin>>ai;
		cnt[ai]++;
	}
	for(int i=1;i<=100000;i++)
	{
		dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
		dp[i][1]=dp[i-1][0]+cnt[i]*i;
	}
	cout<<max(dp[100000][0],dp[100000][1])<<endl;
	return 0;
}


#include<iostream>

using namespace std;
int n;
int a[100005];
int v[100005];
int ans[100005];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[a[i]]+=a[i];
	}
	ans[1]=v[1];
	for(int i=2;i<=100000;i++){
		ans[i]=max(ans[i-1],ans[i-2]+v[i]);
	}
	cout<<ans[100000];

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std;
int main(){
	int N;
	vector<int>A,B;
	scanf("%d",&N);
	A.resize(N);
	for(int i=0;i<N;i++){
		scanf("%d",&A[i]);
	}
	B.resize(1+*max_element(A.begin(),A.end()));
	for(int i=0;i<N;i++){
		B[A[i]]++;
	}
	vector<long long> f;
	f.resize(B.size());
	f[0]=0;
	for(int i=1;i<f.size();i++){
		long long f1=2<=i?f[i-2]:0;
		f[i]=max(f[i-1],f1+(long long)B[i]*i);
	}
	printf("%lld\n",f[f.size()-1]);
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int maxv=100005;
int a[maxv];
long long z;
int main ( ){
	int n,i,m;
	cin>>n;
	for(i=1;i<=n;i++)
	   cin>>m,a[m]++;
	for(i=100005;i>0;i--)
	    if(a[i]!=0&&a[i]*i>a[i-1]*(i-1))
	    {	z+=a[i]*i,i--;}
	cout<<z;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
using namespace std;
long long n,num[100010];
long long f[100010];
long long ans=0;

int main()
{
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	scanf("%lld",&n);
	long long x,big=0;
	for(int i=1; i<=n; i++)
	{
		scanf("%lld",&x);
		num[x]++;
		big=max(big,x);
	}
	f[1]=num[1];
	for(int i=2; i<=big; i++)
	{
		f[i]=max(f[i],f[i-2]+num[i]*i);
	}
	for(int i=1; i<=big; i++) ans=max(ans,f[i]);
	cout<<ans;
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define ll long long
#define sf scanf
#define pf printf
#define pb push_back
#define in insert
using namespace std;
ll mod = 1e9+7;
int nums[100005];
int ans[100005];
int main(){
	int n,m=-1,temp;
	sf("%d",&n);

	for(int i = 0;i<n;i++){
		sf("%d",&temp);
		nums[temp]++;
		m = max(m,temp);
	}

	ans[0]=0;
	ans[1]=nums[1];

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

	cout<<ans[m];
	return 0;
}

/*

2
1 2

2

3
1 2 3

4

9
1 2 1 3 2 2 2 2 3

10

*/


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;

const int Maxd=1e5+7;
const int INF=0x3f3f3f3f;
long long n,big;
long long a[Maxd];
long long dp[Maxd],cnt[Maxd];

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n;
	REP(i,n)
	{
		cin>>a[i];
		cnt[a[i]]++;
		big=max(big,a[i]);
	}
	memset(dp,-INF,sizeof(dp));
	dp[0]=0;
	dp[1]=cnt[1];
	for(long long i=2;i<=big;i++)
	{
		dp[i]=max(dp[i-1],dp[i-2]+cnt[i]*i);
	}

	cout<<dp[big]<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Dec(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=1e5+10,mod=30031;
int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,k;
int a[maxn];
int f[maxn][2];
int cnt[maxn];
int max(int a,int b){return a>b?a:b;}
#undef int
int main()
#define int long long
{
      n=read();
      For(i,1,n)a[i]=read();
      sort(a+1,a+n+1);
      int num=1;
      cnt[1]=1;
      For(i,2,n){
          if(a[i]==a[i-1])cnt[num]++;
          else a[++num]=a[i],cnt[num]++;
      }
      f[1][1]=cnt[1]*a[1];
      For(i,2,num){
          if(a[i]-a[i-1]==1){
              f[i][1]=f[i-1][0]+cnt[i]*a[i];
              f[i][0]=max(f[i-1][0],f[i-1][1]);
          }
          else{
              f[i][1]=max(f[i-1][0],f[i-1][1])+cnt[i]*a[i];
              f[i][0]=max(f[i-1][0],f[i-1][1]);
          }
      }
      cout<<max(f[num][1],f[num][0]);
      return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n;
int a[maxn];
long long cnt[maxn];//桶
long long dp[maxn];
long long ans = 0;
int main(){
	scanf("%d", &n);
	for(register int i = 1; i <= n; i ++){scanf("%d", &a[i]);cnt[a[i]]++;}
	dp[1] = cnt[1];
	for(register int i = n; i >= 1; i --){
		dp[i] = max(dp[i + 1], dp[i + 2] + cnt[i] * i);
		ans = max(ans, dp[i]);
	}
	printf("%lld\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n;
long long a[maxn];
long long cnt[maxn];//桶
long long dp[maxn];
long long ans = 0;
int main(){
	scanf("%d", &n);
	for(register int i = 1; i <= n; i ++){scanf("%d", &a[i]);cnt[a[i]]++;}
	dp[1] = cnt[1];
	for(register int i = n; i >= 1; i --){
		dp[i] = max(dp[i + 1], dp[i + 2] + cnt[i] * i);
		ans = max(ans, dp[i]);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
/*    freopen("k.in", "r", stdin);
    freopen("k.out", "w", stdout); */
// clock_t c1 = clock();
// std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define ls ((x) << 1)
#define rs ((x) << 1 | 1)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 1e5 + 7;
const ll MAXM = 1e5 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
ll dp[MAXN];
int cnt[MAXN];
int main()
{
    ll n;
    scanf("%lld", &n);
    int maxx = -1;
    for (int i = 0; i < n; i++)
    {
        int x;
        scanf("%d", &x);
        cnt[x]++;
        maxx = max(x, maxx);
    }
    dp[1] = cnt[1];
    for (int i = 2; i <= maxx; i++)
        dp[i] = max(dp[i-1], dp[i - 2] + cnt[i] * i);
    printf("%lld\n", dp[maxx]);
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define ri register int
#define N 100005
using namespace std;
int n,mx=0,f[N]= {0},cnt[N]= {0};
signed main() {
	scanf("%lld",&n);
	for (ri i=1,x; i<=n; i++) scanf("%lld",&x),cnt[x]++,mx=max(mx,x);
	f[1]=cnt[1];
	for (ri i=2; i<=mx; i++) f[i]=max(f[i-1],f[i-2]+i*cnt[i]);
	printf("%lld",f[mx]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[100010],f[100010],x,n;
	memset(f,0,sizeof(f));
	memset(a,0,sizeof(a));
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		a[x]++;
	}
	for (int i=100000;i>=0;i--)
		f[i]=max(f[i+1],f[i+2]+i*a[i]);
	printf("%d\n",f[0]);
}


#include<iostream>
#include<algorithm>
using namespace std;
const int maxv=100005;
long long a[maxv],b[maxv];
long long z;
int main ( ){
	int n,i,m;
	cin>>n;
	for(i=1;i<=n;i++)
	   cin>>m,a[m]++;
	b[0]=a[0],b[1]=a[1];
	for(i=2;i<=100000;i++)
	    b[i]=max(b[i-1],b[i-2]+a[i]*i),z=max(z,b[i]);
	cout<<z;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n;
long long num[100010];
long long aim[100010];
long long ans[100010][2];
int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> num[i];
	}
	memset(aim,0,sizeof(aim));
	for(int i = 1;i <= n;i++){
		aim[num[i]]++;
	}
	int f;
	for(int i = 1;i <= n;i++){
		if(aim[i] != 0){
			ans[i][0] = 0;
			ans[i][1] = i*aim[i];
			f = i;
			break;
		}
	}
	long long fans = -1;
	for(int i = f+1;i <= n;i++){
		if(aim[i-1] == 0){
			ans[i][1] = ans[i-1][0] + i*aim[i];
			ans[i][0] = ans[i-1][0];
		}else{
			ans[i][1] = ans[i-1][0] + i*aim[i];
			ans[i][0] = ans[i-1][1];
		}
		//cout << ans[i][0] << " " << ans[i][1] << endl;
		fans = max(max(ans[i][0],ans[i][1]),fans);
	}
	cout << fans << endl;
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<iterator>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;

typedef long long ll;
const int inf=1e9;
const int MAX_N=1e5+7;

ll point[MAX_N];
ll dp[MAX_N];

int main()
{
	int N,x;
	cin>>N;
	for(int i=0;i<N;i++) {
		cin>>x;
		point[x]+=x;
	}
	ll ans=0;
	for(int i=1;i<MAX_N;i++) {
		dp[i]=max(dp[i-1],(i>=2? dp[i-2]:0)+point[i]);
	}
	cout<<dp[MAX_N-1]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t,n,f[100001]={0},v[100001]={0},maxt=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>t;
		v[t]++;
		if(t>maxt) maxt=t;
	}
	f[1]=v[1];
	for(int i=2;i<=maxt;i++) f[i]=max(f[i-1],f[i-2]+i*v[i]);
	cout<<f[maxt];
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N 100005
using namespace std;
inline ll read(){
    ll s=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}
    return s*w;
}

int n;
ll a[N],cnt[N*2];
ll f[N],ans,maxn;
int main(){
    n=read();
    for(int i=1;i<=n;i++) {a[i]=read();cnt[a[i]]++;maxn=max(maxn,a[i]);}
    f[0]=0,f[1]=cnt[1];
    for(int i=2;i<=N*2;i++){
        f[i]=max(f[i-1],f[i-2]+cnt[i]*i);
        ans=max(ans,f[i]);
    }
    cout<<ans<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 100010;
ll cnt[maxn];
int n;
ll dp[maxn];
int main(){
	scanf("%d", &n);
	int t;
	for(int i = 1; i <= n; i++){
		scanf("%lld", &t);
		cnt[t]++;
	}
	dp[0] = 0;
	dp[1] = cnt[1];
	for(int i = 2; i <= maxn; i++){
		dp[i] = max(dp[i - 2] + 1LL * cnt[i] * i, dp[i - 1]);
	}
	printf("%lld\n", dp[maxn]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n;
long long num[100010];
long long aim[100010];
long long ans[100010];
int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> num[i];
	}
	memset(aim,0,sizeof(aim));
	for(int i = 1;i <= n;i++){
		aim[num[i]]++;
	}
	ans[1] = aim[1];
	for(int i = 2;i <= 1000010;i++){
		//cout << ans[i][0] << " " << ans[i][1] << endl;
		ans[i] = max(ans[i-2]+aim[i]*i,ans[i-1]);
	}
	cout << ans[n] << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t,n,v[100001]={0},maxt=0;
    unsigned long long f[100001]={0};
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>t;
		v[t]++;
		if(t>maxt) maxt=t;
	}
	f[1]=v[1];
	for(int i=2;i<=maxt;i++) f[i]=max(f[i-1],f[i-2]+i*v[i]);
	//cout<<f[maxt];
    printf("%I64d\n",f[maxt]);
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int n,a[700010],f[700010],ans=-1,kk,maxm=-1;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&kk);a[kk]++;
		if(kk>maxm)maxm=kk;
	}
	f[1]=a[1];
	for(int i=2;i<=maxm;i++){
		f[i]=max(f[i-2]+a[i]*i,f[i-1]);
	}
	printf("%d",f[maxm]);
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mod=1000000007;
signed main()
{
	 ios_base::sync_with_stdio(false);
	 cin.tie(NULL);
	 cout.tie(NULL);
	 int n;
	 cin>>n;
	 vector<int>v;
	 v.emplace_back(0);
	 for(int i=0;i<n;i++)
	 {
	 	 int x;
	 	 cin>>x;
	 	 v.emplace_back(x);
	 }
	 vector<int>dp(n+1,0);
	 dp[1]=1;
	 for(int j=2;j<=n;j++)
	 {
	 	  int x=v[j];
	 	  vector<int>factors;
	 	  for(int i=2;i<=sqrt(x);i++)
	 	  {
	 	  	  if(x%i==0)
	 	  	  {
	 	  	  	   factors.emplace_back(i);
	 	  	  	   dp[i]+=dp[i-1];
	 	  	  	   dp[i]%=mod;
	 	  	  	   if(i!=x/i)
	 	  	  	   {
	 	  	  	   	 factors.emplace_back(x/i);
	 	  	  	   	 dp[x/i]+=dp[x/i-1];
	 	  	  	   	 dp[x/i]%=mod;
	 	  	  	   }
	 	  	  }
	 	  }
	 	  dp[x]+=dp[x-1];
	 	  dp[x]%=mod;
	 	  dp[1]++;
	 	  dp[1]%=mod;
	 }
	 int res=0;
	 for(int i=1;i<=n;i++)
	 {
	 	  res+=dp[i];
	 	  res%=mod;
	 }
	 cout<<res<<endl;
	 return 0;
}