#include <iostream>
#include <stack>
#include <algorithm>

using namespace std;
int n;
stack <int> num;
int idx=0;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int total=0;
    cin  >>  n;
    while (n--) {
        int x;
        cin >> x;
        if(x==0) {
            total-=num.top();
            num.pop();   
        }
        else {
            num.push(x);
            total+=x;
        }
    }
    cout << total;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: