#include <bits/stdc++.h>
using namespace std;
int x, y, z;
bool check(int a, int b, int c) {
  if (max(a, b) != x) return false;
  if (max(a, c) != y) return false;
  if (max(b, c) != z) return false;
  return true;
}
long long int h[200010];
int main() {
  int t, n, a, b, c;
  while (scanf("%d", &n) != EOF) {
    long long int num = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      if (i) {
        if (b > a) {
          num += b - a;
          a = b;
        }
      }
      b = a;
    }
    printf("%d\n", num);
  }
  return 0;
}
