title: Algorithm to split an array into P subarrays of balanced sum
body: <p>I have an big array of length N, let's say something like:</p>
  
  <pre><code>2 4 6 7 6 3 3 3 4 3 4 4 4 3 3 1
  </code></pre>
  
  <p>I need to split this array into P subarrays (in this example, <code>P=4</code> would be reasonable), such that the sum of the elements in each subarray is as close as possible to sigma, being:</p>
  
  <pre><code>sigma=(sum of all elements in original array)/P
  </code></pre>
  
  <p>In this example, <code>sigma=15</code>.</p>
  
  <p>For the sake of clarity, one possible result would be:</p>
  
  <pre><code>2 4 6    7 6 3 3   3 4 3 4    4 4 3 3 1
  (sums: 12,19,14,15)
  </code></pre>
  
  <p>I have written a very naive algorithm based in how I would do the divisions by hand, but I don't know how to impose the condition that a division whose sums are (14,14,14,14,19) is worse than one that is (15,14,16,14,16).</p>
  
  <p>Thank you in advance.</p>