[백준] 제로

jun·2021년 3월 28일
0
post-thumbnail

유의할점

풀이

0일때 pop / 0아니면 push → 결과 : stack의 모든값 더한값

코드

C++

#include <iostream>
#include <stack>

using namespace std;

int main() {
	stack<int> st;
	int K;
	cin >> K;
	while (K--) {
		int N;
		cin >> N;
		if (N)
			st.push(N);
		else
			st.pop();
	}
	int res = 0;
	while (!st.empty()) {
		res += st.top();
		st.pop();
	}
	cout << res;
}
profile
Computer Science / Algorithm / Project / TIL

0개의 댓글