#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int s;
  long long int sum = 0, m = 0, j = 0;
  cin >> s;
  long long int aa[s];
  for (long long int i = 0; i < s; i++) {
    cin >> aa[i];
    if (aa[i] > m) {
      m = aa[i];
      j = i;
    }
  }
  for (long long int i = 1; i < s; i++) {
    if (aa[i] < aa[i - 1]) {
      if (i >= j)
        sum += m - aa[i];
      else {
        sum += aa[i - 1] - aa[i];
      }
    }
  }
  cout << sum << endl;
  return 0;
}
