added k as a input argument for k-way merge