A. 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 a1a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a single integer — the maximum number of points that Alex can earn.

Examples
input

Copy
  1. 2
  2. 1 2
output

Copy
  1. 2
input

Copy
  1. 3
  2. 1 2 3
output

Copy
  1. 4
input

Copy
  1. 9
  2. 1 2 1 3 2 2 2 2 3
output

Copy
  1. 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.

【题意】

给你一个数组a,里面有n个整数。你每次可以选择数组中的一个元素ak,从数组中删掉它,再删掉所有值等于ak + 1 或者 ak - 1的元素,这样你可以得到 ak 分。你可以重复进行多次该操作,请问你最后最多能得多少分?

【分析】

先求出数列中每一个数字k的出现次数num[k]

状态转移方程:如果取得第i-1个数,那么第i-2和第i个数均不可取;反之可取得第i和第i-2个数

第i个状态的值为 (取得第i-1个数的得分) 与 (取得第i-2个数得分和取得当前数的得分之和) 的最大值

注意,最后一重for循环要从2循环至已知的maxn


【代码】

  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. const int N=1e5+5;
  5. inline int read(){
  6. register int x=0;register char ch=getchar();
  7. for(;ch<'0'||ch>'9';ch=getchar());
  8. for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
  9. return x;
  10. }
  11. int n,mx;long long cnt[N],f[N];
  12. int main(){
  13. n=read();
  14. for(int i=1,x;i<=n;i++) mx=max(mx,x=read()),cnt[x]+=x;
  15. f[1]=cnt[1];
  16. for(int i=2;i<=mx;i++) f[i]=max(f[i-1],f[i-2]+cnt[i]);
  17. printf("%I64d\n",f[mx]);
  18. return 0;
  19. }

 

CF 455A Boredom的更多相关文章

  1. CodeForces 455A Boredom (DP)

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

  2. CF 455A(Boredom-dp)

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

  3. Codeforces 455A Boredom (线性DP)

    <题目链接> 题目大意:给定一个序列,让你在其中挑选一些数,如果你选了x,那么你能够得到x分,但是该序列中所有等于x-1和x+1的元素将全部消失,问你最多能够得多少分. 解题分析:从小到大 ...

  4. Codeforces 455A - Boredom - [DP]

    题目链接:https://codeforces.com/problemset/problem/455/A 题意: 给出一个 $n$ 个数字的整数序列 $a[1 \sim n]$,每次你可以选择一个 $ ...

  5. Codeforces 455A Boredom 取数字的dp

    题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失.即无法获得这2个数的分数 问最高得分. 先统计每一个数出现的次数.然后dp一 ...

  6. codeforces每日一题1-10

    目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...

  7. CF456C Boredom (DP)

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

  8. 蒟蒻修养之cf橙名计划

    因为太弱,蒟蒻我从来没有上过div1(这就是今年的最后愿望啊啊啊啊啊)已达成................打cf几乎每次都是fst...........所以我的cf成绩图出现了惊人了正弦函数图像.. ...

  9. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

随机推荐

  1. 单例模式简介以及C++版本的实现

        本篇博文主要内容参考 C++的单例模式一文,在此,为原作者耐心细致的分析讲解,表示感谢.本文将结合此篇文章,给出自己做实验后的理解以及代码,作为今天学习的小结.     单例模式,它的意图是保 ...

  2. react解析html标签组成的字符串

    转载自:https://blog.csdn.net/tongshuo_11/article/details/61195232 var content = '<strong>content& ...

  3. rsync:重要的安全参数

    ---------------------------------------------------------------------------------------------------- ...

  4. python字符串 分片索引

    字符串是字符的有序集合,可以通过其位置来获得具体的元素.在python中,字符串中的字符是通过索引来提取的,索引从0开始. python可以取负值,表示从末尾提取,最后一个为-1,倒数第二个为-2,即 ...

  5. mysql主从复制-方案1

    mysql主机master 1. 编辑mysql配置文件my.cnf server_id = 1                     #server_id服务器唯一标识 log_bin = mys ...

  6. MySQL的sql_mode解析与设置,sql文件导入报错解决

    在往MySQL数据库中插入一组数据时,出错了!数据库无情了给我报了个错误:ERROR 1365(22012):Division by 0:意思是说:你不可以往数据库中插入一个 除数为0的运算的结果.于 ...

  7. SQL Compare数据库比较工具 完全破解+使用教程

    来源http://www.cnblogs.com/duci/articles/4482665.html 一.使用教程 SQL Compare是编程人员常用的比较两个数据库之间差异的工具.可以用来比较数 ...

  8. FileSaver.js 浏览器导出Excel文件

    限制一:不同浏览器对 blob 对象有不同的限制 具体看看下面这个表格(出自 FileSaver.js): Browser Constructs as Filenames Max Blob Size ...

  9. ASP.NET Web API 使用Swagger使用笔记

    https://www.cnblogs.com/lhbshg/p/8711604.html 最近换了工作,其中Webapi这块没有文档,之前有了解过Swagger借此机会好好整理下常用的地方分享给有需 ...

  10. SpringMVC-----使用Maven创建Web项目

    1.创建一个Maven的project 2.不使用骨架,去掉勾 3.这里的Packing 选择 war的形式 由于packing是war包,那么下面也就多出了webapp的目录 4.由于我们的项目要使 ...