Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000)
n = int(input())

edges=[[] for i in range(n+1)]
d=[-1 for i in range(n+1)]
for i in range(1,n):
	a, b=[int(i) for i in input().split()]
	edges[a].append([b,1])
	edges[b].append([a,1])
def kyori(u):
	for v,w in edges[u]:
		if d[v]==-1:
			d[v]=d[u]+w
			kyori(v)

d[1]=0
kyori(1)

D = list(map(int, input().split()))
D.sort(reverse=True)

ans_lst = [0] * n
ans = sum(D[1:])

ans_lst[0] = D[0]
now_kyori = 1
idx = 1
m = 1
while m < n:
    for i in range(2, n+1):
        if d[i] == now_kyori:
            ans_lst[i-1] = D[idx]
            idx += 1
            m += 1
    now_kyori += 1


ans_lst = list(map(str, ans_lst))
mojiretu = ' '.join(ans_lst)
print(ans)
print(mojiretu)

Fixed submission:
import sys
import numpy as np
sys.setrecursionlimit(100000)
n = int(input())

edges=[[] for i in range(n+1)]
d=[-1 for i in range(n+1)]
for i in range(1,n):
	a, b=[int(i) for i in input().split()]
	edges[a].append([b,1])
	edges[b].append([a,1])
def kyori(u):
	for v,w in edges[u]:
		if d[v]==-1:
			d[v]=d[u]+w
			kyori(v)

d[1]=0
kyori(1)

D = list(map(int, input().split()))
D.sort(reverse=True)

ans_lst = [0] * n
ans = sum(D[1:])

ans_lst[0] = D[0]
now_kyori = 1
idx = 1
m = 1
idx_lst = [i for i in range(n)]

d.pop(0)

lst = np.array([d, idx_lst]).T.tolist()
lst.sort()
d = np.array(lst).T.tolist()[0]
idx_lst = np.array(lst).T.tolist()[1]

for i in range(n):
    ans_lst[idx_lst[i]] = D[i]

ans_lst = list(map(str, ans_lst))
mojiretu = ' '.join(ans_lst)
print(ans)
print(mojiretu)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000)
n = int(input())

edges=[[] for i in range(n+1)]
d=[-1 for i in range(n+1)]
for i in range(1,n):
	a, b=[int(i) for i in input().split()]
	edges[a].append([b,1])
	edges[b].append([a,1])
def kyori(u):
	for v,w in edges[u]:
		if d[v]==-1:
			d[v]=d[u]+w
			kyori(v)

d[1]=0
kyori(1)

D = list(map(int, input().split()))
D.sort(reverse=True)

ans_lst = [0] * n
ans = sum(D[1:])

ans_lst[0] = D[0]
now_kyori = 1
idx = 1
m = 1
while m < n:
    for i in range(2, n+1):
        if d[i] == now_kyori:
            ans_lst[i-1] = D[idx]
            idx += 1
            m += 1
    now_kyori += 1


ans_lst = list(map(str, ans_lst))
mojiretu = ' '.join(ans_lst)
print(ans)
print(mojiretu)

Fixed submission:
import sys
import numpy as np
sys.setrecursionlimit(100000)
n = int(input())

edges=[[] for i in range(n+1)]
d=[-1 for i in range(n+1)]
for i in range(1,n):
	a, b=[int(i) for i in input().split()]
	edges[a].append([b,1])
	edges[b].append([a,1])
def kyori(u):
	for v,w in edges[u]:
		if d[v]==-1:
			d[v]=d[u]+w
			kyori(v)

d[1]=0
kyori(1)

D = list(map(int, input().split()))
D.sort(reverse=True)

ans_lst = [0] * n
ans = sum(D[1:])

ans_lst[0] = D[0]
now_kyori = 1
idx = 1
m = 1
idx_lst = [i for i in range(n)]

d.pop(0)

lst = np.array([d, idx_lst]).T.tolist()
lst.sort()
d = np.array(lst).T.tolist()[0]
idx_lst = np.array(lst).T.tolist()[1]

for i in range(n):
    ans_lst[idx_lst[i]] = D[i]

ans_lst = list(map(str, ans_lst))
mojiretu = ' '.join(ans_lst)
print(ans)
print(mojiretu)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

ks = []
for i in range(M):
    ks.append(list(map(int, input().split())))

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

cnt = 0
for i in range(2**N): # スイッチの組み合わせ
    light = 0
    switch = format(i, f'0{N}b')
    for j in range(M): # j番目の電球はどうなってる？
        pj = p[j]
        kj = ks[j][0]
        on = 0
        for l in range(1, kj+1): # 指定されたスイッチ番号はon?
            if switch[ks[j][l]-1] == '1':
                on += 1
        if on % 2 == pj:
            light += 1
        else:
            break
    if light == M:
        cnt += 1

print(cnt)

Fixed submission:
N, M = map(int, input().split())

ks = []
for i in range(M):
    ks.append(list(map(int, input().split())))

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

cnt = 0
for i in range(2**N):
    light = 0
    switch = format(i, '0'+str(N)+'b')
    for j in range(M):
        pj = p[j]
        kj = ks[j][0]
        on = 0
        for l in range(1, kj+1):
            if switch[ks[j][l]-1] == '1':
                on += 1
        if on % 2 == pj:
            light += 1
        else:
            break
    if light == M:
        cnt += 1

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

q = int(input())

nchange = 0
ming, maxg = [], []
heapq.heapify(ming)
heapq.heapify(maxg)
a = 0
b = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        _, ai, bi = map(int, qi.split())
        b += bi
        nchange += 1
        if nchange % 2 == 1:
            if nchange == 1:
                x = ai
                continue
            Smax = -heapq.heappop(ming)
            Dmin = heapq.heappop(maxg)
            if Smax <= ai and ai <= Dmin:
                heapq.heappush(ming, -Smax)
                heapq.heappush(maxg, Dmin)
                x = ai
            elif ai < Smax:
                heapq.heappush(ming, -ai)
                heapq.heappush(maxg, Dmin)
                x = Smax
                a += Smax - ai
            else:
                heapq.heappush(ming, -Smax)
                heapq.heappush(maxg, ai)
                x = Dmin
                a += ai - Dmin
        else:
            heapq.heappush(ming, -min(x, ai))
            heapq.heappush(maxg, max(x, ai))
            a += abs(x - ai)
            x = -heapq.heappop(ming)
            heapq.heappush(ming, -x)
    else:
        print(x, a+b)

Fixed submission:
import heapq

q = int(input())

nchange = 0
ming, maxg = [], []
heapq.heapify(ming)
heapq.heapify(maxg)
a = 0
b = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        _, ai, bi = map(int, qi.split())
        b += bi
        nchange += 1
        if nchange % 2 == 1:
            if nchange == 1:
                x = ai
                continue
            Smax = -heapq.heappop(ming)
            Dmin = heapq.heappop(maxg)
            if Smax <= ai and ai <= Dmin:
                heapq.heappush(ming, -Smax)
                heapq.heappush(maxg, Dmin)
                x = ai
            elif ai < Smax:
                heapq.heappush(ming, -ai)
                heapq.heappush(maxg, Dmin)
                x = Smax
                a += Smax - ai
            else:
                heapq.heappush(ming, -Smax)
                heapq.heappush(maxg, ai)
                x = Dmin
                a += ai - Dmin
        else:
            heapq.heappush(ming, -min(x, ai))
            heapq.heappush(maxg, max(x, ai))
            a += abs(x - ai)
            x = -heapq.heappop(ming)
            heapq.heappush(ming, -x)
    else:
        print(x, a+b)
Buggy python submission (verdict: Time Limit Exceeded):
from statistics import median

R, G, B, N = map(int,input().split())
 
i = 0
j = 0
cnt = 0

maxi = max(R, G, B)
mini = min(R, G, B)
medi = median([R, G, B])

 
while i*maxi <= N:
    while i*maxi + j*medi <= N:
        if (N - i*maxi - j*medi) % mini == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1
 
print(cnt)

Fixed submission:
R, G, B, N = map(int,input().split())
 
i = 0
j = 0
cnt = 0
 
while i*R <= N:
    while i*R + j*G <= N:
        temp = N - i*R - j*G
        if temp % B == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1
 
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int,input().split())

i = 0
j = 0
cnt = 0

while i*R <= N:
    while i*R + j*G <= N:
        if (N - i*R - j*G) % B == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1

print(cnt)

Fixed submission:
R, G, B, N = map(int,input().split())
 
i = 0
j = 0
cnt = 0
 
while i*R <= N:
    while i*R + j*G <= N:
        temp = N - i*R - j*G
        if temp % B == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1
 
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int,input().split())

i = 0
j = 0
cnt = 0

while i*R <= N:
    while i*R + j*G <= N:
        if (N - i*R - j*G) % B == 0:
            cnt += 1
        j += 1
    j = 0

Fixed submission:
R, G, B, N = map(int,input().split())
 
i = 0
j = 0
cnt = 0
 
while i*R <= N:
    while i*R + j*G <= N:
        temp = N - i*R - j*G
        if temp % B == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1
 
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int,input().split())

i = 0
j = 0
cnt = 0

while i*R <= N:
    while i*R + j*G <= N:
        temp = N - i*R - j*G
        if temp % B == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1

print(cnt)

Fixed submission:
R, G, B, N = map(int,input().split())
 
i = 0
j = 0
cnt = 0
 
while i*R <= N:
    while i*R + j*G <= N:
        temp = N - i*R - j*G
        if temp % B == 0:
            cnt += 1
        j += 1
    j = 0
    i += 1
 
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

ans = 0
for i in range(1, N+1):
    if N//i == N%i:
        ans += i

print(ans)

Fixed submission:
N = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

divisors = make_divisors(N)

ans = 0
for divisor in divisors:
	if divisor != 1:
		shou = N // (divisor-1)
		amari = N % (divisor-1)
		if shou == amari and divisor-1 > amari:
				ans += divisor-1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

ans = 0
for i in range(1, N+1):
    if N//i == N%i:
        ans += i

print(ans)

Fixed submission:
N = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

divisors = make_divisors(N)

ans = 0
for divisor in divisors:
	if divisor != 1:
		shou = N // (divisor-1)
		amari = N % (divisor-1)
		if shou == amari and divisor-1 > amari:
				ans += divisor-1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import fractions
from functools import reduce
import copy
import numpy as np

N = int(input())
A = list(map(int,input().split()))
A = np.array(A)

def gcd_list(*numbers):
    return reduce(fractions.gcd, numbers)

temp = gcd_list(*A)
AA = list(A // temp)

temp2 = 0
for i in range(N):
    a = copy.copy(AA)
    a.pop(i)
    kouyaku = gcd_list(*a)
    temp2 = max(temp2, kouyaku)

print(temp * temp2)

Fixed submission:
import fractions

N = int(input())
A = list(map(int,input().split()))
l = [0] * (N+1)
r = [0] * (N+1)

for i in range(1, N+1):
    l[i] = fractions.gcd(l[i-1], A[i-1])
for i in range(1, N+1):
    r[N-i] = fractions.gcd(r[N-i+1], A[N-i])

ans = 0
for i in range(N):
    kouyaku = fractions.gcd(l[i], r[i+1])
    ans = max(ans, kouyaku)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import fractions
from functools import reduce
import copy

N = int(input())
A = list(map(int,input().split()))

def gcd_list(*numbers):
    return reduce(fractions.gcd, numbers)

ans = 0
for i in range(N):
    a = copy.copy(A)
    a.pop(i)
    kouyaku = gcd_list(*a)
    ans = max(ans, kouyaku)

print(ans)

Fixed submission:
import fractions

N = int(input())
A = list(map(int,input().split()))
l = [0] * (N+1)
r = [0] * (N+1)

for i in range(1, N+1):
    l[i] = fractions.gcd(l[i-1], A[i-1])
for i in range(1, N+1):
    r[N-i] = fractions.gcd(r[N-i+1], A[N-i])

ans = 0
for i in range(N):
    kouyaku = fractions.gcd(l[i], r[i+1])
    ans = max(ans, kouyaku)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
import copy

N = int(input())
A = list(map(int,input().split()))

def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if x%y == 0:
        return y
    else:
        while x%y != 0:
            z = x%y
            x = y
            y = z
        else:
            return z

def gcd_list(*numbers):
    return reduce(gcd, numbers)

ans = 0
for i in range(N):
    a = copy.copy(A)
    a.pop(i)
    kouyaku = gcd_list(*a)
    ans = max(ans, kouyaku)

print(ans)

Fixed submission:
import fractions

N = int(input())
A = list(map(int,input().split()))
l = [0] * (N+1)
r = [0] * (N+1)

for i in range(1, N+1):
    l[i] = fractions.gcd(l[i-1], A[i-1])
for i in range(1, N+1):
    r[N-i] = fractions.gcd(r[N-i+1], A[N-i])

ans = 0
for i in range(N):
    kouyaku = fractions.gcd(l[i], r[i+1])
    ans = max(ans, kouyaku)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import re
import itertools
import copy

N, K = map(int,input().split())
S = input()
lstS = list(S)
where0 = []

pattern = r'0+'

iterator = re.finditer(pattern ,S)
for match in iterator:
    where0.append(list(map(int, match.span())))

if len(where0) <= K:
    print(N)
    exit()

kouho = list(range(len(where0)))
conbination = list(itertools.combinations(kouho,K))

maxlength = 0
for conbi in conbination:
    temp = copy.copy(lstS)
    tempstr = ''
    for i in range(len(conbi)):
        for j in range(where0[conbi[i]][0], where0[conbi[i]][1]):
            temp[j] = '1'
    for x in temp:
        tempstr += x
    cnt = 0
    for i in range(len(tempstr)):
        if temp[i] == '1':
            cnt += 1
            if cnt > maxlength:
                maxlength = cnt
        else:
            cnt = 0

print(maxlength)

Fixed submission:
N, K = map(int,input().split())
S = input()
lstS = list(S)
block = [0]

cnt = 1
for i in range(len(S)):
    if S[i:i+1] == S[i+1:i+2]:
        cnt += 1
    else:
        block.append(cnt)
        cnt = 1
block.append(0)

acc = [0, 0]
for i in range(1, len(block)):
    wa = acc[-1]+block[i]
    acc.append(wa)

if S[0] == '0':
    start = 0
else:
    start = 1
end = len(acc) - (2*K+1)

max = 0
for i in range(start, end, 2):
    if max < acc[i+(2*K+1)] - acc[i]:
        max = acc[i+(2*K+1)] - acc[i]

if start >= end:
    max = N

if len(S) == 1:
    if K == 0 and S =='0':
        max = 0
    else:
        max = 1

print(max)
Buggy python submission (verdict: Runtime Error):
S = input()
L = 0
max_L = 0

for i in len(S):
  s = S[i]
  TF = any(['A'==s, 'C'==s, 'G'==s, 'T'==s])
  if TF:
    L += 1
  elif L >= max_L:
    max_L = L
    L = 0

print(max_L)

Fixed submission:
S = input()
L = 0
max_L = 0
 
for i in range(len(S)):
  s = S[i]
  TF = any(['A' in s, 'C' in s, 'G' in s, 'T' in s])
  if TF:
    L += 1
    if L >= max_L:
        max_L = L
  elif L >= max_L:
    max_L = L
    L = 0

print(max_L)
Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

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

print(gcd(*a))

Fixed submission:
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

print(gcd(*a))

Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

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

print(gcd(*a))

Fixed submission:
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

print(gcd(*a))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
k = int(input())

dic = {}
for i in range(n):
    if s[i] in dic:
        dic[s[i]].append(i)
    else:
        dic[s[i]] = [i]

for i in range(97, 123):
    moji = chr(i)
    if moji not in dic:
        continue
    kouho = dic[moji]
    strings = set([])
    for idx in kouho:
        e = idx+1
        while e <= n:
            strings.add(s[idx:e])
            e += 1
    if len(strings) >= k:
        strings = list(strings)
        strings.sort()
        print(strings[k-1])
        exit()
    else:
        k -= len(strings)

Fixed submission:
s = input()
n = len(s)
k = int(input())

dic = {}
for i in range(n):
    if s[i] in dic:
        dic[s[i]].append(i)
    else:
        dic[s[i]] = [i]

for i in range(97, 123):
    moji = chr(i)
    if moji not in dic:
        continue
    kouho = dic[moji]
    strings = set([])
    for idx in kouho:
        e = idx+1
        string = set([])
        while e <= n and len(string) <= k:
            string.add(s[idx:e])
            e += 1
        strings = strings | string
    if len(strings) >= k:
        strings = list(strings)
        strings.sort()
        print(strings[k-1])
        exit()
    else:
        k -= len(strings)
Buggy python submission (verdict: Runtime Error):
import math

x, y = map(int, input().split())

print(int(math.log2(y/x))+1)

Fixed submission:
x, y = map(int, input().split())

left = 0
right = 10**5
maxi = y//x
while left+1 < right:
    mid = (left+right)//2
    now = 2**mid
    if now > maxi:
        right = mid
    else:
        left = mid

print(left+1)
Buggy python submission (verdict: Time Limit Exceeded):
def calc(left, right, time, vmax):
    if left <= time and time <= right:
        return vmax
    elif time < left:
        return vmax + left - time
    else:
        return vmax + time - right

n = int(input())
t = list(map(int, input().split()))
v = list(map(int, input().split()))
tlst = [[0, t[0]]]
for i in range(1, n):
    start = tlst[-1][1]
    stop = start + t[i]
    tlst.append([start, stop])

tmax = sum(t)
vnow = 0
ans = 0
for i in range(1, 2*tmax+1):
    time = 0.5 * i
    vmin = 10**9
    for j in range(n):
        left, right = tlst[j][0], tlst[j][1]
        vmax = v[j]
        vmin = min(vmin, calc(left, right, time, vmax))
    vmin = min(vmin, calc(0, 0, time, 0))
    vmin = min(vmin, calc(tmax, tmax, time, 0))
    ans += 0.25 * (vmin + vnow)
    vnow = vmin
print(ans)

Fixed submission:
def calc(left, right, time, vmax):
    if left <= time and time <= right:
        return vmax
    elif time < left:
        return vmax + left - time
    else:
        return vmax + time - right

n = int(input())
t = list(map(int, input().split()))
v = list(map(int, input().split()))
tlst = [[0, t[0]]]
for i in range(1, n):
    start = tlst[-1][1]
    stop = start + t[i]
    tlst.append([start, stop])

tmax = sum(t)
vnow = 0
ans = 0
for i in range(1, 2*tmax+1):
    time = 0.5 * i
    vmin = 10**9
    for j in range(n):
        left, right = tlst[j][0], tlst[j][1]
        vmax = v[j]
        vmin = min(vmin, calc(left, right, time, vmax))
    vmin = min(vmin, calc(0, 0, time, 0))
    vmin = min(vmin, calc(tmax, tmax, time, 0))
    ans += 0.25 * (vmin + vnow)
    vnow = vmin
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**10)

n = int(input())
dic = {}
for i in range(n+1):
    if i <= 1: continue
    ai = int(input())
    if ai in dic:
        dic[ai].append(i)
    else:
        dic[ai] = [i]

def dfs(nxt):
    lst = []
    if nxt in dic:
        while dic[nxt] != []:
            child = dic[nxt].pop()
            tempmax = dfs(child)
            lst.append(tempmax)
    else:
        return 0
    lst.sort(reverse=True)
    for i in range(len(lst)):
        lst[i] += i+1
    return max(lst)

ans = dfs(1)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**5)

n = int(input())
dic = {}
for i in range(n+1):
    if i <= 1: continue
    ai = int(input())
    if ai in dic:
        dic[ai].append(i)
    else:
        dic[ai] = [i]

def dfs(nxt):
    lst = []
    if nxt in dic:
        while dic[nxt] != []:
            child = dic[nxt].pop()
            tempmax = dfs(child)
            lst.append(tempmax)
    else:
        return 0
    lst.sort(reverse=True)
    for i in range(len(lst)):
        lst[i] += i+1
    return max(lst)

ans = dfs(1)
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
dic = {}
for i in range(n+1):
    if i <= 1: continue
    ai = int(input())
    if ai in dic:
        dic[ai].append(i)
    else:
        dic[ai] = [i]

def dfs(nxt):
    lst = []
    if nxt in dic:
        while dic[nxt] != []:
            child = dic[nxt].pop()
            tempmax = dfs(child)
            lst.append(tempmax)
    else:
        return 0
    lst.sort(reverse=True)
    for i in range(len(lst)):
        lst[i] += i+1
    return max(lst)

ans = dfs(1)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**5)

n = int(input())
dic = {}
for i in range(n+1):
    if i <= 1: continue
    ai = int(input())
    if ai in dic:
        dic[ai].append(i)
    else:
        dic[ai] = [i]

def dfs(nxt):
    lst = []
    if nxt in dic:
        while dic[nxt] != []:
            child = dic[nxt].pop()
            tempmax = dfs(child)
            lst.append(tempmax)
    else:
        return 0
    lst.sort(reverse=True)
    for i in range(len(lst)):
        lst[i] += i+1
    return max(lst)

ans = dfs(1)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
T = ''
i = 0

while len(T) < len(S):
    if S[i:i+6] == 'eraser':
        T += 'eraser'
        i += 6
    elif S[i:i+5] == 'erase':
        T += 'erase'
        i += 5
    elif S[i:i+5] == 'dream':
        if S[i+5:i+6] == 'd':
            T += 'dream'
            i += 5
        elif S[i+5:i+8] == 'era':
            T += 'dream'
            i += 5
        elif S[i+5:i+6] == '':
            T += 'dream'
            i += 5
        elif S[i+5:i+8] == 'ere':
            T += 'dreamer'
            i += 7
        elif S[i+5:i+8] == 'erd':
            T += 'dreamer'
            i += 7
        elif S[i+5:i+8] == 'er':
            T += 'dreamer'
            i += 7
    else:
        print('NO')
        exit()

print('YES')

Fixed submission:
S = input()
T = ''
i = 0

while len(T) < len(S):
    if S[i:i+6] == 'eraser':
        T += 'eraser'
        i += 6
    elif S[i:i+5] == 'erase':
        T += 'erase'
        i += 5
    elif S[i:i+5] == 'dream':
        if S[i+5:i+6] == 'd':
            T += 'dream'
            i += 5
        elif S[i+5:i+8] == 'era':
            T += 'dream'
            i += 5
        elif S[i+5:i+6] == '':
            T += 'dream'
            i += 5
        elif S[i+5:i+8] == 'ere':
            T += 'dreamer'
            i += 7
        elif S[i+5:i+8] == 'erd':
            T += 'dreamer'
            i += 7
        elif S[i+5:i+8] == 'er':
            T += 'dreamer'
            i += 7
        else:
            T += 'dream'
            i += 5
    else:
        print('NO')
        exit()

print('YES')