[백준] 10773 - 제로

zzenee·2022년 5월 20일
0

Algorithm&Coding-test

목록 보기
15/30
post-thumbnail

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

Problem

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.io.IOException;

class Main {
	static int n; 
	static Stack<Integer> stack;

  	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		stack = new Stack<>();
		for (int i=0; i<n; i++) {
			int tmp = Integer.parseInt(br.readLine());
			if (tmp == 0) stack.pop();
			else stack.push(tmp);
		}
		int sum = 0;
		for (int num : stack) {
			sum += num;
		}
		System.out.print(sum);
  	}
}

Result

profile
꾸준히

0개의 댓글