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 ...
随机推荐
- Pthread 用法笔记
什么是线程? 从技术上讲,一个线程被定义为一个独立的指令流. 一个进程可以包含一个或多个线程. 线程操作包括线程创建,终止,同步(连接,阻塞),调度,数据管理和进程交互. 进程内的所有线程共享: 相同 ...
- react事件处理函数中绑定this的bind()函数
问题引入 import React, { Component } from 'react'; import { Text, View } from 'react-native'; export def ...
- linux18.04+jdk11.0.2+hadoop3.1.2部署伪分布式
1. 下载 安装hadoop3.1.2http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.1.2/hadoop-3.1.2.tar.gz 注意 ...
- beego框架的最简单登入演示
一.controllers逻辑代码 func (c *UserController) Get() { c.TplName="login.html" } func (c *UserC ...
- centos7搭建gitlab服务器、汉化
1.下载rpm安装包 https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.8.5-ce.1.el7.x86_64.rp ...
- Pandas系列(二)- DataFrame数据框
一.初识DataFrame dataFrame 是一个带有索引的二维数据结构,每列可以有自己的名字,并且可以有不同的数据类型.你可以把它想象成一个 excel 表格或者数据库中的一张表DataFram ...
- C#获取根目录的方法总结
1.控制台应用程序 static void Main(string[] args) { //1.Environment.CurrentDirectory Console.WriteLine(Envir ...
- C++ 实现简单命令行学生管理系统
C++ 实现简单命令行学生管理系统 预览: 编译环境是macOS.system("clear") 在windows下请换成 system("cls") #inc ...
- Django小技巧——使用package管理app
在一个学习视频上看见的小技巧,遂记录下来. 1. 如下图所示,项目中有多个app,本技巧要解决的问题是集中管理这多个app,将其放入一个package下集中管理,改善项目的视图环境 2. 建立一个pa ...
- Git上传文件容量大小限制
使用Git LFS突破git的100M容量限制. 1.安装Git LFS https://git-lfs.github.com./ 2.安装后把里面的git-lfs.exe放到你要上传的项目文件夹 3 ...