#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll n = 0;
  cin >> n;
  vector<ll> vec(n);
  for (long long i = 0; i < (n); i++) cin >> vec[i];
  sort(vec.begin(), vec.end());
  ll ans = 0;
  for (long long i = 0; i < (n - 1); i++) {
    ll j = i + 1;
    if (vec[i] < vec[j]) {
      ans = ans + (vec[j] - vec[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
