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

        int n = sc.nextInt();
        int[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    }
} 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[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    }
} Main public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        

        int n = sc.nextInt();
        int[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    } main {
        Scanner sc = new Scanner(System.in);
        

        int n = sc.nextInt();
        int[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] A = new int[n]; A new int[n] n for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        } int i =0; int i =0; i 0 i < n i n i++ i++ i {
            A[i] = sc.nextInt();
        } {
            A[i] = sc.nextInt();
        } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc int[] help = new int[n]; help new int[n] n help[0] = A[0] help[0] help 0 A[0] A 0 for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         } {
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         } if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1]; A[i-1] >= A[i] A[i-1] A i-1 i 1 A[i] A i help[i] = help[i-1] help[i] help i help[i-1] help i-1 i 1 if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1]; A[i] > A[i-1] && A[i] > help[i-1] A[i] > A[i-1] A[i] A i A[i-1] A i-1 i 1 A[i] > help[i-1] A[i] A i help[i-1] help i-1 i 1 help[i] = A[i] help[i] help i A[i] A i if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1]; A[i] > A[i-1] && A[i] < help[i-1] A[i] > A[i-1] A[i] A i A[i-1] A i-1 i 1 A[i] < help[i-1] A[i] A i help[i-1] help i-1 i 1 help[i] = help[i-1] help[i] help i help[i-1] help i-1 i 1 long minH = 0L; minH 0L for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         } int j = 0; int j = 0; j 0 j < n j n j++ j++ j {
             minH += Math.abs(A[j] - help[j]);
         } {
             minH += Math.abs(A[j] - help[j]);
         } minH += Math.abs(A[j] - help[j]) minH Math.abs(A[j] - help[j]) Math.abs Math A[j] - help[j] A[j] A j help[j] help j System.out.println(minH) System.out.println System.out System System.out minH String args[] args public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        

        int n = sc.nextInt();
        int[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    }
} public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        

        int n = sc.nextInt();
        int[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    }
} Main
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        

        int n = sc.nextInt();
        int[] A = new int[n];
        for(int i =0; i < n; i++){
            A[i] = sc.nextInt();
        }
         

         int[] help = new int[n];
         help[0] = A[0];
         for(int i = 1; i < n; i++){
            if(A[i-1] >= A[i] )
               help[i] = help[i-1];

            else if(A[i] > A[i-1] && A[i] > help[i-1])
               help[i] = A[i];
             else if(A[i] > A[i-1] && A[i] < help[i-1])
               help[i] = help[i-1];
         }
         long minH = 0L;
         for(int j = 0; j < n; j++){
             minH += Math.abs(A[j] - help[j]);
         }
         System.out.println(minH);
    }
}