D3-8931. 제로
Stack의 기본 개념을 이용한 문제
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
class Solution {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int test_case = 1; test_case <= T; test_case++) {
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.pop();
} else {
stack.add(num);
}
}
int sum = 0;
while (!stack.isEmpty()) {
sum += stack.pop();
}
System.out.println("#" + test_case + " " + sum);
}
}
}