title: C# split integer in parts given part weights algorithm
body: <p>I have an integer and a list of non-negative weights, how can I 'split' the integer into same number of 'buckets' with corresponding weights? <br></p>
  <pre><code>public int[] SplitIntoBuckets(int count, int[] weights) {
      // some magic algorithm
      Debug.Assert(solution.Sum() == count);
      return solution;
  }
  </code></pre>
  <p>A trivial example would be <code>count = 200</code> and <code>weights = { 25, 25, 50 }</code> with solution <code>{50, 50, 100}</code> (50+50+100 = 200). The inputs, however, does not have to be 'nice' numbers, another example without nice solution would be <code>count = 150</code> and weights <code>{753, 42, 95, 501}</code>.<br>
  The sum of buckets must be always equal to the <code>count</code> input, the algorithm should distribute the input among buckets as closely to weights as possible. What is as 'close as possible' does not matter (for example it could be either lowest absolute, relative or squared error).<br>
  The closest questions I could find are <a href="https://stackoverflow.com/questions/577427/evenly-divide-in-c-sharp">Split evenly into buckets</a>, however my buckets are not 'even' and <a href="https://stackoverflow.com/questions/1581394/split-value-in-24-randomly-sized-parts-using-c-sharp">Split randomly into buckets</a> however the weights are chosen randomly to be 'nice' numbers.</p>