#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MX = 1e18;
const long double PI = acos(-1);
int main() {
  int n;
  cin >> n;
  int mx = 0;
  int step = 0;
  for (int i = 0; i < (n); ++i) {
    int a;
    cin >> a;
    if (a < mx) {
      step += (mx - a);
    } else {
      mx = a;
    }
  }
  cout << step << endl;
  return 0;
}
