```
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > result; vector<int> Data;
sort(candidates.begin(), candidates.end());
int i = 0; int remain = target;
while( i<candidates.size() ) {
if(remain <= candidates[i]) {
if(remain==candidates[i]){
Data.push_back(i);
result.push_back(Data);
Data.pop_back();
}
if(!Data.empty()) {
remain += candidates[Data.back()];
i=Data.back()+1;
Data.pop_back();
while(i==candidates.size() && !Data.empty()) {
remain +=candidates[Data.back()];
i=Data.back()+1;
Data.pop_back();
}
} else break;
}
else {
remain -= candidates[i];
Data.push_back(i);
}
}
for(int i=0; i<result.size(); i++)
for(int j=0; j<result[i].size(); j++)
result[i][j] = candidates[result[i][j]];
return result;
}
};
```