입력이 0이 아니면 추가하고 0이면 이전 값을 지우는 전형적인 LIFO 문제이다.
문제가 요구한 그대로 0이면 pop하고 0이 아니면 push하면 된다. 코드를 조금이라고 줄이기위해 result라는 총 합을 저장할 변수를 선언하고, push, pop을 할때 동시에 총 합을 계산하였다. 이를 위해 push의 반환값을 input과 같게 하였고, head가 null일때 반환값을 0으로 하였다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Baekjoon_10773 {
static class Stack {
static class Node {
private final int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
private Node head;
public Stack() {
head = null;
}
public int push(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
return data; // 더하기 뺴기에 쓸꺼라 data 반환
}
public int pop() {
if (head == null) return 0; // 더하기 뺴기에 쓸꺼라 없으면 0
int result = head.data;
head = head.next;
return result;
}
}
public void result() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 입력받기
Stack stack = new Stack();
String str = br.readLine(); // 한줄씩 받기
StringTokenizer st = new StringTokenizer(str); // " "를 기준으로 나눈기
int n = Integer.parseInt(st.nextToken());
int result = 0;
for (int i = 0; i < n; i++) {
str = br.readLine(); // 한줄씩 받기
st = new StringTokenizer(str); // " "를 기준으로 나눈기
int input = Integer.parseInt(st.nextToken());
if (input == 0) result -= stack.pop();
else result += stack.push(input);
}
System.out.println(result);
}
public static void main(String[] args) throws Exception {
new Baekjoon_10773().result();
}
}