#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const long long INF = 1001001001;
const long long MOD = 1000000007;
const double EPS = 1e-10;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n];
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum1 += a[i];
  }
  int maxv = 0;
  for (int i = 0; i < n; i++) {
    maxv = max(maxv, a[i]);
    sum2 += maxv;
  }
  cout << sum2 - sum1 << endl;
}
