link to the question: http://mirror.codeforces.com/contest/455/problem/A
Source-code:
from collections import Counter import sys sys.setrecursionlimit(100000000)
raw_input() a = map(int, raw_input().split())
f = Counter(a)
memo = {} def dp(i): if i in memo: return memo[i] elif i <= 0: temp = 0 else: if i not in f: temp = max(dp(i-1), dp(i-2)) else: temp = i*f[i] + max(dp(i-2), dp(i-3)) memo[i] = temp return temp
print max(dp(max(a)), dp(max(a) — 1))