Maximum Subarray Sum with Permutations
Swift
Hard
arrays
Task «Maximum Subarray Sum with Permutations»
Given an array of integers. It is allowed to perform at most one operation: choose any subarray and rearrange its elements arbitrarily. Find the maximum possible sum of any contiguous subarray after performing this operation (or without it). The array may contain negative numbers. Constraints: array length up to 10^5, elements from -10^9 to 10^9.
Input Format
First line: an integer n (1 ≤ n ≤ 10^5) — the length of the array. Second line: n space-separated integers — the elements of the array.
Output Format
One integer — the maximum sum of a subarray after at most one permutation.
Examples
Example 1
INPUT
5
1 -2 3 4 -5
OUTPUT
8
Example 2
INPUT
4
-1 -2 -3 -4
OUTPUT
-1
Example 3
INPUT
/tmp/testdir
*.txt
OUTPUT
/tmp/testdir/subdir/file1.txt a1b2c3d4e5f6...
/tmp/testdir/file2.txt 1234567890abcdef...
Example 4
INPUT
Hello World
OUTPUT
3
Example 5
INPUT
3
5 -1 3
OUTPUT
8
main.swift