백준 10773번 : 제로

dldzm·2021년 1월 28일
0

알고리즘 공부

목록 보기
16/42
post-thumbnail

링크 : https://www.acmicpc.net/problem/10773

앗 이것도 자료구조 수업 때 풀었던 문제.. 쉽게 패스한다.
그래도 양심 챙기고 stack으로 구현..

#include <iostream>
#include <stack>
using namespace std;

int main() {
	int num, elem, total = 0;
	cin >> num;
	stack<int> stk;
	for (int i = 0; i < num; i++) {
		cin >> elem;
		if (elem == 0)
			stk.pop();
		else
			stk.push(elem);
	}

	num = stk.size();
	for (int i = 0; i < num; i++) {
		total += stk.top();
		stk.pop();
	}
	cout << total << endl;
	return 0;
}
profile
🛰️ 2021 fall ~

0개의 댓글