Working on Subsequent Weighting problem on Hackerrank