Advertisement
daskalot

Untitled

Apr 2nd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import java.util.*;
  2. public class I5 {
  3. static int[] arr;
  4. static int N;
  5. static void read(){
  6. Scanner sc = new Scanner(System.in);
  7. N = 0;
  8. arr = new int[100];
  9. while(sc.hasNextInt()){
  10. arr[N]=sc.nextInt();
  11. N++;
  12. }
  13. int max_sum = solver(arr, 0, N-1);
  14. sc.close();
  15. }
  16. static void print(int arr[],int l,int h,int res){
  17. System.out.print("[");
  18. for(int i=l;i<=h;i++){
  19. System.out.print(arr[i]);
  20. if(i+1<=h) System.out.print(", ");
  21. }
  22. System.out.println("]: "+res);
  23. }
  24. static int conquer(int a[],int l, int r){
  25. int max = Integer.MIN_VALUE, maxPrev = 0;
  26. for (int i = l; i <= r; i++){
  27. maxPrev = maxPrev + a[i];
  28. if (max < maxPrev)
  29. max = maxPrev;
  30. if (maxPrev < 0)
  31. maxPrev = 0;
  32. }
  33. print(arr,l,r,max);
  34. return max;
  35. }
  36. static int solver(int arr[], int l, int h){
  37. if (l == h){
  38. print(arr,l,h,arr[l]);
  39. return arr[l];
  40. }
  41. int m = (l + h)/2;
  42. return Math.max(Math.max(solver(arr, l, m), solver(arr, m+1, h)), conquer(arr, l, h));
  43. }
  44. public static void main(String[] args){
  45. read();
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement