Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def calcScore(t, s, c):
    val = 0
    for i in range(26):
        last = 0
        for j in range(len(t)):
            if t[j] == i:
                val += s[j][i]
                val -= c[i]*(j-last)*(j-last-1)//2
                last = j
        val -= c[i]*(len(t)-last)*(len(t)-last-1)//2
    return val

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

score = calcScore(t, s, c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    t[d] = q-1
    ans[i] = calcScore(t, s, c)

for v in ans:
    print(v)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def calcScore(t, s, c):
    scores = [0]*26
    lasts = [0]*26
    for i in range(1, len(t)):
        scores[t[i]] += s[i][t[i]]
        dif = i - lasts[t[i]]
        scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
        lasts[t[i]] = i
    for i in range(26):
        dif = len(t) - lasts[i]
        scores[i] -= c[i] * dif * (dif-1) // 2
    return scores

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

scores = calcScore(t, s, c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    t[d] = q-1
    ans[i] = sum(calcScore(t, s, c))

for v in ans:
    print(v)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def update(day, t, q, score, c, last):
    t[day] = q
    for i in range(day, len(score)):
        for j in range(26):
            last[i][j] = last[i-1][j]
        score[i] = score[i-1] + s[i][t[i]]
        last[i][t[i]] = i
        score[i] -= penalty(i, last[i], c)

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

last = [[0]*26 for _ in range(D+1)]
score = [0]*(D+1)

for i in range(1, D+1):
    for j in range(26):
        last[i][j] = last[i-1][j]
    score[i] = score[i-1] + s[i][t[i]]
    last[i][t[i]] = i
    score[i] -= penalty(i, last[i], c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    update(d, t, q-1, score, c, last)
    ans[i] = score[-1]

for v in ans:
    print(v)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def calcScore(t, s, c):
    scores = [0]*26
    lasts = [0]*26
    for i in range(1, len(t)):
        scores[t[i]] += s[i][t[i]]
        dif = i - lasts[t[i]]
        scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
        lasts[t[i]] = i
    for i in range(26):
        dif = len(t) - lasts[i]
        scores[i] -= c[i] * dif * (dif-1) // 2
    return scores

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

scores = calcScore(t, s, c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    t[d] = q-1
    ans[i] = sum(calcScore(t, s, c))

for v in ans:
    print(v)

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

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def update(day, t, q, score, c, last):
    t[day] = q
    for i in range(day, len(score)):
        last[i] = deepcopy(last[i-1])
        score[i] = score[i-1] + s[i][t[i]]
        last[i][t[i]] = i
        score[i] -= penalty(i, last[i], c)

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

last = [[0]*26 for _ in range(D+1)]
score = [0]*(D+1)

for i in range(1, D+1):
    last[i] = deepcopy(last[i-1])
    score[i] = score[i-1] + s[i][t[i]]
    last[i][t[i]] = i
    score[i] -= penalty(i, last[i], c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    update(d, t, q-1, score, c, last)
    ans[i] = score[-1]

for v in ans:
    print(v)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def calcScore(t, s, c):
    scores = [0]*26
    lasts = [0]*26
    for i in range(1, len(t)):
        scores[t[i]] += s[i][t[i]]
        dif = i - lasts[t[i]]
        scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
        lasts[t[i]] = i
    for i in range(26):
        dif = len(t) - lasts[i]
        scores[i] -= c[i] * dif * (dif-1) // 2
    return scores

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

scores = calcScore(t, s, c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    t[d] = q-1
    ans[i] = sum(calcScore(t, s, c))

for v in ans:
    print(v)

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

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def update(day, t, q, score, c, last):
    t[day] = q
    for i in range(day, len(score)):
        last[i] = deepcopy(last[i-1])
        score[i] = score[i-1] + s[i][t[i]]
        last[i][t[i]] = i
        score[i] -= penalty(i, last[i], c)

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

last = [[0]*26 for _ in range(D+1)]
score = [0]*(D+1)

for i in range(1, D+1):
    last[i] = deepcopy(last[i-1])
    score[i] = score[i-1] + s[i][t[i]]
    last[i][t[i]] = i
    score[i] -= penalty(i, last[i], c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    update(d, t, q-1, score, c, last)
    ans[i] = score[-1]

for v in ans:
    print(v)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def calcScore(t, s, c):
    scores = [0]*26
    lasts = [0]*26
    for i in range(1, len(t)):
        scores[t[i]] += s[i][t[i]]
        dif = i - lasts[t[i]]
        scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
        lasts[t[i]] = i
    for i in range(26):
        dif = len(t) - lasts[i]
        scores[i] -= c[i] * dif * (dif-1) // 2
    return scores

D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
    s[i] = list(map(int, input().split()))
t = [-1]*(D+1)
for i in range(1, D+1):
    t[i] = int(input()) - 1
M = int(input())

scores = calcScore(t, s, c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    t[d] = q-1
    ans[i] = sum(calcScore(t, s, c))

for v in ans:
    print(v)