Where am I going wrong

Please help. I tried solving this by traversing the array twice without the last element in second turn. Got wrong answer. Where am I getting it wrong

while solving this question using the same array twice you need to make ensure that the window size doesnt become more than n for example in the case
1
2
1 2
you answer would be 6 because you are not maintaining the window size.
Please visit this the below given link for easier and more intuitive solution: