링크 : https://www.acmicpc.net/problem/10773
/*
문제 : 제로
링크 : https://www.acmicpc.net/problem/10773
*/
#include <iostream>
#include <stack>
using namespace std;
int main(){
int n ;
cin >> n;
stack <int> s;
for(int i = 0; i < n; i++){
int tmp;
cin >> tmp;
if(tmp == 0){
s.pop();
}
else{
s.push(tmp);
}
}
int sum = 0;
while(!s.empty()){
sum += s.top();
s.pop();
}
cout << sum;
return 0;
}