#include <stack>
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int k;
cin >> k;
stack<int> money;
for (int i = 0; i < k; ++i) {
int input;
cin >> input;
if (input == 0) money.pop();
else money.push(input);
}
int sum = 0;
while (!money.empty()) {
sum += money.top();
money.pop();
}
cout << sum;
return 0;
}