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

Solution 1 (runtime: 437 ms):
num = int(input())



for _ in range(num):

    eh_possivel = True

    tam_array = int(input()) 

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

 

    for i in range(len(lista) - 1):

        menor = min(lista[i], lista[i + 1])

        lista[i] = lista[i] - menor

        lista[i + 1] = lista[i + 1] - menor

        if lista[i] > lista[i + 1]:

            eh_possivel = False

            break 

    if eh_possivel:

        print("YES")

    else:

        print("NO")



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: 437 ms):
num = int(input())



for _ in range(num):

    eh_possivel = True

    tam_array = int(input()) 

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

 

    for i in range(len(lista) - 1):

        menor = min(lista[i], lista[i + 1])

        lista[i] = lista[i] - menor

        lista[i + 1] = lista[i + 1] - menor

        if lista[i] > lista[i + 1]:

            eh_possivel = False

            break 

    if eh_possivel:

        print("YES")

    else:

        print("NO")



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: 437 ms):
num = int(input())



for _ in range(num):

    eh_possivel = True

    tam_array = int(input()) 

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

 

    for i in range(len(lista) - 1):

        menor = min(lista[i], lista[i + 1])

        lista[i] = lista[i] - menor

        lista[i + 1] = lista[i + 1] - menor

        if lista[i] > lista[i + 1]:

            eh_possivel = False

            break 

    if eh_possivel:

        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: 437 ms):
num = int(input())



for _ in range(num):

    eh_possivel = True

    tam_array = int(input()) 

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

 

    for i in range(len(lista) - 1):

        menor = min(lista[i], lista[i + 1])

        lista[i] = lista[i] - menor

        lista[i + 1] = lista[i + 1] - menor

        if lista[i] > lista[i + 1]:

            eh_possivel = False

            break 

    if eh_possivel:

        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: 311 ms):
t=int(input())

for i in range(t):

    n=int(input())

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

    i=0

    ans='YES'

    while i<len(a)-1:

        s=min(a[i],a[i+1])

        a[i]=a[i]-s

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

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

           ans='YES'

        else:

            ans='NO'

            break

        i+=1

    print(ans)

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: 311 ms):
t=int(input())

for i in range(t):

    n=int(input())

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

    i=0

    ans='YES'

    while i<len(a)-1:

        s=min(a[i],a[i+1])

        a[i]=a[i]-s

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

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

           ans='YES'

        else:

            ans='NO'

            break

        i+=1

    print(ans)

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: 311 ms):
t=int(input())

for i in range(t):

    n=int(input())

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

    i=0

    ans='YES'

    while i<len(a)-1:

        s=min(a[i],a[i+1])

        a[i]=a[i]-s

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

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

           ans='YES'

        else:

            ans='NO'

            break

        i+=1

    print(ans)

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: 311 ms):
t=int(input())

for i in range(t):

    n=int(input())

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

    i=0

    ans='YES'

    while i<len(a)-1:

        s=min(a[i],a[i+1])

        a[i]=a[i]-s

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

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

           ans='YES'

        else:

            ans='NO'

            break

        i+=1

    print(ans)

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: 311 ms):
t=int(input())

for i in range(t):

    n=int(input())

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

    i=0

    ans='YES'

    while i<len(a)-1:

        s=min(a[i],a[i+1])

        a[i]=a[i]-s

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

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

           ans='YES'

        else:

            ans='NO'

            break

        i+=1

    print(ans)

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: 311 ms):
t=int(input())

for i in range(t):

    n=int(input())

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

    i=0

    ans='YES'

    while i<len(a)-1:

        s=min(a[i],a[i+1])

        a[i]=a[i]-s

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

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

           ans='YES'

        else:

            ans='NO'

            break

        i+=1

    print(ans)

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: 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")

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: 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")

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: 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")

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: 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")

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: 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")

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: 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)

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: 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)

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: 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)

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: 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)

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: 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)

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):
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)

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):
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)

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