Problem:
"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.  Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.  Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.  Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Incorrect submission (verdict: Wrong Answer):
def solve(xv_sorted):
    x = [False for _ in range(n)]
    y = [False for _ in range(n)]

    s = 0
    buf = (- 1) * float('inf')
    for i in range(n):
        s += xv_sorted[i][1]##cal 
        x[i] = max(buf, s - 2 * xv_sorted[i][0])##pos
        buf = x[i]

    print(x)

    s = 0
    buf = (- 1) * float('inf')
    for i in sorted(range(n), reverse=True):
        s += xv_sorted[i][1]##cal 
        y[i] = max(buf, s - (c - xv_sorted[i][0]))##pos
        buf = y[i]

    print(y)

    ans = max(x[n - 1], y[0])
    for i in range(n - 1):
        ans = max(ans, x[i] + y[i + 1])

    return ans

if __name__ == '__main__':
    n, c = map(int, input().split())
    xv = []
    for i in range(n):
        xv.append(list(map(int, input().split())))
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = 0
    ans = max(ans, solve(xv_sorted))

    for i in range(n):
        xv_sorted[i][0] = abs(c - xv_sorted[i][0]) 
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = max(ans, solve(xv_sorted))
    print(ans)

Fixed, accepted submission:
def main():
    from collections import namedtuple
    import sys
    input = sys.stdin.readline

    Sushi = namedtuple('Sushi', 'x cal')

    n, c = map(int, input().split())

    a = []
    for _ in range(n):
        x, v = map(int, input().split())
        a.append(Sushi(x=x, cal=v))
    

    clock = [0] * (n + 1)  
    clock_to_0 = [0] * (n + 1)  
    ma = 0  
    ma0 = 0  
    curr = 0  
    for i, s in enumerate(a, start=1):
        curr += s.cal
        ma = max(ma, curr - s.x)
        ma0 = max(ma0, curr - s.x * 2)
        clock[i] = ma
        clock_to_0[i] = ma0

    anti = [0] * (n + 1)  
    anti_to_0 = [0] * (n + 1)  
    ma = 0  
    ma0 = 0  
    curr = 0  
    for i, s in zip(range(n, -1, -1), reversed(a)):
        curr += s.cal
        ma = max(ma, curr - (c - s.x))
        ma0 = max(ma0, curr - (c - s.x) * 2)
        anti[i] = ma
        anti_to_0[i] = ma0

    ans = 0
    for exit_pos in range(1, n + 1):
        ans = max(
            ans,
            clock_to_0[exit_pos - 1] + anti[exit_pos],
            anti_to_0[(exit_pos + 1) % (n + 1)] + clock[exit_pos]
        )

    print(ans)


if __name__ == '__main__':
    main()