Problem p02618 (python)

Fixed submission:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


D = int(input())
c = list(map(int, input().split()))
s = [None]*D
for i in range(D):
    s[i] = list(map(int, input().split()))
    
current = [0]*26
ans = [None]*D
def greedy():
    ans = [None]*D
    for i,ss in enumerate(s):
        vs = [None]*26
        for j in range(26):
            vs[j] = (ss[j] + current[j], j)
            current[j] += c[j]
        vs.sort()
        v = vs[-1][1]
        ans[i] = v+1
        current[v] = 0
    return ans
ans = greedy()
write("\n".join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


# import time, random
# start = time.time()

D = int(input())
c = list(map(int, input().split()))
s = [None]*D
for i in range(D):
    s[i] = list(map(int, input().split()))
    
current = [0]*26
ans = [None]*D
def greedy(weight=1.0):
    ans = [None]*D
    for i,ss in enumerate(s):
        vs = [None]*26
        for j in range(26):
            vs[j] = (ss[j] + current[j]*weight, j)
            current[j] += c[j]
        vs.sort()
        v = vs[-1][1]
        ans[i] = v+1
        current[v] = 0
    return ans
def val(ans):
    current = [0]*26
    val = 0
    for i,(ss, v) in enumerate(zip(s, ans)):
        for j in range(26):
            current[j] += c[j]
        current[v-1] = 0
        val += ss[i] - sum(current)
    return val

ans = greedy()
best = ans
bval = val(ans)
num = 2
ren = 0
w = 1.0

for w in range(100, 500, 10):
    weight = w/100
    tmpans = greedy(weight)
    tmpval = val(tmpans)
    if tmpval>bval:
        bval = tmpval
        best = tmpans
#         print(bval)
    
write("\n".join(map(str, best)))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


import time, random
start = time.time()

D = int(input())
c = list(map(int, input().split()))
s = [None]*D
for i in range(D):
    s[i] = list(map(int, input().split()))
    
current = [0]*26
ans = [None]*D
def greedy():
    ans = [None]*D
    for i,ss in enumerate(s):
        vs = [None]*26
        for j in range(26):
            vs[j] = (ss[j] + current[j], j)
            current[j] += c[j]
        vs.sort()
        v = vs[-1][1]
        ans[i] = v+1
        current[v] = 0
    return ans
def val(ans):
    current = [0]*26
    val = 0
    for i,(ss, v) in enumerate(zip(s, ans)):
        for j in range(26):
            current[j] += c[j]
        current[v-1] = 0
        val += ss[i] - sum(current)
    return val
ans = greedy()
num = 2
ren = 0
while (time.time()-start)<=1.90:
    v = val(ans)
#     print(v)
    index = random.sample(range(D),num)
    prev = index[0]
    tmp = ans[prev]
    tmpans = ans[:]
    for ind in index[1:]:
        ans[prev] = ans[ind]
        prev = ind
    ans[index[-1]] = tmp
    if v<val(ans):
        pass
    else:
        ren += 1
        if ren>=100:
            num = min(num+1, D)
            res = 0
        ans = tmpans
# print(v)
write("\n".join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


import time, random
start = time.time()

D = int(input())
c = list(map(int, input().split()))
s = [None]*D
for i in range(D):
    s[i] = list(map(int, input().split()))
    
current = [0]*26
ans = [None]*D
def greedy():
    ans = [None]*D
    for i,ss in enumerate(s):
        vs = [None]*26
        for j in range(26):
            vs[j] = (ss[j] + current[j], j)
            current[j] += c[j]
        vs.sort()
        v = vs[-1][1]
        ans[i] = v+1
        current[v] = 0
    return ans
def val(ans):
    current = [0]*26
    val = 0
    for i,(ss, v) in enumerate(zip(s, ans)):
        for j in range(26):
            current[j] += c[j]
        current[v-1] = 0
        val += ss[i] - sum(current)
    return val
ans = greedy()
num = 2
ren = 0
while (time.time()-start)<=1.90:
    v = val(ans)
#     print(v)
    index = random.sample(range(D),num)
    prev = index[0]
    tmp = ans[prev]
    tmpans = ans[:]
    for ind in index[1:]:
        ans[prev] = ans[ind]
        prev = ind
    ans[index[-1]] = tmp
    if v<val(ans):
        pass
    else:
        ren += 1
        if ren>=100:
            num = min(num+1, D)
            res = 0
        ans = tmpans
# print(v)
write("\n".join(map(str, ans)))