백준 10773(스택 구조)

김경욱·2025년 9월 11일

백준

목록 보기
75/121

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

import java.util.*;

import static java.util.Collections.*;

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

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int N = Integer.parseInt(br.readLine());


    Stack<Integer> stack = new Stack<>();


    for (int i = 0; i < N; i++) {

        int M = Integer.parseInt(br.readLine());

        if (M == 0 && stack.isEmpty() != true)
        {
            stack.pop();
        continue;
        }

        else {
            stack.push(M);
        }



    }


    int total = 0;

    for (Integer integer : stack) {
        total += integer;
    }

    System.out.println(total);












}
}

스택 구조 문제였다. 여기선 continue;가 중요한 역할을 했다!

0개의 댓글