#include <bits/stdc++.h>
using namespace std;
int ceil(int a, int m) {
  if (a % m == 0) {
    return a / m;
  } else {
    return a / m + 1;
  }
}
vector<int> sort(vector<int> arr) {
  sort(arr.begin(), arr.end());
  return arr;
}
int minInVector(vector<int> arr) {
  int mins = 1e9;
  for (int i = 0; i < arr.size(); i++) {
    mins = min(mins, arr[i]);
  }
  return mins;
}
int maxInVector(vector<int> arr) {
  int maxs = -1;
  for (int i = 0; i < arr.size(); i++) {
    maxs = max(maxs, arr[i]);
  }
  return maxs;
}
bool checkElement(vector<int> arr, int k) {
  for (int i = 0; i < arr.size(); i++) {
    if (arr[i] == k) {
      return true;
    }
  }
  return false;
}
void solve() {
  int n;
  cin >> n;
  vector<int> ins;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ins.push_back(a);
  }
  int ans = 0;
  for (int i = 1; i < ins.size(); i++) {
    if (ins[i] < ins[i - 1]) {
      ans += (ins[i - 1] - ins[i]);
      ins[i] = ins[i - 1];
    }
  }
  cout << ans;
}
char nl = '\n';
char bl = ' ';
const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
