Boredom

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121334#problem/G

Description

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.

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.

Sample Input

Input

2

1 2

Output

2

Input

3

1 2 3

Output

4

Input

9

1 2 1 3 2 2 2 2 3

Output

10

Hint

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.

##题意:

给出n个数,每次任意选择其中一个数Ai:
删除Ai(一个)以及所有的Ai-1 Ai+1; 此次删除操作得分为Ai;
问删除所有元素最多可以获得多少分.


##题解:

由于数组元素的范围是10^5,故可以排序后直接DP:
dp[i][0/1]分别表示删除i或不删除(由其他数删掉)所获得的最大分数.
转移方程:
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = dp[i-1][0] + cnt[i]*i;


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

int cnt[maxn];

LL dp[maxn][2];

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%d", &n) != EOF)
{
memset(cnt, 0, sizeof(cnt));
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
cnt[x]++;
} memset(dp, 0, sizeof(dp));
for(int i=1; i<=100000; i++) {
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = dp[i-1][0] + (LL)(cnt[i])*(LL)(i);
} LL ans = max(dp[100000][0], dp[100000][1]);
printf("%I64d\n", ans);
} return 0;

}

CodeForces 455A Boredom (DP)的更多相关文章

  1. 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 ...

  2. Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)

    题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...

  3. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  4. codeforces #260 DIV 2 C题Boredom(DP)

    题目地址:http://codeforces.com/contest/456/problem/C 脑残了. .DP仅仅DP到了n. . 应该DP到10w+的. . 代码例如以下: #include & ...

  5. Codeforces Round #260 (Div. 1) Boredom(DP)

    Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  6. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  7. CF456C Boredom (DP)

    Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...

  8. Codeforces 1110D Jongmah (DP)

    题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 解析:首先我们容易发现,我们 ...

  9. Codeforces 837D--Round Subset (DP)

    原题链接:http://codeforces.com/contest/837/problem/D 题意:在n个数字中,取k个数,使这些数的乘积后缀“0”的个数最大,输出后缀0的最大数量. 思路:显然只 ...

随机推荐

  1. Control Flow in Async Programs

    Control Flow in Async Programs You can write and maintain asynchronous programs more easily by using ...

  2. 修练8年C++面向对象程序设计之体会

    http://pcedu.pconline.com.cn/empolder/gj/c/0504/609482_1.html

  3. ios 开发中 developer tools access 总是要输入密码问题的解决

    我一直没有想法去解决这个问题:打开iphone模拟器的时候,老是弹出developer tools access 让我输入密码, 今天我在打开模拟器的时候又弹出这个对话框,我愤怒了,于是我在网上查了一 ...

  4. linux制作livecd

    执行: $sudo cp /home/jxg/backup-2011.01.05/backup2011.01.05.squashfs /home/jxg/livecd/casper/filesyste ...

  5. listview默认选择第一项,点击换子项背景图

    (不是大神,没有几百子项目,去你丫的) private int last_item_position ; @Override public void onItemClick(AdapterView&l ...

  6. 如何借助于UML完成我们对系统的设计?谈谈我的理解

    首先要说的是我对面向对象的理解,以及设计类的依据: http://www.cnblogs.com/xinchrome/p/4904931.html 理解了这篇文章,也就理解了现在要说的. 在面向对象编 ...

  7. Solr DIH以Mysql为数据源批量创建索引

    演示使用solr管理后台,以mysql为数据源,批量建索引的方法 测试于:Solr 4.5.1, mmseg4j 1.9.1, Jdk 1.6.0_45, Tomcat 6.0.37 | CentOS ...

  8. 基于Spring AOP实现对外接口的耗时监控

    AOP是Spring的核心,Spring不但自身对多种框架的集成是基于AOP,并且以非常方便的形式暴露给普通使用者.以前用AOP不多,主要是因为它以横截面的方式插入到主流程中,担心导致主流程代码不够清 ...

  9. Windows 编译器选项 Runtime Library

    https://msdn.microsoft.com/library/2kzt1wy3%28v=vs.100%29.aspx http://blog.csdn.net/ybxuwei/article/ ...

  10. replace() MySQL批量替换指定字段字符串

    mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...