Algorithm

[백준 10773번] JAVA로 푼 스택 - 장부

도도고영 2022. 1. 19. 14:31

import java.util.*;

public class StackZero {
	public static void main(String[] args) {
		Stack<Integer> account = new Stack<Integer>();
		Scanner sc = new Scanner(System.in);
		int	k, money, total;
		
		k = sc.nextInt();
		for (int i = 0; i < k; i++) {
			money = sc.nextInt();
			
			if(money == 0) account.pop();
			else account.push(money);
		}
		
		total = 0;
		while(!account.empty()) {
			total += account.pop();
		}
		System.out.println(total);
	}
}