#pragma region 라이브러리
#include <iostream>
#include <cmath>
#include <functional>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
#pragma endregion
#pragma region 최적화용
#define FAST_IO() ios::sync_with_stdio(0); cin.tie(0);
#define x first
#define x second
using IntPair = pair<int, int>;
using DoubleVec = vector<vector<int>>;
using Vec = vector<int>;
// 상하좌우
IntPair dir[]
{
{ 1, 0 },
{-1, 0 },
{ 0, -1 },
{ 0, 1 }
};
#pragma endregion
int main()
{
int t;
cin >> t;
stack<int> s;
while (t--)
{
int n;
cin >> n;
if (n > 0)
s.push(n);
else
s.pop();
}
int sum = 0;
while (!s.empty())
{
sum += s.top();
s.pop();
}
cout << sum;
return 0;
}