Advertisement
Spidey2182

Remove from ends (Java)

Jun 7th, 2023 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.function.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         int n = sc.nextInt();
  10.         int[] a = new int[n];
  11.         for (int i = 0; i < n; i++)
  12.             a[i] = sc.nextInt();
  13.  
  14.         long ans = Integer.MIN_VALUE, sum = 0;
  15.         for (int x : a) {
  16.             sum += x;
  17.             ans = Math.max(ans, sum);
  18.             if (sum < 0)
  19.                 sum = 0;
  20.         }
  21.  
  22.         System.out.println(ans);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement