all_pairs memory optimization
merge