#include <bits/stdc++.h>
using namespace std;
int n, ans;
const int SIZE = 2e5 + 7;
int a[SIZE];
int main() {
  cin >> n;
  for (int(i) = 0; (i) < (int)(n); (i)++) cin >> a[i];
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
