CSU-ACM2017暑期训练8-动态规划初步 F
来源:互联网 发布:ipad淘宝卖家 编辑:程序博客网 时间:2024/05/29 18:12
F - Boredom
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.
Example
Input21 2Output2Input31 2 3Output4Input91 2 1 3 2 2 2 2 3Output10
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.
由于被删除的元素只与被取出的元素的值有关,所以输入的序列是没有顺序的,只需要考虑每个元素的值是否被选中。
假设有一个足够长的序列,从中取出5,那么4和6就不会被考虑;小于4,和大于6的值则不受影响。
一般情况:从最小数字开始考虑,设当前考虑的数字为
其中
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>using namespace std;const int maxn = 1e5+4;long long n, maxNum;long long occurrence[maxn], sample, dp[maxn];//数组occurrence[]是对输入的样例进行桶排序用的int main(){#ifdef TESTfreopen("test.txt", "r", stdin);#endif // TEST while(cin >> n){ memset(occurrence, 0, sizeof(occurrence)); memset(dp, 0, sizeof(dp)); maxNum = 0; for(int i = 1; i <= n; i++){ scanf("%lld", &sample); occurrence[sample]++; if(sample > maxNum) maxNum = sample; } dp[1] = occurrence[1]; for(int i = 2; i <= maxNum; i++){ long long pts = i*occurrence[i]; dp[i] = max(dp[i-2]+pts, dp[i-1]); } cout << dp[maxNum] << endl; } return 0;}
阅读全文
0 0
- CSU-ACM2017暑期训练8-动态规划初步 F
- CSU-ACM2017暑期训练8-动态规划初步 C
- CSU-ACM2017暑期训练8-动态规划初步 D
- CSU-ACM2017暑期训练4-dfs F
- CSU-ACM2017暑期训练12-KMP F
- CSU-ACM2017暑期训练7-模拟&&贪心 F
- CSU-ACM2017暑期训练10-并查集&&HASH F
- CSU-ACM2017暑期训练16-树状数组 F
- CSU-ACM2017暑期训练3 J
- CSU-ACM2017暑期训练4-dfs D
- CSU-ACM2017暑期训练4-dfs E
- CSU-ACM2017暑期训练4-dfs G
- CSU-ACM2017暑期训练5-三分 E
- CSU-ACM2017暑期训练6-bfs C
- CSU-ACM2017暑期训练6-bfs I
- CSU-ACM2017暑期训练6-bfs H
- CSU-ACM2017暑期训练6-bfs G
- CSU-ACM2017暑期训练5-三分 A
- android常用英语总结
- OpenStack高可用核心架构分析
- html个人简单练习
- 设计模式C++实现(9)——享元模式
- dubbo 服务的consumer client 调用堆栈 调用链 -- 学习dubbo启动后执行逻辑和代码
- CSU-ACM2017暑期训练8-动态规划初步 F
- dfs hdu2209 翻纸牌游戏
- 装饰者模式
- python中的闭包
- 求一个无序数组的中位数。
- 常见知识点
- 两军交锋 HDU-2548 【思维】
- 图像算法面试
- 【GDAL/OGR】利用GDAL/OGR读取shp文件并转换为json文件(Java版)