import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.nio.charset.StandardCharsets; StandardCharsets java.nio.charset public class Main {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    }
} Main public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    } main {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    } InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); reader new InputStreamReader(System.in, StandardCharsets.UTF_8) BufferedReader in = new BufferedReader(reader); in new BufferedReader(reader) long size = Long.parseLong(in.readLine()); size Long.parseLong(in.readLine()) Long.parseLong Long in.readLine() in.readLine in String[] str = in.readLine().split(" "); str in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " long num1 = 0; num1 0 long num2 = 0; num2 0 long step = 0; step 0 long res = 0; res 0 for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        } int i=0; int i=0; i 0 i < size-1 i size-1 size 1 i++ i++ i {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        } {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        } num1 = Long.parseLong(str[i]) + step num1 Long.parseLong(str[i]) + step Long.parseLong(str[i]) Long.parseLong Long str[i] str i step num2 = Long.parseLong(str[i+1]) num2 Long.parseLong(str[i+1]) Long.parseLong Long str[i+1] str i+1 i 1 if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	} num1 > num2 num1 num2 {
        		step = num1 - num2;
        		res += step;
        	} step = num1 - num2 step num1 - num2 num1 num2 res += step res step {
        		step = 0;
        	} step = 0 step 0 System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    }
} public class Main {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        long size = Long.parseLong(in.readLine());
        String[] str = in.readLine().split(" ");
        long num1 = 0;
        long num2 = 0;
        long step = 0;
        long res = 0;

        for(int i=0; i < size-1; i++) {
        	num1 = Long.parseLong(str[i]) + step;
        	num2 = Long.parseLong(str[i+1]);

        	if(num1 > num2) {
        		step = num1 - num2;
        		res += step;
        	}else {
        		step = 0;
        	}
        }

        System.out.println(res);

    }
}