import java.util.Scanner;
public class Main {
public static void main
(String[] args
) { Scanner scanner
= new Scanner
(System.
in);
// Read the number of elements
int n = scanner.nextInt();
// Initialize arrays to store values, prefix sums, and maximums
int[] b = new int[n + 1]; // Original array
int[] p = new int[n + 1]; // Array to store maximum prefix sums
int[] s = new int[n + 1]; // Array to store sums of increasing sequences
int[] u = new int[n + 1]; // Array to store maximum values at each index
int[] max_s = new int[n + 1]; // Array to store maximum sums from index i to n
// Input the values into the array b
for (int i = 1; i <= n; i++) {
b[i] = scanner.nextInt();
}
// Calculate the maximum prefix sums
// p[i] will store the maximum sum of subarrays ending at index i
p[1] = b[1]; // Base case: the first element is the only subarray
for (int i = 2; i <= n; i++) {
// Either take the current element or extend the previous maximum sum
p
[i
] = Math.
max(b
[i
], p
[i
- 1] + b
[i
]); }
// Initialize the last elements for further calculations
s[n] = b[n]; // Start with the last element
u[n] = s[n]; // Initialize u[n] as well
// Calculate sums of increasing sequences and their maximums
for (int j = n - 1; j >= 1; j--) {
// If the current element is less than the next, add to the sum
if (b[j] < b[j + 1]) {
s[j] = b[j] + s[j + 1]; // Extend the increasing sequence
} else {
s[j] = b[j]; // Start a new sequence
}
// Store the maximum value between the current element and the sum
u
[j
] = Math.
max(b
[j
], s
[j
]); // Update the maximum sums from index j to n
max_s
[j
] = Math.
max(u
[j
], max_s
[j
+ 1]); }
// Variable to store the final answer
int answer = 0;
// Calculate the maximum possible sum by combining prefix sums and maximum suffix sums
for (int i = 1; i <= n - 1; i++) {
// Combine the maximum prefix sum up to i and the maximum suffix sum from i+1
int l = p[i] + max_s[i + 1];
// Update the answer if the current combination is larger
answer
= Math.
max(answer, l
); }
// Output the final answer
}
}