문제링크
https://www.acmicpc.net/problem/10773
package BOJ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main_10773 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Integer> stack = new Stack<>();
int result = 0;
int K = Integer.parseInt(br.readLine());
for(int k=0;k<K;k++){
int number = Integer.parseInt(br.readLine());
if(number!=0) stack.push(number);
else stack.pop();
}
while (!stack.isEmpty()){
result += stack.pop();
}
System.out.println(result);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[JAVA]백준_2583_영역구하기 (0) | 2021.06.26 |
---|---|
[JAVA]백준_2750_수 정렬하기 (0) | 2021.06.24 |
[JAVA] 백준_2468_안전영역 (0) | 2021.06.24 |
[JAVA]백준_10814_나이순정렬 (0) | 2021.06.23 |
[JAVA]백준_14425_문자열집합 (0) | 2021.06.23 |