typedef unsigned long long ulong;int theSum = 345; // requested sumint * val = new int[num_val]; .... // read valuesulong minMask = GetMinMask(theSum, val, num_val);ulong maxMask = GetMaxMask(theSum, val, num_val);ulong mask = minMask;while (mask < maxMask) { ++mask; ulong mask2 = mask; int sum = 0; int index = 0; while (mask2 && index < num_val) { if (mask2 & 1) { sum += val[index]; if (sum >= theSum) { if (sum == theSum) { // found, save it } break; } ++index; mask2 >>= 1; }}