#include <bits/stdc++.h>
using namespace std;
int n;
int pprev;
int ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int val;
    cin >> val;
    if (pprev > val) ans += pprev - val, val = pprev;
    pprev = val;
  }
  cout << ans << endl;
}
