#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 10;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  a[0] = 0;
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] >= a[i - 1]) continue;
    cnt += a[i - 1] - a[i];
    a[i] = a[i - 1];
  }
  cout << cnt;
  return 0;
}
