All problems

Problem 650

Delete and Earn

Medium · 20 pts · Dynamic Programming, Hash Table

Problem

In one operation you pick any element x of the array, earn x points, and delete it together with every element equal to x - 1 or x + 1. Repeat until the array is empty, and maximise your points. Once you take one copy of x, taking all copies is best, so total up the points for each value. Then it is House Robber over the values: you cannot take two adjacent values, so best(v) = max(best(v - 1), best(v - 2) + points(v)).

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the maximum number of points.

Constraints

1 <= N <= 20000; 1 <= A[i] <= 10000

Example input

6
2 2 3 3 3 4

Example output

9

Explanation

In 2 2 3 3 3 4, taking all three 3s earns 9 points and deletes the 2s and the 4. Taking the 2s and the 4 instead would earn only 8, so the answer is 9.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Largest Divisible Subset Next: Tallest Billboard