Problem p03372 (python)

Fixed submission:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N,C = map(int,input().split())
    xv = [tuple(map(int,input().split())) for i in range(N)]
    r1,r2,l1,l2= [0]*(N+1),[0]*(N+1),[0]*(N+1),[0]*(N+1)
    ans = 0
    dis = 0
    for i in range(N):
        r1[i+1] = r1[i]+xv[i][1]-(xv[i][0]-dis)
        r2[i+1] = r2[i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = xv[i][0]
    dis = C
    for i in reversed(range(N)):
        l1[N-i]=l1[N-i-1]+xv[i][1]-(dis-xv[i][0])
        l2[N-i]=l2[N-i-1]+xv[i][1]-2*(dis-xv[i][0])
        dis = xv[i][0]
    for i in range(N):
        l1[i+1] = max(l1[i+1],l1[i])
        r1[i+1] = max(r1[i+1],r1[i])
    for i in range(N+1):
        subans = max(r2[i]+l1[N-i],l2[i]+r1[N-i])
        ans = max(ans,subans)
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Buggy submission (verdict: Runtime Error):
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N,C = map(int,input().split())
    xv = [tuple(map(int,input().split())) for i in range(N)]
    r1,r2,l1,l2= [0]*(N+1),[0]*(N+1),[0]*(N+1),[0]*(N+1)
    ans = 0
    dis = 0
    for i in range(N):
        r1[i+1] = r1[i]+xv[i][1]-(xv[i][0]-dis)
        r2[i+1] = r2[i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = xv[i][0]
    dis = C
    for i in reversed(range(N)):
        l1[N-i+1]=l1[N-i]+xv[i][1]-(xv[i][0]-dis)
        l2[N-i+1]=l2[N-i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = C-xv[i][0]
    for i in range(N):
        l1[i+1] = max(l1[i+1],l1[i])
        l2[i+1] = max(r1[i+1],r1[i])
    for i in range(N):
        subans = max(r2[i]+l1[N-i],l2[i]+r1[N-i])
        ans = max(ans,subans)
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================