#include <bits/stdc++.h>
using namespace std;
class input {
 private:
  bool fail;
  istream* stream;

 public:
  input(istream& stream_ = cin) { stream = &stream_; }
  istream& getStream() { return *stream; }
  template <typename T = int>
  T read() {
    T value;
    if (*stream >> value) {
      fail = false;
    } else {
      fail = true;
    }
    return value;
  }
  template <typename T = int>
  vector<T> readArray() {
    size_t size = read<size_t>();
    return readArray<T>(size);
  }
  template <typename T = int>
  vector<T> readArray(size_t size) {
    vector<T> v(size);
    for (size_t i = 0; i < size; i++) {
      if (*stream >> v[i]) {
        fail = false;
      } else {
        fail = true;
      }
    }
    return v;
  }
  bool isFailed() { return fail; }
  static input& getStdin() {
    static input in(cin);
    return in;
  }
};
class output {
 private:
  ostream* stream;

 public:
  output(ostream& stream_ = cout) { stream = &stream_; }
  ostream& getStream() { return *stream; }
  template <typename T>
  output& put(T& value) {
    *stream << value;
    return *this;
  }
  template <typename T>
  output& put(T& value, int precision) {
    *stream << setprecision(precision) << value;
    return *this;
  }
  output& flush() {
    stream->flush();
    return *this;
  }
  template <typename T>
  output& write(const T& value) {
    *stream << value << endl;
    return *this;
  }
  template <typename T>
  output& write(T& value, int precision) {
    *stream << setprecision(precision) << fixed << value << endl;
    return *this;
  }
  template <typename T>
  output& writeArray(const vector<T>& v, bool appendSize = true) {
    if (appendSize) {
      write(v.size());
    }
    for (auto& x : v) {
      *stream << x << " ";
    }
    *stream << endl;
    return *this;
  }
  output& writeLine() {
    *stream << endl;
    return *this;
  }
  output getStdout() {
    output out(cout);
    return out;
  }
  output getStderr() {
    output err(cerr);
    return err;
  }
};
void solve(input in, output out, output err) {
  auto a = in.readArray();
  vector<int> b(a.size());
  for (auto i = 1; i < a.size(); i++) {
    b[i] = a[i];
    if (b[i - 1] > b[i]) {
      b[i] = b[i - 1];
    }
  }
  for (auto i = 1; i < a.size(); i++) {
    auto j = a.size() - i - 1;
    if (b[j] < b[j + 1] - 1) {
      b[j] = b[j + 1] - 1;
    }
  }
  auto ans = 0LL;
  for (auto i = 0; i < a.size(); i++) {
    ans += b[i] - a[i];
  }
  out.write(ans);
}
int main() {
  input in(cin);
  output out(cout);
  output e(cerr);
  while (true) {
    auto startT = clock();
    solve(in, out, cerr);
    auto endT = clock();
    cerr << endl;
    cerr << "Working time: " << (endT - startT) << " ms" << endl;
    cerr << "-------------------------" << endl;
    bool ok = false;
    while (!in.isFailed() && !ok) {
      ok = in.read<char>() == '$';
    }
    if (!ok) {
      break;
    }
  }
}
