[code] finished merge teams algorithm