#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  map<int, int> v;
  vector<int> vec(n);
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
    v[vec[i]] = 0;
  }
  long long count = 0;
  vector<int> temp(vec);
  sort(temp.begin(), temp.end());
  if (is_sorted(vec.begin(), vec.end())) {
    cout << "t";
    int max = vec[n - 1];
    for (int i = n - 1; i >= 0; --i) {
      if (vec[i] < max) count += (max - vec[i]);
    }
  } else if (is_sorted(vec.begin(), vec.end(),
                       [](int a, int b) { return a > b; })) {
  } else {
    int j = n - 1;
    for (int i = n - 1; i >= 0; --i) {
      if (v[temp[i]] == 0) {
        while (vec[j] != temp[i]) {
          count += (temp[i] - vec[j]);
          v[vec[j]] = 1;
          vec[j] = temp[i];
          --j;
        }
        --j;
      }
    }
  }
  cout << count;
}
