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

题意:我们每次都删除一个数字n,并且n-1,n+1也删除,我们要获得n的总和要最大

解法:DP,我们记录每个数字出现次数,这样删除之后还能乘以i得到一个和,我们还要把最大的数字找出来

这样我们就从2—>max 有dp[i]=dp[i-1],因为i-1也要删除的,dp[i]=dp[i-2]+i*出现次数,求他们之间的最大值

注意起始dp[0],dp[1]

#include<bits/stdc++.h>
using namespace std;
map<long long,long long>q;
long long dp[100010];
int main()
{
long long n;
long long m;
long long MAX=-1;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>m;
q[m]++;
MAX=max(MAX,m);
}
dp[0]=0;
dp[1]=q[1];
for(int i=2;i<=MAX;i++)
{
dp[i]=max(dp[i-1],(long long)(dp[i-2]+i*q[i]));
}
cout<<dp[MAX]<<endl;
return 0;
}

  

Codeforces Round #260 (Div. 2) C的更多相关文章

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

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

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

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

  3. Codeforces Round #260 (Div. 2)AB

    http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...

  4. Codeforces Round #260 (Div. 1) D. Serega and Fun 分块

    D. Serega and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/pro ...

  5. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

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

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  7. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

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

    题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...

  9. Codeforces Round #260 (Div. 2)

    A. Laptops 题目意思: 给定n台电脑,第i台电脑的价格是ai ,质量是bi ,问是否存在一台电脑价格比某台电脑价格底,但质量确比某台电脑的质量高,即是否存在ai < aj 且 bi & ...

随机推荐

  1. 封装application类

    <?php  //判断用户是否是通过入口文件访问   if(!defined('ACCESS')){     echo '非法请求';     die;   }   //封装初始化类   cla ...

  2. linux env

    .Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.1 永久的:需要修改配置文件,变量永久生效. 1.2 临时的:使用export命令声明即可,变量在关闭shell时失效. ...

  3. C#访问PostGreSQL数据库的方法 http://www.jb51.net/article/35643.htm

    这次的项目中的一个环节要求我把PostGreSQL数据取出来,然后放到SqlServer里,再去处理分析. http://www.jb51.net/article/35643.htm - 我对Post ...

  4. paper 39 :Matlab绘制误差棒图(errorbar函数的使用)

    同很多非数学相关专业的朋友一样,我第一次碰到这个图时也是丈二和尚摸不着头脑.只知道这个工字型的图案,中间的点代表的是平均值,上下的两条横线代表的是方差值,除此之外,连这个图叫什么名字都不知道,只好硬着 ...

  5. 夺命雷公狗—angularjs—23—copy拷贝对象

    copy这在angularjs中是一个拷贝对象的方法: <!DOCTYPE html> <html lang="en" ng-app="myapp&qu ...

  6. 导出excel部分代码

    public static function header_file($doc,$file,$title,$type='Excel5'){ if(!empty($doc)){ $objWriter = ...

  7. 《Focus On 3D Terrain Programming》中一段代码的注释一

    取自<Focus On 3D Terrain Programming>中的一段: //--------------------------------------------------- ...

  8. SSAS中角色(Role)定义需要注意的两个地方

    开发过SSAS Cube的朋友应该都知道,我们可以在SSAS中设置若干个角色,把windows账号放入这些角色中来限制不同的windows账号可以看到的数据有哪些,这里有两点需要注意一下. 首先在Cu ...

  9. NOIP200205均分纸牌

                                                                  均分纸牌 描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张 ...

  10. 《REWORK》启示录 发出你的心声——程序员与身体

    Sound Like You 所谓的标题在这里并不是为了吸引眼球,不过也是为了吸引眼球,只是出发点已经不一样了.这是一篇适合给程序员看的关于健康的文章,也许你认识李开复也可以给他看看,上过养生过,觉得 ...