#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  vector<int> v1;
  int a = v[0];
  for (int i = 0; i < v.size(); i++) {
    if (a > v[i]) {
      int b = a - v[i];
      v1.push_back(b);
    } else {
      a = v[i];
      v1.push_back(0);
    }
  }
  int sum = 0;
  for (int i = 0; i < v1.size(); i++) {
    sum += v1[i];
  }
  cout << sum << endl;
}
