title: How to remove K characters from a string such that there is a minimum of each character
body: <p>I want to remove <em>K</em> characters from a string such that the occurrence of each character is at a minimum. </p>
  
  <p>For example:</p>
  
  <p>String: <code>abcdefghijkllllll</code></p>
  
  <p>K: <code>5</code></p>
  
  <p>Answer: 12 (abcdeghijkl)</p>
  
  <p>String: <code>ababac</code></p>
  
  <p>K: <code>4</code></p>
  
  <p>Answer: 3 (aac)</p>
  
  <p>String: <code>aaaab</code></p>
  
  <p>K: <code>4</code></p>
  
  <p>Answer: 1(b)</p>
  
  <p>I want to remove 5 characters. Those characters would be 5 <code>l</code>'s</p>
  
  <p>What I've done so far is count the occurence of each character using a map</p>
  
  <p>But I'm stuck as to what to do next.</p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  using namespace std;
  string s;
  int l, k;
  map&lt;char, int&gt; m;
  int main() {
      getline(cin, s);
      scanf("%d %d", &amp;l, &amp;k);
      for(int i=0; i&lt;s.length(); i++) {
          m[s[i]]++;
      }
      for(auto &amp;x : m) {
          cout &lt;&lt; x.second &lt;&lt; "\n";
      }
      return 0;
  }
  
  </code></pre>
  
  <p>The expected result is the minimum length of a string after removing the characters of any given string (can be sorted or unsorted). </p>
  
  <p>You can remove any character in the String</p>
  
  <h3>Update:</h3>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  using namespace std;
  string s;
  int l, k;
  map&lt;char, int&gt; m;
  int main() {
      getline(cin, s);
      cin &gt;&gt; l &gt;&gt; k;
      for(int i = 0; i &lt; s.length(); i++) {
          m[s[i]]++;
      }
      for(auto it = m.end(); it != m.begin(); it--) {
          // cout &lt;&lt; it-&gt;second &lt;&lt; "\n";
      }
  
      vector&lt;pair&lt;int, int&gt;&gt; pairs;
      for (auto itr = m.begin(); itr != m.end(); itr++) {
          pairs.push_back(*itr);
      }
      sort(pairs.begin(), pairs.end(), [=](pair&lt;int, int&gt;&amp; a, pair&lt;int, int&gt;&amp; b) { return a.second &lt; b.second; } );
  
      for(auto it = m.end(); it != m.begin(); it--) {
          if(it-&gt;second - k &gt;= 1) {
              it-&gt;second-=k;
              k -= it-&gt;second;
          }
      }
      int sum = 0;
      for(auto it = m.end(); it != m.begin(); it--) {
          sum += it-&gt;second;
          // cout &lt;&lt; it-&gt;second &lt;&lt; "\n";
      }
      cout &lt;&lt; sum &lt;&lt; "\n";
      return 0;
  }
  </code></pre>
  
  <p>The current problem with this is that it doesn't read all the characters and map them correctly to the map. </p>