스택을 이용하는 전형적인 문제
C++ 의 STL에 정의되어있는 stack을 이용하면 굉장히 쉽다.
(오류 없이 한번에 맞은 간만의 문제라(...) 굉장히 신났다!)
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
stack<int> s;
int t;
int x;
int ans = 0;
cin >> t;
while(t--) {
cin >> x;
if(x == 0) {
s.pop();
} else {
s.push(x);
}
}
while(!s.empty()) {
ans += s.top();
s.pop();
}
cout << ans << '\n';
return 0;
}