#include<iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  int a,now = 0;
  long long ans = 0;
  int i;
  for(i=0;i<N;i++) {
    cin >> a;
    if(now>a)ans += now-a;
    else now = a;
  }
  cout << ans << endl;
}