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