【CodeForces 455A】Boredom(dp)

    xiaoxiao2021-03-25  89

     A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

    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 itak) and delete it, at that all elements equal toak + 1 andak - 1 also must be deleted from the sequence. That step bringsak 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.

    Examples 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 Note

    Consider the third test example. At first step we need to choose any element equal to2. 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 to2. In total we earn 10 points.

    题目大意:每次选一个数x,会得到这个数的分x,但会删除所有 x-1 和 x+1,求得分最大

    思路:dp,每个数只有选或不选两种情况,如果选,则与它相同的数的得分都会得到,需先统计每个数的个数

    #include<iostream> #include<cstdio> #define manx 100005 typedef long long ll; using namespace std; int main() { ll n,a[manx]={0},dp[manx]={0},x,cot=0; scanf("%lld",&n); for (int i=0; i<n; i++){ scanf("%lld",&x); cot=max(cot,x); a[x]++; } dp[1]=a[1]; for (int i=2; i<=cot; i++){ dp[i]=max(dp[i-1],dp[i-2]+a[i]*i); } printf("%lld\n",dp[cot]); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-25499.html

    最新回复(0)