import java.util.*;
public class Main {
public static void main
(String[] args
) { Scanner scanner
= new Scanner
(System.
in); int n = scanner.nextInt();
int[] nums = new int[n+1];
for (int i = 1; i <= n; i++) {
nums[i] = scanner.nextInt();
}
System.
out.
println("Maximum sum of two non-overlapping subarrays: " + maxTwoNonOverlappingSubarraysSum
(nums
)); }
public static int maxTwoNonOverlappingSubarraysSum(int[] nums) {
int n = nums.length - 1;
if (n == 0) return 0;
int[] prefixMaxSum = calculatePrefixMaxSum(nums);
int[] suffixMaxSum = calculateSuffixMaxSum(nums);
int[] maxPrefixSum = new int[n + 2];
maxPrefixSum[1] = prefixMaxSum[1];
for (int i = 2; i <= n; i++) {
maxPrefixSum
[i
] = Math.
max(maxPrefixSum
[i
- 1], prefixMaxSum
[i
]); }
int[] maxSuffixSum = new int[n + 2];
maxSuffixSum[n] = suffixMaxSum[n];
for (int i = n - 1; i >= 1; i--) {
maxSuffixSum
[i
] = Math.
max(maxSuffixSum
[i
+ 1], suffixMaxSum
[i
]); }
int maxSum = 0;
for (int i = 0; i <= n; i++) {
maxSum
= Math.
max(maxSum, maxPrefixSum
[i
] + maxSuffixSum
[i
+ 1]); }
return maxSum;
}
public static int[] calculatePrefixMaxSum(int[] nums) {
int n = nums.length - 1;
int[] prefixMaxSum = new int[n + 1];
int currentMax = nums[1];
prefixMaxSum[1] = nums[1];
for (int i = 2; i <= n; i++) {
currentMax
= Math.
max(0,
Math.
max(nums
[i
], currentMax
+ nums
[i
])); prefixMaxSum[i] = currentMax;
}
return prefixMaxSum;
}
public static int[] calculateSuffixMaxSum(int[] nums) {
int n = nums.length - 1;
int[] suffixMaxSum = new int[n + 1];
int currentMax = nums[n];
suffixMaxSum[n] = nums[n];
for (int i = n - 1; i >= 1; i--) {
currentMax
= Math.
max(0,
Math.
max(nums
[i
], currentMax
+ nums
[i
])); suffixMaxSum[i] = currentMax;
}
return suffixMaxSum;
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgaW50IG4gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICBpbnRbXSBudW1zID0gbmV3IGludFtuKzFdOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgICAgICBudW1zW2ldID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgfQogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTWF4aW11bSBzdW0gb2YgdHdvIG5vbi1vdmVybGFwcGluZyBzdWJhcnJheXM6ICIgKyBtYXhUd29Ob25PdmVybGFwcGluZ1N1YmFycmF5c1N1bShudW1zKSk7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyBpbnQgbWF4VHdvTm9uT3ZlcmxhcHBpbmdTdWJhcnJheXNTdW0oaW50W10gbnVtcykgewogICAgICAgIGludCBuID0gbnVtcy5sZW5ndGggLSAxOwogICAgICAgIGlmIChuID09IDApIHJldHVybiAwOwoKICAgICAgICBpbnRbXSBwcmVmaXhNYXhTdW0gPSBjYWxjdWxhdGVQcmVmaXhNYXhTdW0obnVtcyk7CiAgICAgICAgaW50W10gc3VmZml4TWF4U3VtID0gY2FsY3VsYXRlU3VmZml4TWF4U3VtKG51bXMpOwoKICAgICAgICBpbnRbXSBtYXhQcmVmaXhTdW0gPSBuZXcgaW50W24gKyAyXTsKICAgICAgICBtYXhQcmVmaXhTdW1bMV0gPSBwcmVmaXhNYXhTdW1bMV07CiAgICAgICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgICAgIG1heFByZWZpeFN1bVtpXSA9IE1hdGgubWF4KG1heFByZWZpeFN1bVtpIC0gMV0sIHByZWZpeE1heFN1bVtpXSk7CiAgICAgICAgfQoKICAgICAgICBpbnRbXSBtYXhTdWZmaXhTdW0gPSBuZXcgaW50W24gKyAyXTsKICAgICAgICBtYXhTdWZmaXhTdW1bbl0gPSBzdWZmaXhNYXhTdW1bbl07CiAgICAgICAgZm9yIChpbnQgaSA9IG4gLSAxOyBpID49IDE7IGktLSkgewogICAgICAgICAgICBtYXhTdWZmaXhTdW1baV0gPSBNYXRoLm1heChtYXhTdWZmaXhTdW1baSArIDFdLCBzdWZmaXhNYXhTdW1baV0pOwogICAgICAgIH0KCiAgICAgICAgaW50IG1heFN1bSA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgICAgIG1heFN1bSA9IE1hdGgubWF4KG1heFN1bSwgbWF4UHJlZml4U3VtW2ldICsgbWF4U3VmZml4U3VtW2kgKyAxXSk7CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gbWF4U3VtOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgaW50W10gY2FsY3VsYXRlUHJlZml4TWF4U3VtKGludFtdIG51bXMpIHsKICAgICAgICBpbnQgbiA9IG51bXMubGVuZ3RoIC0gMTsKICAgICAgICBpbnRbXSBwcmVmaXhNYXhTdW0gPSBuZXcgaW50W24gKyAxXTsKICAgICAgICBpbnQgY3VycmVudE1heCA9IG51bXNbMV07CiAgICAgICAgcHJlZml4TWF4U3VtWzFdID0gbnVtc1sxXTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgICAgIGN1cnJlbnRNYXggPSBNYXRoLm1heCgwLCBNYXRoLm1heChudW1zW2ldLCBjdXJyZW50TWF4ICsgbnVtc1tpXSkpOwogICAgICAgICAgICBwcmVmaXhNYXhTdW1baV0gPSBjdXJyZW50TWF4OwogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIHByZWZpeE1heFN1bTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIGludFtdIGNhbGN1bGF0ZVN1ZmZpeE1heFN1bShpbnRbXSBudW1zKSB7CiAgICAgICAgaW50IG4gPSBudW1zLmxlbmd0aCAtIDE7CiAgICAgICAgaW50W10gc3VmZml4TWF4U3VtID0gbmV3IGludFtuICsgMV07CiAgICAgICAgaW50IGN1cnJlbnRNYXggPSBudW1zW25dOwogICAgICAgIHN1ZmZpeE1heFN1bVtuXSA9IG51bXNbbl07CgogICAgICAgIGZvciAoaW50IGkgPSBuIC0gMTsgaSA+PSAxOyBpLS0pIHsKICAgICAgICAgICAgY3VycmVudE1heCA9IE1hdGgubWF4KDAsIE1hdGgubWF4KG51bXNbaV0sIGN1cnJlbnRNYXggKyBudW1zW2ldKSk7CiAgICAgICAgICAgIHN1ZmZpeE1heFN1bVtpXSA9IGN1cnJlbnRNYXg7CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gc3VmZml4TWF4U3VtOwogICAgfQp9