Codeforces Round菗260(Div.1)A-Boredom DP
6415 ワード
A.Boredom
Time Limit:20 Sec
メモリLimit:256 MB
タイトル接続
http://codeforces.com/contest/455/problem/A
Description
Alex doesn't like boredom.That's why whenever he gets bored,hecos up with games.One long witer evining he came up with a game and decided to play it.
Given a sequence a consisting of n integers.The pleyer can make several step s.In a single stephecan chose e e e e e e e e e e e e e of the sequence(let's denote it ak)and delete it,a that alaleleeeement t t t t t thererererererererererereeeeeeeeeemt t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t step brigs ak points to the player.
Alex is a perfectionst,so he decided to get as many points as possible.Help him.
Input
The first line contains integer n(1̵≦̴n̵≦105)that s how s how manumberss are in Alex's sequence.The second line n integersa 1,a 2,…,an(820)(1)
Output
Print a single integer-the maximnumber of points that Alex can earn.
Sample Input
21 2
Sample Output
2
HINT
題意
n個をあげます。毎回一つの数xを削除することができますが、x+1とx-1に等しい数は削除しなければなりません。
毎回の操作でx点が得られます。
クイズ:
dp,dp[i]はiまでの最大得点を表します。
dp[i]=max(dp[i-1],dp[i-2]+a[i-1]*(i-1);
コード:
Time Limit:20 Sec
メモリLimit:256 MB
タイトル接続
http://codeforces.com/contest/455/problem/A
Description
Alex doesn't like boredom.That's why whenever he gets bored,hecos up with games.One long witer evining he came up with a game and decided to play it.
Given a sequence a consisting of n integers.The pleyer can make several step s.In a single stephecan chose e e e e e e e e e e e e e of the sequence(let's denote it ak)and delete it,a that alaleleeeement t t t t t thererererererererererereeeeeeeeeemt t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t step brigs ak points to the player.
Alex is a perfectionst,so he decided to get as many points as possible.Help him.
Input
The first line contains integer n(1̵≦̴n̵≦105)that s how s how manumberss are in Alex's sequence.The second line n integersa 1,a 2,…,an(820)(1)
Output
Print a single integer-the maximnumber of points that Alex can earn.
Sample Input
21 2
Sample Output
2
HINT
題意
n個をあげます。毎回一つの数xを削除することができますが、x+1とx-1に等しい数は削除しなければなりません。
毎回の操作でx点が得られます。
クイズ:
dp,dp[i]はiまでの最大得点を表します。
dp[i]=max(dp[i-1],dp[i-2]+a[i-1]*(i-1);
コード:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//**************************************************************************************
ll a[100010],dp[100010];
int main()
{
int n=read();
for(int i=0;i<n;i++)
{
int x=read();
a[x]++;
}
for(int i=2;i<100010;i++)
dp[i]=max(dp[i-1],dp[i-2]+a[i-1]*(i-1));
cout<<dp[100009]<<endl;
}