#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 998244353;
const int N = 2e5 + 5;
int power(int a, int b) {
  if (!b) return 1;
  int c = power(a, b / 2);
  c = (1LL * c * c) % mod;
  if (b % 2) c = (1LL * c * a) % mod;
  return c;
}
vector<int> adj[N];
vector<int> ans(N + 1, 0);
void dfs(int s, int e) {
  ans[s] = 1;
  for (auto u : adj[s]) {
    if (u == e) continue;
    dfs(u, s);
    ans[s] += ans[u];
  }
}
void done() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
  }
  dfs(1, 0);
  for (int i = 1; i < n + 1; ++i) {
    cout << ans[i] << " ";
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  int d = 1e9 + 1;
  for (int &c : a) {
    cin >> c;
    d = min(d, c);
  }
  bool ok = 1;
  for (int i = 1; i < n; ++i)
    if (a[i - 1] > a[i]) ok = 0;
  if (ok) {
    cout << "YES\n" << '\n';
    return;
  }
}
void another() {
  int s = 0;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &c : a) cin >> c;
  int d = 0;
  for (int c : a) {
    d = max(d, c);
    s += d - c;
  }
  cout << s << '\n';
}
void test_case() {
  int t;
  cin >> t;
  while (t--) another();
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  another();
}
