
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    } main {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int currTop = 0; currTop 0 long steps = 0; steps 0 for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        } {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        } int A = sc.nextInt(); A sc.nextInt() sc.nextInt sc if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            } A < currTop A currTop {
                steps += currTop - A;
            } steps += currTop - A steps currTop - A currTop A if (currTop < A) {
                currTop = A;
            } currTop < A currTop A {
                currTop = A;
            } currTop = A currTop A System.out.println(steps) System.out.println System.out System System.out steps String[] args args public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    }
} public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    }
} Main

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        final int N = sc.nextInt();
        
        int currTop = 0;
        long steps = 0;
        
        for (int i = 0; i < N; i++) {
            int A = sc.nextInt(); 
            
            if (A < currTop) {
                steps += currTop - A;
            } else if (currTop < A) {
                currTop = A;
            }
        }
        
        System.out.println(steps);
    }
}
