Problem p02618 (python)

Fixed submission:
d = int(input())
c = list(map(int,input().split()))
s = []
for i in range(d):
  si = list(map(int,input().split()))
  s.append(si)
lastd = [-1 for _ in range(26)]
for i in range(d):
  mx = 0
  mj = 0
  #s = 0
  #for j in range(26):
    #s += (i-lastd[j])*c[j]
  for j in range(26):
    if s[i][j] + (i-lastd[j])*c[j] > mx:
      mx = s[i][j] + (i-lastd[j])*c[j]
      mj = j
  lastd[mj] = i
  print(mj+1)

Buggy submission (verdict: Runtime Error):
import copy
import random
d = int(input())
c = list(map(int,input().split()))
s = []
t = [0 for _ in range(d)]
for i in range(d):
  si = list(map(int,input().split()))
  s.append(si)
lastd = [-1 for _ in range(26)]
for i in range(d):
  mx = 0
  mj = 0
  for j in range(26):
    if s[i][j] + (i-lastd[j])*c[j] > mx:
      mx = s[i][j] + (i-lastd[j])*c[j]
      mj = j
  lastd[mj] = i
  t[i] = mj
lastd = [-1 for _ in range(26)]
ans = 0
for i in range(d):
  di = t[i]
  lastd[di-1] = i
  ans += s[i][di-1]
  for j in range(26):
    ans -= (i-lastd[j])*c[j]
mxans = ans
mxt = copy.deepcopy(t)
for time in range(80000):
  da = random.randint(0, d-1)
  qb = t[da]
  if random.random() < 0.7:
    bord = c[qb]*(0.7)
    qac = []
    cap = qac.append
    for i in range(0,qb):
      if s[da][i] > bord:
        cap(i)
      continue
    for i in range(qb+1,26):
      if s[da][i] > bord:
        cap(i)
      continue
    if len(qac) == 0:
      continue
    qa = random.choice(qac)
  else:
    bord = c[qb]//2
    qac = []
    cap = qac.append 
    for i in range(0,qb):
      if c[i] > bord:
        cap(i)
    for i in range(qb+1,26):
      if c[i] > bord:
        cap(i)
    if len(qac) == 0:
      continue
    qa = random.choice(qac) 
  #print(da,qb,qa,t)
  qb1 = -1
  qa1 = -1
  for i in range(da-1,-1,-1):
    if t[i] == qb:
      qb1 = i
      break
  for i in range(da-1,-1,-1):
    if t[i] == qa:
      qa1 = i
      break
  qb2 = d
  qa2 = d
  for i in range(da+1,d):
    if t[i] == qb:
      qb2 = i
      break
  for i in range(da+1,d):
    if t[i] == qa:
      qa2 = i
      break
  delta = -s[da][qb]+s[da][qa]-c[qb]*(qb2-da)*(da-qb1)+c[qa]*(qa2-da)*(da-qa1)
  if delta > 0:
    t[da] = qa
    ans += delta
    if ans > mxans:
      mxans = ans
      mxt = copy.deepcopy(t)
  else:
    if random.random() < math.exp(delta/(10*time)):
      t[da] = qa
  #print(mxans)
for ansd in mxt:
  print(ansd+1)

Buggy submission (verdict: Runtime Error):
import copy
import random
d = int(input())
c = list(map(int,input().split()))
s = []
t = [0 for _ in range(d)]
for i in range(d):
  si = list(map(int,input().split()))
  s.append(si)
lastd = [-1 for _ in range(26)]
for i in range(d):
  mx = 0
  mj = 0
  for j in range(26):
    if s[i][j] + (i-lastd[j])*c[j] > mx:
      mx = s[i][j] + (i-lastd[j])*c[j]
      mj = j
  lastd[mj] = i
  t[i] = mj
lastd = [-1 for _ in range(26)]
ans = 0
for i in range(d):
  di = t[i]
  lastd[di-1] = i
  ans += s[i][di-1]
  for j in range(26):
    ans -= (i-lastd[j])*c[j]
mxans = ans
mxt = copy.deepcopy(t)
for time in range(80000):
  da = random.randint(0, d-1)
  qb = t[da]
  if random.random() < 0.7:
    bord = c[qb]*(0.7)
    qac = []
    cap = qac.append
    for i in range(0,qb):
      if s[da][i] > bord:
        cap(i)
      continue
    for i in range(qb+1,26):
      if s[da][i] > bord:
        cap(i)
      continue
    if len(qac) == 0:
      continue
    qa = random.choice(qac)
  else:
    bord = c[qb]//2
    qac = []
    cap = qac.append 
    for i in range(0,qb):
      if c[i] > bord:
        cap(i)
    for i in range(qb+1,26):
      if c[i] > bord:
        cap(i)
    if len(qac) == 0:
      continue
    qa = random.choice(qac) 
  #print(da,qb,qa,t)
  qb1 = -1
  qa1 = -1
  for i in range(da-1,-1,-1):
    if t[i] == qb:
      qb1 = i
      break
  for i in range(da-1,-1,-1):
    if t[i] == qa:
      qa1 = i
      break
  qb2 = d
  qa2 = d
  for i in range(da+1,d):
    if t[i] == qb:
      qb2 = i
      break
  for i in range(da+1,d):
    if t[i] == qa:
      qa2 = i
      break
  delta = -s[da][qb]+s[da][qa]-c[qb]*(qb2-da)*(da-qb1)+c[qa]*(qa2-da)*(da-qa1)
  if delta > 0:
    t[da] = qa
    ans += delta
    if ans > mxans:
      mxans = ans
      mxt = copy.deepcopy(t)
  else:
    if random.random() < math.exp(delta/(10*time)):
      t[da] = qa
  #print(mxans)
for ansd in mxt:
  print(ansd+1)