import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int minheight = -1; minheight -1 1 long out = 0; out 0 for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        } int i =0; int i =0; i 0 i<n i n i++ i++ i {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        } {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        } int height = sc.nextInt(); height sc.nextInt() sc.nextInt sc if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          } minheight == -1 minheight -1 1 {
            minheight = height;
          } minheight = height minheight height {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          } if(height < minheight) {
              out += (minheight-height);
            } height < minheight height minheight {
              out += (minheight-height);
            } out += (minheight-height) out (minheight-height) minheight height if(height > minheight) {
              minheight = height;
            } height > minheight height minheight {
              minheight = height;
            } minheight = height minheight height System.out.println(out) System.out.println System.out System System.out out String[] args args public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int minheight = -1;
        long out = 0;
        
        
        for(int i =0; i<n; i++) {
          int height = sc.nextInt();
          if(minheight == -1) {
            minheight = height;
          }
          else {
            if(height < minheight) {
              out += (minheight-height);
            }
            if(height > minheight) {
              minheight = height;
            }
          }
        }
        
        System.out.println(out);
        
    }
}