#include <stdio.h>
#include <string.h>

int n ;
int p ;
int q ;
int numlist[5010] ;
bool changed ;
bool vis[5010] ;

void proc ( ) ;

int main ( )
{
	while ( scanf ( "%d" , &n ) == 1 && n ) {
		for ( int i = 0 ; i < n ; i++ ) {
			scanf ( "%d" , &numlist[i] ) ;
		}
		p = 0 ;
		q = n - 1 ;
		changed = true ;
		memset ( vis , false , sizeof ( false ) ) ;
		while ( numlist[p] <= 0 ) {
			p++ ;
		}
		while ( numlist[q] <= 0 ) {
			q-- ;
		}
		while ( changed == true ) {
			changed = false ;
			proc ( ) ;
		}
		int total = 0 ;
		for ( int i = p ; i <= q ; i++ ) {
			total += numlist[i] ;
		}
		printf ( "%d\n" , total ) ;
	}
	return 0 ;
}

void proc ( )
{
	for ( int i = p ; i <= q ; i++ ) {
		if ( numlist[i] < 0 && vis[i] == false ) {
			vis[i] = true ;
			int tmp = 0 ;
			for ( int j = p ; j < i ; j++ ) {
				tmp += numlist[j] ;
			}
			if ( tmp < numlist[i] * ( -1 ) ) {
				p = i + 1 ;
				changed = true ;
			}
			break ;
		}
	}
	for ( int i = q ; i >= p ; i-- ) {
		if ( numlist[i] < 0 && vis[i] == false ) {
			vis[i] = true ;
			int tmp = 0 ;
			for ( int j = q ; j > i ; j-- ) {
				tmp += numlist[j] ;
			}
			if ( tmp < numlist[i] * ( -1 ) ) {
				q = i - 1 ;
				changed = true ;
			}
			break ;
		}
	}
}