fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[] S = {1,2,-3,4,5,-6};
  13. solution(S);
  14. }
  15. static int solution(int[] S) {
  16. int max_sum = 0;
  17. int current_sum = 0;
  18. boolean positive = false;
  19. int n = S.length;
  20. for (int i = 0; i < n; ++i) {
  21. int item = S[i];
  22. if (item < 0) {
  23. if (max_sum < current_sum) {
  24. max_sum = current_sum;
  25. current_sum = 0;
  26. }
  27. } else {
  28. positive = true;
  29. current_sum += item;
  30. }
  31. }
  32. if (current_sum > max_sum) {
  33. max_sum = current_sum;
  34. }
  35. if (positive) {
  36. return max_sum;
  37. }
  38. return -1;
  39. }
  40. }
Success #stdin #stdout 0.12s 46896KB
stdin
Standard input is empty
stdout
Standard output is empty