Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 484 ms):
// Problem Page: https://codeforces.com/contest/2060/problem/D

import java.util.*;

public class SubtractMinSort {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            solve(sc);
        }
    }

    static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }

        if (n == 2) {
            if (a[1] <= a[2]) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
            return;
        }

        String ans = "YES";
        int diff = a[2] - a[1];
        for (int i = 3; i <= n; i++) {
            if ((diff >= 0) && (a[i] >= diff)) {
                diff = a[i] - diff;
            } else {
                ans = "NO";
                break;
            }
        }
        System.out.println(ans);
    }
}


Solution 2 (runtime: 312 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class prob {

    static class FastReader {
        BufferedReader b;
        StringTokenizer s;

        public FastReader() {
            b = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (s == null || !s.hasMoreElements()) {
                try {
                    s = new StringTokenizer(b.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return s.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        float nextFloat() {
            return Float.parseFloat(next());
        }

        boolean nextBoolean() {
            return Boolean.parseBoolean(next());
        }

        // Method to read the next line as a string
        String nextLine() {
            String str = "";
            try {
                if (s == null || !s.hasMoreTokens()) {
                    str = b.readLine();
                } else {
                    str = s.nextToken("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    //===================================================================================================

    public static void main(String[] args) {
        FastReader fastReader = new FastReader();

        int t=0;
        t = fastReader.nextInt();

        while(t-- >0){
            int n = fastReader.nextInt();
            int[] arr = new int[n];

            for(int i=0;i<n;i++){
                arr[i] = fastReader.nextInt();
            }

            solve(arr, n);
        }
    }

    public static void solve(int[] arr, int n){

        for(int i=0;i<n-1;i++){
            int var = Math.min(arr[i], arr[i+1]);
            arr[i]-=var;
            arr[i+1]-=var;
        }

        for(int i=0;i<n-1;i++){
            if(arr[i+1]<arr[i]){
                System.out.println("NO");
                return;
            }
        }

        System.out.println("YES");
    }
}