#include <bits/stdc++.h>
using namespace std;
int main() {
  int ans = 0;
  int a, aa;
  cin >> a;
  while (cin >> a) {
    if (aa > a) {
      ans += aa - a;
    } else {
      aa = a;
    }
  }
  cout << ans;
  return 0;
}
