반응형

 

📖 문제

 

📋 코드

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Stack<Integer> stack = new Stack<>();
		int N = sc.nextInt();
		int ans = 0;
		for (int i = 0; i < N; i++) {
			int x = sc.nextInt();
			if(x == 0) {
				stack.pop();
			}else {
				stack.push(x);
			}
		}
		
		while(!stack.isEmpty()) {
			ans += stack.peek();
			stack.pop();
		}
		System.out.println(ans);
	}
}

👨🏻‍💻 결과

 

 

 

🔗 링크

https://www.acmicpc.net/problem/10773

반응형

+ Recent posts