Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- public class Main2{
- public static int Rect(int[] a){
- int r = 0;
- int area;
- int vt;
- Stack<Integer> s = new Stack<>();
- int i = 0;
- while(i < a.length){
- if(s.empty() || a[s.peek()] <= a[i]){
- s.push(i++);
- }
- else if (!s.empty()){
- vt = s.peek();
- s.pop();
- area = a[vt] * (s.empty() ? i : i - s.peek() - 1);
- r = Math.max(r, area);
- }
- }
- while(!s.empty()){
- vt = s.peek();
- s.pop();
- area = a[vt] * (s.empty() ? i : i - s.peek() - 1);
- r = Math.max(r, area);
- }
- return r;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while(true){
- int n = sc.nextInt();
- if (n == 0) break;
- int[] a = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = sc.nextInt();
- }
- System.out.println(Rect(a));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement