#include <iostream>

#include <limits>
#include <tuple>
#include <vector>

// left: [low, mid)  right: [mid, high)
template<typename T>
std::tuple<std::size_t, std::size_t, T> find_max_crossing_subarray
(const std::vector<T> &A, std::size_t low, std::size_t mid, std::size_t high) {
    std::size_t max_left = mid - 1;
    std::size_t max_right = mid;

    T left_sum = std::numeric_limits<T>::min();
    T sum = 0;
    for(std::size_t i = 1; i <= mid - low; ++i) {
        sum += A[mid - i];
        if(sum > left_sum) {
            left_sum = sum;
            max_left = mid - i;
        }
    }

    T right_sum = std::numeric_limits<T>::min();
    sum = 0;
    for(std::size_t j = mid; j < high; ++j) {
        sum += A[j];
        if(sum > right_sum) {
            right_sum = sum;
            max_right = j;
        }
    }

    return std::make_tuple(max_left, max_right + 1, left_sum + right_sum);
}

// [low, high)
template<typename T>
std::tuple<std::size_t, std::size_t, T> find_maximum_subarray
(const std::vector<T> &A, std::size_t low, std::size_t high) {
    if(high - low < 2) return std::make_tuple(low, high, A[low]);

    std::size_t mid = (low + high) / 2;
    auto left = find_maximum_subarray<T>(A, low, mid);
    auto right = find_maximum_subarray<T>(A, mid, high);
    auto cross = find_max_crossing_subarray<T>(A, low, mid, high);

    T left_sum = std::get<2>(left);
    T right_sum = std::get<2>(right);
    T cross_sum = std::get<2>(cross);

    if(left_sum >= right_sum && left_sum >= cross_sum) return left;
    else if(right_sum >= left_sum && right_sum >= cross_sum) return right;
    else return cross;
}

int main() {
    std::vector<int> A(5000);
    while(true) {
        std::size_t n;
        std::cin >> n;
        if(n == 0) break;

        for(std::size_t i = 0; i < n; ++i) std::cin >> A[i];
        std::cout << std::get<2>(find_maximum_subarray<int>(A, 0, n)) << std::endl;
    }
}
