72 lines
1.4 KiB
Java
72 lines
1.4 KiB
Java
package chelper;
|
|
|
|
import io.InputReader;
|
|
import io.OutputWriter;
|
|
import misc.SimpleSavingChelperSolution;
|
|
|
|
|
|
public class H extends SimpleSavingChelperSolution {
|
|
|
|
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
|
wrapSolve(testNumber, in, out);
|
|
}
|
|
|
|
@Override
|
|
public void solve(int testNumber) {
|
|
int n = in.nextInt();
|
|
long[] a = in.nextLongArray(n);
|
|
|
|
long[] needL = new long[n];
|
|
long[] endL = new long[n];
|
|
|
|
long[] needR = new long[n];
|
|
long[] endR = new long[n];
|
|
|
|
needL[0] = 0;
|
|
endL[0] = a[0];
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
endL[i] = Math.max(endL[i - 1] + 1, a[i]);
|
|
needL[i] = needL[i - 1] + endL[i] - a[i];
|
|
}
|
|
|
|
needR[n - 1] = 0;
|
|
endR[n - 1] = a[n - 1];
|
|
|
|
for (int i = n - 2; i >= 0; i--) {
|
|
endR[i] = Math.max(endR[i + 1] + 1, a[i]);
|
|
needR[i] = needR[i + 1] + endR[i] - a[i];
|
|
}
|
|
|
|
long min = Long.MAX_VALUE;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
{
|
|
long cost = needL[i];
|
|
if (i < n - 1) {
|
|
cost += needR[i + 1];
|
|
|
|
if (endL[i] == endR[i + 1]) {
|
|
cost++;
|
|
}
|
|
}
|
|
min = Math.min(min, cost);
|
|
}
|
|
|
|
{
|
|
long cost = needR[i];
|
|
if (i > 0) {
|
|
cost += needL[i - 1];
|
|
|
|
if (endL[i - 1] == endR[i]) {
|
|
cost++;
|
|
}
|
|
}
|
|
min = Math.min(min, cost);
|
|
}
|
|
}
|
|
|
|
out.println(min);
|
|
}
|
|
}
|