#include <bits/stdtr1c++.h>

using namespace std;

typedef long long ll;

ll K;

int main ()
{
	cin >> K;
	stack<int> stk;
	for (int i = 0; i < K; i++)
	{
		int j;
		cin >> j;
		if (j == 0) 
		{
			if (!stk.empty())
			{
				stk.pop();
			}
		}
		else stk.push(j);
	}

	ll sum = 0;

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

	cout << sum;

}