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

Output

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

Sample test(s)
input
  1. 2
  2. 1 2
output
  1. 2
input
  1. 3
  2. 1 2 3
output
  1. 4
input
  1. 9
  2. 1 2 1 3 2 2 2 2 3
output
  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.

给出n个数,每次能够选择消除一个值为ai的数,那么全部ai-1。ai+1的数也会被消掉,同一时候会获得值ai,问最多能够获得多少?

看完题就可想到这应该是一个dp问题,首先,哈希一下,存下每一个数的个数放在p中,消除一个数i,会获得p[i]*i的值(由于能够消除p[i]次),假设从0的位置開始向右消去,那么,消除数i时,i-1可能选择了消除,也可能没有。假设消除了i-1,那么i值就已经不存在,dp[i] = dp[i-1]。假设没有被消除。那么dp[i] = dp[i-2]+ p[i]*i。

那么初始的dp[0] = 0 ; dp[1] = p[1] ;得到了公式 dp[i] = max(dp[i-1],dp[i-2]+p[i]*i).

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define LL __int64
  5. using namespace std;
  6. LL p[110000] , dp[110000] ;
  7. int main()
  8. {
  9. LL i , n , x , maxn = -1;
  10. memset(p,0,sizeof(p));
  11. memset(dp,0,sizeof(dp));
  12. scanf("%I64d", &n);
  13. for(i = 1 ; i <= n ; i++)
  14. {
  15. scanf("%I64d", &x);
  16. if(x > maxn)
  17. maxn = x ;
  18. p[x]++ ;
  19. }
  20. dp[1] = p[1] ;
  21. for(i = 2 ; i <= maxn ; i++)
  22. {
  23. dp[i] = max( dp[i-1],dp[i-2]+p[i]*i );
  24. }
  25. printf("%I64d\n", dp[maxn]);
  26. return 0;
  27. }

Codeforces Round #260 (Div. 2)C. Boredom(dp)的更多相关文章

  1. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  2. Codeforces Round #658 (Div. 2) D. Unmerge(dp)

    题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...

  3. Codeforces Round #471 (Div. 2) F. Heaps(dp)

    题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...

  4. 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

  5. Codeforces Round #652 (Div. 2) D. TediousLee(dp)

    题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...

  6. Codeforces Round #247 (Div. 2) C. k-Tree (dp)

    题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...

  7. Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)

    Greenhouse Effect time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #119 (Div. 2) Cut Ribbon(DP)

    Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

随机推荐

  1. [转]WIBKIT技术资料

    WebKit结构和流程分析 http://inedx.blog.hexun.com/28830354_d.html webkit架构 http://inedx.blog.hexun.com/28795 ...

  2. hdoj 3065 病毒侵袭持续中(AC自动机)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 思路分析:问题需要模式匹配多个模式串,需要注意的是模式串会包含和重叠,需要对AC自动机的匹配过 ...

  3. 飘逸的python - 一个最简单的服务器

    python拥有这种单独起一个服务器监听端口的能力,用标准库的wsgiref就行. from wsgiref.simple_server import make_server def simple_a ...

  4. ExtJs 4 的filefield上传后 返回值success接受不正常

    问题解决了,我修改了返回类型为setContentType("text/html")可以正确解析了,感到很奇怪,其他的地方使用setContentType("applic ...

  5. JavaScript基础知识----基本语法

    JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. 分号 ; 分号用于分隔 JavaScript 语句. 通常我们在每条可执行的语句结尾添加分号 ...

  6. HTML5 总结-拖放-3

    HTML5 拖放 拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放 ...

  7. jQuery File Upload blueimp with struts2 简单试用

    Official Site的话随便搜索就可以去了 另外新版PHP似乎都有问题  虽然图片都可以上传  但是response报错  我下载的是8.8.7木有问题   但是8.8.7版本结合修改main. ...

  8. 图的DFS递归和非递归

    看以前写的文章: 图的BFS:http://www.cnblogs.com/youxin/p/3284016.html DFS:http://www.cnblogs.com/youxin/archiv ...

  9. MFC技术内幕系列之(四)---MFC消息映射与消息传递内幕

    ////////////////////////////////////////////////////////////////////////////////////                 ...

  10. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...