java
[java] 백준 10773 제로
MiaCoder
2024. 1. 31. 11:22
매우 간단한 문제이다.
Stack클래스의 메소드를 연습한다고 생각하면 된다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int K = Integer.parseInt(br.readLine());
int sum = 0;
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 (num == 0 && !stack.isEmpty()) {
stack.pop();
}
}
while (!stack.isEmpty()) {
sum += stack.pop();
}
System.out.print(sum);
}
}