(Replenishment CF 455A) Boredom (DP)

Principle link poke me ~

Subject to the effect

A game score: number to a string, takes out a number \ (S_I \) , deleting \ (a_i + 1 \) and \ (a_i-1 \) of all numbers, plus score \ (a_i \) . How to get the most number of points

Sample Input

2
1 2

Sample Output

2

Sample Input

3
1 2 3

Sample Output

4

Sample Input

9
1 2 1 3 2 2 2 2 3

Sample Output

10

Problem-solving ideas

This is a one-dimensional state dp, i and dispensing not take two state i
if i then takes dp [i] = dp [i -2] + cnt [i] * i
if i-1 is not taken then not possible is removed dp [i] = dp [i -1]

Sample Code


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;
long long dp[maxn];
long long cnt[maxn];

int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int MAX=0;
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;
        MAX=max(MAX,temp);
        cnt[temp]++;
    }
    dp[1] = cnt[1];
    for(int i=2; i <= MAX; i++)
        dp[i]=max(dp[i-1],dp[i-2]+i*cnt[i]);
    cout << dp[MAX] << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/cafu-chino/p/11862576.html