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

比赛中遇到的dp题,比赛时没有思路,赛后有点思路但不完善,听了讲解后算是懂了,还需要多积累。

若取当前的值,则与其相邻的值就不能取,故状态转移方程:

  dp[i][0]=max(dp[i-1][0],dp[i-1][1]);

  dp[i][1]=dp[i-1][0]+value[i];

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; long long vis[];
long long dp[][];
long long value[];
int main()
{
int n,num;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&num);
vis[num]++;
}
int cnt=;
for(int i=;i<=1e5;i++)
{
dp[i][]=max(dp[i-][],dp[i-][]);
dp[i][]=dp[i-][]+vis[i]*i;
}
//cout<<dp[cnt-1][0]<<endl<<dp[cnt-1][1]<<endl;
printf("%I64d\n",dp[][]>dp[][]?dp[][]:dp[][]);
return ;
}

codeforces_456C_dp的更多相关文章

随机推荐

  1. AE After Effect 如何分段渲染

    如果只要第一段的话,你把要输出的那段首尾处分别按下B键和N键,这样输出时就会只输出这一段了(拖动首尾的栏目修改起始和终止的时间):如果是批量渲染的话你只要在这些不同的合成层里,每个按下ctrl+M键, ...

  2. 关于ping以及TTL的分析

    首先介绍一下ping这个工具 ping [目标] 的意思就是向目标发送几个数据包,之后假设目标接受到一个数据包.那么目标就会向发送ping的主机返回一个数据包 比方上图.我ping了百度的server ...

  3. 总结react中遇到的坑和一些小的知识点

    在使用react 中经常会遇到各种个样的问题,如果对react不熟悉则会对遇到的问题感到莫名其妙而束手无策,接下来分析一下react中容易遇到的问题和注意点. 1.setState()是异步的this ...

  4. 使用fiddler将网站上的css js重定向至本地文件

    使用fiddler将网站上的css js重定向至本地文件,进行在线调试 https://github.com/annnhan/ReRes 1条回复 这是一篇写给公司负责切图和调样式的前端的文章.主要适 ...

  5. 跟踪oracle中sql语句运行过程及相关知识拓展

    select * from v$sqlarea; select * from v$sqlarea where first_load_time>'2010-11-27/09:30:00'; 这种方 ...

  6. HDU 4850 Wow! Such String!(欧拉道路)

    HDU 4850 Wow! Such String! 题目链接 题意:求50W内的字符串.要求长度大于等于4的子串,仅仅出现一次 思路:须要推理.考虑4个字母的字符串,一共同拥有26^4种,这些由这些 ...

  7. (转)C3P0配置

    C3P0是一个开源的JDBC 连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. sourceforge 下载: ...

  8. 蓝牙驱动分析 linux

    蓝牙驱动分析 这个驱动分析的是OK6410开发板自带的内核版本是linux3.0.1,所支持的wifi和蓝牙一体芯片是marvell的8688和8787.根据开发板的设计,芯片与主机之间是通过sdio ...

  9. docker 清理容器和镜像

    在docker运行过程中,会不知不觉造出很多容器,很多都是不用的,需要清理. 下面就是一些清理办法,一个个清理肯定很低效,批量清理很有意思. 查看正在运行的容器 # docker ps -q 9b9f ...

  10. redis.exceptions.ConnectionError: Error 10061 connecting to 127.0.0.1:6379. 由于目标计算机积极拒绝,无法连接

    redis.exceptions.ConnectionError: Error 10061 connecting to 127.0.0.1:6379. 由于目标计算机积极拒绝,无法连接   是由于没有 ...