import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int K= Integer.parseInt(br.readLine());
Stack<Integer> stack = new Stack<>();
for(int i=0; i<K; i++) {
int num = Integer.parseInt(br.readLine());
if( num!=0 ) {
stack.push(num);
}else {
if(!stack.isEmpty()) {
stack.pop();
}
}
}
int sum = 0;
while(!stack.isEmpty()) {
sum += stack.pop();
}
System.out.println(sum);
}
}
'공부기록 > 백준' 카테고리의 다른 글
[백준] 2941번 크로아티아 알파벳 (0) | 2023.10.15 |
---|---|
[백준] 9012번 괄호 (0) | 2023.10.15 |
백준 10828번 스택 (0) | 2023.10.11 |
[백준] 10825번 국영수 (0) | 2023.10.08 |
[백준] 2470번 두 용액 (0) | 2023.10.02 |