Calculate the result
if ( memo . count ( currval ) != 0 ) { if ( currval > 0 ) res = max ( res , currsum - memo [ currval ] + currval ) ; else res = max ( res , currsum - memo [ currval ] + 2 * currval ) ; } else memo [ currval ] = currsum ; if ( currval < 0 ) currsum = currsum - currval ; }