CF#260 C.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.

Sample test(s)

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

渣渣被虐的体无完肤,闭关修行去了,附上渣代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
using namespace std;
const int maxn=1e5+10;
int hash[maxn];
long long dp[maxn];
int main()
{
    int n,x;
    while(~scanf("%d",&n))
    {
        int minn=0;
        memset(hash,0,sizeof(hash));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            if(x>minn)
                minn=x;
            hash[x]++;
        }
        dp[0]=0;
        dp[1]=hash[1];
        for(int i=2;i<=minn;i++)
        {
            dp[i]=dp[i-1];
            long long temp=(long long)i*hash[i];
            if(dp[i-2]+temp>dp[i-1])
                dp[i]=dp[i-2]+temp;
        }
        printf("%I64d\n",dp[minn]);
    }
    return 0;
}

CF#260 C.Boredom

时间: 2024-10-07 11:00:19

CF#260 C.Boredom的相关文章

CF# 260 A. Laptops

One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (

CF#260 B. Fedya and Maths

Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression: (1n?+?2n?+?3n?+?4n) mod 5 for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can e

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

题目链接:http://codeforces.com/problemset/problem/455/A 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

dp解Codeforces Round #260 (Div. 2)C. Boredom

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; lo

递推DP Codeforces Round #260 (Div. 1) A. Boredom

题目传送门 1 /* 2 DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 1e5 + 10; 12 const int INF

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

题目传送门 1 /* 2 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 3 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) i * cnt[i]); 4 只和x-1,x-2有关,和顺序无关,x-1不取,x-2取那么累加相同的值,ans = dp[mx] 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring&

CF 455A Boredom

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 ga

#260 (div.1) A.Boredom

1.题目描述:点击打开链接 2.解题思路:本题属于01背包型的dp问题.为了使得问题变得便与思考,我们可以顺序考虑每一个数字.事先用数组cnt统计输入的数字的个数.接下来,考虑数字i.如果选择删除它的话,由于是按顺序考虑的,因此所有的i-1都将被删去,那么分数为d[i]+i*cnt[i]:如果不删除它,那么分数就是d[i-1].只需要取较大者即可. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algo

Codeforces Round #260 (Div. 2)C. Boredom

题意:N个数,我们可以选择某个数A,然后去掉A,和等于A+1,A-1的所有数字,得到A价值,问最后价值最大 思路:我们可以得到去掉A,得到的价值为A*A的个数,那么dp[i]=max(dp[i]+dp[i-2],dp[i-1]).记得开long long , 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll a[100005]; 5 ll dp[100005]; 6 map<ll ,