n = int(input())
l = [int(i) for i in input().split()][::-1]
count = 0
result = 0
while n>0:
    obj = l[0]
    t = 1
    for i in l[1:]:
        if obj==i:
            t+=1
    result+=obj * t
    count+=t
    l = l[t:]
    for i in range(0, n-t):
        l[i] = max(0, l[i] - t)
    n-=t
print(result)