#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long ans = 0;
  for (int it = n; it > 0; it--) {
    int idx = max_element(arr.begin(), arr.begin() + it) - arr.begin();
    while (--it != idx) {
      ans += arr[idx] - arr[it];
    }
    it++;
  }
  cout << ans;
}
