Sir, i got confused with constraints of this problem. Complexity of this problem is O(sum*n). But that will not work here. Please clarify.
Constraints Of Problem
Yes, complexity is O(sum*n). Constraints are not defined properly, their product will not exceed 10^8