没事刷刷题之三(简单英文题)I - Boredom

I - Boredom 

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.

Example
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
 
     
 
     
简单的动态规划,一开始我们假定选取最优解中选取了m个点,把这些点命名为最优点,其中一个点a(i),我们找其上一个最优点,那么上一个最优点必定在a(i - 3),a(i - 2)之间产生,至于为什么会这样呢,我们假设上一个最优点是a(i - 4),那么,显然a(i - 2)是可取的点,且若要达到最优则必定要取,对于a(i - 3)是最优点证明类似。由此,我们假定s(n)表示在前n个点中得到的最大值,我们得到一个程式:
s(i) = max(s(i),s(i - 1),s(i - 2) + a(i),s(i - 3) + a(i)),而因为对s(i) 与 s(i - 1)之间选取了两者中的最优解,那么对于s(i - 2) + a(i)与s(i - 3)+a(i)的比较是多余的,这是因为s(i - 2)包含了对于s(i - 3)的取法,那么我们最终得到s(i) = max(s(i),s(i - 1),s(i - 2) + a(i)).
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
long long a[100001], s[100001];
int main() {
	long long n;
	while (scanf("%lld",&n) != EOF) {
		long long k,Max = -1;
		memset(a, 0, sizeof(a));
		memset(s, 0, sizeof(s));
		while (n--) {
			scanf("%lld", &k);
			Max = max(Max, k);
			a[k] += k;
		}
		s[1] = a[1];
		s[2] = max(a[1], a[2]);
		for (int i = 3; i <= Max; i++) {
			s[i] = max(s[i], s[i - 2] + a[i]);
			s[i] = max(s[i], s[i - 1]);
		}
		cout << s[Max] << endl;
	}
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值