#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MX = 1000005;
int main() {
  int n;
  int a, b, ans = 0;
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a;
    if (a < b) {
      ans += (b - a);
    } else
      b = a;
  }
  cout << ans << endl;
}
