题目链接:CodeForces -456C

Description

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 Input

2

1 2

9

1 2 1 3 2 2 2 2 3

Sample Output

2

10

Hint

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.

题意

给你一串数,作为一个游戏参与者,你需要选择把一些数拿走以获得最大的分数,但是你不可以拿相邻的数字,比如你拿了3就不能拿2和4但是可以拿5,当然为了拿到尽可能多的分数,选择一个数就要把这个数都拿完。

题解:

DP中的水题,在输入时进行计数,算出每个数有几个,然后从1开始,计算从1开始到n个数之间能拿到的最大分数,状态转移式是DP[n]=max(DP[n-1],DP[n-2]+a[n]*n),前2个需要手算,剩下的O(n)跑一遍就可以了。

代码


#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std; typedef long long ll; const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 5;
long long a[100100];
long long dp[100100];
int main() {
int n;
long long t;
while (cin >> n) {
memset(a, 0, sizeof a);
for (int i(0); i <n; i++) {
cin >> t;
a[t]++;
}
dp[1] = a[1] * 1;
dp[2] = max(a[2] * 2, a[1]);
for (int i(3); i < 100100; i++) {
dp[i] = max(dp[i - 2] + a[i] * i, dp[i - 1]);
}
cout << dp[100099] << endl;
}
return 0;
}

CodeForces 456-C Boredom的更多相关文章

  1. Codeforces 260 C. Boredom

    题目链接:http://codeforces.com/contest/456/problem/C 解题报告:给出一个序列,然后选择其中的一个数 k 删除,删除的同时要把k - 1和k + 1也删除掉, ...

  2. codeforces 456 E. Civilization(并查集+数的直径)

    题目链接:http://codeforces.com/contest/456/problem/E 题意:给出N个点,M条边,组成无环图(树),给出Q个操作,操作有两种: 1 x,输出x所在的联通块的最 ...

  3. codeforces 456 D. A Lot of Games(字典数+博弈+思维+树形dp)

    题目链接:http://codeforces.com/contest/456/problem/D 题意:给n个字符串.进行k次游戏.每局开始,字符串为空串,然后两人轮流在末尾追加字符,保证新的字符串为 ...

  4. [Codeforces Round #433][Codeforces 853C/854E. Boredom]

    题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...

  5. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

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

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

  7. Codeforces Round #456 (Div. 2)

    Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...

  8. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  9. 递推DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

随机推荐

  1. springboot logback 相关使用

    参考: https://www.cnblogs.com/EasonJim/p/9159195.html https://blog.csdn.net/tianyaleixiaowu/article/de ...

  2. [转] 一张图理解prototype、proto和constructor的三角关系

    前面的话 javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则是prototype.proto和constructor ...

  3. 前端:Jquery 处理同一Name的Radio组时,绑定checked属性异常的问题.(已解决)

    将: $("input[type=radio][name=optionsContractGroup][value=201]").attr("checked",t ...

  4. 最长k可重区间集问题&&最长k可重线段集问题

    题解: 洛谷上这两题的题意都是有问题的 按照标程题意不应该是开区间而是左开右闭区间 然后连边比较巧妙 我们可以看成选k条不相交的路径,其中i-i+1中有k条边 所以建图i-i+1流量为k,权值为0 l ...

  5. 从oracle导出数据成csv,将csv导入mongodb问题

  6. day84-仿照admin实现一个自定义的增删改查组件

    一.admin的使用 app01的admin.py文件: class BookConfig(admin.ModelAdmin): list_display=[] list_display_links= ...

  7. python--json、jsonpath

    1.遇到一个问题:android返回的基本都是标准的json格式,当我们想要对层层嵌套的json中找到自己想要的字段并进行校验时 难道需要一层一层的解析?? 2.使用jsonpath list_3={ ...

  8. 查找常用字符(给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。)

    给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表. 例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 ...

  9. gradle上传本地文件到远程maven库(nexus服务器)

    自定义aar-upload.gradle文件 artifacts { archives file('./build/outputs/aar/Lib_ads-baidu-debug.aar') } up ...

  10. Stall Reservations POJ - 3190(贪心)

    Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked ...