We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

fromitertoolsimportaccumulaten,m=map(int,input().split(' '))dx=[0]*(n+1)# allow run past endfor_inrange(m):a,b,c=map(int,input().split(' '))dx[a-1]+=cdx[b]-=cprint(max(accumulate(dx)))

The question asks for a 1 indexed list and the a,b values read in are read in starting from 1 not 0. If you do not use (n+1)if b happens to be the last number of your list it will go out of bounds.

The 0 index value will always be 0 so it doesn't affect your answer when you sum for max difference.

## Array Manipulation

You are viewing a single comment's thread. Return to all comments →

Same solution but optimized:

That's so good! You are awesome!

can you explain why it's (n+1) and not (n)?

The question asks for a 1 indexed list and the a,b values read in are read in starting from 1 not 0. If you do not use (n+1)if b happens to be the last number of your list it will go out of bounds.

The 0 index value will always be 0 so it doesn't affect your answer when you sum for max difference.

why are you subtracting dx[b] -= c ?????

Walk through the array it may help.

Query 1 -> [1, 2, 100] Array 1 -> [0, 100, 0, -100, 0, 0, 0] Query 2 -> [2, 5, 100] Array 2 -> [0, 100, 100, -100, 0, 0, -100] Query 3 -> [3, 4, 100] Array 3 -> [0, 100, 100, 0, 0, -100, -100] Array Accumulated -> [0, 100, 200, 200, 200, 100, 0] Result -> 200

The use of accumulate is so brilliant!

Nice