You are given an array A of N elements. Now you need to choose the best index of this array . An index of the array is called best if the special sum of this index is maximum across the special sum of all the other indices.
To calculate the special sum for any index i , you pick the first element that is A[i] and add it to your sum. Now you pick next two elements i.e. A[i+1] and A[i+2] and add both of them to your sum.
Now you will pick the next elements and this continues till the index for which it is possible to pick the elements. For example: If our array contains 10 elements and you choose index to be then your special sum is denoted by -(A)+(A+A)+(A+A+A) , beyond this its not possible to add further elements as the index value will cross the value 10.
Find the best index and in the output print its corresponding special sum. Note that there may be more than one best indices but you need to only print the maximum special sum.
First line contains an integer as input. Next line contains space separated integers denoting the elements of the array .
In the output you have to print an integer that denotes the maximum special sum
1 3 1 2 5
2 1 3 9 2 4 -10 -9 1 3
Solution: Python Program
import math n = int(input()) a = list(map(int, input().split())) for i in range(1, n): a[i] += a[i-1] max_sum = -10000000 for i in range(n): left = n - i sum = 0 k = int((-1 + math.sqrt(8*left + 1))/2) sum = a[(k*(k+1))//2 + i - 1] if i != 0: sum -= a[i-1] if max_sum < sum: max_sum = sum print(max_sum)