| 4 | #include <algorithm> |
| 5 | using namespace std; |
| 6 | int main() |
| 7 | { |
| 8 | int n, nt; |
| 9 | long s = 0, m; |
| 10 | cin >> n; |
| 11 | int *a = new int[n];// dynamic array allocation |
| 12 | long *b = new long[n]; |
| 13 | for (int i = 0; i < n; i++) |
| 14 | cin >> a[i]; |
| 15 | b[0] = a[0]; |
| 16 | for (int i = 1; i < n; i++) |
| 17 | b[i] = a[i]+b[i-1];// store cumulative values of a |
| 18 | for (int i = 0; i < n; i++) |
| 19 | { |
| 20 | nt = 2 * (n - i); |
| 21 | nt = ((int)sqrt(1 + 4 * nt) - 1) / 2; |
| 22 | nt = (nt * (nt + 1)) / 2;//find number of terms by ap series sum formula |
| 23 | if(i == 0) |
| 24 | { |
| 25 | m = b[nt-1]; |
| 26 | s = m; |
| 27 | } |
| 28 | else |
| 29 | s = b[nt+i-1]-b[i-1];// find sum of the series with the help of cumulative values |
| 30 | m = max(m, s);//store maximum sum in m |
| 31 | s = 0;//reinitialize sum |
| 32 | } |
| 33 | cout << m; |
| 34 | return 0; |
| 35 | } |