Java教程

2021-7-17 Boredom

本文主要是介绍2021-7-17 Boredom,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

难度 1500

题目 CodeForces:

Boredom time limit per test 1 second memory limit per test 256 megabytes

  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.

KeyWord

boredom 无聊

perfectionist 完美主义者

 

题目解析

本题大意是对给出的n个数进行操作,使得最后得到的分数尽可能高,操作包括去掉一个元素,然后去掉数组内等于该元素-1和+1的所有元素,并使分数加上该元素的值。

这题是简单的dp,所以直接看代码

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll x[100005] = { 0 }, y[100005] = { 0 };
ll n,ans;
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int t; cin >> t;
        x[t]++;
    }
    y[0] = 0;
    y[1] = x[1];
    for (int i = 2; i < 100005; i++)
    {
        y[i] = max(y[i - 1], y[i - 2] + x[i] * i);
        ans = max(ans, y[i]);
    }
    cout << ans<<endl;
    return 0;
}

 

 

这篇关于2021-7-17 Boredom的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!