#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto &it : v) cin >> it;
    int stool = 0;
    int diff;
    for (int i = 1; i < n; i++) {
      diff = abs(v[i] - v[i - 1]);
      if (v[i] > v[i - 1]) {
        continue;
      } else {
        stool += diff;
        v[i] += diff;
      }
    }
    cout << stool << '\n';
  }
  return 0;
}
