Computer Engineering/알고리즘 테스트
백준[10773]- 제로 (java)
말하는호구마
2021. 2. 25. 03:50
너어무우 쉬운 문제
스택 사용을 눈치 채고
스택이 비어있는지 잘 확인만 해주면 된다
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
Stack<Integer> st =new Stack<>();
long sum=0;
for(int i=0;i<n;i++){
int temp = sc.nextInt();
if(temp==0) {
if(!st.isEmpty()) {
st.pop();
}
else
continue;
}
else {
st.push(temp);
}
}
while(!st.isEmpty()){
sum+=st.pop();
}
System.out.println(sum);
}
}