#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0;
  int Max = arr[0];
  for (int i = 1; i < n; i++) {
    Max = max(arr[i], Max);
    if (Max > arr[i]) ans += (Max - arr[i]) % mod;
  }
  cout << ans << '\n';
  return 0;
}
