Thus, the OP problem as a whole can be solved using the DP algorithm given in this question . I will repeat the essence of this here for completeness:
, d [i] [j] , s [1],..., s [i] j . :
- d [0] [j] = 0 j
- d [i] [1] = Sum (s [1],..., s [i]) i
- d [i] [j] = min (max (d [it] [j-1], Sum (s [i-t + 1],..., s [i]) 1 <= <= )
O (NK), . , (3): d[_][j] d[_][j-1]. , d[_][j], , d[_][j-1], d[_][j+1], .
d[0][j] , . , , , O (N) - d[i][1], O (N), j-1 - O (N), j - , . : O (N).
Edit: , , OP , . , w[i][j] i,j th, c[i][j] - , . :
c[0][j] = j w[0][j] = 0 jc[i][1] = 1 w[i][1] = Sum(s[1], .., s[i]) iu t, pt (3) .d[i-u][j-1] > Sum(s[i-u+1], .., s[i]):c[i][j] = c[i-u][j-1] w[i][j] = w[i-u][j-1]j th Sum(s[i-u+1], .., s[i] , 1, .., j-1, d[i-u][j-1].
d[i-u][j-1] < Sum(s[i-u+1], .., s[i]):c[i][j] = 1 w[i][j] = Sum(s[i-u+1], .., s[i])j th s[i-u+1], .., s[i] - .
- If
d[i-u][j-1] == Sum(s[i-u+1], .., s[i]):c[i][j] = c[i-u][j-1]+1 w[i][j] = d[i-u][j-1]j th , .
, O(N), O(N) .