Buggy python submission (verdict: Time Limit Exceeded):
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    # 時計回り
    f1 = [sum(sushi_value_cw[:i + 1]) - sushi_pos_cw[i] for i in range(N)]
    g1 = [max(f1[:i + 1]) for i in range(N)]
    f2 = [sum(sushi_value_ccw[:i + 1]) - sushi_pos_ccw[i] for i in range(N)]
    g2 = [max(f2[:i + 1]) for i in range(N)]

    tmp1 = 0
    for b in range(N + 1):  # b:反時計回りに歩いて食べる寿司の個数
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  # a:時計回りに歩いて食べる寿司の個数
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))

Fixed submission:
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    # 時計回り
    f1,g1,f2,g2=[],[],[],[]
    s1,s2=0,0
    for i in range(N):
        s1+=sushi_value_cw[i]-sushi_pos_cw[i]
        f1.append(s1)
        g1.append(s1)
        s1+=sushi_pos_cw[i]
        s2+=sushi_value_ccw[i]-sushi_pos_ccw[i]
        f2.append(s2)
        g2.append(s2)
        s2+=sushi_pos_ccw[i]
    for i in range(1,N):
        if g1[i]<g1[i-1]:
            g1[i]=g1[i-1]
        if g2[i]<g2[i-1]:
            g2[i]=g2[i-1]
    # print(f1,g1,f2,g2)
    # f1 = [sum(sushi_value_cw[:i + 1]) - sushi_pos_cw[i] for i in range(N)]
    # g1 = [max(f1[:i + 1]) for i in range(N)]
    # #反時計回り
    # f2 = [sum(sushi_value_ccw[:i + 1]) - sushi_pos_ccw[i] for i in range(N)]
    # g2 = [max(f2[:i + 1]) for i in range(N)]
    # print(f1,g1,f2,g2)
    tmp1 = 0
    for b in range(N + 1):  # b:反時計回りに歩いて食べる寿司の個数
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  # a:時計回りに歩いて食べる寿司の個数
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))

Buggy python submission (verdict: Time Limit Exceeded):
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    # 時計回り
    f1 = [sum(sushi_value_cw[:i + 1]) - sushi_pos_cw[i] for i in range(N)]
    g1 = [max(f1[:i + 1]) for i in range(N)]
    f2 = [sum(sushi_value_ccw[:i + 1]) - sushi_pos_ccw[i] for i in range(N)]
    g2 = [max(f2[:i + 1]) for i in range(N)]

    tmp1 = 0
    for b in range(N + 1):  # b:反時計回りに歩いて食べる寿司の個数
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  # a:時計回りに歩いて食べる寿司の個数
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))

Fixed submission:
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    # 時計回り
    f1,g1,f2,g2=[],[],[],[]
    s1,s2=0,0
    for i in range(N):
        s1+=sushi_value_cw[i]-sushi_pos_cw[i]
        f1.append(s1)
        g1.append(s1)
        s1+=sushi_pos_cw[i]
        s2+=sushi_value_ccw[i]-sushi_pos_ccw[i]
        f2.append(s2)
        g2.append(s2)
        s2+=sushi_pos_ccw[i]
    for i in range(1,N):
        if g1[i]<g1[i-1]:
            g1[i]=g1[i-1]
        if g2[i]<g2[i-1]:
            g2[i]=g2[i-1]
    # print(f1,g1,f2,g2)
    # f1 = [sum(sushi_value_cw[:i + 1]) - sushi_pos_cw[i] for i in range(N)]
    # g1 = [max(f1[:i + 1]) for i in range(N)]
    # #反時計回り
    # f2 = [sum(sushi_value_ccw[:i + 1]) - sushi_pos_ccw[i] for i in range(N)]
    # g2 = [max(f2[:i + 1]) for i in range(N)]
    # print(f1,g1,f2,g2)
    tmp1 = 0
    for b in range(N + 1):  # b:反時計回りに歩いて食べる寿司の個数
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  # a:時計回りに歩いて食べる寿司の個数
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))