#include <cstdio>
#include <algorithm>
#include <stack>

using namespace std;

int main()
{
    stack<int> stk;
    int k;
    scanf("%d",&k);
    for(int i=0; i<k; i++)
    {
        int a;
        scanf("%d",&a);
        if(a==0 && !stk.empty())
            stk.pop();
        else
            stk.push(a);
    }

    int sum=0;
    while(!stk.empty())
    {
        sum+=stk.top();
        stk.pop();
    }

    printf("%d",sum);
    return 0;
}

Embed on website

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