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

Solution 1 (runtime: 234 ms):
# Jai Shree Ram

def main() -> None:
    n=int(input())
    v=list(map(int,input().strip().split()))
    
    i,j=0,1
    while j<n:
        m=min(v[i],v[j])
        v[i]-=m
        v[j]-=m
        i+=1
        j+=1
    
    for i in range(n-1):
        if v[i]!=0:
            print("NO")
            return
    
    print("YES")

t=int(input())

while t>0:
    t-=1
    main()

Solution 2 (runtime: 202 ms):


for _ in range(int(input())):

	n = int(input())

	a = list(map(int,input().split(' ')))

	minim = float('-inf')

	complete = False



	for i in range(n-1):

		curr = a[i]

		nx = a[i+1]

		if nx-curr>=0:

			a[i+1] = nx-curr

		else:

			print("NO")

			complete = True

			break

	if not complete:

		print("YES")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 234 ms):
# Jai Shree Ram

def main() -> None:
    n=int(input())
    v=list(map(int,input().strip().split()))
    
    i,j=0,1
    while j<n:
        m=min(v[i],v[j])
        v[i]-=m
        v[j]-=m
        i+=1
        j+=1
    
    for i in range(n-1):
        if v[i]!=0:
            print("NO")
            return
    
    print("YES")

t=int(input())

while t>0:
    t-=1
    main()

Solution 2 (runtime: 218 ms):
for _ in range(int(input())):

    n=int(input())

    a=list(map(int,input().split()))

    ans='yes'

    for i in range(n-1):

        if a[i+1]<a[i]:

            ans = 'NO'

            break

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

        a[i]=0

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

Solution 1 (runtime: 234 ms):
# Jai Shree Ram

def main() -> None:
    n=int(input())
    v=list(map(int,input().strip().split()))
    
    i,j=0,1
    while j<n:
        m=min(v[i],v[j])
        v[i]-=m
        v[j]-=m
        i+=1
        j+=1
    
    for i in range(n-1):
        if v[i]!=0:
            print("NO")
            return
    
    print("YES")

t=int(input())

while t>0:
    t-=1
    main()

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

answers = []

for _ in range(t):

n =  int(input())

seq = list(map(int, input().split()))

answer = 'YES'

i = 0

while i < len(seq):

if i == len(seq) - 1:

if seq[i] < seq[i-1]:

answer = 'NO'

break

min_val = min(seq[i], seq[i + 1])

seq[i] -= min_val

seq[i + 1] -= min_val

if (seq[i] < seq[i-1] or seq[i] > seq[i+1]) and i > 0:

answer = 'NO'

break

i += 1

answers.append(answer)



for answer in answers:

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

Solution 1 (runtime: 234 ms):
# Jai Shree Ram

def main() -> None:
    n=int(input())
    v=list(map(int,input().strip().split()))
    
    i,j=0,1
    while j<n:
        m=min(v[i],v[j])
        v[i]-=m
        v[j]-=m
        i+=1
        j+=1
    
    for i in range(n-1):
        if v[i]!=0:
            print("NO")
            return
    
    print("YES")

t=int(input())

while t>0:
    t-=1
    main()

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



while t > 0:

n = int(input())

solved = False

arr = list(map(int, input().split()))

for i in range(n-1):

q = min(arr[i], arr[i+1])

arr[i] = arr[i]- q

arr[i+1] = arr[i+1]- q

for i in range(n-1):

if arr[i+1]<arr[i]:

solved = True

print("NO")

break

if not solved:

print("YES")





t -= 1
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 234 ms):
# Jai Shree Ram

def main() -> None:
    n=int(input())
    v=list(map(int,input().strip().split()))
    
    i,j=0,1
    while j<n:
        m=min(v[i],v[j])
        v[i]-=m
        v[j]-=m
        i+=1
        j+=1
    
    for i in range(n-1):
        if v[i]!=0:
            print("NO")
            return
    
    print("YES")

t=int(input())

while t>0:
    t-=1
    main()

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: 234 ms):
# Jai Shree Ram

def main() -> None:
    n=int(input())
    v=list(map(int,input().strip().split()))
    
    i,j=0,1
    while j<n:
        m=min(v[i],v[j])
        v[i]-=m
        v[j]-=m
        i+=1
        j+=1
    
    for i in range(n-1):
        if v[i]!=0:
            print("NO")
            return
    
    print("YES")

t=int(input())

while t>0:
    t-=1
    main()

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: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 281 ms):
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    if a[0] > a[1]:
        print('NO')
        continue
    for i in range(n - 1):
        if a[i] <= a[i + 1]:
            mn = min(a[i], a[i + 1])
            a[i] -= mn
            a[i + 1] -= mn
        else:
            print('NO')
            break
    else:
        print('YES')
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 312 ms):
tc=int(input())
for _ in range(tc):
    n=int(input())
    a=[int(x) for x in input().split()]
    i=0
    b=True
    while i<n-1:
        if i+2<n and a[i+2]>=a[i+1]-a[i] and a[i+1]-a[i]>=0:
            a[i+2]=a[i+2]-(a[i+1]-a[i])
            pass            
        elif i+2==n and 1000000001>=a[i+1]-a[i] and a[i+1]-a[i]>=0:
            pass
        else:
            b=False
            break
        i+=2
    if b:
        print("YES")
    else:
        print("NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


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

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 467 ms):
for i in range(int(input())):
    N = int(input())
    A = [int(x) for x in input().split()]
    for i in range(N-1):
        minimum = min(A[i], A[i+1])
        A[i] -= minimum
        A[i+1] -= minimum
    print("YES" if A==sorted(A) else "NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 218 ms):
# cook your dish here

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    
    for i in range(1, n):
        mn = min(a[i - 1], a[i])
        a[i - 1] -= mn
        a[i] -= mn
            
    
    if a == sorted(a):
        print("Yes")
    else:
        print("NO")
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 265 ms):
cases=int(input(''))
sol=[]
for i in range(0,cases):
    elem=int(input())
    a=list(map(int,input().split()))
    count=0
    for j in range (0,len(a)-1):
        if(a[j]>a[j+1]):
            count+=1
            break
        m=min(a[j],a[j+1])
        a[j]=a[j]-m
        a[j+1]=a[j+1]-m
    if(count>0):
        sol.append('NO')
    else:
        sol.append('YES')
for a in sol:
    print(a)
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


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

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 218 ms):
def solve():
    import sys
    
    input_data = sys.stdin.read().strip().split()
    t = int(input_data[0])
    index = 1
    
    results = []
    for _ in range(t):
        n = int(input_data[index])
        index += 1
        
        # Read the array of integers
        a = list(map(int, input_data[index:index+n]))
        index += n
        
        # Perform the "subtract min" operation in a single left-to-right pass
        for i in range(n - 1):
            if a[i] <= a[i + 1]:
                # Subtract a[i] from a[i+1], and make a[i] zero
                a[i+1] -= a[i]
                a[i] = 0
        
        # Check if the final array is non-decreasing
        if all(a[i] <= a[i+1] for i in range(n-1)):
            results.append("YES")
        else:
            results.append("NO")
    
    # Output one result per test case
    print("\n".join(results))
    
solve()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 218 ms):
from itertools import accumulate, pairwise
import math
import bisect
import heapq
from collections import Counter, defaultdict
from functools import cache
def solve():
    n = int(input())
    arr = [int(x) for x in input().split()]
    """
    4 5 4 5 4 5 4 5
    0 1 4 5 4 5 4 5
    0 0 3 4 4 5 4 5
    0 0 0 1 1 2 4 5
    
    2 3 4 1
    0 1 4 1
    
    
    """
    left = prev = 0
    for i in range(n - 1):
        cur = 0
        while left < i:
            prev = max(0, arr[left] - prev)
            left += 1
        if arr[i] - prev > arr[i + 1]:
            print("NO")
            return
    print("YES")
for _ in range(int(input())):
    solve()
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 218 ms):
#include <bits/stdc++.h>
using namespace std;
int a[200005];
map<int,int> mp;
int main() {
	int t;cin>>t;
	while(t--){
		int n;
		cin>>n;
		bool flag=false;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=1;i<n;i++)
		{
			if(a[i]>a[i+1])
			{
				flag=true;
				break;
			}
			a[i+1]-=a[i];
		}
		if(flag)
			cout<<"NO"<<endl;
		else
			cout<<"YES"<<endl;
	}
	return 0;
}
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))

    for i in range(n-1):
        temp = min(a[i], a[i+1])
        a[i] -= temp
        a[i+1] -= temp

    possible = True
    for i in range(n-1):
        if a[i] > a[i+1]:
            possible = False
            break

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

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 202 ms):
#include <bits/stdc++.h>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    vector<int> A(n);
    for(int i=0;i<n;i++) {
        cin >> A[i];
    }

    for(int i=1;i<n;i++) {
        int min_v=min(A[i-1],A[i]);
        A[i-1]-=min_v;
        A[i]-=min_v;
    }

    for(int i=0;i<n-1;i++) {
        if(A[i]) {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}
int main()
{
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (runtime: 202 ms):
def solve_test_case():
    n = int(input())
    loads = list(map(int, input().split()))
    if n <= 1:
        print("YES")
        return

    for i in range(n - 1):
        transfer = min(loads[i], loads[i+1])
        loads[i] -= transfer
        loads[i+1] -= transfer

    for i in range(n - 1):
        if loads[i] > loads[i+1]:
            print("No")
            return

    print("YES")
    return

def main():
    T = int(input())
    for _ in range(T):
        solve_test_case()

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

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (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')
Problem: 2060D - Subtract Min Sort
Language: python

Solution 1 (runtime: 265 ms):
def ls(): return input().split()
def ints(): return map(int, input().split())
def strs(): return map(str, input().split())
def it(): return int(input())
def s(): return input()
def li(): return list(map(int, input().split()))

for _ in range(it()):
    n = it()
    arr = li()
    idx = 0
    for i in range(1, n):
        if arr[i - 1] > arr[i]:
            idx = i
    if idx == 0:  
        print("YES")
        continue

    for i in range(idx):
        mn = min(arr[i], arr[i + 1])
        arr[i] -= mn
        arr[i + 1] -= mn

    print("YES" if arr == sorted(arr) else "NO")


Solution 2 (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()