import sys from collections import deque def main(): N = int(sys.stdin.readline()) P = list(map(int, sys.stdin.readline().split())) sorted_rounds = sorted(range(1, N+1), key=lambda x: -x) available_S = deque(range(1, N+1)) total = 0 for i in sorted_rounds: p_i = P[i-1] if available_S and available_S[-1] > p_i: total += i available_S.pop() else: if available_S: s = available_S.popleft() if s < p_i: total -= i print(total) if __name__ == "__main__": main()