#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, sub = 0;
  int sum = 0;
  int hig = 0;
  cin >> n;
  int a[n + 4];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  hig = a[0];
  for (int i = 1; i < n; i++) {
    if (hig < a[i]) {
      sub = a[i] - hig;
      hig = a[i];
    } else {
      sub = hig - a[i];
      sum += sub;
    }
  }
  cout << sum << endl;
  return (0);
}
