#include <bits/stdc++.h>
using namespace std;
int main() {
  int ans = 0;
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  for (auto i = 1; i < v.size(); i++) {
    int diff = max(v[i], v[i - 1]) - v[i];
    ans += diff;
    v[i] += diff;
  }
  cout << ans << endl;
}
