#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int sum = 0;
  vector<long long> v1;
  v1.push_back(v[0]);
  for (int i = 1; i < n; i++) {
    v1.push_back(max(v1[i - 1], v[i]));
  }
  for (int i = 0; i < n; i++) {
    sum += abs(v1[i] - v[i]);
  }
  cout << sum << endl;
  return 0;
}
