• 문제 링크
https://www.acmicpc.net/problem/13676
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
Stack<Integer> stk = new Stack<>();
int a;
while ((a = read()) != 0) {
int c = read(), i = read(), sum = a - stk.push(i);
while (c-- > 1) if ((i = stk.pop() - stk.push(read())) > 0) sum += i;
sb.append(sum).append('\n');
}
bw.write(sb.toString());
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 12595 Odd Man Out (Small), 12596 Odd Man Out (Large) - Data Structure / Java (0) | 2025.09.10 |
---|---|
[백준] 33042 이변마작 1 - Data Structure / Java (0) | 2025.09.09 |
[백준] 29721 변형 체스 놀이 : 다바바(Dabbaba) - Data Structure / Java (0) | 2025.09.07 |
[백준] 25816 Overall Ranking - Data Structure / Java (0) | 2025.09.06 |
[백준] 3294 PASTE - Data Structure / Java (0) | 2025.09.05 |
댓글