Block Adventure (Codeforces 1200B)
Tags: Greedy, dp

Gildong is playing a video game called Block Adventure. In Block Adventure, there are $$$n$$$ columns of blocks in a row, and the columns are numbered from $$$1$$$ to $$$n$$$. All blocks have equal heights. The height of the $$$i$$$-th column is represented as $$$h_i$$$, which is the number of blocks stacked in the $$$i$$$-th column.Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the $$$1$$$-st column. The goal of the game is to move the character to the top of the $$$n$$$-th column.The character also has a bag that can hold infinitely many blocks. When the character is on the top of the $$$i$$$-th column, Gildong can take one of the following three actions as many times as he wants:   if there is at least one block on the column, remove one block from the top of the $$$i$$$-th column and put it in the bag;  if there is at least one block in the bag, take one block out of the bag and place it on the top of the $$$i$$$-th column;  if $$$i < n$$$ and $$$|h_i - h_{i+1}| \le k$$$, move the character to the top of the $$$i+1$$$-st column. $$$k$$$ is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. In actions of the first two types the character remains in the $$$i$$$-th column, and the value $$$h_i$$$ changes.The character initially has $$$m$$$ blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question.

Input: Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows.The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le 10^6$$$, $$$0 \le k \le 10^6$$$) — the number of columns in the game, the number of blocks in the character's bag at the beginning, and the non-negative integer $$$k$$$ described in the statement.The second line of each test case contains $$$n$$$ integers. The $$$i$$$-th integer is $$$h_i$$$ ($$$0 \le h_i \le 10^6$$$), the initial height of the $$$i$$$-th column.

Output: For each test case, print "YES" if it is possible to win the game. Otherwise, print "NO".You can print each letter in any case (upper or lower).

Note: In the first case, Gildong can take one block from the $$$1$$$-st column, move to the $$$2$$$-nd column, put the block on the $$$2$$$-nd column, then move to the $$$3$$$-rd column.In the second case, Gildong has to put the block in his bag on the $$$1$$$-st column to get to the $$$2$$$-nd column. But it is impossible to get to the $$$3$$$-rd column because $$$|h_2 - h_3| = 3 > k$$$ and there is no way to decrease the gap.In the fifth case, the character is already on the $$$n$$$-th column from the start so the game is won instantly.

Sample Inputs:
5
3 0 1
4 3 5
3 1 2
1 4 7
4 10 0
10 20 10 20
2 5 5
0 11
1 9 9
99

Sample Outputs:
YES
NO
YES
NO
YES

Solution (Python):
def main():
    t = int(input())
    for _ in range(t):
        cnt = 0
        (n, m, k) = map(int, input().split())
        h = list(map(int, input().split()))
        for i in range(n - 1):
            mx = max(0, h[i + 1] - k)
            m += h[i] - mx
            if m < 0:
                cnt = 1
                break
        if cnt == 1:
            print('NO')
        else:
            print('YES')
if __name__ == '__main__':
    main()

Solution (Python):
from collections import *
from itertools import *
from bisect import *
from heapq import *
from math import ceil, prod, gcd
mod = 10 ** 9 + 7
d = ((1, 0), (0, 1), (0, -1), (-1, 0))
alpha = 'abcdefghijklmnopqrstuvwxyz'

def helper(m):
    for i in range(n - 1):
        m = m + arr[i] - max(0, arr[i + 1] - k)
        if m < 0:
            return 'no'
    return 'yes'

def pivot(m):
    return m - m // n < k
for _ in range(int(input())):
    (n, m, k) = map(int, input().split())
    arr = list(map(int, input().split()))
    print(helper(m))

Solution (Python):
R = lambda : map(int, input().split())
for _ in [0] * int(input()):
    (n, m, k) = R()
    h = (*R(),)
    l = 0
    for (x, y) in zip(h, h[1:]):
        m += x - max(0, y - k)
        l |= m
    print('YNEOS'[l < 0::2])