#include <bits/stdc++.h>
using namespace std;
vector<bool> steve();
long long mod_plus(long long n1, long long n2);
long long mod_multiply(long long n1, long long n2);
void file() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void zuka() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  zuka();
  int n;
  cin >> n;
  vector<long long> v(n);
  long long ctr = 0;
  long long mx = 0, mn = 1e10, mp = -1;
  bool asc = true;
  bool flag = true;
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int i = 0; i < n - 1; i++) {
    if (v[i] > v[i + 1]) {
      long long h = mn;
      mx = max(mx, max((long long)v[i], (long long)v[i + 1]));
      mn = min(mn, min((long long)v[i], (long long)v[i + 1]));
      if (mn != h) mp = i;
      asc = false;
    } else {
      if (!asc) {
        ctr += mx - mn;
        v[mp] = mx;
        mx = 0;
        mn = 1e10;
        asc = true;
        flag = false;
        i--;
      }
    }
  }
  if (!asc) ctr += mx - mn;
  cout << ctr;
}
vector<bool> steve() {
  vector<bool> primes(100005, 1);
  for (long long i = 2; i * i <= 100005; i++) {
    if (primes[i]) {
      for (long long j = i * i; j <= 100005; j += i) primes[j] = 0;
    }
  }
  primes[0] = primes[1] = 0;
  return primes;
}
long long mod_plus(long long n1, long long n2) {
  return ((n1 % 1000003) + (n2 % 1000003)) % 1000003;
}
long long mod_multiply(long long n1, long long n2) {
  return ((n1 % 1000003) * (n2 % 1000003)) % 1000003;
}

### Prompt

Construct a cpp code solution to the problem outlined:

The Little Elephant loves sortings.

He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r.

Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing.

Output

In a single line print a single integer — the answer to the problem.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

3
1 2 3


Output

0


Input

3
3 2 1


Output

2


Input

4
7 4 1 47


Output

6

Note

In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.

In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).

In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
vector<bool> steve();
long long mod_plus(long long n1, long long n2);
long long mod_multiply(long long n1, long long n2);
void file() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
void zuka() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  zuka();
  int n;
  cin >> n;
  vector<long long> v(n);
  long long ctr = 0;
  long long mx = 0, mn = 1e10, mp = -1;
  bool asc = true;
  bool flag = true;
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int i = 0; i < n - 1; i++) {
    if (v[i] > v[i + 1]) {
      long long h = mn;
      mx = max(mx, max((long long)v[i], (long long)v[i + 1]));
      mn = min(mn, min((long long)v[i], (long long)v[i + 1]));
      if (mn != h) mp = i;
      asc = false;
    } else {
      if (!asc) {
        ctr += mx - mn;
        v[mp] = mx;
        mx = 0;
        mn = 1e10;
        asc = true;
        flag = false;
        i--;
      }
    }
  }
  if (!asc) ctr += mx - mn;
  cout << ctr;
}
vector<bool> steve() {
  vector<bool> primes(100005, 1);
  for (long long i = 2; i * i <= 100005; i++) {
    if (primes[i]) {
      for (long long j = i * i; j <= 100005; j += i) primes[j] = 0;
    }
  }
  primes[0] = primes[1] = 0;
  return primes;
}
long long mod_plus(long long n1, long long n2) {
  return ((n1 % 1000003) + (n2 % 1000003)) % 1000003;
}
long long mod_multiply(long long n1, long long n2) {
  return ((n1 % 1000003) * (n2 % 1000003)) % 1000003;
}

```