ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 233 ms):
t = int(input())
for i in range(t):
    n = int(input())
    nums = list(map(int, input().split()))
    left = 0
    check = True
    while left < n-1:
        if nums[left] > nums[left+1]:
            check = False
            break
        else:
            nums[left+1] -= nums[left]
            nums[left] = 0
            left += 1


    if check:
        print('Yes')
    else:
        print('No')

        






Solution 2 (runtime: 233 ms):
'''********************* بسم الله الرحمن الرحيم ***********************'''



def listinp(): #list of integers

return list(map(int,input().split()))

def mulint(): #consecutive integers

return map(int,input().split())

def mulstr(): #consecutive str

return map(str,input().split())

def strlist():  #str list

return list(input())

def intinp():  #integer

return int(input())

def inp():   #string

return input()



from collections import defaultdict,deque

from bisect import bisect_left,bisect_right

from heapq import heappop,heappush,heapify

from math import ceil,isqrt



def solution():

x = intinp()

arr = listinp()

for i in range(1,x):

if arr[i-1]<=arr[i]:

arr[i] -= arr[i-1]

arr[i-1] = 0

elif arr[i-1] > arr[i]:

arr[i-1] -= arr[i]

arr[i] = 0

print('YnEoS'[not(arr == sorted(arr))::2])

for _ in range(intinp()):

solution()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 233 ms):
t = int(input())
for i in range(t):
    n = int(input())
    nums = list(map(int, input().split()))
    left = 0
    check = True
    while left < n-1:
        if nums[left] > nums[left+1]:
            check = False
            break
        else:
            nums[left+1] -= nums[left]
            nums[left] = 0
            left += 1


    if check:
        print('Yes')
    else:
        print('No')

        






Solution 2 (runtime: 187 ms):
def solve():

import sys

data = sys.stdin.read().split()

if not data:

return

t = int(data[0])

index = 1

output = []

for _ in range(t):

n = int(data[index])

index += 1

a = list(map(int, data[index:index+n]))

index += n



# For n=2, we just need to check the first two elements.

if n >= 2 and a[0] > a[1]:

output.append("NO")

continue



# Build alternating prefix sum array P where:

# P[0] = a[0], P[i] = P[i-1] + (-1)**i * a[i]

P = [0] * n

P[0] = a[0]

for i in range(1, n):

if i % 2 == 1:

P[i] = P[i-1] - a[i]

else:

P[i] = P[i-1] + a[i]



# Check necessary conditions:

# For i from 1 to n-2 (corresponding to pairs (a[i], a[i+1]) for i>=1 in 0-index)

ok = True

for i in range(1, n-1):

gap = a[i] - a[i+1]

# In the original 1-indexed formulation:

# if (i+1) is even, require: a[i]-a[i+1] <= P[i-1]

# if (i+1) is odd (and i+1>=3), require: a[i]-a[i+1] <= -P[i-1]

if (i + 1) % 2 == 0:

if gap > P[i-1]:

ok = False

break

else:

if gap > -P[i-1]:

ok = False

break

output.append("YES" if ok else "NO")

sys.stdout.write("\n".join(output))





if __name__ == '__main__':

solve()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 171 ms):
#!/usr/bin/env python3
import sys
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict as dd, deque

input = sys.stdin.readline

m9 = 10**9 + 7  # 998244353
yes, no = "YES", "NO"
d4 = [(1,0),(0,1),(-1,0),(0,-1)]
d8 = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
d6 = [(2,0),(1,1),(-1,1),(-2,0),(-1,-1),(1,-1)]  # hexagonal layout
abc = "abcdefghijklmnopqrstuvwxyz"
abc_map = {c:i for i,c in enumerate(abc)}
MAXINT = sys.maxsize
e18 = 10**18 + 10

# if testing locally, print to terminal with a different color
OFFLINE_TEST = False
CHECK_OFFLINE_TEST = True
# CHECK_OFFLINE_TEST = False  # uncomment this on Codechef
if CHECK_OFFLINE_TEST:
    import getpass

    OFFLINE_TEST = getpass.getuser() == "htong"


def log(*args):
    if CHECK_OFFLINE_TEST and OFFLINE_TEST:
        print("\033[36m", *args, "\033[0m", file=sys.stderr)


def solve(*args):
    # screen input
    if OFFLINE_TEST:
        log("----- solving ------")
        log(*args)
        log("----- ------- ------")
    return solve_(*args)


def read_matrix(rows):
    return [list(map(int, input().split())) for _ in range(rows)]


def read_strings(rows):
    return [input().strip() for _ in range(rows)]


def minus_one(arr):
    return [x - 1 for x in arr]


def minus_one_matrix(mrr):
    return [[x - 1 for x in row] for row in mrr]


# ---------------------------- template ends here ----------------------------

def solve_():
    n=int(input())
    *arr,=map(int,input().split())
    for i in range(n-1):
        if arr[i+1]-arr[i]<0:print(no);return
        arr[i+1]-=arr[i]
    print(yes)
for _ in range(int(input())):
    solve()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 249 ms):
import math

t = int(input())
for _ in range(t):
	n = int(input())
	f = 1
	arr = list(map(int,input().split()))
	for i in range(n-1):
		if arr[i] == 0:
			continue
		m = min(arr[i],arr[i+1])
		arr[i]-=m
		arr[i+1]-=m
		
		if arr[i] == 0 and arr[i+1] == 0:
			continue
		elif arr[i+1] == 0:
			print("NO")
			f = 0
			break
	if f==1:
		print("YES")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 311 ms):
t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    o = all(a[i] <= a[i+1] for i in range(len(a)-1))
    if o:
        print("YES")
        continue

    for i in range(0, n-2):
        m = min(a[i], a[i+1])
        a[i] = a[i] - m
        a[i+1] = a[i+1] - m
    o = all(a[i] <= a[i+1] for i in range(len(a)-1))
    if o:
        print("YES")
    else:
        print("NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 264 ms):
for i in range(int(input())):
    n = int(input())
    array = list(map(int, input().split()))
    ans = "YES"
    # 1 3 2
    for i in range(n - 1):
        mn = min(array[i], array[i + 1])
        array[i] -= mn
        array[i + 1] -= mn
    for i in range(1, n):
        if array[i] < array[i - 1]:
            ans = "NO"
            break
    print(ans)
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 202 ms):
L=[]
Q=[]
n=int(input())
for i in range(n):
     b=int(input())
     Q.append(b)
     a=list(map(int,input().split()))
     L.append(a)
for i in L:
     for t in range(len(i)-1):
          s=min(i[t],i[t+1])
          i[t]=i[t]-s
          i[t+1]=i[t+1]-s
for i in L:
     if all(i[j] <= i[j + 1] for j in range(len(i) - 1)):
          print("YES")
     else:
          print("NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 234 ms):

def subtract_min(nums):
    lst = nums
    if lst[0] > lst[1]:
        return "NO"
    
    for i in range(len(lst) - 1):
        subtract = min(lst[i], lst[i + 1])
        lst[i] -= subtract
        lst[i + 1] -= subtract
        if lst[i] == lst[-2]:
            break

    if lst == sorted(lst):
        return "yes"
        
    return "no"

def main():
    n = int(input().strip()) 
    for _ in range(n):
        size = int(input().strip())  
        lst = list(map(int, input().strip().split())) 
        if len(lst) != size:
            continue         

        print(subtract_min(lst)) 
        
if __name__ == "__main__":
    main()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 202 ms):
for _ in range(int(input())):
     n = int(input())
     a=list(map(int,input().split()))
     bol = True
     for i in range(n-1):
          if a[i] >a[i+1]:
               bol =False
               break
          else:
               a[i+1] = a[i+1]- a[i]
     print("YES" if bol else"NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 327 ms):
test_cases = int(input())

for t in range(test_cases):
    _ = input()
    nums = [int(n) for n in input().split(' ')]
    res = True

    for i in range(len(nums) - 1):
        
        # if i do the operation for all (a, b) where a <= b,
        # it is always sure to be in non-decreasing order
        a, b = nums[i], nums[i+1]
        a -= min(nums[i], nums[i+1])
        b -= min(nums[i], nums[i+1])
        nums[i], nums[i+1] = a, b
        
        # break if theyre not in non-decreasing order after the operation
        if a > b:
            res = False
            break
    
    print("YES" if res else "NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 218 ms):

def substract_min_sort():
    t = int(input())
    for _ in range(t):
        n = int(input())
        nums = list(map(int, input().split()))
        can_sort = True
        for j in range(n - 1):
            m = min(nums[j], nums[j + 1])
            nums[j] -= m
            nums[j + 1] -= m
            if j > 0 and nums[j] < nums[j - 1]:
                can_sort = False
                break   
        if can_sort:
            for k in range(n - 1):
                if nums[k] > nums[k + 1]:
                    can_sort = False
                    break
        print("YES" if can_sort else "NO")
substract_min_sort()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 249 ms):
# Vaibhav Varshney
import math
import bisect
 
def li():
    return list(map(int,input().split()))
def mi():
    return map(int,input().split())
 
def solve():
    n = int(input())
    a = li()
    diff = a[1]-a[0]
    if (diff<0):
        print("NO")
        return
    for i in range(2,n):
        diff = a[i]-diff
        if(diff<0):
            print("NO")
            return
    print("YES")    
        
    
def main():
    t = int(input())
    for _ in range(t):
        solve()
 
if __name__ == "__main__":
    main()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 217 ms):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 21:15:24 2025

@author: hp
"""

case = input()

ans = []
for i in range(int(case)):
    value = input()
    v = input().split()
    if int(len(v)) == int(value):
        flag = 1
        answer = 0 
        for j in range(len(v)):
            if j < int(value)-1:
                if j == 0:
                    answer = int(v[j+1]) - int(v[j])
                else:
                    answer = int(v[j+1]) - answer
            
            if answer < 0:
                flag=-1
                
        if flag == 1:
            ans.append("yes")
        
        else:
            ans.append("no")
            

for m in ans:
    print(m)
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 202 ms):
t = int(input())

for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    k = 0
    while k < n - 1 and a[k] <= a[k + 1]:
        a[k + 1] -= a[k]
        a[k] = 0
        k += 1

    if k == n - 1:
        print('yes')
    else:
        print('no')
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 249 ms):
def c(a):
    for i in range(len(a)-1):
        if a[i] > a[i+1]:
            return False
    return True


t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int, input().split()))
    f = c(l)
    if f:
        print('yes')
        continue
    for i in range(len(l)-2):
        m = min(l[i], l[i+1])
        if l[i] - m <= l[i+1] - m and l[i+1] - m <= l[i+2]:
            l[i] -=m
            l[i+1] -=m
    f = c(l)
    if f:
        print('yes')
    else:    
        print("no")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 171 ms):
t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    possible = True

    for i in range(1, n):
        curr = a[i-1]
        next_num = a[i]

        a[i-1] -= curr
        a[i] -= curr

        if a[i-1] > a[i]:
            possible = False

    if possible:
        print("YES")
    else:
        print("NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 187 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    a = list(map(int, inp().split()))

    for i in range(1, N):
        m = fmin(a[i - 1], a[i])
        a[i - 1], a[i] = a[i - 1] - m, a[i] - m

    if any(a[i] > a[i + 1] for i in range(N - 1)):
        return 'NO'
    
    return 'YES'
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()

Solution 2 (runtime: 155 ms):
import sys
input = sys.stdin.readline

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def main():
    t = inp()
    
    def solve():
        n = invr()
        a = inlt()

        """
        use the op when a_i+1 <= a_i
        can use the op at most n - 1 times

        the op doesn't change the relationship between a_i and a_i+1
        but can change the relationship between a_i+1 and a_i+2

        if a_i+1 > a_i+2
            then use op(a_i)

        if a_i+1 still > a_i+2 then not possible
        else continue with new a
        """
        if a[1] < a[0]:
            print("NO")
        else:
            for i in range(1, len(a) - 1):
                if a[i] - a[i-1] > a[i+1]:
                    print("NO")
                    break
                a[i] = a[i] - a[i-1]
            else:
                print("YES")
    
    for _ in range(t):
        solve()

main()