#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i < n; i++)
using ll = long long;
using P = pair<int,int>;

const ll INF = 1e18;
const int MX = 2005;

int main() {
    ll n;
    cin >> n;
    vector<ll>A(n);
    rep(i,n) cin >> A[i];
    vector<ll>maxS(n);
    vector<ll>sumS(n);
    rep(i,n){
        if(i == 0){
            sumS[i] = A[0];
            maxS[i] = max(0ll,A[0]);
        }else{
            sumS[i] = sumS[i-1] + A[i];
            maxS[i] = max(maxS[i-1],sumS[i-1] + A[i]);
        }
    }
    ll m = 0;
    ll sum = 0;
    for(int i = 0; i < n; i++){
        if(sum + maxS[i] > m){
            m = sum + maxS[i];
        }
        sum += sumS[i];
    }
    cout << m << endl;
}
#include <bits/stdc++.h>

#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; while(t--)
#define sz(x) (int)(x).size()
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;

template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

const int N = 1e5+5;
const int inf = 0x3f3f3f3f;
/**
Read problem statement carefully
sum + len*y == x
**/


void solve(){
    int n;
    cin >> n;
    LL res = 1e9;
    LL mnA = 1e9, mnB = 1e9;
    for(int i = 0; i < n; ++i){
        LL a, b;
        cin >> a >> b;
        res = min({res, a+b, max(a, mnB), max(b, mnA)});
        mnA = min(mnA, a);
        mnB = min(mnB, b);
    }
    cout << res << "\n";
}

int main()
{
    Fast;
    //louisfghbvc
        solve();
    return 0;
}

/**
Enjoy the problem.
**/
