nya_n = int(raw_input()) buylist = [] bought = 0 discount = 0 while nya_n > 0: buylist.append(int(raw_input())) nya_n -= 1 buylist = sorted(buylist) nya_n = 0 while nya_n < len(buylist): buyval = buylist[nya_n] if buyval - discount > 0: bought = bought + buyval - discount % 1000 discount += buyval nya_n += 1 print bought