class Solution {
public:
vector<string>res;int m;
vector<string>generateParenthesis(int n){
m = n;dfs("",0,0);return res;}voiddfs(string path,int l,int r){if(l == m && r == m){
res.push_back(path);return;}if(l < m)dfs(path +"(", l +1, r);if(r < l)dfs(path +")", l, r +1);}};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
bool cmp(ListNode* a, ListNode* b){return a->val < b->val;}
class Solution {
public:
ListNode*mergeKLists(vector<ListNode*>& lists){
vector<ListNode*> node_vec;for(int i =0; i < lists.size(); i ++){
ListNode* head = lists[i];while(head){
node_vec.push_back(head);
head = head->next;}}if(node_vec.size()==0)returnNULL;sort(node_vec.begin(), node_vec.end(), cmp);for(int i =1; i < node_vec.size(); i ++)
node_vec[i -1]->next = node_vec[i];
node_vec[node_vec.size()-1]->next =NULL;return node_vec[0];}};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode*mergeKLists(vector<ListNode*>& lists){if(lists.size()==0)returnNULL;if(lists.size()==1)return lists[0];if(lists.size()==2)returnmergeTwoLists(lists[0], lists[1]);int mid = lists.size()/2;
vector<ListNode*> sub1_lists;
vector<ListNode*> sub2_lists;for(int i =0; i < mid; i ++)
sub1_lists.push_back(lists[i]);for(int i = mid; i < lists.size(); i ++)
sub2_lists.push_back(lists[i]);
ListNode* l1 =mergeKLists(sub1_lists);
ListNode* l2 =mergeKLists(sub2_lists);returnmergeTwoLists(l1, l2);}
ListNode*mergeTwoLists(ListNode* l1, ListNode* l2){
ListNode* dummy = new ListNode(-1);
ListNode* head = dummy;while(l1 && l2){if(l1->val <= l2->val){
head->next = l1;
l1 = l1->next;}else{
head->next = l2;
l2 = l2->next;}
head = head->next;}
head->next =(l1 ==NULL)? l2 : l1;return dummy->next;}};