CF456C Boredom (DP)

时间:2022-12-23 10:05:58

CF#260 div2 C. Boredom

Codeforces Round #260

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

题意:给出一堆数,一次操作是取某个数,然后等于(它+1)或(它-1)的数全删掉,得到(它)分,求最高得分。

有两种dp方法,一种排序后dp[i][j]表示取到第i个得到的最大分,j表示取或不取;

另一种是我用的,dp[i]表示在1~i的数中取,必取i,得到的最高分,从2扫到100000。

            dp[i]=dp[i-];
if(a[i]>)dp[i]=max(dp[i],dp[i-]+1LL*i*a[i]);

注意10^5 * 10^5= 10^10会爆long long,我就是这样掉分飞起的。

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout) const int maxn=;
int a[maxn];
ll dp[maxn]; int main(){
ll ma;
int n,i,x;
while(scanf("%d",&n)!=EOF){
mz(a);
for(i=;i<n;i++){
scanf("%d",&x);
a[x]++;
}
dp[]=;
dp[]=a[];
ma=dp[];
for(i=;i<=;i++)
{
dp[i]=dp[i-];
if(a[i]>)dp[i]=max(dp[i],dp[i-]+1LL*i*a[i]);
if(dp[i]>ma)ma=dp[i];
}
printf("%I64d\n",ma);
}
return ;
}

CF456C Boredom (DP)的更多相关文章

  1. Codeforces Round &num;260 &lpar;Div&period; 2&rpar;C&period; Boredom(dp)

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

  2. Codeforces Round &num;260 &lpar;Div&period; 1&rpar; 455 A&period; Boredom (DP)

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

  3. CodeForces 455A&Tab;Boredom (DP)

    Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...

  4. Codeforces Round &num;260 &lpar;Div&period; 1&rpar; Boredom(DP)

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

  5. codeforces &num;260 DIV 2 C题Boredom(DP)

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

  6. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  7. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

随机推荐

  1. &lbrack;NHibernate&rsqb;事务

    目录 写在前面 文档与系列文章 事务 增删改查 总结 写在前面 上篇文章介绍了nhibernate的增删改查方法及增加修改操作,这篇文章将介绍nhibernate的事务操作. SQL Server中的 ...

  2. 关于java序列化中的一个细节

    java序列化机制的可以参考很多资料了,最近在看的时候发现了一些问题. 1. 默认的序列化机制,很多书里讲到序列化类只序列化类名,实例变量,不会实例化类变量(static)和瞬态变量(transien ...

  3. C&plus;&plus;数组小知识

    数组大小 我们一般情况下可以使用sizeof(数组名)/sizeof(数组元素)求数组元素个数,但需要注意的是,当我们需要调用函数处理数组时,数组的长度要在调用函数之前获取,因为调用函数的时候,数组退 ...

  4. MF-800U

    MF-800U 价格:200元左右 https://item.taobao.com/item.htm?spm=a230r.1.14.6.kfkqoY&id=4963072384&ns= ...

  5. SQLSERVER中按年月分组

    SQLSERVER中按年月分组 一个表有三个字段id,dt,d  分别存放id,时间,数值  id    dt    d 1 2004-08-11 12:12:00.000 9  2 2005-09- ...

  6. trash目录: ~&sol;&period;local&sol;share&sol;Trash

    trash目录:~/.local/share/Trash

  7. Linux系统安装VM-Tools

    安装 vmware-tools的安装包有两个,一个是rpm包,一个是tar包,下面分别是用了这两种方法安装: 一.rpm包安装 1.在启动LINUX 虚拟机之后,在WMWare 的菜单栏中点击&quo ...

  8. SQL AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  9. Linux block&lpar;1k&rpar; block&lpar;4k&rpar; 换算 gb

    输入 df  显示1k blocks  大小   再输入  df -h  显示 gb换算大小  结论 block(1k) 计算公式为:  block(1k)   /1024/1000  = xx gb ...

  10. 优化 SQL SELECT 语句性能

    SELECT语句的性能调优有时是一个非常耗时的任务,在我看来它遵循帕累托原则.20%的努力很可能会给你带来80%的性能提升,而为了获得另外20%的性能提升你可能需要花费80%的时间. 检查索引:在SQ ...