#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=1e8;
const int mod=1e9+7; 
map<int ,int> hash1;


void solve() {
	ll n;
	cin >> n;
	ll sum=0;
	ll a[n],c[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
		c[i]=a[i];
	}
	for(int i=1;i<n;i++){
		if(c[i]-c[i-1]<0){
			sum+=abs(c[i]-c[i-1]);
			c[i]=c[i-1];
		}
	}
	cout<<sum<<endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int t;
	t=1;

	for(int i=1; i<=t; ++i) {
	//	cout << "Case #" << i << ": ";
		solve();
	}
}

/*123
 * 1
 * 2
 * 3
 * 12
 * 13
 * 23	
 * 123
 * ans	=>	4 2 1
 * 112
 * 1
 * 1
 * 2
 * 11
 * 12
 * 12
 * 112
 * ans	=> 	6 0 1
 * 122
 * 112
 * 1
 * 2
 * 2
 * 12
 * 12
 * 22
 * 122
 * ans	=>	3 4 0
 * my	=>	4 3 0	kuki mai count consider nhi kr rha tha
 * 1223
 * 1122
 * 1111
 * 1
 * 2
 * 2
 * 3
 * 12
 * 12
 * 13
 * 22
 * 23
 * 23
 * 122
 * 122
 * 123
 * 223
 * 1223
 * ans	=>	5 9 1 0
 * r	=>	8 4 2 1
 * my	=>	8 6	1 0
 *  
 * 12234
 * 11222
 * 1
 * 2
 * 2
 * 3
 * 4
 * 12
 * 12
 * 13
 * 14
 * 22
 * 23
 * 24
 * 23
 * 24
 * 34
 * 122
 * 123
 * 124
 * 123
 * 124
 * 134
 * 223
 * 224
 * 234
 * 234
 * 1223
 * 1224
 * 1234
 * 1234
 * 2234
 * 12234
 * ans	=>	11 17 2 1 0
 * r	=>	16 8 4 2 1
 * my	=>	16 12 2 1 0
 * 1223
 * 1122
 * 1111
 * 1	1
 * 2	2
 * 2	3
 * 3	4
 * 12	12
 * 12	13
 * 13	14
 * 22	23
 * 23	24
 * 23	34
 * 122	123	- due 2
 * 122	124	- due 2
 * 123	134
 * 223	234
 * 1223	1234	-due 2
 * ans	=>	5 9 1 0
 * r	=>	8 4 2 1
 * my	=>	8 6	1 0
 * 1233
 * 1113
 * 1	1
 * 2	2	
 * 3	3
 * 3	4	+
 * 12	12
 * 13	13
 * 13	14
 * 23	23
 * 23	24
 * 33	34
 * 123	123
 * 123	124
 * 133	134	-due 3
 * 233	234	-2 due 3
 * 1233	1234 - due 3
 * ans	=>		6 3 6 0
 * my code2	=>  8 4 2 1
 * 			=>	8 4 3 0
 * 12233
 * 11222
 * 1	1
 * 2	2
 * 2	3
 * 3	4
 * 3	5
 * 12	12
 * 12	13
 * 13	14
 * 13	15
 * 22	23
 * 23	24
 * 23	25
 * 23	34
 * 23	35
 * 33	45
 * 122	123	-due 2
 * 123	124
 * 123	125
 * 123	134
 * 123	135
 * 133	145	-due 3
 * 223	234
 * 223	235
 * 233	245	-2 due 3
 * 233  345	-2 due 3
 * 1223 1234	- due 2
 * 1223 1235	-due 2
 * 1233 1245	-due 3
 * 1233 1345	-due 3
 * 2233 2345
 * 12233 12345	- due 2
 * ans	=>	9  14 8 0 0
 * my 	=>	16 8  4 2 1
 * mycod=>	16 12 3 0 0 shi de rha h   
 * 
 * 1234
 * 14
 * 24
 * 34
 * 124
 * 134
 * 234
 * 1234
 * -----------
 * 123
 * 1	=>	dp[1][1]=1	
 * 2	=>	dp[2][1]=1
 * 3	=>	dp[3][1]=1
 * 
 * strt=>>
 * dp[1][1]=3
 * dp[2][1]=2
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 2->1 taken
 * 3->4 taken
 * 4->11 taken
 * 5->26 taken
 * 6->57 taken
 * 
 * 3*26=78
 * 3
 * 12
 * 22+11=33
 * 
 * 
 * 52+26=78
 * 
 * 122333
 * 12 22 29
 * 32 24 7
 * 
 * 
 * 111222
 * 111111
 * 41 22		=>15
 * 56 7
 * 
 * 
 * 
 * 
 * 
 * 12223
 * 1	1
 * 2	2
 * 2	3
 * 2	4
 * 3	5
 * 12	12
 * 12	13
 * 12	14
 * 13	15
 * 22	23
 * 22	24
 * 23	25
 * 22	34
 * 23	35
 * 23	45
 * 122	123	-due 2
 * 122	124 -due 2
 * 123	125
 * 122	134 -due 2
 * 123	135
 * 123	145	
 * 222	234 
 * 223	235
 * 223	245	
 * 223  345	
 * 1222 1234	- due 2
 * 1223 1235	-due 2
 * 1223 1245	-due 3
 * 1223 1345	-due 3
 * 2223 2345
 * 12223 12345	- due 2
 * 
 * 2345
 * 23
 * 24
 * 25
 * 34
 * 35
 * 45
 * 234
 * 235
 * 245
 * 345
 * 2345
 * 
 * 
 * 18
 * 56+64=120-98=22
 * 
 * 111222{nC1+nC2....nC(hash of next-1)}
 * 122
 * 122
 * 122
 * 122
 * 122
 * 122
 * 122
 * 122
 * 122
 * 1222
 * 1222
 * 1222
 * 11222
 * 11222
 * 11222
 * 56
 * 41
 * --
 * 3C2=3+3C1=3
 * 15=3+4*3
 * 
 * 112233
 * 
 * 1*2+2c2=3
 * 1*2+2C2+2+1=4
 * 3
 * 1	=>-7+32+16=41
 * 2	=>-3+12=9+3=12
 * 3	=>3+4+3=10
 * 
 * 
 * 1111222
 * 4*4+4c2=6
 * 4C2=6
 * 22=3*6
 * 11111222
 * 30=3*10
 * 111111222=2^6=63-6=57
 * 4*6+6C2+=3*5=24+15=39
 * //main:::
 * 122233
 * (hash+2^n){2 ke liye}(2^3-hash-1)*(2^2-1){3 ke liye}=4*3=12
 * 1222333
 * 8+16+32=56-28=26
 * (2^3-3-1)=4*7=28
 * 11122
 * the n of 1 ka +1 - hoga
 * 
 * 
 * 122
 * 122
 * 122
 * 1222 4
 * 32-4=28
 * 
 * 23
 * 23
 * 23
 * 
 * 
 * 64  32  16  8  4  2  1
 * 1 2 2 2 2 3 3 3 3 3
 * 2=>(3C3*(3C1+3C2) +3C2*(3C1))=6+9=15=15
 * 1=>(3C3*(3C2+3C1+3C0)+3C2*(3C1+3C0))=(3+3+1)+3*(3+1)=7+3*4=12+7
 * so,3 => 7+x+15=22+19=41
 * 2=>(2^3-1-3)*(2^3-1-3c3)=4*3=12
 * or 2=>(3c3(2^3)+3C2(3C2+3C1+3C0))=(8+3*(3+3+1))=8+21=29
 * 2=56+29=85-15=70
 * 2 = 32+16+8+12-15=56-3=53
 * 1=64-12-12=40
 * */





Measured memory_bytes: 6768