#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200000], d = 0;
  long long int k = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[j] < a[i]) {
        k = k + (a[i] - a[j]);
        if (j == n - 1) {
          d = 1;
        }
      } else {
        if (j == n - 1) {
          d = 1;
        }
        break;
      }
    }
    if (d == 1) {
      break;
    }
  }
  cout << k;
  return 0;
}
