Algorithm

[BOJ]10773번 : 제로

rudgh99_algo 2024. 8. 6. 19:42

1. problem :

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

 

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std; 

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);

	stack<int> s;
	int K;
	cin >> K;
	while (K--) {
		int op;
		cin >> op;
		if (op != 0) s.push(op);
		else s.pop();
	}
	int ans = 0;
	while (!s.empty()) {
		ans += s.top();
		s.pop();
	}
	cout << ans;
}