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 Main
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int[] arr = new int[n];
  15. for(int i=0;i<n;i++){
  16. arr[i] = sc.nextInt();
  17. }
  18.  
  19. int[] lns = new int[n];
  20. int[] rns = new int[n];
  21. Stack<Integer> st = new Stack<>();
  22.  
  23. for(int i = 0; i < n; i++) {
  24. while(!st.isEmpty() && arr[st.peek()] >= arr[i]) {
  25. st.pop();
  26. }
  27. lns[i] = st.isEmpty() ? -1 : st.peek();
  28. st.push(i);
  29. }
  30.  
  31. st.clear();
  32.  
  33. for(int i = n - 1; i >= 0; i--) {
  34. while(!st.isEmpty() && arr[st.peek()] >= arr[i]) {
  35. st.pop();
  36. }
  37. rns[i] = st.isEmpty() ? n : st.peek();
  38. st.push(i);
  39. }
  40.  
  41.  
  42. int maxCost = 0;
  43. for(int i=0;i<n;i++){
  44. int mini = arr[i];
  45. int left = lns[i]+1;
  46. int right = rns[i]-1;
  47. int cost = arr[i]*(right-left+1);
  48. maxCost = Math.max(cost, maxCost);
  49. }
  50. System.out.println(maxCost);
  51. }
  52. }
Success #stdin #stdout 0.12s 56668KB
stdin
1
50 
stdout
50