import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); sa = br.readLine().split(" "); int[] a = new int[n]; Set set = new HashSet<>(); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); if (a[i] <= n) { set.add(a[i]); } } br.close(); long ans = 0; int goal = n; for (int i = n - 1; i >= 0; i--) { while (set.contains(goal)) { set.remove(goal); goal--; } if (goal == 0) { break; } ans += a[i] - goal - (n - set.size() - 1); set.add(goal); } System.out.println(ans); } }