#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = (2e5) + 5;
ll a[N];
ll ac[N];
ll ac2[N];
multiset<ll> ms;
multiset<ll> :: iterator it;
int n;

int main() {
  ios::sync_with_stdio(!cin.tie(0));
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ac[0] = a[0];
  for (int i = 1; i < n; ++i) {
    ac[i] = ac[i - 1] + a[i];
  }
  ac2[0] = ac[0];
  for (int i = 1; i < n; ++i) {
    ac2[i] = ac2[i - 1] + ac[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    ms.insert(-ac2[i]);
  }
  ll mx = 0;
  for (int i = 0; i < n; ++i) {
    ll x = -*ms.begin();
    mx = max(mx, ac[i] + x);
    if (i - 1 >= 0) {
      ms.erase(-ac2[i - 1]);
    }
  }
  cout << mx << "\n";
  return 0;
}

/*
   Problem name: -
   Difficulty level: -
   Techniques: -
   Hints: -
   Written by Thalyson
*/

#include <bits/stdc++.h>
using namespace std;

#define vi vector< int >
#define vii vector< pair<int,int> >
#define vvi vector<vector< int > >
#define vvii vector<vector< pair<int,int> > >
#define int int64_t


#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define sortv(V) sort(all(V))
#define reversev(V) reverse(all(V))
#define uniquev(V) sortv(V),(V).erase(unique(all(V)),(V).end())
#define pb push_back


#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define debug(x) cout << #x << " is " << x << "\n";

// Trace dinâmico (parameter pack (c++11))
template<typename T>
void println(T a) { cout << a << "\n"; }
template<typename T, typename... Args>
void println(T a, Args... args) { cout << a << " "; println(args...); }

template<typename T>
void print(T a) { cout << a; }
template<typename T, typename... Args>
void print(T a, Args... args) { cout << a << " "; print(args...); }

template<typename T>
void printv(T a, string last="\n") {
    for(auto el: a)
      print(el, "");
    print(last);
}

template<typename T>
void scanv(T &a) {
    for(auto &el: a)
      cin >> el;
}


//Constants
#define EPS 1e-6
#define oo int(1LL<<60)
#define endl '\n'

//Directions ^>v<
const int dx4[4] = {-1, 0, 1, 0};
const int dy4[4] = {0, 1, 0, -1};

const int dx8[8] = {-1, -1, 0, 1, 1,  1,  0, -1};
const int dy8[8] = { 0,  1, 1, 1, 0, -1, -1, -1};


//Main
int N, M, K, CASES;

main(){
   std::ios::sync_with_stdio(false);
   cin.tie(0);
   cout << setprecision(9) << fixed;

   cin >> N;

   int cur = 0;
   int ans = 0;
   int sum = 0;
   int great = 0;

   for(int i = 0; i < N; i++){
      int value;
      cin >> value;
      sum += value;
      great = max(great, sum);
      ans = max(ans, cur+great);
      cur += sum;
   }
   println(ans);

   /*
      Before submit:
         Check the corners cases
         Check solution restrictions

      For implementation solutions
         Check the flow of the variables
   */
}





















