#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;
int main() {
  int n;
  cin >> n;
  vector<int> stu(n);
  for (int i = 0; i < (int)(n); i++) cin >> stu[i];
  int tmp = 0, sum = 0;
  for (int i = 1; i < n; i++) {
    if (stu[i] < stu[i - 1]) {
      tmp = stu[i - 1] - stu[i];
      stu[i] += tmp;
      sum += tmp;
    }
  }
  cout << sum << endl;
  return 0;
}
