#include <bits/stdc++.h>
using namespace std;
const int maxn=100010;
int n,tot,maxi;
long long ans;
long long a[maxn];
int s[maxn];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for (int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1,cmp);
for (int i=1;i<=n-2;i++)
if (a[i]<a[i+1]+a[i+2]) s[++tot]=i;
for (int i=1;i<=tot;i++)
if (s[i]-s[1]>=3)
{
maxi=i;
break;
}
if (maxi!=0)ans=a[s[1]]+a[s[1]+1]+a[s[1]+2]+a[s[maxi]]+a[s[maxi]+1]+a[s[maxi]+2];
if (n-s[1]+1<6)
{
cout<<ans;
return 0;
}
bool flag=false;
for (int i=1;i<=6;i++)
for (int j=i+1;j<=6;j++)
{
if (i==j) continue;
for (int k=j+1;k<=6;k++)
{
if (i==k || j==k) continue;
for (int l=1;l<=6;l++)
{
if (i==l || j==l || k==l) continue;
for (int o=l+1;o<=6;o++)
{
if (i==o || j==o || k==o || l==o) continue;
for (int p=o+1;p<=6;p++)
{
if (i==p || j==p || k==p || l==p || o==p) continue;
if (a[s[1]+k-1]+a[s[1]+j-1]>a[s[1]+i-1] && a[s[1]+p-1]+a[s[1]+o-1]>a[s[1]+l-1])
{
flag=true;
}
}
}
}
}
}
if (flag) ans=a[s[1]]+a[s[1]+1]+a[s[1]+2]+a[s[1]+3]+a[s[1]+4]+a[s[1]+5];
cout<<ans<<endl;
return 0;
}