#include<iostream>
#include<vector>
using namespace std;
class Route{
private:
int forward;
int back;
int front_cap;
int back_cap;
int forward_sum;
int back_sum;
vector<int> route;
int size;
public:
Route(int size){
//cout<<"Konstruktor"<<endl;
this->forward = 0;
this->back = size - 1;
this->front_cap = forward;
this->back_cap = back;
this->forward_sum = 0;
this->back_sum = 0;
this->size = size;
}
void getData(){
int buf;
//cout<<"Dane"<<endl;
for(int i=0; i<size; ++i){
cin>>buf;
route.push_back(buf);
}
/*cout<<"|"<<endl;
for(int i=0; i<size; ++i){
cout<<route[i];
}
cout<<"|"<<endl;*/
}
/*void calculate(){
//cout<<"Oblicz"<<endl;
long long max_value = -1000000000;
long long current_value;
for(int i=0; i<=back_cap; ++i){
current_value = 0;
for(int j=i; j<=back_cap; ++j){
current_value = current_value + route[j];
}
if(current_value > max_value){
max_value = current_value;
front_cap = i;
}
}
cout<<max_value<<endl;
cout<<front_cap<<endl;
max_value = route[back_cap];
for(int i=back_cap; i>=front_cap; --i){
//cout<<"| "<<i<<" |"<<endl;
current_value = 0;
for(int j=i; j>=front_cap; --j){
current_value = current_value + route[j];
}
if(current_value > max_value){
max_value = current_value;
}
}
if(max_value > 0){
cout<<max_value;
} else{
cout<<0;
}
}*/
void calculate(){
//cout<<"Oblicz"<<endl;
bool again = true;
while(again){
again = false;
if(forward_sum < back_sum){
if(forward <= back_cap){
again = true;
forward_sum = forward_sum + route[forward];
++forward;
if(forward_sum < 0){
front_cap = forward;
forward_sum = 0;
}
}
} else{
if(back >= front_cap){
again = true;
back_sum = back_sum + route[back];
--back;
if(back_sum < 0){
back_cap = back;
back_sum = 0;
}
}
}
}
cout<<getValue();
}
/*void calculate(){
//cout<<"Oblicz"<<endl;
bool again = true;
while(again){
again = false;
if(forward <= back_cap){
again = true;
forward_sum = forward_sum + route[forward];
++forward;
if(forward_sum < 0){
front_cap = forward;
forward_sum = 0;
}
}
if(back >= front_cap){
again = true;
back_sum = back_sum + route[back];
--back;
if(back_sum < 0){
back_cap = back;
back_sum = 0;
}
}
}
cout<<getValue();
}*/
int getValue(){
int score = 0;
for(int i=front_cap; i <= back_cap; ++i){
score = score + route[i];
}
return score;
}
};
int main(){
int quantity;
cin>>quantity;
Route *route = new Route(quantity);
route->getData();
route->calculate();
}
/*
5
1
-2
4
5
-2
3
10 -11 1
20
1
2
-100
3
4
-100
10
-3
-4
-2
7
-100
5
-100
4
2
-100
1
5
-100
*/