Hello, everybody!

Yesterday I tried to solve this problem on spoj. I spent about three hours on it, but I didn't manage to solve it. In the comments I read that there is a solution in O(n) time complexity. Can you help me, please?

Thanks in advance! :)

Obviously, for non-negative

A_{i},W_{i}=W_{i - 1}+ 1. Making it smaller wouldn't help anything.Take the last negative

A_{i}. We can see that the sum ofA_{i}W_{i}to its right is . The first term is fixed; the second works as the last element of a modified arrayA'. If it's negative,W_{i}= 2; otherwise, we can repeat the same idea onA'.This leads to a simple implementation with one traversal of the array.