#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, ans = 0, Max;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < (n); ++i) {
    cin >> v.at(i);
  }
  Max = v.at(0);
  for (int i = 0; i < (n - 1); ++i) {
    Max = max(Max, v.at(i + 1));
    if (Max > v.at(i + 1)) {
      ans += (Max - v.at(i + 1));
    }
  }
  cout << ans << endl;
  return 0;
}
