#include <bits/stdc++.h>
using namespace std;
const long long INFL = 1LL << 60;
const int INF = 1000000005;
const int MAX = 300000;
const int MOD = 1000000007;
struct Graph {
  int to, coin;
  long long cost;
};
struct Edge {
  int from, to, coin;
  long long cost;
};
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); }
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int pre = -1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ans += max(0, pre - a);
    pre = a + max(0, pre - a);
  }
  cout << ans << endl;
}
