#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long int MAXN = 1000005;
long long int spf[1000009];
void sieve() {
  spf[1] = 1;
  for (long long int i = 2; i < MAXN; i++) spf[i] = i;
  for (long long int i = 4; i < MAXN; i += 2) spf[i] = 2;
  for (long long int i = 3; i * i < MAXN; i++)
    if (spf[i] == i) {
      for (long long int j = i * i; j < MAXN; j += i)
        if (spf[j] == j) spf[j] = i;
    }
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void solve() {
  long long int n;
  cin >> n;
  long long int arr[n];
  for (long long int i = 0; i < n; ++i) cin >> arr[i];
  long long int maxm = -1, pos = -1;
  for (long long int i = 0; i < n; i++) {
    if (arr[i] > maxm) maxm = arr[i], pos = i;
  }
  long long int s = 0;
  for (long long int i = pos; i < n; i++) s = s + abs(maxm - arr[i]);
  maxm = arr[0];
  for (long long int i = 0; i < pos; i++) {
    if (arr[i] < maxm) s = s + abs(arr[i] - maxm);
  }
  cout << s << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) solve();
  return 0;
}
