#K8471. Maximum Sum of Three Consecutive Elements

    ID: 36480 Type: Default 1000ms 256MiB

Maximum Sum of Three Consecutive Elements

Maximum Sum of Three Consecutive Elements

You are given a sequence of integers representing the heights of individuals standing in a line. Your task is to compute the maximum sum of the heights for any three consecutive individuals. If there are fewer than three individuals, simply output the sum of all their heights.

In mathematical terms, given an array (a_1, a_2, \dots, a_n), if (n < 3) then output (\sum_{i=1}^{n} a_i); otherwise, output (\max_{1 \le i \le n-2} (a_i + a_{i+1} + a_{i+2})).

inputFormat

The input is provided via standard input (stdin). The first line contains a single integer (n) representing the number of individuals. The second line contains (n) space-separated integers representing the heights of these individuals.

outputFormat

Print a single integer to standard output (stdout) which is the maximum sum of heights from any three consecutive individuals, or the sum of all heights if there are fewer than three individuals.## sample

5
1 2 3 4 5
12