#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
int a[M];
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] >= a[i - 1]) continue;
    int id = a[i - 1] - a[i];
    a[i] += id;
    ans += id;
  }
  printf("%d", ans);
  return 0;
}
