Buggy python submission (verdict: Time Limit Exceeded):
D = int(input())
C = list(map(int,input().split()))
S = [list(map(int,input().split()))for _ in range(D)]

SELECT = []
for i in range(D):
    SELECT.append(int(input())-1)

M = int(input())
d = []
q = []
for _ in range(M):
    td, tq = map(int,input().split())
    d.append(td-1)
    q.append(tq-1)

L_ALL = [[0]*26 for _ in range(D)]
L = [0]*26
ans = 0
for i in range(D):
    ans += S[i][SELECT[i]]
    L[SELECT[i]]=i+1
    if i != 0:
        L_ALL[i] = L_ALL[i-1][:]
    L_ALL[i][SELECT[i]] = i+1
    for j in range(26):
        ans -= C[j]*(i+1-L[j])

for i in range(M):
    day = d[i]
    before = SELECT[day]
    after = q[i]
    ans -= S[day][before]
    ans += S[day][after]
    SELECT[day] = q[i]

    if day == 0:
        tmp = 0
    else:
        tmp = L_ALL[day-1][before]
    j = day
    while 0<=j<D and L_ALL[j][before]==day+1:
        L_ALL[j][before] = tmp
        ans -= C[before]*(day+1-tmp)
        j += 1

    if day == 0:
        tmp = 0
    else:
        tmp = L_ALL[day-1][after]
    j = day
    while 0<=j<D and L_ALL[j][after]==tmp:
        L_ALL[j][after]=day+1
        ans += C[after] * (day+1-tmp)
        j += 1

    print(ans)

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

    SELECT = []
    for i in range(D):
        SELECT.append(int(input())-1)

    M = int(input())
    d = []
    q = []
    for _ in range(M):
        td, tq = map(int,input().split())
        d.append(td-1)
        q.append(tq-1)

    L_ALL = [[0]*26 for _ in range(D)]
    L = [0]*26
    ans = 0
    for i in range(D):
        ans += S[i][SELECT[i]]
        L[SELECT[i]]=i+1
        if i != 0:
            L_ALL[i] = L_ALL[i-1][:]
        L_ALL[i][SELECT[i]] = i+1
        for j in range(26):
            ans -= C[j]*(i+1-L[j])

    for i in range(M):
        day = d[i]
        before = SELECT[day]
        after = q[i]
        ans -= S[day][before]
        ans += S[day][after]
        SELECT[day] = q[i]

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][before]
        j = day
        while 0<=j<D and L_ALL[j][before]==day+1:
            L_ALL[j][before] = tmp
            ans -= C[before]*(day+1-tmp)
            j += 1

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][after]
        j = day
        while 0<=j<D and L_ALL[j][after]==tmp:
            L_ALL[j][after]=day+1
            ans += C[after] * (day+1-tmp)
            j += 1

        print(ans)

if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from _collections import deque
def main():
    D = int(input())
    C = list(map(int,input().split()))
    S = deque([])
    for _ in range(D):
        S.append(list(map(int,input().split())))

    SELECT = deque([])
    for i in range(D):
        SELECT.append(int(input())-1)

    def score(selects):
        score = 0
        L = [0 for _ in range(26)]
        def calc(ans, select, n):
            for i, l in enumerate(L):
                if i != select:
                    ans -= C[i] * (n - L[i])
                else:
                    L[i] = n
            return ans

        for i,select in enumerate(selects):
            score += S[i][select]

            for j, l in enumerate(L):
                if j != select:
                    score -= C[j] * (i+1 - L[j])
                else:
                    L[j] = i+1
        return score

    M = int(input())
    for _ in range(M):
        i, s = map(int,input().split())
        SELECT[i-1]=s-1
        print(score(SELECT))

if __name__=="__main__":
    main()

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

    SELECT = []
    for i in range(D):
        SELECT.append(int(input())-1)

    M = int(input())
    d = []
    q = []
    for _ in range(M):
        td, tq = map(int,input().split())
        d.append(td-1)
        q.append(tq-1)

    L_ALL = [[0]*26 for _ in range(D)]
    L = [0]*26
    ans = 0
    for i in range(D):
        ans += S[i][SELECT[i]]
        L[SELECT[i]]=i+1
        if i != 0:
            L_ALL[i] = L_ALL[i-1][:]
        L_ALL[i][SELECT[i]] = i+1
        for j in range(26):
            ans -= C[j]*(i+1-L[j])

    for i in range(M):
        day = d[i]
        before = SELECT[day]
        after = q[i]
        ans -= S[day][before]
        ans += S[day][after]
        SELECT[day] = q[i]

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][before]
        j = day
        while 0<=j<D and L_ALL[j][before]==day+1:
            L_ALL[j][before] = tmp
            ans -= C[before]*(day+1-tmp)
            j += 1

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][after]
        j = day
        while 0<=j<D and L_ALL[j][after]==tmp:
            L_ALL[j][after]=day+1
            ans += C[after] * (day+1-tmp)
            j += 1

        print(ans)

if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from _collections import deque
D = int(input())
C = list(map(int,input().split()))
S = deque([])
for _ in range(D):
    S.append(list(map(int,input().split())))

SELECT = deque([])
for i in range(D):
    SELECT.append(int(input())-1)

def score(selects):
    score = 0
    L = [0 for _ in range(26)]
    def calc(ans, select, n):
        for i, l in enumerate(L):
            if i != select:
                ans -= C[i] * (n - L[i])
            else:
                L[i] = n
        return ans

    for i,select in enumerate(selects):
        score += S[i][select]

        for j, l in enumerate(L):
            if j != select:
                score -= C[j] * (i+1 - L[j])
            else:
                L[j] = i+1
    return score

M = int(input())
for _ in range(M):
    i, s = map(int,input().split())
    SELECT[i-1]=s-1
    print(score(SELECT))


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

    SELECT = []
    for i in range(D):
        SELECT.append(int(input())-1)

    M = int(input())
    d = []
    q = []
    for _ in range(M):
        td, tq = map(int,input().split())
        d.append(td-1)
        q.append(tq-1)

    L_ALL = [[0]*26 for _ in range(D)]
    L = [0]*26
    ans = 0
    for i in range(D):
        ans += S[i][SELECT[i]]
        L[SELECT[i]]=i+1
        if i != 0:
            L_ALL[i] = L_ALL[i-1][:]
        L_ALL[i][SELECT[i]] = i+1
        for j in range(26):
            ans -= C[j]*(i+1-L[j])

    for i in range(M):
        day = d[i]
        before = SELECT[day]
        after = q[i]
        ans -= S[day][before]
        ans += S[day][after]
        SELECT[day] = q[i]

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][before]
        j = day
        while 0<=j<D and L_ALL[j][before]==day+1:
            L_ALL[j][before] = tmp
            ans -= C[before]*(day+1-tmp)
            j += 1

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][after]
        j = day
        while 0<=j<D and L_ALL[j][after]==tmp:
            L_ALL[j][after]=day+1
            ans += C[after] * (day+1-tmp)
            j += 1

        print(ans)

if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
D = int(input())
C = list(map(int,input().split()))
S = []
for _ in range(D):
    S.append(list(map(int,input().split())))

SELECT = []
for i in range(D):
    SELECT.append(int(input())-1)

def score(selects):
    score = 0
    L = [0 for _ in range(26)]
    def calc(ans, select, n):
        for i, l in enumerate(L):
            if i != select:
                ans -= C[i] * (n - L[i])
            else:
                L[i] = n
        return ans

    for i,select in enumerate(selects):
        score += S[i][select]
        score = calc(score, select, i+1)
    return score

M = int(input())
for _ in range(M):
    i, s = map(int,input().split())
    SELECT[i-1]=s-1
    print(score(SELECT))


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

    SELECT = []
    for i in range(D):
        SELECT.append(int(input())-1)

    M = int(input())
    d = []
    q = []
    for _ in range(M):
        td, tq = map(int,input().split())
        d.append(td-1)
        q.append(tq-1)

    L_ALL = [[0]*26 for _ in range(D)]
    L = [0]*26
    ans = 0
    for i in range(D):
        ans += S[i][SELECT[i]]
        L[SELECT[i]]=i+1
        if i != 0:
            L_ALL[i] = L_ALL[i-1][:]
        L_ALL[i][SELECT[i]] = i+1
        for j in range(26):
            ans -= C[j]*(i+1-L[j])

    for i in range(M):
        day = d[i]
        before = SELECT[day]
        after = q[i]
        ans -= S[day][before]
        ans += S[day][after]
        SELECT[day] = q[i]

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][before]
        j = day
        while 0<=j<D and L_ALL[j][before]==day+1:
            L_ALL[j][before] = tmp
            ans -= C[before]*(day+1-tmp)
            j += 1

        if day == 0:
            tmp = 0
        else:
            tmp = L_ALL[day-1][after]
        j = day
        while 0<=j<D and L_ALL[j][after]==tmp:
            L_ALL[j][after]=day+1
            ans += C[after] * (day+1-tmp)
            j += 1

        print(ans)

if __name__=="__main__":
    main()