Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws java.lang.Exception {
- Scanner sc = new Scanner(System.in);
- long n = sc.nextLong();
- long a[] = new long[(int) n];
- for (int i = 0; i < (int) n; i++) {
- a[i] = sc.nextLong();
- }
- System.out.print(ar(a, n));
- }
- public static long ar(long hist[], long n) {
- Stack<Integer> st1 = new Stack<>();
- Stack<Integer> st2 = new Stack<>();
- int h1[] = new int[(int) n];
- int h2[] = new int[(int) n];
- int k = -1;
- for (int i = (int) n - 1; i >= 0; i--) {
- while (!st1.isEmpty() && hist[st1.peek()] >= hist[i])
- st1.pop();
- k = i;
- if (st1.isEmpty())
- h1[i] = (int) n;
- else
- h1[i] = st1.peek();
- st1.push(k);
- }
- for (int i = 0; i < (int) n; i++) {
- while (!st2.isEmpty() && hist[st2.peek()] >= hist[i])
- st2.pop();
- k = i;
- if (st2.isEmpty())
- h2[i] = -1;
- else
- h2[i] = st2.peek();
- st2.push(k);
- }
- long mx = 0;
- for (int i = 0; i < n; i++) {
- if (hist[i] * (h1[i] - h2[i] - 1) > mx) {
- mx = hist[i] * (h1[i] - h2[i] - 1);
- }
- }
- return mx;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement