Problem: There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.  For each square, you will perform either of the following operations once:    * Decrease the height of the square by 1.   * Do nothing.  Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Submission attempts (in order):
N = int(input())
H = list(map(int, input().split(' ')))

is_trimmed = [False] * N
max_v = 0
max_ids = []
succeed = True
for i in range(N):
    diff = max_v - H[i]
    if diff > 1:
        print('No')
        succeed = False
        break
    elif diff == 1:
        for j in max_ids:
            if is_trimmed[j]:
                print('No')
                succeed = False
                break
            is_trimmed[j] = True
        max_ids.append(i)
    elif diff == 0:
        max_ids.append(i)
    elif diff < 0:
        max_v = max(max_v, H[i])
        max_ids = [i]
    # print(max_v, max_ids, is_trimmed)

if succeed:
    print('Yes')


N = int(input())
H = list(map(int, input().split(' ')))

is_trimmed = [False] * N
max_v = 0
max_ids = []
for i in range(N):
    if max_v > H[i]:
        if max_v - H[i] > 1:
            print('No')
            exit(0)
        if max_v - H[i] == 1:
            for j in max_ids:
                if is_trimmed[j]:
                    print('No')
                    exit(0)
                is_trimmed[j] = True
    elif max_v == H[i]:
        max_ids.append(i)
    elif max_v < H[i]:
        max_v = max(max_v, H[i])
        max_ids = [i]
    print(max_v, max_ids, is_trimmed)
print('Yes')


N = int(input())
H = list(map(int, input().split(' ')))

is_trimmed = [False] * N
max_v = 0
max_ids = []
succeed = True
for i in range(N):
    if max_v > H[i]:
        if max_v - H[i] > 1:
            print('No')
            succeed = False
            break
        if max_v - H[i] == 1:
            for j in max_ids:
                if is_trimmed[j]:
                    print('No')
                    succeed = False
                    break
                is_trimmed[j] = True
    elif max_v == H[i]:
        max_ids.append(i)
    elif max_v < H[i]:
        max_v = max(max_v, H[i])
        max_ids = [i]
    print(max_v, max_ids, is_trimmed)

if succeed:
    print('Yes')


N = int(input())
H = list(map(int, input().split(' ')))

is_trimmed = [False] * N
max_v = 0
max_ids = []
succeed = True
for i in range(N):
    if max_v > H[i]:
        if max_v - H[i] > 1:
            print('No')
            succeed = False
            break
        if max_v - H[i] == 1:
            for j in max_ids:
                if is_trimmed[j]:
                    print('No')
                    succeed = False
                    break
                is_trimmed[j] = True
    elif max_v == H[i]:
        max_ids.append(i)
    elif max_v < H[i]:
        max_v = max(max_v, H[i])
        max_ids = [i]
    # print(max_v, max_ids, is_trimmed)

if succeed:
    print('Yes')


N = int(input())
H = list(map(int, input().split(' ')))

is_trimmed = [False] * N
max_v = 0
max_ids = []
succeed = True
for i in range(N):
    diff = max_v - H[i]
    if diff > 1:
        print('No')
        succeed = False
        exit(0)
    elif diff == 1:
        for j in max_ids:
            if is_trimmed[j]:
                print('No')
                succeed = False
                exit(0)
            is_trimmed[j] = True
        max_ids.append(i)
        max_v -= 1
    elif diff == 0:
        max_ids.append(i)
    elif diff < 0:
        max_v = max(max_v, H[i])
        max_ids = [i]
    # print(max_v, max_ids, is_trimmed)

if succeed:
    print('Yes')


Verdicts: Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Accepted