Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-10773번/Java] 제로 본문

Algorithm

[백준-10773번/Java] 제로

1984 2022. 11. 12. 01:48

* Stack 사용

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

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner scan = new Scanner(System.in);
		Stack<Integer> stack = new Stack<Integer>();

		int N = scan.nextInt();

		for (int i = 0; i < N; i++) {
			int num = scan.nextInt();
			if (num != 0) {
				stack.push(num);
			} else {
				stack.pop();
			}
		}

		long sum = 0;
		for (Integer i : stack) {
			sum += i;
		}

		System.out.println(sum);

		scan.close();

	}

}
728x90
Comments