Baekjoon Online Judge
백준 10773번) 제로
judy1467
2023. 1. 16. 02:51
본문의 코드보다 더 효율적인 코드가 분명 존재합니다.
참고만 해주시면 감사하겠습니다.
코드 지적은 언제나 환영입니다.

풀이
간단한 스택 문제이다.
첫 번째 줄에 주어지는 K 만큼 숫자를 입력받고
입력받은 숫자가 0일 때에는 pop을 0이 아닐 때에는 push를 하고
스택에 남아있는 원소의 합을 출력하면된다.
코드
#include "iostream"
#include "stack"
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc, sum = 0;
cin >> tc;
stack<int> list;
for(int i = 0 ; i < tc ; i++){
int tmp;
cin >> tmp;
if(tmp == 0){
sum -= list.top();
list.pop();
continue;
}
list.push(tmp);
sum += list.top();
}
cout << sum;
return 0;
}