fork download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. int main()
  5. {
  6. long long i, a, j, cnt=0;
  7. stack<int> st;
  8. cin>>a;
  9. for(i=1;i<=a;i++)
  10. {
  11. cin>>j;
  12. if(j!=0)
  13. {
  14. st.push(j);
  15. cnt+=j;
  16. }
  17. else
  18. {
  19. cnt-=st.top();
  20. st.pop();
  21. }
  22. cout<<cnt;
  23. }
  24. }
Success #stdin #stdout 0.01s 5304KB
stdin
10
1
3
5
4
0
0
7
0
0
6
stdout
149139411417