Search for the maximum subarray sum considering negative elements and cyclic shift

TypeScript Hard loops

Task «Search for the maximum subarray sum considering negative elements and cyclic shift»

Given an array of integers, find the maximum sum of a subarray (contiguous sequence of elements) after performing at most one cyclic shift of the array to the left or right. A cyclic shift means that elements move as if in a circle. If no shift is performed, the subarray is selected from the original array. If a shift is performed, the subarray can be selected from the array after the shift. Output the maximum possible sum. The array length is from 1 to 10^5, elements are from -10^4 to 10^4. Use Kadane's algorithm taking into account the cyclic shift.

Input Format

The first line contains an integer N — the number of elements in the array. The second line contains N integers separated by spaces.

Output Format

One integer — the maximum sum of the subarray.

Examples

Example 1

INPUT
3 10 -5 10
OUTPUT
20

Example 2

INPUT
7 abacaba 2 3
OUTPUT
2 aba acb
main.ts