• + 0 comments

    This is very clever. I wasn't able to figure it out before I saw your solution. Hats off! Here is a solution in Python 3 which should do better for space and time complexity with the listed input constraints. I believe. I'm definitely welcome to disagreement! :D

    (n,m),D = map(int,input().split()),{}
    for _ in range(m):
        a, b, k = map(int,input().split())
        D[a-1] = D[a-1]+k if a-1 in D else k
        D[b] = D[b]-k if b in D else -k
    high = acc = 0
    for key in sorted(D.keys()):
       acc += D[key]
       high = max(high,acc)
    print(high)