
import java.util.Map.Entry;
import java.util.*;
 
//import org.w3c.dom.css.Counter;
 
public class Main {
//public class App {
 
    public static void main(final String[] args){
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    }
} import java.util.Map.Entry; Entry java.util.Map import java.util.*; util.* java public class Main {
//public class App {
 
    public static void main(final String[] args){
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    }
} Main public static void main(final String[] args){
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    } main {
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    } final Scanner s = new Scanner(System.in); s new Scanner(System.in) long N = Integer.parseInt(s.next()); N Integer.parseInt(s.next()) Integer.parseInt Integer s.next() s.next s int tmp = 0; tmp 0 long ans=0; ans 0 for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    } long i =0; long i =0; i 0 i<N i N i++ i++ i {
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    } {
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    } int T = Integer.parseInt(s.next()); T Integer.parseInt(s.next()) Integer.parseInt Integer s.next() s.next s if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        } tmp >T tmp T {
            ans += tmp -T;
        } ans += tmp -T ans tmp -T tmp T {
            tmp = T;
        } tmp = T tmp T System.out.println(ans) System.out.println System.out System System.out ans final String[] args args public class Main {
//public class App {
 
    public static void main(final String[] args){
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    }
} public class Main {
//public class App {
 
    public static void main(final String[] args){
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    }
} Main

import java.util.Map.Entry;
import java.util.*;
 
//import org.w3c.dom.css.Counter;
 
public class Main {
//public class App {
 
    public static void main(final String[] args){
     //スキャン
     final Scanner s = new Scanner(System.in);
      //String N = Integer.parseInt(s.next());
      long N = Integer.parseInt(s.next());
      //int T = Integer.parseInt(s.next());
      //int ans = N /X;
      //String hoge = s.next();
      //int huge = hoge.length();
      //int hage = 0;
      int tmp = 0;
      long ans=0;
    for(long i =0;i<N;i++){
        int T = Integer.parseInt(s.next());
        if(tmp >T){
            ans += tmp -T;
        }else{
            tmp = T;
        }
    }
    System.out.println(ans);
    }
}