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