codeforce 455A—— DP—— Boredom

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

Sample Input

Input

21 2

Output

2

Input

31 2 3

Output

4

Input

91 2 1 3 2 2 2 2 3

Output

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.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long  dp[100010][2];
int a[100010];
int temp[100010];
int n;
int main()
{
      while(~scanf("%d", &n)){
          memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
            temp[a[i]]++;
        }

        dp[1][1] = temp[1];
        dp[1][0] = 0;
        for(int i = 2; i <= 100000; i++){
            dp[i][1] = dp[i-1][0] + 1ll*temp[i]*i;
            dp[i][0] = max(dp[i-1][1], dp[i-1][0]);
        }

        printf("%lld\n", max(dp[100000][1], dp[100000][0]));
    }
    return 0;
}

  

时间: 2024-10-27 19:09:22

codeforce 455A—— DP—— Boredom的相关文章

CodeForce 113B DP

Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find t

codeforce 597C-Subsequences(dp+树状数组)

题目和南阳那道题一样链接http://www.cnblogs.com/zzuli2sjy/p/4943774.html 代码: 1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<iostream> 6 #include<map> 7 typedef long long ll; 8 const ll

Codeforces 455A Boredom (dp)

很裸的dp 状态转移方程 dp[i]=max(dp[i-1],dp[i-2]+dp[i]*i) #include<bits/stdc++.h> using namespace std; long long dp[100020]; int main() { int n,a; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a); dp[a]++; } for(int i=2;i&

Codeforces 455A Boredom 取数字的dp

题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失,即无法获得这2个数的分数 问最高得分. 先统计每个数出现的次数,然后dp一下,对于每个数只有取或不取2种状态. #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #include <cstring> #include <

cf 455A Boredom (dp)

455A 普及组dp题居然不会了,要尽快回到状态才行. 题意就是给定一个序列,然后玩家每次选择一个数并得该数值的分,并且选了\(a_i\)以后,值为\(a_{i}-1\)和\(a_{i}+1\)都不能选了.要求最大化得分. 这样的dp,和值域有关.考虑将值域作为状态. 设\(dp(i,1)\)为\([1,i]\)的数被处理,并且\(a_i\)被选的最大得分. \(dp(i,0)\)为\([1,i]\)的数被处理,并且\(a_i\)没被选的最大得分. 转移: \(dp(i,1)=dp(i-1,0)

Boredom easy dp

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 455A 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

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

题目链接:http://codeforces.com/problemset/problem/455/A 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

dp解Codeforces Round #260 (Div. 2)C. Boredom

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; lo

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

题目传送门 1 /* 2 DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 1e5 + 10; 12 const int INF