import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    }
} import java.util.*; util.* java public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    } main {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int loop = sc.nextInt(); loop sc.nextInt() sc.nextInt sc int[] array = new int[loop]; array new int[loop] loop long count = 0; count 0 for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < loop i loop i++ i++ i {
            array[i] = sc.nextInt();
        } {
            array[i] = sc.nextInt();
        } array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        } int i = 0; int i = 0; i 0 i < (loop - 1) i (loop - 1) loop 1 i++ i++ i {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        } {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        } int temp = array[i]; temp array[i] array i int next = array[i+1]; next array[i+1] array i+1 i 1 if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            } next < temp next temp {
                count += (temp - next);
                array[i+1] = temp;
            } count += (temp - next) count (temp - next) temp next array[i+1] = temp array[i+1] array i+1 i 1 temp System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    }
} public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    }
} Main
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        int[] array = new int[loop];
        long count = 0;
        for(int i = 0; i < loop; i++) {
            array[i] = sc.nextInt();
        }
        for(int i = 0; i < (loop - 1); i++) {
            int temp = array[i];
            int next = array[i+1];
            if(next < temp){
                count += (temp - next);
                array[i+1] = temp;
            }
        }
        System.out.println(count);

    }
}
