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