Codeforces round #260 (Div. 1) A. Boredom (DP)

Source: Internet
Author: User


A. boredomtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 sequenceAConsistingNIntegers. 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 equalAK? +? 1 andAK? -? 1 also must be deleted from the sequence. That step bringsAKPoints to the player.

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


The first line contains integerN(1? ≤?N? ≤? 105) that shows how should numbers are in Alex's sequence.

The second line containsNIntegersA1,A2 ,...,AN(1? ≤?AI? ≤? 105 ).


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

Sample test (s) Input
21 2
31 2 3
91 2 1 3 2 2 2 2 3

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.


Place the number of each number in C. If I is eliminated, the value of C [I] * I will be obtained (because C [I] times can be eliminated ),
If the elimination starts from the position 0 to the right, then the I-1 may have chosen to remove the number I, or not,
If the I-1 is removed, the I value does not exist, DP [I] = DP [I-1],
If not eliminated, DP [I] = DP [I-2] + C [I] * I.

The Code is as follows:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define LL __int64const int MAXN = 100017;LL c[MAXN], dp[MAXN];void init(){memset(dp,0,sizeof(dp));memset(c,0,sizeof(c));}int main(){LL n;LL tt;int i, j;while(~scanf("%I64d",&n)){init();int maxx = 0;for(i = 1; i <= n; i++){scanf("%I64d",&tt);if(tt > maxx)maxx = tt;c[tt]++;}dp[0] = 0, dp[1] = c[1];for(i = 2; i <= maxx; i++){dp[i] = max(dp[i-1],dp[i-2]+c[i]*i);}printf("%I64d\n",dp[maxx]);}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.