import functools
import copy


def find_paths(graph, encode, start, destination, visited, path, paths):
    visited[start] = True
    path.append(start)
    if start == destination:
        # sum = functools.reduce(lambda x, y: x + y, [encode[p] for p in path])
        paths.append(copy.deepcopy(path))
    else:
        for adj in graph[start]:
            if not visited[adj]: find_paths(graph, encode, adj, destination, visited, path, paths)
    visited[start] = False
    path.pop()


def get_sum(ls):
    if len(ls) == 1: return ls[0]
    range_length = range(len(ls))
    encode = {i:ls[i] for i in range_length}
    graph = {i:[j for j in range(i+1, len(ls)) if encode[j] > 0] if encode[i] < 0 else [j for j in range(i+1, len(ls)) if encode[j] < 0] for i in range_length}
    paths = list()
    for start in graph:
        for destination in graph:
            if start != destination:
                find_paths(graph, encode, start, destination, {c:False for c in graph}, list(), paths)
    if not len(paths): return max(ls)
    max_length = max([len(p) for p in paths])
    ps = [[encode[m] for m in p] for p in paths if len(p) == max_length]
    return sum(max(ps, key=lambda xs: sum(xs)))


if __name__ == '__main__':
    '''test_case = int(input())
    result = list()
    for _ in range(test_case):
        n = int(input())
        ls = [int(i) for i in input().split()]
        result.append(get_sum(ls))
    for r in result:
        print(r)'''
    print(get_sum([-1, -2, -2, -3]))
