#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a[100005];
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  long long Max = -1;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    Max = max(a[i], Max);
    ans += Max - a[i];
  }
  cout << ans;
}
