#include <bits/stdc++.h>
using namespace std;
void printVector(vector<int> v) {
  char ch = ' ';
  for (int i = 0; i < v.size(); i++) {
    printf("%d", v[i]);
    printf("%c", ch);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  stack<int> s;
  vector<int> diff;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      s.push(v[i]);
    else {
      if (v[i] < s.top()) {
        diff.push_back(s.top() - v[i]);
      } else {
        s.push(v[i]);
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < diff.size(); i++) {
    sum += diff[i];
  }
  cout << sum;
  return 0;
}
