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