#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
	int n;cin>>n;
	vector<ll> a(n);
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	vector<ll> sum_a(n + 1,0);
	vector<ll> max_a(n + 1,0);
	for(int i = 0; i < n; i++) {
		sum_a[i + 1] = sum_a[i] + a[i];
	}
	for(int i = 0; i < n; i++) {
		max_a[i + 1] = max(max_a[i],sum_a[i + 1]); 
	}
	ll ans = 0;
	ll res = 0;
	for(int i = 1; i <= n; i++) {
		chmax(res,ans + max_a[i]);
		ans += sum_a[i];
	}
	cout << res << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define codeWizard      ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define testCase(tc)    while(tc--)
#define vi              vector<int>
#define vvi             vector<vi>
#define pi              pair<int,int>
#define int             long long
#define lb              lower_bound
#define ub              upper_bound
#define mod             1000000007
//#define mod           998244353
#define ff              first
#define ss              second
#define inf             (int)1e17
#define ninf            -inf
#define endl            "\n"
#define fill(a,b)       memset(a,b,sizeof(a))
#define all(v)          v.begin(),v.end()                               //codeWizard
int LCM(int a, int b) {return (a * b) / __gcd(a, b);}

void init_code() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
int n;
vvi player;
bool check(int x){
    set<int> st;

    for(auto k:player){
        int bit=0;
        int i=0;
        for(auto i:k){
            bit <<= 1;
                bit |= i >= x;

        }
        st.insert(bit);
    }

    for(auto x:st){
        for(auto y:st){
            for(auto z:st){
                if((x|y|z)==31)
                    return true;
            }
        }
    }
    return false;
}
void solve(){
   cin>>n;
   for(int i=1;i<=n;i++){
    vector<int> temp(5);
    for(int j=0;j<5;j++)
        cin>>temp[j];
    player.push_back(temp);
   }
   int low=1,high=1000000001;
   int ans=0;
   while(high>low+1){
    //cout<<low<<" "<<high<<endl;
    int mid=(low+high)/2;
    if(check(mid)){
        low=mid;
    }
    else
        high=mid;
   }
   cout<<low<<endl;
}
signed main()
{
    codeWizard;
    init_code();
    int tc = 1;
    //cin >> tc;
    int num=1;
    testCase(tc) {
        //cout<<"Case #"<<num<<": ";

        num++;
        solve();
    }

    //cout << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;

}

