CSU-ICPC 2019年冬休み合宿結訓テストJ-Boredom


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 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 ≤ 10^5) that shows how many numbers are in Alex’s sequence. The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 10^5)

Output


Print a single integer — the maximum number of points that Alex can earn.
Sample Input
91 2 1 3 2 2 2 2 3
Sample Output
10

Hint


Consider the 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.

問題面の大意


一連の数列では,akサイズの要素を1つ削除するたびにスコアakが得られるが,ak−1サイズでak+1の要素も削除される.取得できる最大点数を求める.

構想


これは簡単なdp問題で、ブラシメーターで解決します.まず、値が等しい要素については、配列cnt[]を宣言できることは明らかです.cnt[i]は、要素iが数列に現れる回数を意味します.動的計画セクションでは、配列dp[]を宣言し、dp[i]は、要素を小さいものから大きいものに、その要素に取ったときに得られる最大スコアを表します.明らかに、2つの状況があります.
  • 値i-1の要素を選択すると、値iの要素は無視され、dp[i]=dp[i-1].
  • 値i-2の要素と値iの要素を選択し、値i-1の要素を無視し、dp[i]=dp[i-2]+cnt[i]*i;

  • また,問題は点数最大値をとることを要求するので,状態遷移方程式:dp[i]=max(dp[i-1],dp[i-2]+cnt[i]*i)を得る.
    ACコード:
    #include
    using namespace std;
    long long cnt[100000+5];
    long long dp[100000+5];
    int main()
    {
     long long a,n,maxx=-1;
     scanf("%lld",&n);
     while(n--)
     {
      scanf("%lld",&a);
      maxx=max(a,maxx);// dp 
      cnt[a]++;
     }
     dp[1]=cnt[1];
     for(long long i=2;i<=maxx;i++)
      dp[i]=max(dp[i-1],dp[i-2]+cnt[i]*i);
     printf("%lld",dp[maxx]);
     return 0;
    }