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. ubuntu下vi的使用

    ubuntu下vi的使用 ssh之后对于server的文件,我习惯用gedit,可是不好改动,于是就用vi. 1.vi的基本概念 基本上vi能够分为三种状态,各自是命令模式(command mode) ...

  2. ajax请求同步与异步的区别

    //同步请求 $.ajax({    type:'post', url:"<c:url value='/device/org/' />"+val, data:{'org ...

  3. 2016/1/12 String 笔记整理

    String  简介                        文件名 Teststring 有实例 String类 即字符串类型,并不是Java的基本数据类型,但可以像基本数据类型一样使用,用双 ...

  4. HTTP要点概述:六,HTTP报文

    一,HTTP报文: 用于HTTP交互的信息称为HTTP报文.请求端(客户端)的HTTP报文叫做请求报文,响应端(服务器)的叫做响应报文.HTTP报文本身是由多行(用CR+LF换行)数据构成的字符串文本 ...

  5. ResolveUrl in external JavaScript file in asp.net project

    https://stackoverflow.com/questions/11263425/page-resolveurl-is-not-working-in-javascript The proble ...

  6. 【POI2007】【Bzoj 1103】大都市meg

    http://www.lydsy.com/JudgeOnline/problem.php?id=1103 在线查询某点到根节点的点权和,参考DFS序&欧拉序列,用树状数组维护即可O(nlogn ...

  7. Notification操作大全

    目录 一:普通的Notification Notification 的基本操作 给 Notification 设置 Action 更新 Notification 取消 Notification 设置 ...

  8. 杂项-Java:FreeMarker

    ylbtech-杂项-Java:FreeMarker 1.返回顶部 1. FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源 ...

  9. JSP-Runoob:JSP 教程

    ylbtech-JSP-Runoob:JSP 教程 1.返回顶部 1. JSP 教程 JSP 与 PHP.ASP.ASP.NET 等语言类似,运行在服务端的语言. JSP(全称Java Server ...

  10. Cpp module