Notice that the maximum sum of all elements can be at most 10510^5. So, we can easily use Dynamic Programming to solve this problem.

Let dp1[i][j]dp1[i][j] be the number of subsequences with sum jj ended at or before the ii-th element. We either take the ii-th element for this subsequence of not take it. So the transition is dp1[i][j]=dp1[i1][jA[i]]+dp1[i1][j]dp1[i][j] = dp1[i-1][j-A[i]] + dp1[i-1][j].

Again, let dp2[i][j]dp2[i][j] be the number of subsequences with sum jjstarted from or after the ii-th element.

The transition for this case is dp2[i][j]=dp2[i+1][jA[i]]+dp2[i+1][j]dp2[i][j] = dp2[i+1][j-A[i]] + dp2[i+1][j].

So the number of subsequences with sum 2j2*j with the second equal part starting at the ii-th element is dp1[i1][j]dp2[i+1][jA[i]]dp1[i-1][j] * dp2[i+1][j-A[i]].

Solve this for all ii and jj and the summation of the results is the desired answer.

Statistics

80% Solution Ratio
sh2018331053Earliest, Apr '22
S_SubrataFastest, 0.0s
S_SubrataLightest, 918 kB
rafi_1703076Shortest, 685B
Toph uses cookies. By continuing you agree to our Cookie Policy.