#include <stdio.h>
//스택은 마지막 top만 잇음
int top=-1;
int arr[100000];
// 맨위에 꺼냄
int pop() {
if(top==-1) return -1;
return arr[top--];
}
//데이터를 맨 위에 쌓음
void push(int n) {
arr[++top]=n;
}
int main() {
int n,num;
int sum=0;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&num);
if(num==0) pop();
else push(num);
}
for(int i=0; i<=top; i++) {
sum+=arr[i];
}
printf("%d",sum);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: