a53

SumaMaxSecv

a53
Nov 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 100003
  3. using namespace std;
  4.  
  5. int a[nmax], st[nmax], dr[nmax], n;
  6.  
  7. void Citire()
  8. {
  9. cin >> n;
  10. for (int i = 1; i <= n; i++)
  11. cin >> a[i];
  12. a[0] = a[n + 1] = n + 1;
  13. }
  14.  
  15. void StangaDreapta()
  16. {
  17. int i, x;
  18. stack<int> q;
  19. q.push(0);
  20. for (i = 1; i <= n; i++)
  21. {
  22. x = a[i];
  23. while (x > a[q.top()])
  24. q.pop();
  25. st[i] = i - q.top();
  26. q.push(i);
  27. }
  28. /// golire stiva
  29. while (!q.empty()) q.pop();
  30.  
  31. q.push(n + 1);
  32. for (i = n; i >= 1; i--)
  33. {
  34. x = a[i];
  35. while (x > a[q.top()])
  36. q.pop();
  37. dr[i] = q.top() - i;
  38. q.push(i);
  39. }
  40. }
  41.  
  42. void CostTotal()
  43. {
  44. long long cost = 0;
  45. for (int i = 1; i <= n; i++)
  46. cost += 1LL * st[i] * dr[i] * a[i];
  47. cout << cost << "\n";
  48. }
  49.  
  50. int main()
  51. {
  52. Citire();
  53. StangaDreapta();
  54. CostTotal();
  55. return 0;
  56. }
Add Comment
Please, Sign In to add comment