import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
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());
long[] a = new long[n+2];
long[] change_bef = new long[n+2];
long[] change_aft = new long[n+2];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(st.nextToken());
change_bef[i] = change_bef[i - 1] + Math.max(a[i - 1] - a[i] + 1, 0);
}
// Build suffix array
for (int i = n; i > 0; i--) {
change_aft[i] = change_aft[i + 1] + Math.max(a[i + 1] - a[i] + 1, 0);
}
long ans = Long.MAX_VALUE;
// Compute the final answer
for (int i = 1; i <= n; i++) {
ans = Math.min(ans, Math.max(change_bef[i], change_aft[i]));
}
System.out.println(ans);
br.close();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |