• + 5 comments

    Same algorithm in python3

    from sys import stdin
    
    if __name__ == '__main__':
        (N, M) = [int(i) for i in stdin.readline().split()]
    
        vals = [0] * N
    
        for i in range(M):
            (a, b, k) = [int(i) for i in stdin.readline().split()]
            vals[a-1] += k
            try:
                vals[b] -= k
            except:
                # out of range of list (at the end)
                pass
    
        prev = 0
        for i in range(len(vals)):
            prev += vals[i]
            vals[i] = prev
    
        print(max(vals))
    

    Small difference is that the array is not N+1 long, but handle out of range via catching the exception.