For each element of the prefix_sum array do : compute the cand array . take k maximum values from maxi and cand using maxmerge function . insert prefix_sum [ i ] to mini array if needed using insertMini function .
for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) { if ( pre_sum [ i ] < 0 && mini [ j ] == numeric_limits < int > :: max ( ) ) cand [ j ] = ( - pre_sum [ i ] ) - mini [ j ] ; else cand [ j ] = pre_sum [ i ] - mini [ j ] ; } maxMerge ( maxi , cand ) ; insertMini ( mini , pre_sum [ i ] ) ; }