#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> v(n);
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  long long high = 0, ans = 0;
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int i = 0; i < n; i++) {
    if (high <= v[i])
      high = v[i];
    else
      ans += high - v[i];
  }
  cout << ans << endl;
  return 0;
}
