lina_os

Untitled

Mar 10th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  8. int n;
  9. cin >> n;
  10. stack<ll>st;
  11. st.push(1);
  12. ll op=0;
  13. int f=0;
  14. int o=0;
  15. while (n--) {
  16. string s;
  17. cin >> s;
  18. if (s=="for") {
  19. int x; cin >> x;
  20. if (st.top()>UINT_MAX) o=1;
  21. st.push(st.top()*x);
  22. }
  23. else if (s=="add") {
  24. if (o) f=1;
  25. op+=st.top();
  26. }
  27. else {
  28. st.pop();
  29. if (st.top()<=UINT_MAX) o=0;
  30.  
  31. }
  32. if (op>UINT_MAX) f=1;
  33. }
  34. if (op>UINT_MAX || f) cout << "OVERFLOW!!!";
  35. else cout << op;
  36.  
  37. return 0;
  38. }
  39.  
  40. /*
  41. 13
  42. for 100
  43. for 100
  44. for 100
  45. for 100
  46. for 100
  47. for 100
  48. end
  49. end
  50. end
  51. end
  52. end
  53. end
  54. add
  55. *UINT_MAX
  56. */
Advertisement
Add Comment
Please, Sign In to add comment