fork download
  1. import java.io.*;
  2. import java.util.*;
  3. class HISTOGRA
  4. {
  5. static void calculate(long a[], int n)
  6. {
  7. long maxArea = 0;
  8. int i = 0;
  9. Stack <Integer> stack = new Stack<Integer> ();
  10. for(i = 0; i<n;)
  11. {
  12. if(stack.empty() == true || a[stack.peek()] <= a[i])
  13. {
  14. stack.push(i);
  15. i++;
  16. }
  17. else
  18. {
  19. long temp = a[stack.pop()];
  20. long tempArea = temp * (stack.empty() ? i : i - 1 - stack.peek());
  21. maxArea = Math.max(tempArea , maxArea);
  22. }
  23. }
  24. while(stack.empty() == false)
  25. {
  26. long temp = a[stack.pop()];
  27. long tempArea = temp * (stack.empty() ? i : i - 1 - stack.peek());
  28. maxArea = Math.max(tempArea , maxArea);
  29. }
  30. System.out.println(maxArea);
  31. return;
  32. }
  33. public static void main(String[] args) throws IOException{
  34. String s[] = br.readLine().trim().split("\\s+");
  35. int n = Integer.parseInt(s[0]);
  36. while(n != 0)
  37. {
  38. long a[] = new long[n];
  39. for(int i = 1; i<=n ; i++)
  40. {
  41. a[i-1] = Long.parseLong(s[i]);
  42. }
  43. calculate(a, n);
  44. s = br.readLine().trim().split("\\s+");
  45. n = Integer.parseInt(s[0]);
  46. }
  47. }
  48. }
Runtime error #stdin #stdout #stderr 0.05s 2184192KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at HISTOGRA.main(Main.java:35)