C++)
#include<iostream>
#include<stack>
using namespace std;
stack<int> s;
int main(void){
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
int n, temp;
cin >> n;
for(int i=0; i<n; i++){
cin >> temp;
if(temp==0) s.pop();
else s.push(temp);
}
int ans=0;
while(!s.empty()){
ans+=s.top();
s.pop();
}
cout << ans;
return 0;
}
Python)
import sys
read = sys.stdin.readline
k = int(read().rstrip())
st = []
for _ in range(k):
n = int(read().rstrip())
if n == 0 and st:
st.pop()
else:
st.append(n)
print(sum(st))'백준 1 > 자료구조' 카테고리의 다른 글
| [백준 1158] 요세푸스 문제 (C++/Python) (0) | 2020.12.06 |
|---|---|
| [백준 2161] 카드1 (C++/Python) (0) | 2020.12.06 |
| [백준 10845] 큐 (C++/Python) (0) | 2020.12.06 |
| [백준 9012] 괄호 (C++/Python) (0) | 2020.12.06 |
| [백준 10828] 스택 (C++/Python) (0) | 2020.12.06 |