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