title: Optimizing this combinatorics algorithm
body: <p>Assume you are given a number, <strong>N</strong>, which is your <em>goal</em> number. You are then given a series of <strong>p</strong> numbers, and you have to find the smallest sum of those numbers that is bigger than N, that is, that exceeds it minimaly (or that its equal to it).</p>
  
  <p>You can take any sum, of any combination of elements. <strong>p</strong> can be as big as <strong>100</strong>.</p>
  
  <p><strong>My current algorithm:</strong> After scanning all information, I create a 100 bit long bitset, and by using a loop I cast all integers from <strong>0</strong> to <strong>(2^p)-1</strong> into it, effectively ending up with all binary numbers between <strong>000...000</strong> and <strong>111...111</strong>.</p>
  
  <p>As you know, these vectors can be interpreted as each possible different combination of the <strong>p</strong> numbers, where a <strong>0</strong> in the i-th position means that the i-th number isn't included in the combination, whereas a <strong>1</strong> means it is included.</p>
  
  <p>Now, by looping through this bitset and checking each combination, I will surely be able to reach my goal, as I'll have checked every possible combination. If all numbers added together are not enough to reach the goal, then there is no solution.</p>
  
  <p>The actual code:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;bitset&gt;
  
  using namespace std;
  
  int main()
  {
      int N, p;
      cin &gt;&gt; N &gt;&gt; p;
      int bars[p], tot = 0;
      for (int j=0;j&lt;p;j++){
          cin &gt;&gt; bars[j];
          tot += bars[j];
      }
      if (tot &lt; N) cout &lt;&lt; "NO SOLUTION" &lt;&lt; endl;
      else {
           int diff = 3000;
           for (int j=0;j&lt;pow(2.0,p)-1;j++){
               tot = 0;
               bitset&lt;100&gt; x(j);
               for (int k=0;k&lt;p;k++){
                   if (x[k] == 1) tot += bars[k];
               }
               if (tot == N) {
                  cout &lt;&lt; N &lt;&lt; endl;
                  break;
               }
               if (tot-N&gt;0&amp;&amp;tot-N&lt;diff) diff = tot-N;
           }
           cout &lt;&lt; N+diff &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>The problem:</strong> As we can have up to 100 numbers, the amount of possible combinations is huge, and its just not viable. I've been able to solve problems of this kind by using this method, but they included a max of 20 numbers, which made the calculations possible. This leads me to thinking there must be a better method to compute the problem.</p>
  
  <p><strong>Extra info that I haven't used:</strong> The problem comes with extra information that I haven't considered at the time of designing my algorithm, which might be useful for the optimized algorithm. Those datums are:</p>
  
  <ul>
  <li>100 ≤ <strong>N</strong> ≤ 2500</li>
  <li><strong>N</strong> is a multiple of 10</li>
  <li>5 ≤ <strong>p</strong> ≤ 100</li>
  <li>Each of the numbers is between 50 and 2500</li>
  <li>They are all multiples of 10 too</li>
  </ul>
  
  <p><strong>The question:</strong> I've been thinking on a way to optimize my algorithm, which I realize is the most brute force thing you can do, however, I have failed at this goal. Can anybody help me optimize this so the resulting calculations are plausible? There is a time limit for this, which even though I ignore, I'm sure it won't be precisely high.</p>
  
  <p>Thanks in advance.</p>