for this test case you code will print 12 but answer should be 8.

4 2

1 3 2 4

and it’s because question is saying that 1 <= Y - X <= K. but you are choosing k in kind of every case… Which will not give optimal result all time at all. you can think of dp solution for this.

Just for hint, I will say try to think in this direction… lets say you know minimal multiplication(how answer is defined in question) for every element of array, then if I add new element in array then can you find answer for new element. Till now you can easily solve First subtask by just using couple of arrays and for loops.

For subtask 2… You will see that Complexity is increasing because of for loops over k, which are doing redundant calculations again and again. To optimise that part… I will ask, can you do some pre-calculation of O(n) and answer queries like, find minimum element in a range of array in O(1) or O(log(n))? If yes, then you will see that second subtask is also solved.

I tried to mention how you could approach this problem and how problems should be approached(main purpose of subtasks in problems). I did not mention exact algorithm because that is already there in editorial. But you can ask questions if something thing is not clear…