Boredom easy dp

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.

Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings akpoints to the player.

Alex is a perfectionist, so he decided to get as many points as possible. Help him.

Input

The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a single integer — the maximum number of points that Alex can earn.

Sample Input

Input
2
1 2
Output
2
Input
3
1 2 3
Output
4
Input
9
1 2 1 3 2 2 2 2 3
Output
10

Hint

Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points.

My code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<algorithm>
 5 using namespace std;
 6 bool cmp(int x,int y)
 7 {
 8     return x>y;
 9 }
10 
11 int main()
12 {
13     int n;
14     long long dp[100005];
15     int a[100005],b[100005],m[100005];
16     int i,j;
17     while(scanf("%d",&n)!=EOF)
18     {
19         memset(m,0,sizeof(m));
20         memset(a,0,sizeof(a));
21         memset(dp,0,sizeof(dp));
22         for(i=1;i<=n;i++)
23         {
24             scanf("%d",&a[i]);
25             if(m[a[i]]>0)
26             {
27                 m[a[i]]++;
28                 a[i]=0;i--;n--;
29             }
30             else
31             {
32                 m[a[i]]++;
33             }
34         }
35         /*printf("%d\n",n);
36         for(i=1;i<=n;i++)
37             printf("%d ",a[i]);*/
38 
39         sort(a+1,a+n+1,cmp);
40         dp[0]=0;
41         dp[1]=(long long)a[1]*m[a[1]];
42         if(a[1]-1==a[2])
43             dp[2]=(long long)a[2]*m[a[2]];
44         else
45             dp[2]=dp[1]+(long long)a[2]*m[a[2]];
46         //printf("%I64d %I64d\n",dp[1],dp[2]);
47         for(i=3;i<=n;i++)
48         {
49             long long maa=0;
50             maa=max(dp[i-2],dp[i-3]);
51             if(a[i-1]-1==a[i])
52                 dp[i]=maa+(long long)a[i]*m[a[i]];
53             else
54             {
55                 dp[i]=(long long)a[i]*m[a[i]]+max(maa,dp[i-1]);
56             }
57         }
58         long long ans=0;
59         for(i=1;i<=n;i++)
60             if(ans<dp[i])
61                 ans=dp[i];
62         printf("%I64d\n",ans);
63     }
64     return 0;
65 }
View Code

别宁家的代码 短小&精悍

 1 #include <cstdio>  
 2 #include <cstring>  
 3 #include <algorithm>  
 4 using namespace std;  
 5 #define LL __int64  
 6 const int MAXN = 100017;  
 7 LL c[MAXN], dp[MAXN];  
 8 void init()  
 9 {  
10     memset(dp,0,sizeof(dp));  
11     memset(c,0,sizeof(c));  
12 }  
13 int main()  
14 {  
15     LL n;  
16     LL tt;  
17     int i, j;  
18     while(~scanf("%I64d",&n))  
19     {  
20         init();  
21         int maxx = 0;  
22         for(i = 1; i <= n; i++)  
23         {  
24             scanf("%I64d",&tt);  
25             if(tt > maxx)  
26                 maxx = tt;  
27             c[tt]++;  
28         }  
29         dp[0] = 0, dp[1] = c[1];  
30         for(i = 2; i <= maxx; i++)  
31         {  
32             dp[i] = max(dp[i-1],dp[i-2]+c[i]*i);  
33         }  
34         printf("%I64d\n",dp[maxx]);  
35     }  
36     return 0;  
37 }  
View Code

 

posted @ 2015-07-16 09:37  cyd2014  阅读(165)  评论(0编辑  收藏  举报