Calculate the result
if ( memo . has ( currval ) ) { if ( currval > 0 ) res = Math . max ( res , currsum - memo . get ( currval ) + currval ) ; else res = Math . max ( res , currsum - memo . get ( currval ) + 2 * currval ) ; } else memo . set ( currval , currsum ) ; if ( currval < 0 ) currsum = currsum - currval ; }