Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long int
- int area(int *arr,int n){
- stack<int> s;
- int top;
- int mx=INT_MIN;
- int area;
- int i=0;
- while(i<n){
- if(s.empty() || arr[s.top()] < arr[i]){
- s.push(i);
- i++;
- }
- else{
- top=s.top();
- s.pop();
- if(s.empty()) area = arr[top]*i;
- else area = arr[top]*(i-s.top()-1);
- mx=max(area,mx);
- }
- }
- while(!s.empty()){
- top=s.top();
- s.pop();
- if(s.empty()) area = arr[top]*i;
- else{
- area = arr[top]*(i-s.top()-1);
- }
- mx=max(mx,area);
- }
- return mx;
- }
- int32_t main() {
- #ifndef ONLINE_JUDGE
- // for getting input from input.txt
- freopen("input.txt", "r", stdin);
- // for writing output to output.txt
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- int arr[n];
- for(int i=0;i<n;i++){
- cin >> arr[i];
- }
- cout << area(arr,n) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement