#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int main() {
  int n;
  int a[200010];
  scanf("%d", &n);
  for (int i = int(0); i < int(n); i++) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = int(1); i < int(n); i++) {
    if (a[i] >= a[i - 1]) continue;
    ans += a[i - 1] - a[i];
    a[i] = a[i - 1];
  }
  printf("%d\n", ans);
  return 0;
}
