#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
int main() {
  int N, S = 0;
  cin >> N;
  int a[200000];
  for (int i = 0; i < N; i++) cin >> a[i];
  int M = 0;
  for (int i = 0; i < N; i++) {
    if (M > a[i])
      S += (M - a[i]);
    else
      M = a[i];
  }
  cout << S << endl;
  return 0;
}
