Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- long[] dl = new long[100001];
- long[] dr = new long[100001];
- long[] a = new long[100001];
- int n = sc.nextInt();
- for (int i = 1; i <= n; i++) {
- a[i] = sc.nextLong();
- }
- for (int i = 1; i <= n; i++) {
- dl[i] = Math.max(dl[i - 1] + a[i], a[i]);
- }
- for (int i = n; i >= 1; i--) {
- dr[i] = Math.max(dr[i + 1] + a[i], a[i]);
- }
- long max = -1000;
- for (int i = 1; i <= n - 1; i++) {
- if (dl[i - 1] + dr[i + 1] > max) {
- max = dl[i - 1] + dr[i + 1];
- }
- }
- for (int i = 1; i <= n; i++) {
- if (dl[i] > max) {
- max = dl[i];
- }
- }
- System.out.println(max);
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment