Problem p02618 (python)

Fixed submission:
def solve():
  D = int(input())
  C = list(map(int, input().split()))
  S = [list(map(int, input().split())) for _ in range(D)]

  ans = [1]*D
  
  return ans
print(*solve(),sep='\n')

Buggy submission (verdict: Runtime Error):
import time
from bisect import *
from random import randint
start = time.time()
def solve():
  D = int(input())
  C = list(map(int, input().split()))
  S = [list(map(int, input().split())) for _ in range(D)]

  ans = [0]*D
  points = [0]*26
  dates = [[-1] for _ in range(26)]
  last = [-1]*26
  for d in range(D):
    for i in range(26):
      points[i] = S[d][i]+C[i]*(d-last[i])*(d-last[i]+1)//2
    maxi = 0
    for i in range(26):
        if points[i]>maxi:
            con = i
            maxi = points[i]
    ans[d] = con+1
    last[con] = d
  score = 0
  for d in range(D):
    dates[ans[d]].append(d)
    score += S[d][ans[d]]
    last[ans[d]] = d
    for j in range(26):
        score -= C[j]*(d-last[j])
  for i in range(26):
    dates[i].append(D)
  # print(score)
  for _ in range(10000):
    d = randint(0,D-1)
    after = randint(0,25)
    before = ans[d]
    new_score = score
    new_score += S[d][after]-S[d][before]
    ind_bef = bisect_right(dates[before],d)-1
    # print(ind_bef)
    new_score -= C[before]*(before-dates[before][ind_bef-1])*(dates[before][ind_bef+1]-before)
    ind_aft = bisect_right(dates[after],d)
    new_score += C[after]*(after-dates[after][ind_aft-1])*(dates[after][ind_aft]-after)
    if new_score>score:
      score = new_score
      ans[d] = after
      insort_right(dates[after],d)
      dates[before].pop(ind_bef)
      print(score)
  return ans
print(*solve(),sep='\n')

Buggy submission (verdict: Runtime Error):
import time
from bisect import *
from random import randint
start = time.time()
def solve():
  D = int(input())
  C = list(map(int, input().split()))
  S = [list(map(int, input().split())) for _ in range(D)]

  ans = [0]*D
  points = [0]*26
  dates = [[-1] for _ in range(26)]
  last = [-1]*26
  for d in range(D):
    for i in range(26):
      points[i] = S[d][i]+C[i]*(d-last[i])*(d-last[i]+1)//2
    maxi = 0
    for i in range(26):
        if points[i]>maxi:
            con = i
            maxi = points[i]
    ans[d] = con+1
    last[con] = d
  score = 0
  for d in range(D):
    dates[ans[d]].append(d)
    score += S[d][ans[d]]
    last[ans[d]] = d
    for j in range(26):
        score -= C[j]*(d-last[j])
  for i in range(26):
    dates[i].append(D)
  while time.time()-start<1.8:
    d = randint(0,D-1)
    after = randint(0,25)
    before = ans[d]
    ans[d] = after
    insort_right(dates[after],d)
    new_score = score
    new_score += S[d][after]-S[d][before]
    ind_bef = bisect_right(dates[before],d)-1
    new_score -= C[before]*(dates[before][ind_bef]-dates[before][ind_bef-1])*(dates[before][ind_bef+1]-dates[before][ind_bef])
    ind_aft = bisect_right(dates[after],d)-1
    new_score += C[after]*(dates[after][ind_aft]-dates[after][ind_aft-1])*(dates[after][ind_aft+1]-dates[after][ind_aft])
    dates[before].pop(ind_bef)
    if new_score>score:
      score = new_score
    else:
      ans[d] = before
  return ans
print(*solve(),sep='\n')