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));
Stack<Integer> remainingTime = new Stack<>();
Stack<Integer> score = new Stack<>();
int sum = 0;
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
String[] str = br.readLine().split(" ");
if (remainingTime.isEmpty() && str[0].equals("0")) {
continue;
}
if (str[0].equals("0")) {
int count = remainingTime.pop();
remainingTime.push(count - 1);
} else {
score.push(Integer.parseInt(str[1]));
remainingTime.push(Integer.parseInt(str[2])-1);
}
if (remainingTime.peek() == 0) {
sum += score.pop();
remainingTime.pop();
}
}
System.out.println(sum);
}
}
깨달은 점:
배열을 Arrays.toString으로 간편하게 값을 출력할 수 있음
Stack의 top 값은 수정이 불가능 하기에 pop과 push를 다시 함으로써 top값이 수정된 결과를 얻을 수 있음
String.split으로 잘린 문자열을 배열로 받아올 수 있음
'Java > AlgorithmPS' 카테고리의 다른 글
백준) 좋은 단어(3986번) - 복습 필요 (0) | 2023.09.23 |
---|---|
백준) 단어 뒤집기2(17413번) (0) | 2023.09.23 |
백준) 스택 수열(1874번) (0) | 2023.09.22 |
백준) 세로읽기(10798번) (0) | 2023.09.20 |
백준) 팰린드롬수(1259번) (0) | 2023.09.20 |