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 a1a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a single integer — the maximum number of points that Alex can earn.

Examples
input

Copy
2
1 2
output

Copy
2
input

Copy
3
1 2 3
output

Copy
4
input

Copy
9
1 2 1 3 2 2 2 2 3
output

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

【题意】

给你一个数组a,里面有n个整数。你每次可以选择数组中的一个元素ak,从数组中删掉它,再删掉所有值等于ak + 1 或者 ak - 1的元素,这样你可以得到 ak 分。你可以重复进行多次该操作,请问你最后最多能得多少分?

【分析】

先求出数列中每一个数字k的出现次数num[k]

状态转移方程:如果取得第i-1个数,那么第i-2和第i个数均不可取;反之可取得第i和第i-2个数

第i个状态的值为 (取得第i-1个数的得分) 与 (取得第i-2个数得分和取得当前数的得分之和) 的最大值

注意,最后一重for循环要从2循环至已知的maxn


【代码】

#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e5+5;
inline int read(){
register int x=0;register char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x;
}
int n,mx;long long cnt[N],f[N];
int main(){
n=read();
for(int i=1,x;i<=n;i++) mx=max(mx,x=read()),cnt[x]+=x;
f[1]=cnt[1];
for(int i=2;i<=mx;i++) f[i]=max(f[i-1],f[i-2]+cnt[i]);
printf("%I64d\n",f[mx]);
return 0;
}

 

CF 455A Boredom的更多相关文章

  1. CodeForces 455A Boredom (DP)

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

  2. CF 455A(Boredom-dp)

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

  3. Codeforces 455A Boredom (线性DP)

    <题目链接> 题目大意:给定一个序列,让你在其中挑选一些数,如果你选了x,那么你能够得到x分,但是该序列中所有等于x-1和x+1的元素将全部消失,问你最多能够得多少分. 解题分析:从小到大 ...

  4. Codeforces 455A - Boredom - [DP]

    题目链接:https://codeforces.com/problemset/problem/455/A 题意: 给出一个 $n$ 个数字的整数序列 $a[1 \sim n]$,每次你可以选择一个 $ ...

  5. Codeforces 455A Boredom 取数字的dp

    题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失.即无法获得这2个数的分数 问最高得分. 先统计每一个数出现的次数.然后dp一 ...

  6. codeforces每日一题1-10

    目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...

  7. CF456C Boredom (DP)

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

  8. 蒟蒻修养之cf橙名计划

    因为太弱,蒟蒻我从来没有上过div1(这就是今年的最后愿望啊啊啊啊啊)已达成................打cf几乎每次都是fst...........所以我的cf成绩图出现了惊人了正弦函数图像.. ...

  9. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

随机推荐

  1. css实现图片横向排列滚动

    .imageList{ overflow-x: auto; overflow-y: hidden; height:180px; white-space: nowrap; img{ width:auto ...

  2. JS中 try...catch...finally (转)

    JS的try..catch..finally var array = null; try { document.write(array[0]); } catch(err) { document.wri ...

  3. Java面试经典题:线程池专题

    1.什么是线程池 线程池的基本思想是一种对象池,在程序启动时就开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池, ...

  4. 2 URL的玩法

    preface 这里我主要说说flask的URL玩法 include: 动态URL规则 自定义URL转换器 HTTP方法 唯一的URL 构造URL 跳转和重定向 动态URL规则 URL规则可以添加变量 ...

  5. C#调用Delphi的dll之详解

    C#调用Delphi接口方法,有两种解决办法: 一.将Delphi程序编译成一个COM组件,然后在C#里引用COM组件. 二.非托管调用Dephi的DLL文件. 这里我们主要讲解一下第二种方法,讲第二 ...

  6. VS2017 Pro未能找到路径“……\bin\roslyn\csc.exe”的解决方案

    VS2017改用roslyn编译的,新的roslyn编译器,支持c# 6.0语法.它放到bin里面去是为了支持asp.net应用的动态编译. 它是通过nuget的包Microsoft.CodeDom. ...

  7. CSS3制作美丽的3D表单

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  8. SpringMVC -- 梗概--源码--壹--springMVC json处理

    附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...

  9. InsertSql

    declare @hobby table(hobbyID int,hName nvarchar(100));insert into @hobby(hobbyID,hName)Select 1,'爬山' ...

  10. 线程同步 –Mutex和Semaphore

    上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...