#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector <int> arr;
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
if (n == 0)
arr.pop_back();
else
arr.push_back(n);
}
int sum = 0;
for (int i = 0; i < arr.size(); i++)
sum += arr[i];
cout << sum;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKHZvaWQpCnsKCXZlY3RvciA8aW50PiBhcnI7CgoJaW50IHQ7CgljaW4gPj4gdDsKCXdoaWxlICh0LS0pCgl7CgkJaW50IG47CgkJY2luID4+IG47CgoJCWlmIChuID09IDApCgkJCWFyci5wb3BfYmFjaygpOwoJCWVsc2UKCQkJYXJyLnB1c2hfYmFjayhuKTsKCX0KCWludCBzdW0gPSAwOwoJZm9yIChpbnQgaSA9IDA7IGkgPCBhcnIuc2l6ZSgpOyBpKyspCgkJc3VtICs9IGFycltpXTsKCWNvdXQgPDwgc3VtOwp9