[DP]CF455A Boredom

A. Boredom

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 ak points 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 a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

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

Correct solution:

First thought it was an interval dp, that is to a [k] omitted, a [k + 1] and a [k-1] number also omitted.

But the open interval array 1e5 open no less, see solution to a problem, that there is a better way.

So ...... wrong topic

It is the deletion of a certain number, a + 1 and a-1 should be deleted.

We set f [i] is the maximum value for the maximum value of i.

If i is not deleted, then, f [i] = f [i-1]

Young bunch i 删除, f [i] = f [i-2] + cnt [i] * i;   

(After deleting i, i-1 can not be selected, it is necessary [i-2] is transferred from the beginning f)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<string>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<cmath>
11 #include<cstdlib>
12 #include<stack>
13 #define de printf("\ndebug:\n")
14 #define End printf("\nend\n\n")
15 #define fi first
16 #define se second
17 #define P pair< int, int >
18 #define PII pair< pair<int, int> ,int>
19 #define INF 0x3f3f3f3f
20 using namespace std;
21 typedef long long ll;
22 const ll mod=1000000007;
23 const int N=100000+100;
24 const int inf=0x7fffffff;
25 int n,a[N],maxx;
26 ll cnt[N];
27 ll f[N];
28 int main()
29 {
30     scanf("%d",&n);
31     for(int i=1;i<=n;i++)
32     {
33         scanf("%d",&a[i]);
34         maxx=max(maxx,a[i]);
35         cnt[a[i]]++;
36     }
37     f[0]=0;
38     f[1]=cnt[1];
39     for(int i=2;i<=maxx;i++)
40         f[i]=max(f[i-1],f[i-2]+cnt[i]*i);
41     printf("%lld\n",f[maxx]);
42 
43 
44 
45     return 0;
46 }
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/Kaike/p/11260597.html