import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] sa = br.readLine().split(" "); int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(sa[i]); } br.close(); long ans = 0; int j = 0; for (int i = 0; i < n; i++) { while (b[j] == 0) { j++; } ans += Math.abs(i - j); b[j]--; } System.out.println(ans); } }