백준
백준 10773 제로 (파이썬, 자바)
MC류짱
2022. 12. 6. 23:10
풀이 (파이썬)
import sys
N = int(sys.stdin.readline())
S = []
for _ in range(N):
num = int(sys.stdin.readline())
if num > 0:
S.append(num)
elif num == 0:
S.pop()
print(sum(S))
풀이 (자바)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int K = Integer.parseInt(bf.readLine());
List<Integer> nums = new ArrayList<>();
for (int i = 0; i < K; i++) {
int num = Integer.parseInt(bf.readLine());
if (num != 0) {
nums.add(num);
} else {
nums.remove(nums.size()-1);
}
}
int rst = nums.stream().mapToInt(Integer::intValue).sum();
System.out.println(rst);
}
}