Codeforces 456C - Boredom(简单DP)

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.

Examples

input

21 2

output

2

input

31 2 3

output

4

input

91 2 1 3 2 2 2 2 3

output

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.

 题意:

  每次从n个数中选中数值为Ak将其删去,并将值为Ak-1的数和值为Ak+1的数全部删去,得到Ak的分数。问最大可获得的分数。

题解:

  DP一下就好了。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
long long cnt[maxn],dp[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            cnt[x]++;
        }
        dp[0]=0,dp[1]=cnt[1];
        long long ans=0;
        for(int i=2;i<maxn;i++)
        {
            dp[i]=max(dp[i-1],dp[i-2]+cnt[i]*i);
            ans=max(ans,dp[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-03 06:56:33

Codeforces 456C - Boredom(简单DP)的相关文章

Codeforces 41D Pawn 简单dp

题目链接:点击打开链接 给定n*m 的矩阵 常数k 下面一个n*m的矩阵,每个位置由 0-9的一个整数表示 问: 从最后一行开始向上走到第一行使得路径上的和 % (k+1) == 0 每个格子只能向或走一步 求:最大的路径和 最后一行的哪个位置作为起点 从下到上的路径 思路: 简单dp #include <cstdio> #include <algorithm> #include<iostream> #include<string.h> #include &

Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. 简单dp,dp[i]表示取i时zui最大和为多少,方程为dp[i] = max(dp[i - 1] , dp[i - 2] + cont[i]*i). 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef __int64 LL;

Codeforces 698A - Vacations - [简单DP]

题目链接:http://codeforces.com/problemset/problem/698/A 题意: 有 $n$ 天假期,每天有四种情况:0.体育馆不开门,没有比赛:1.体育馆不开门,有比赛:2.体育馆开门,没有比赛:3.体育馆开门,有比赛. 每天都可以选择一件事做:休息.去体育馆运动.打比赛. 现在有一个限制条件:不能连续两天都去体育馆,或者连续两天都打比赛.要求尽量使得休息的天数最少,求出这个天数. 题解: $f[i][0,1,2]$ 表示前 $i$ 天,第 $i$ 天休息/运动/

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

CodeForces 30C Shooting Gallery 简单dp

题目链接:点击打开链接 给定n个气球 下面n行 x y t val 表示气球出现的坐标(x,y) 出现的时刻t,气球的价值val 枪每秒移动1个单位的距离 问: 射击的最大价值,开始时枪瞄准的位置任意. 思路: dp一下.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set

codeforces Gym 100500H A. Potion of Immortality 简单DP

Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen Un

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 <

POJ 3250 Bad Hair Day 简单DP 好题

Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a sp