#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long dp[3003][3004] = {};
void solve() {
  int n, k = 1, m = 0, i = 1, j, x, y, l, r, p, q, z, a[3000];
  long long ans = 0;
  string s;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
    ans += m - a[i];
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) solve();
  return 0;
}
