隐藏页面特效

CF 455A Boredom

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; }

 

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/10415251.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2017-02-21 [SPOJ705]不同的子串
2017-02-21 [JLOI2014]松鼠的新家
2017-02-21 [HAOI2012]外星人
2017-02-21 [HAOI2012]音量调节
2017-02-21 [HAOI2012]外星人
点击右上角即可分享
微信分享提示