이번 문제는 자료구조인 Stack을 이해하고 있다면 별 고민없이 바로 풀리는 문제이므로 힌트는 없다..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class b10773 {
public static void main(String[] args) throws IOException {
Stack<Integer> s = new Stack<>();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
int result = 0;
for(int i = 0; i < n ; i++){
int a = Integer.parseInt(bf.readLine());
if(a!=0){
s.push(a);
result +=s.peek();
} else{
result -=s.peek();
s.pop();
}
}
System.out.println(result+"");
}
}
'백준 문제풀이' 카테고리의 다른 글
[백준] 1009번 (0) | 2024.04.20 |
---|---|
[백준] 9012번 (0) | 2024.04.18 |
[백준] 4673번 (0) | 2024.04.12 |
[백준] 1021번 (0) | 2024.04.09 |
[백준] 10815번 (2) | 2024.04.01 |