class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> res;
vector<int> out;
helper(candidates, out, 0, target, res);
return res;
}
void helper(vector<int>& candidates, vector<int>& out, int pos, int target, vector<vector<int>>& res){
if(target==0) res.push_back(out);
if(target<0) return;
for(int i=pos;i<candidates.size();i++){
out.push_back(candidates[i]);
helper(candidates, out, i, target-candidates[i], res);
out.pop_back();
}
}
};