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

题意:

给定一个序列,每次从序列中选出一个数ak,获得ak的得分,同时删除序列中所有的ak−1,ak+1,

求最大得分的值。

dp[i]=max(dp[i-2]+n[i]*i,dp[i-1])
dp[i]表示的是前i个的最大值,对于第i个有取和不取的情况,对于可以取到i的情况,删掉的一定是i-1这个点,剩下的就是前i-2的情况了,由于dp[i-2]包括了取i-2的情况,于是就不用再重复考虑i-2也要加一遍的情况了,然后再考虑不取i的情况,就不用考虑i了,于是就是dp[i-1]了

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+1;
long long num[N],dp[N];
int main()
{
int n,a,i;
cin>>n;
int maxa=-1;
int mina=N;
for(i=0;i<n;i++){
cin>>a;
num[a]++;
maxa = max(a,maxa);
mina = min(a,mina);
}
dp[mina] = num[mina]*mina;
for(i=mina+1; i <= maxa; i++){
dp[i] = max(dp[i-2]+num[i]*i,dp[i-1]);
}
cout<<dp[maxa]<<endl;
return 0;
}

  

Boredom的更多相关文章

  1. CF456C Boredom (DP)

    Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...

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

  3. CodeForces 455A Boredom (DP)

    Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...

  4. cf455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

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

    C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. [Codeforces Round #433][Codeforces 853C/854E. Boredom]

    题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...

  7. CodeForces 456-C Boredom

    题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...

  8. CF 455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. [CodeForce455A]Boredom

    题面描述 Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long ...

随机推荐

  1. 【LOJ2542】【PKUWC 2018】随机游走 min-max容斥 树上高斯消元

    题目描述 有一棵 \(n\) 个点的树.你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(q\) 次询问,每次询问给定一个集合 \(S\),求如果从 \(x\) 出发一 ...

  2. 探索 Python 学习

    Python 是一种敏捷的.动态类型化的.极富表现力的开源编程语言,可以被自由地安装到多种平台上(参阅 参考资料).Python 代码是被解释的.如果您对编辑.构建和执行循环较为熟悉,则 Python ...

  3. A.01.12—模块的输出—通讯(CAN&LIN)

    AN和LIN相关的内容很多,今天仅对几年前困扰过我的一个疑问进行说明. 以前最常见的通迅方式为CAN和LIN,但现在也有很多其他的通讯方式了,而这两种通讯方式仍使用广泛. 前几年常听人说CAN的成本和 ...

  4. [FJOI2018]领导集团问题

    [FJOI2018]领导集团问题 dp[i][j],i为根子树,最上面的值是j,选择的最大值 观察dp方程 1.整体Dp已经可以做了. 2.考虑优美一些的做法: dp[i]如果对j取后缀最大值,显然是 ...

  5. zabbix Server 4.0 部署及之内置item使用案例

    zabbix Server 4.0 部署及之内置item使用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.zabbix组件架构概述(图片摘自网络) 1>.zabbi ...

  6. 深入学习CSS外边距margin(重叠效果,margin传递效果,margin:auto实现块级元素水平垂直居中效果)

    前言 margin是盒模型几个属性中一个非常特殊的属性.简单举几个例子:只有margin不显示当前元素背景,只有margin可以设置为负值,margin和宽高支持auto,以及margin具有非常奇怪 ...

  7. SQL修改日期类型字段为字符串类型

    select * from test1 --添加行 ) --将转换格式后的数据放到列中 ) --删除老的字段 alter table test1 drop column startdate --修改字 ...

  8. 报文段、协议、MAC地址

  9. slot

    本文涉及的slot有:<slot>,v-slot吗,vm.$slots,vm.$scopedSlots 废弃的使用特性:slot,slot-scope,scope(使用v-slot,2.6 ...

  10. 封装ajax,让调用变得简单优化

    思考一下: 通常我们在使用ajax来发送接口请求时,每一次都会调用ajax固定的元素,比如data.url.method.success.error等.那么我们想一下能不能先把ajax封装起来,在每次 ...