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.
Examples
2
1 2
2
3
1 2 3
4
9
1 2 1 3 2 2 2 2 3
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的更多相关文章
- CF456C Boredom (DP)
Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...
- 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 ...
- CodeForces 455A Boredom (DP)
Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...
- cf455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- 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 ...
- [Codeforces Round #433][Codeforces 853C/854E. Boredom]
题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...
- CodeForces 456-C Boredom
题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...
- CF 455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- [CodeForce455A]Boredom
题面描述 Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long ...
随机推荐
- Android学习第十天
计算机表示图形的几种方式 a) Bmp:以高质量保存,用于计算机 b) Jpg:以良好的质量保存,用于计算机或网络 c) Png:以高质量保存 d) ...
- Django Cookie,Session
Cookie Cookie的由来 HTTP协议是无状态的,每次请求都是独立的,对服务器来说,每次的请求都是全新的,上一次的访问是数 据是无法保留到下一次的 某些场景需要状态数据或者中间数据等相关对下一 ...
- pgsql 并行相关配置
- idea搭建springboot
1.创建新项目 2.继续项目配置 Name:项目名称Type:我们是Maven构建的,那么选择第一个Maven ProjectPackaging:打包类型,打包成Jar文件Java Version: ...
- 第四十篇-private,public,protected的区别
1.public: public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private: private表示私有,私有的意思就是除了class自己之外,任何人都不可 ...
- FreeNAS插件打造ownCloud私有云网盘
ownCloud 是一个自由开源的个人云存储解决方案,可以自由获取无需付费,但用户需要自行架设服务器,好在FreeNAS可以通过插件轻松的构建ownCloud服务器. ownCloud 分为服务器端和 ...
- 金融量化分析【day112】:量化平台的使用-初始化函数
一.set_benchmark - 设置基准 1.实现代码 # 导入函数库 import jqdata #初始化函数,设定基准等等 def initialize(context): set_bench ...
- 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
一. 说在前面的话 本节主要在前面章节的基础上补充了几个简单的知识点,比如:第三方调用通过 GlobalHost.ConnectionManager.GetHubContext<MySpecHu ...
- MySQL关于日志配置安全整改及处理方法
[环境介绍] 系统环境:Linux + mysql 5.7.18 + 主从复制架构 [背景描述] 需求:MySQL数据库都有每年的集团安全整改,常常要求弱口令扫描,基线扫描,漏洞扫描等等.对于MySQ ...
- [物理学与PDEs]第1章习题10 自由电磁场在 Lorentz 规范变换下可使标势为零
在自由电磁场的情况, 证明: 在保持 Lorentz 条件下的规范变换下, 可使标势恒为零. 证明: 取 $\psi$ 满足 $\cfrac{\p \psi}{\p t}=\phi$ 且 $\cfra ...