码迷,mamicode.com
首页 > 其他好文 > 详细

CF 455A Boredom

时间:2019-02-21 21:53:11      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:ase   tran   lib   otto   key   ucid   font   注意   nta   

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]
考虑取任意一个数x时只会影响到x+1和x−1,我们可以先设dp[i]表示选取num后可以取得的最大值。因为任意取两个数a和b,若选取a后可以选取b,则选取b后可以选取a,因此我们只考虑x与x−1之间的关系。这样我们就很容易得到递推式:
技术图片

 

注意,最后一重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

标签:ase   tran   lib   otto   key   ucid   font   注意   nta   

原文地址:https://www.cnblogs.com/shenben/p/10415251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!