package backjoon;
import java.util.Scanner;
import java.util.Stack;
public class 제로_10773 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<Integer> stack = new Stack<>();
int n = sc.nextInt();
for(int i =0; i<n; i++) {
int a = sc.nextInt();
if(a != 0) {
stack.push(a);
} else if(!stack.isEmpty()){
stack.pop();
}
}
sc.close();
// 스택에 남아있는 값들의 합 구하기
int sum = 0;
while(!stack.isEmpty()) {
sum += stack.pop();
}
System.out.println(sum);
}
}
오랜만에 백준을 풀어봤는데 처음부터 다 써야되는 게 어색하긴하다!
stack의 stack.push()와 stack.pop()을 잘 적용해서 써볼 수 있어서 좋았다.