fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. cout.tie(0);
  8. int n;
  9. while(cin >> n && n != 0)
  10. {
  11. long long int arr[n];
  12. for(int i = 0; i < n;i++)
  13. {
  14. cin >> arr[i];
  15. }
  16. stack<long long int> s;
  17. long long int i = 0;
  18. long long int cur_area = 0;
  19. long long int max_area = 0;
  20. while(i < n)
  21. {
  22. if(s.empty() == true || arr[i] >= arr[s.top()])
  23. s.push(i++);
  24. else
  25. {
  26. long long int temp = s.top();
  27. s.pop();
  28. if(s.empty() == true)
  29. cur_area = arr[temp] * i;
  30. else
  31. cur_area = arr[temp] * (i-s.top()-1);
  32. }
  33. if(cur_area > max_area)
  34. max_area = cur_area;
  35. }
  36. while(!s.empty())
  37. {
  38. long long int temp = s.top();
  39. s.pop();
  40. if(s.empty() == true)
  41. cur_area = arr[temp] * i;
  42. else
  43. cur_area = arr[temp] * (i-s.top()-1);
  44. }
  45. if(cur_area > max_area)
  46. max_area = cur_area;
  47. cout << max_area << "\n";
  48. }
  49. }
  50.  
Success #stdin #stdout 0s 4492KB
stdin
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
stdout
8
4000