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的更多相关文章
随机推荐
- javaweb_page指令
jsp指令: 1.作用:jsp指令是为jsp引擎设计的.他们并不直接产生不论什么课件输出.而是告诉引擎怎样处理jsp页面中的其余部分 2.jsp指令包含:page指令.include指令,taglib ...
- 记录一次node中台转发表单上传文件到后台过程
首发掘金 记录一次node中台转发表单上传文件到后台过程 本篇跟掘金为同一个作者leung 公司几个项目都是三层架构模式即前台,中台(中间层),后台.前台微信端公众号使用vue框架,后台管理前端使 ...
- Android系统之路(初识MTK) ------ OTA打包ROM安装系统img等到ZIP
在做OTA升级包的时候,我编译了好多次都没过.老是IO异常.刚開始以为是我 make 的错误.后来多次检查 Error 发现是我的配置信息写错了,与驱动project师一起检查源代码, 改动配置信息后 ...
- 华为云分布式数据库中间件DDM和开源MyCAT对比
前言 华为云分布式数据库中间件(Distributed Database Middleware)是解决数据库容量.性能瓶颈和分布式扩展问题的中间件服务,提供分库分表.读写分离.弹性扩容等能力,应对海量 ...
- Matplotlib作图基础
折线图 import matplotlib.pylab as pylab import numpy as npy x=[1,2,3,4,8] y=[5,7,2,1,5] #折线图 pylab.plot ...
- charset='utf8mb4'
charset='utf8mb4' conn = pymysql.connect(host=h, port=pt, user=u, passwd=p, db=db, charset='utf8mb4' ...
- python 三维坐标图
绘制3D柱状图,其数据格式为,二维数组或三维数组. from numpy import * file=open('C:\\Users\\jyjh\\Desktop\\count.txt','r') a ...
- USACO 刷题有感
最近每天都尽量保持着每天一道USACO的题目,呵呵,一开始都是满简单的,一看题目基本上思路就哗啦啦地出来了,只不过代码实现有点麻烦,花了一些时间去调试,总体感觉还不错,但是越往后做,应该就很难保持一天 ...
- E20180119
Foundation n. 基础; 地基; 粉底; 基金(会); hybrid n. 杂种; 杂交生成的生物体; 混合物; 混合词; adj. 混合的; 杂种的;
- python实现汉诺塔程序
# 汉诺塔思想笔记# 认识汉诺塔的目标:把A柱子上的N个盘子移动到C柱子# 递归的思想就是把这个目标分解成三个子目标# 子目标1:将前n-1个盘子从a移动到b上# 子目标2:将最底下的最后一个盘子从a ...