#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  int tool[N];
  int ans = 0;
  for (int i = 0; i < N; i++) cin >> tool[i];
  for (int i = 1; i < N; i++) {
    if (tool[i - 1] > tool[i]) {
      ans += tool[i - 1] - tool[i];
      tool[i] = tool[i - 1];
    }
  }
  cout << ans;
}
