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. VC,VB程序button、图标样式美化

    此处的"美化"指的不是通过代码进行美化你的程序.关于想进一步优化自己的程序界面的,最好还是去了解下SkinSharp吧.本文提及的是利用第三方资源编辑软件在不更改程序不论什么框架和 ...

  2. 两个月后才更新一篇。。。。LIB和DLL的差别

     共同拥有两种库: 一种是LIB包括了函数所在的DLL文件和文件里函数位置的信息(入口).代码由执行时载入在进程空间中的DLL提供,称为动态链接库dynamic link library. 一种是 ...

  3. 我是怎样自学 Android 的?

    1. Java知识储备 本知识点不做重点解说: 对于有基础的同学推荐看<Java编程思想>,巩固基础,查漏补全,了解并熟悉很多其它细节知识点. 对于没有基础的同学推荐看一本Java基础的书 ...

  4. AVD那些事儿

    启动了AVD却说找不到AVD 错误提示: No active compatible AVD's or devices found. Relaunch this configuration after ...

  5. 关于spring配置文件中编辑时没有提示信息的问题

    spring配置文件头部信息主要是提供一个xml的编写规范作用. 新创建的配置文件引入头部信息后,编辑时没有提示信息,重启elipse即可解决.

  6. 51Nod 1522 上下序列 —— 区间DP

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1522 区间DP,从大往小加: 新加入一种数有3种加法:全加左边,全 ...

  7. 深入理解JMM(Java内存模型) --(四)volatile

    volatile的特性 当我们声明共享变量为volatile后,对这个变量的读/写将会很特别.理解volatile特性的一个好方法是:把对volatile变量的单个读/写,看成是使用同一个监视器锁对这 ...

  8. bzoj4870

    http://www.lydsy.com/JudgeOnline/problem.php?id=4870 矩阵快速幂... 人话题意:从nk个物品里选模k余r个物品,问方案数模P 那么我们有方程 f[ ...

  9. Thinkphp模板标签if和eq的区别和比较

    在TP模板语言中.if和eq都可以用于变量的比较.总结以下几点: 1.两个变量的比较: <if condition=”$item.group_id eq $one.group_id”> & ...

  10. crosswalk 初步使用

    这里简单的说下 crossWalk 的配置; 我在学习 crosswalk 的时候,看到一篇博客,他所记录的已经是很完整的教程了: 点击查看 本文就根据该博客和官网说明,进行部分修改,补充; 首先 c ...