Guest User

Untitled

a guest
Mar 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. long[] dl = new long[100001];
  7. long[] dr = new long[100001];
  8. long[] a = new long[100001];
  9. int n = sc.nextInt();
  10. for (int i = 1; i <= n; i++) {
  11. a[i] = sc.nextLong();
  12. }
  13. for (int i = 1; i <= n; i++) {
  14. dl[i] = Math.max(dl[i - 1] + a[i], a[i]);
  15. }
  16. for (int i = n; i >= 1; i--) {
  17. dr[i] = Math.max(dr[i + 1] + a[i], a[i]);
  18. }
  19. long max = -1000;
  20. for (int i = 1; i <= n - 1; i++) {
  21. if (dl[i - 1] + dr[i + 1] > max) {
  22. max = dl[i - 1] + dr[i + 1];
  23. }
  24. }
  25. for (int i = 1; i <= n; i++) {
  26. if (dl[i] > max) {
  27. max = dl[i];
  28. }
  29. }
  30. System.out.println(max);
  31. sc.close();
  32. }
  33. }
Add Comment
Please, Sign In to add comment