CSES - Shared codeLink to this code: https://cses.fi/paste/bd6c241d1bb5736121e3b2/
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        Scanner scin = new Scanner(System.in);
        int t = scin.nextInt();
        int[] a = new int[t];
        int count = 0;
        for (int i = 0 ; i < t ; i++){
            int k = scin.nextInt();
            a[i]=k;
        }
        for (int i = 1; i < t; i++){
            if (a[i-1]>a[i]){
                count+=a[i-1]-a[i];
            }
        }
        System.out.println(count);
    }
}