#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;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) cin >> v[i];
  auto it = v.end();
  long long count = 0;
  while (it != v.begin()) {
    auto temp = it;
    it = max_element(v.begin(), it);
    for (auto i = it; i != temp; ++i) {
      count += (*it - *i);
      *i = *it;
    }
  }
  cout << count;
}
