#include <iostream>
#include <stack>

using namespace std;

int main() {
    int K; cin >> K;
    stack<int> s;
    while (K--) {
        int number; cin >> number;
        if (number == 0) {
            s.pop();
        } else {
            s.push(number);
        }
    }
    int sum = 0;
    while (!s.empty()) {
        sum += s.top();
        s.pop();
    }
    cout << sum << endl;
}