Unable to solve SNTEMPLE problem.need help

Temple of snakes
Can any one tell me how to solve this problem? I am out of ideas.There is a video solution but it is poorly explained.Please help.

Hey @rdas6313
Thanks for asking your doubt.

Let a magic sequence of order m, denoted also by magic(m),For example magic(3) is 1 2 3 2 1.

if we can form a sequence magic(m) in h, then for each k<m, we can also for a sequence magic(k) in h.
for example magic(3) = 1 2 3 2 1
to make magic(2) decrease each element be 1 you get 0 1 2 1 0.

This gives an idea of binary search. Just you have to find if magic(k) is possible or not.

Here is the link to editorial https://discuss.codechef.com/t/sntemple-editorial/14748

Please free to ask any doubt