Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | Function to find the max sum after processing q queries ; Store the cumulative sum ; Store the maximum sum ; Iterate over the range 0 to q ; Variables to extract values from vector ; Iterate over the range [ 1 , n ] ; Calculate cumulative sum ; Calculate maximum sum ; Return the maximum sum after q queries ; Stores the size of array and number of queries ; Stores the sum ; Storing input queries ; Function call to find the maximum sum
def max_sum ( a , v , q , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE m = - 10 ** 9 ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT p = v [ i ] [ 0 ] [ 0 ] ; NEW_LINE q = v [ i ] [ 0 ] [ 1 ] ; NEW_LINE k = v [ i ] [ 1 ] ; NEW_LINE a [ p ] += k ; NEW_LINE if ( q + 1 <= n ) : NEW_LINE INDENT a [ q + 1 ] -= k ; NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT x += a [ i ] ; NEW_LINE m = max ( m , x ) ; NEW_LINE DEDENT return m ; NEW_LINE DEDENT n = 10 NEW_LINE q = 3 ; NEW_LINE a = [ 0 ] * ( n + 5 ) ; NEW_LINE v = [ [ [ 0 for i in range ( 2 ) ] for x in range ( 2 ) ] for z in range ( q ) ] NEW_LINE v [ 0 ] [ 0 ] [ 0 ] = 1 ; NEW_LINE v [ 0 ] [ 0 ] [ 1 ] = 5 ; NEW_LINE v [ 0 ] [ 1 ] = 3 ; NEW_LINE v [ 1 ] [ 0 ] [ 0 ] = 4 ; NEW_LINE v [ 1 ] [ 0 ] [ 1 ] = 8 ; NEW_LINE v [ 1 ] [ 1 ] = 7 ; NEW_LINE v [ 2 ] [ 0 ] [ 0 ] = 6 ; NEW_LINE v [ 2 ] [ 0 ] [ 1 ] = 9 ; NEW_LINE v [ 2 ] [ 1 ] = 1 ; NEW_LINE print ( max_sum ( a , v , q , n ) ) ; NEW_LINE