#include <iostream>

using namespace std;

int arr[100005] = { 0 };

int main()
{
	int n;
	cin >> n;
	int cur = 0;
	while (n--)
	{
		int t;
		cin >> t;
		if (t == 0)
			cur--;
		else
		{
			arr[cur] = t;
			cur++;
		}
	}
	long long sum = 0;
	for (int i = 0; i < cur; i++)
	{
		sum += arr[i];
	}
	cout << sum << endl;
	return 0;
}