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