#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, nxt = 0, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (a < nxt)
      cnt += nxt - a;
    else
      nxt = a;
  }
  cout << cnt << endl;
}
