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. Node.js修改全局安装默认路径

    因为苦于C盘不够的烦恼,不想把全局安装包的路径弄在C盘,于是有了这篇文章: 查看设置 npm config ls //查看设定信息,,找到prefix一行,默认是一般是在C盘 修改命令如下 npm c ...

  2. C++ Under the Hood

    The original article is taken from http://msdn.microsoft.com/archive/en-us/dnarvc/html/jangrayhood.a ...

  3. BZOJ4552 HEOI2016/TJOI2016排序(线段树合并+线段树分裂)

    很久以前写过二分答案离线的做法,比较好理解.事实上这还是一个线段树合并+分裂的板子题,相比离线做法以更优的复杂度做了更多的事情.具体不说了.怎么交了一遍luogu上就跑第一了啊 #include< ...

  4. linux系统无法启动或无法登入

    修改root权限: https://blog.csdn.net/houjue2298/article/details/78539827 修改密码: https://www.cnblogs.com/we ...

  5. 网页三剑客之CSS

    1.CSS概述 CSS中文简称层叠样式表(英文全称:Cascading Style Sheets),用来控制页面的表现,即使页面更好看的语言. 2.CSS基本语法和页面引用 2.1 css的定义方法 ...

  6. python之路(1)数据类型

    目录 整型 布尔值 字符串 列表 元组 字典 整型(int) 将字符串转换成整型 num = "123" v = int(num) 2. 将字符串按进制位转换成整型 num = & ...

  7. css浮动(float)全方位案例解析

    前言 浮动最早的使用是出自<img src="#" align="right" />,用于文本环绕图片的排版处理.当然也是一种常用的布局方式. fl ...

  8. Django2.1配置xadmin2.0

    系统:Ubuntu18.04 环境:py3.6, django2.17, xadmin-django2.0 xadmin-django2.0下载:xadmin2.0(进入github后,在Branch ...

  9. AWT 新建窗口

    新建一个窗口 包 import java.awt.*; 定义 Frame frm_Draw = new Frame("Text"); 初始化代码 public void Frame ...

  10. Dilated Convolution

    各种各样的卷积方式, 详细见 各种卷积的 gif 图 Convolution animations  Padding, strides Transposed convolution animatio ...