#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
void solve() {
  int n, a[MAX], sum = 0;
  cin >> n;
  a[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] < a[i - 1]) sum += (a[i - 1] - a[i]), a[i] = a[i - 1];
  }
  cout << sum << endl;
}
int main() {
  int _ = 1;
  while (_--) {
    solve();
  }
  return 0;
}
