import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = sc.nextInt(); } sc.close(); TreeSet set = new TreeSet<>(); for (int i = 1; i <= n; i++) { set.add(i); } long ans = 0; for (int i = n - 1; i >= 0; i--) { Integer h = set.higher(p[i]); if (h != null) { set.remove(h); ans += i + 1; } else { Integer l = set.pollFirst(); if (l != p[i]) { ans -= i + 1; } } } System.out.println(ans); } }