题目链接:http://codeforces.com/problemset/problem/455/A

A. Boredom
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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 test(s)
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
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,

求最大得分的值。

思路:

存下每一个数的个数放在c中。消除一个数i,会获得c[i]*i的值(由于能够消除c[i]次),

假设从0的位置開始向右消去,那么。消除数i时。i-1可能选择了消除。也可能没有,

假设消除了i-1,那么i值就已经不存在,dp[i] = dp[i-1]。

假设没有被消除,那么dp[i] = dp[i-2]+ c[i]*i。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL __int64
const int MAXN = 100017;
LL c[MAXN], dp[MAXN];
void init()
{
memset(dp,0,sizeof(dp));
memset(c,0,sizeof(c));
}
int main()
{
LL n;
LL tt;
int i, j;
while(~scanf("%I64d",&n))
{
init();
int maxx = 0;
for(i = 1; i <= n; i++)
{
scanf("%I64d",&tt);
if(tt > maxx)
maxx = tt;
c[tt]++;
}
dp[0] = 0, dp[1] = c[1];
for(i = 2; i <= maxx; i++)
{
dp[i] = max(dp[i-1],dp[i-2]+c[i]*i);
}
printf("%I64d\n",dp[maxx]);
}
return 0;
}

Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)的更多相关文章

  1. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

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

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

  3. Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...

  4. Codeforces Round #245 (Div. 1) B. Working out (dp)

    题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...

  5. Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)

    C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. Codeforces Round #369 (Div. 2) C. Coloring Trees (DP)

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)

    题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...

  8. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  9. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

随机推荐

  1. angular4 select 绑定(ngModel)对象

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 <h1>My Application</h1> <select [(ngModel)]=& ...

  2. php 获取随机字符串(原创)

    //获取随机数字字母字符串 function get_rand_str($len=8){ $randArr=array_merge(range(0,9),range('a','z'),range('A ...

  3. libhiredis.so.0.13 => not found 缺少

    wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz tar -xzvf v0.13.3.tar.gz cd hiredis- ma ...

  4. 使用autofac在mvc5下依赖注入

    把遇到的问题汇总一下: 一.安装mvc5版本 命令:pm> Install-Package Autofac 结果安装的Autofac.Integration.Mvc(版本为4.0),所引用的依赖 ...

  5. WPF下DataGrid的简单应用

    Dim dt As New DataTable() '------------------- dt.Columns.Add(New DataColumn("名称")) dt.Col ...

  6. Socket server

    Socket server的使用方法(精华部分),仅供自用. class MyServer(socketserver.BaseRequestHandler): def handle(self): wh ...

  7. List 常用方法解析

    1.Count属性 (获得List中元素数目) 2.Add( ) 在List中添加一个对象的公有方法 3.AddRange( ) 公有方法,在List尾部添加实现了ICollection接口的多个元素 ...

  8. wpf ComboBox 获取选中项的文本内容

    一:根据数据源类型获取选中项 类: public class Region { public int REGION_ID { get; set; } public string REGION_CODE ...

  9. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

  10. java RPC系列之一 rmi

    java RPC系列之一    rmi 一.java RPC简单的汇总 java的RPC得到技术,基本包含以下几个,分别是:RMI(远程方法调用) .Caucho的Hessian 和 Burlap . ...