Problems > Delete and Earn

Delete and Earn

Difficulty: 1600 3000 ms 128 MB

dp

Choosing value v earns v × (its count), but deletes every occurrence of v−1 and v+1 from the pool. Repeat until empty. Maximize the total earned.

Input: the first line has N; the second line has N positive integers.

Output: the maximum total.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ ai ≤ 104

Sample Tests

Input 1
3
3 4 2
Output 1
6
Input 2
5
2 2 3 3 3
Output 2
9

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.