We have a sorted array, say
30, 20, 10, -1, -2, -15
Now we want to calculate its partial sums (for each number, you can choose to add it or not add it), and fetch top n largest numbers of them (say, top 11 of them) in the descending order, i.e.
60 = 30 + 20 + 10
59 = 30 + 20 + 10 -1
58 = 30 + 20 + 10 -2
57 = 30 + 20 + 10 -1 -2
50 = 30 + 20
49 = 30 + 20 -1
48 = 30 + 20 -2
47 = 30 + 20 -1 -2
45 = 30 + 20 + 10 -15
44 = 30 + 20 + 10 -1 -15
43 = 30 + 20 + 10 -2 -15
A naive solution is to calculate all the combinations (for the example above, there are 2^6 = 64 combinations), sort them in the descending order, and then fetch the first 11 numbers of them:
60, 59, 58, 57, 50, 49, 48, 47, 45, 44, 43, 42, 40, 39, 38, 37, 35, 34, 33, 32, 30, 30, 29, 29, 28, 28, 27, 27, 25, 24, 23, 22, 20, 19, 18, 17, 15, 15, 14, 14, 13, 13, 12, 12, 10, 9, 8, 7, 5, 4, 3, 2, 0, -1, -2, -3, -5, -6, -7, -8, -15, -16, -17, -18
However, when the length of the array is large, calculating all the combinations is unfeasible. So the question is, is it possible to fetch the top n largest partial sums in an iterative manner, e.g. fetch one number at a time?
EDIT
To be clear, the end goal is, assuming the array to be global, I want a function f(): by calling f() iteratively, it returns the largest partial sums in the descending order
Call f(), return 60
Call f() again, return 59
Call f() again, return 58
......