-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo_10773.java
More file actions
70 lines (60 loc) · 1.75 KB
/
No_10773.java
File metadata and controls
70 lines (60 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Problem_10773_제로
https://www.acmicpc.net/problem/10773
*/
import java.io.*;
public class No_10773 {
private static class Stack {
private static Stack.Node top = null;
private static int size = 0;
private static class Node {
int data = 0;
Stack.Node next = null;
Node(int data, Stack.Node next) {
this.data = data;
this.next = next;
}
}
private void push(int value) {
Stack.Node node = new Stack.Node(value, top);
top = node;
size++;
}
private int pop() {
if (top == null) {
return -1;
}
int res = top.data;
top = top.next;
size--;
return res;
}
private int empty() {
//스택이 비어있으면 1리턴 아니면 0리턴
return size == 0 ? 1 : 0;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Stack stack = new Stack();
int repeat = Integer.parseInt(br.readLine());
for (int current = 0; current < repeat; current++) {
int num = Integer.parseInt(br.readLine());
if (num == 0) {
int zero = stack.pop();
}
else {
stack.push(num);
}
}
int sum = 0;
for (int size = 0; stack.empty() == 0; size++) {
sum += stack.pop();
}
bw.write(sum + "");
bw.flush();
bw.close();
br.close();
}
}