#include <bits/stdc++.h>
using namespace std;
template <class TAT>
inline void read(TAT &a) {
  static char cc;
  static bool f;
  while ((cc = getchar()) != '-' && (cc < '0' || cc > '9'))
    ;
  if (cc == '-')
    f = 1, a = 0;
  else
    f = 0, a = cc - '0';
  while ((cc = getchar()) >= '0' && cc <= '9') a = a * 10 + cc - '0';
  if (f) a = -a;
}
template <class TAT>
inline void write(TAT a) {
  static char cc[27];
  static int ct;
  if (a == 0) {
    putchar('0');
    return;
  }
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  ct = 0;
  while (a) cc[++ct] = a % 10 + '0', a /= 10;
  while (ct) putchar(cc[ct--]);
}
template <class TAT>
inline void Ckmin(TAT &a, const TAT &b) {
  if (a > b) a = b;
}
template <class TAT>
inline void Ckmax(TAT &a, const TAT &b) {
  if (a < b) a = b;
}
void begin() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void end() {
  fclose(stdin);
  fclose(stdout);
}
const int maxn = 3115;
int n, S, T;
int A[maxn];
long long Ans = 0;
struct edge {
  int to, next, cost;
  long long flow;
  edge() {}
  edge(int _t, int _n, long long _f, int _c)
      : to(_t), next(_n), flow(_f), cost(_c) {}
} e[maxn * 11];
int head[maxn], et = -1;
int dist[maxn];
int from[maxn];
void Init() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(A[i]);
  }
}
void Addedge(int fr, int to, long long fl, int co) {
  e[++et] = edge(to, head[fr], fl, co), head[fr] = et;
  e[++et] = edge(fr, head[to], 0, -co), head[to] = et;
}
void Build() {
  S = 0;
  T = n;
  memset(head, -1, sizeof(head));
  for (int i = 1; i < n; ++i) {
    if (A[i + 1] > A[i] + 1) {
      Addedge(S, i, A[i + 1] - A[i] - 1, 0);
    } else if (A[i + 1] < A[i] + 1) {
      Addedge(i, T, A[i] + 1 - A[i + 1], 0);
    }
  }
  Addedge(S, 1, 1e16, 1);
  Addedge(S, n - 1, 1e16, 1);
  for (int i = 1; i < n - 1; ++i) {
    Addedge(i, i + 1, 1e16, 1);
    Addedge(i + 1, i, 1e16, 1);
  }
}
void Spfa() {
  const int maxl = 3e3 + 115;
  static int line[maxl + 115];
  static bool ever[maxn];
  int h = 0, t = 0;
  memset(dist, 0x3f, sizeof(dist));
  memset(ever, 0, sizeof(ever));
  dist[S] = 0;
  ever[S] = 1;
  line[h++] = S;
  while (h != t) {
    int u = line[t];
    ever[u] = 0;
    t = (t == maxl) ? 0 : (t + 1);
    for (int i = head[u]; i != -1; i = e[i].next) {
      if (e[i].flow > 0 && dist[u] + e[i].cost < dist[e[i].to]) {
        dist[e[i].to] = dist[u] + e[i].cost;
        from[e[i].to] = i;
        line[h] = e[i].to;
        if (dist[line[h]] < dist[line[t]]) swap(line[h], line[t]);
        h = (h == maxl) ? 0 : (h + 1);
      }
    }
  }
}
void Augment() {
  long long Flow = 2e18;
  int Cost = 0;
  int x;
  x = T;
  while (x != S) {
    Ckmin(Flow, e[from[x]].flow);
    Cost += e[from[x]].cost;
    x = e[from[x] ^ 1].to;
  }
  x = T;
  while (x != S) {
    e[from[x]].flow -= Flow;
    e[from[x] ^ 1].flow += Flow;
    x = e[from[x] ^ 1].to;
  }
  Ans += Flow * Cost;
}
void Solve() {
  if (n == 1)
    Ans = 0;
  else {
    while (1) {
      Spfa();
      if (dist[T] > 1e9) break;
      Augment();
    }
  }
  write(Ans);
  putchar('\n');
}
int main() {
  Init();
  Build();
  Solve();
  return 0;
}
