template <typename T> using minheap = priority_queue<T, vector<T>, greater>; vector<int> kwaymerge(vector<vector<int>>& vs) { int k = vs.size(); vector<int> readcount(k, 0); vector<int> res; vector<pair<int, int>> vec; for (int i = 0; i < k; i++) { if (vs[i].size() > 0){ vec.push_back(make_pair(vs[i].front(), i)); readcount[i]++; } } // this way it uses heapify minheap q(vec.begin(), vec.end()); while (!q.empty()) { int v = q.top().first, i = q.top().second; q.pop(); res.push_back(v); if (readcount[i] < vs[i].size()) { vec.push_back(make_pair(vs[i][readcount[i], i)); readcount[i]++; } } }