codeforces_456C_dp
1 second
256 megabytes
standard input
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.
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).
Print a single integer — the maximum number of points that Alex can earn.
2
1 2
2
3
1 2 3
4
9
1 2 1 3 2 2 2 2 3
10
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.
比赛中遇到的dp题,比赛时没有思路,赛后有点思路但不完善,听了讲解后算是懂了,还需要多积累。
若取当前的值,则与其相邻的值就不能取,故状态转移方程:
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
dp[i][1]=dp[i-1][0]+value[i];
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; long long vis[];
long long dp[][];
long long value[];
int main()
{
int n,num;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&num);
vis[num]++;
}
int cnt=;
for(int i=;i<=1e5;i++)
{
dp[i][]=max(dp[i-][],dp[i-][]);
dp[i][]=dp[i-][]+vis[i]*i;
}
//cout<<dp[cnt-1][0]<<endl<<dp[cnt-1][1]<<endl;
printf("%I64d\n",dp[][]>dp[][]?dp[][]:dp[][]);
return ;
}
codeforces_456C_dp的更多相关文章
随机推荐
- C# 插件热插拔 .NET:何时应该 “包装异常”? log4.net 自定义日志文件名称
C# 插件热插拔 所谓热插拔就是插件可以 在主程序不重新启动的情况直接更新插件, 网上有很多方案: https://www.cnblogs.com/happyframework/p/3405811 ...
- TMS320F28335项目开发记录6_28335之cmd文件具体解释
1.CMD文件的作用 CMD文件的作用就像仓库的货物摆放记录一样,为程序代码和数据分配指定的空间. 2.C语言生成的段 C语言生成的段大致分为两大类:初始化和未初始化,已初始化的段含有真正的指令和数据 ...
- 跨线程访问UI控件时的Lambda表达式
工作中经常会用到跨线程访问UI控件的情况,由于.net本身机制,是不允许在非UI线程访问UI控件的,实际上跨线程访问UI控件还是 将访问UI的操作交给UI线程来处理的, 利用Control.Invok ...
- APDU报文【转】
本文转载自:http://www.cnbolgs.com/snail0404/p/5436348.html APDU # APDU # 定义:APDU(ApplicationProtocolDat ...
- YTU 2697: 血型统计
2697: 血型统计 时间限制: 1 Sec 内存限制: 128 MB 提交: 405 解决: 164 题目描述 黑猫警长在犯罪现场发现了一些血迹,现已经委托检验机构确定了血型,需要统计各种血型的 ...
- 美国诚实签经验——我们准备了XX万的存款,这足以应付我预算表中涉及的所有开支,如果有意外情况我们的资金不足以应付,我们双方的父母也会支援我们,绝对不会占用美国的任何福利
闲话少絮,直接分享本人诚实签的经验,希望对有此打算的朋友有帮助! 个人经验有限,经历仅供参考~~ 第一步:决定赴美生子. 第二步:选择诚实签. 第三步:填写DS160表格(网址https://ceac ...
- Cpp module
- E20171226-hm
stack n.栈 heap n.堆 backtracking 回溯法,後戻り storage n. 贮存; 贮藏; 储藏处,仓库; 贮存器,蓄电(瓶); ストレージ
- J20170602-ts
アソシエーション association n. 联想; 协会,社团; 联合,联系; アンケート 英文是questionnaire
- ngCordova插件说明
转载自 http://my.oschina.net/u/1416844/blog/495026 参 考http://blog.csdn.net/superjunjin/article/details/ ...