Getting wa in some test cases of problem KFIB.

I am wrong answer for some test cases of the problem:KFIB.Can someone point out the error in my is my solution:

Wait, what are you doing?

According to the problem, T(n) = ΣT(i) where n-k <= i <= n-1.

Your solution deals only with previous two terms, which is not what the problem demands.

The brute force solution will have complexity O(NK) which is unlikely to pass the time limit. By brute force, I mean using a nested loop back to n-k and calculating the sum as you travserve; like here:

To pass the TL, you need to prepare a prefix sum array as you traverse through N. You can calculate sum of any range in constant time using the prefix sum array.

My solution: