#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double pi = 3.14159265358979323846;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long pwmd(long long b, long long exp) {
  long long ret = 1;
  while (exp > 0) {
    if (exp % 2 == 1) ret = (ret * b) % MOD;
    exp /= 2;
    b = (b * b) % MOD;
  }
  return ret;
}
struct pair_hash {
  inline std::size_t operator()(
      const std::pair<long long, long long>& v) const {
    return v.first * 31 + v.second;
  }
};
int n, a;
void solve(int tc) {
  cin >> n;
  int ans = 0, mx = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    mx = max(mx, a);
    ans += (mx - a);
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  for (int t = 0; t < tc; t++) solve(t);
  return 0;
}
