title: knapsack with weight only
body: <p>if i had given the maximum weight say w=20 .and i had given a set on weights say m=[5,7,12,18] then how could i calculate the max possible weight that we can hold inside the maximum weight using the m. in this case the answer is 19.by adding 12+7=19. and my code is giving me 18.please help me in this.</p>
  <pre><code>int weight(int W, vector&lt;int&gt; &amp;m) {
  
    int current_weight = 0;
    int temp;
    for (int i = 0; i &lt; w.size(); i++) {
      for (int j = i + 1; j &lt; m.size(); j++) {
        if (m[i] &lt; m[j]) {
          temp = m[j];
          m[j] = m[i];
          m[i] = temp;
          }
        }
      }
  
    for (size_t i = 0; i &lt; m.size(); ++i) {
      if (current_weight + m[i] &lt;= W) {
         current_weight += m[i];
        }
      }
   
    return current_weight;
    }
  </code></pre>