#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, a[200000], k = 0, d = 0, i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    for (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;
}
