백준 10773번(Java)

박은지·2025년 2월 12일
0

백준

목록 보기
29/89
post-thumbnail

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Stack<Integer> stack = new Stack<>();
		
		int K = Integer.parseInt(br.readLine());
		int result = 0;
		
		for(int i=0; i<K; i++) {
			int num = Integer.parseInt(br.readLine());
			
			if(num == 0) {
				stack.pop();
			} else {
				stack.push(num);
			}
		}
		
		for(int n:stack) {
			result += n;
		}
		System.out.println(result);
	}
}
profile
백엔드 개발자가 되고싶은 eunzi😊

0개의 댓글