#include <iostream>
#include <stack>
int main() {
int command_count, sum = 0;
std::stack<int> pc;
std::cin >> command_count;
for (int i = 0; i < command_count; i++) {
int a;
std::cin >> a;
if (a == 0 && !pc.empty()) {
pc.pop();
} else {
pc.push(a);
}
}
while (!pc.empty()) {
sum += pc.top();
pc.pop();
}
std::cout << sum << std::endl;
return 0;
}