Not enough karma to reply to the comment in question so this is a separate comment.
@kal013 you’re missing a couple of edgecases - one with a continue and one where there are no -1’s but the sum of the array elements is less than s. Fix those 2 and it passes - see https://www.codechef.com/viewsolution/21278595.
OK so I too found out the bug in my solution. In my solution, if the initial sum was unequal to required sum and there were no -1 present, it would print sum of gcd instead of 0.
But, the problem is, how do we know how many times a partition will appear. Fortunately, we can rush to Combinatorics for answers. We can see, It is just the number of Distinct permutations of the given sequence. ??
You are only following that part of the editorial which will give 30pts. Follow editorial from above statement to fetch rest 70pts.
If we consider non-decreasing permutations, we can obtain them by a recursive function f(pos, sum_left, min) where pos is index of array, sum_left is the sum we want to achieve using remaining elements, and since we want our sequence to be increasing, our current element is never less than min, the element at position pos-1.
If we remove the min constraint, all we get will be any permutation having sum S, not only non-decreasing ones.
We assign a value to a position, say pos, try all possible values for all remaining positions (pos+1, n-1) and consider all cases.
Then, backtrack to pos-1 and again assign a different value at position pos and try all possible values for all remaining positions (pos+1, n-1) and consider all cases.
In case we know, from values assigned in range (0, pos) that no possible solution exist, we terminate immediately.