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