using System.Collections.Generic;
public IList<IList<long>> CombinationSum(long[] candidates, long target)
IList<long> path = new List<long>();
IList<IList<long>> result = new List<IList<long>>();
CombinationSumHelper(candidates, target, 0, path, result);
private void CombinationSumHelper(long[] candidates, long target, int index, IList<long> path, IList<IList<long>> result)
result.Add(new List<long>(path));
for (int i = index; i < candidates.Length; i++)
if (target - candidates[i] >= 0)
CombinationSumHelper(candidates, target - candidates[i], i+1, path, result);
path.RemoveAt(path.Count - 1);
public static void Main()
var candidates = new long[] {
var target = 16629575554;
var solution = new Solution();
var result = solution.CombinationSum(candidates, target);
foreach (var combination in result)
Console.WriteLine(string.Join(",", combination));