OMG! This is merely a 250-point-question! I spent more than half hour thinking the algorithm. Still I failed at the last huge test case with possible memory allocation failure. Obviously my 'using-loop-avoiding-recursion' still cannot solve large memory consumption issue of this kind of NP-complete problem! Let me debug further when come back from work.