#include<bits/stdc++.h>
#define ll long long
#define opl(n) cout<<n<<endl
#define op(n) cout<<n<<" "
#define pb(n) push_back(n)
#define all(n) (n).begin(),(n).end()
#define ios  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main(){
    ios;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int i,l(INT_MIN),m(INT_MIN),c(0);
        for(int x=0;x<n;x++){
            cin>>i;
            if(i>=1)
                c++;
                if(i>=m){
                    l=m;
                    m=i;
                }
                else if(i>l&&i<m)
                    l=i;
        }
        opl(max(0,min(l-1,c-2)));
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define opl(n) cout<<n<<endl
#define op(n) cout<<n<<" "
#define pb(n) push_back(n)
#define all(n) (n).begin(),(n).end()
#define ios  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main(){
    ios;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n<3){
            opl(0);
            continue;
        }
        int i,l(INT_MIN),m(INT_MIN),c(0);
        for(int x=0;x<n;x++){
            cin>>i;
                c++;
                if(i>m){
                    l=m;
                    m=i;
                }
                else if(i>l&&i<=m)
                    l=i;
        }
        opl(min(l-1,n-2));
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define opl(n) cout<<n<<endl
#define op(n) cout<<n<<" "
#define pb(n) push_back(n)
#define all(n) (n).begin(),(n).end()
#define ios  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main(){
    ios;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n<3){
            opl(0);
            continue;
        }
        int i,l(INT_MIN),m(INT_MIN),c(0);
        for(int x=0;x<n;x++){
            cin>>i;
            if(i>=1)
                c++;
                if(i>m){
                    l=m;
                    m=i;
                }
                else if(i>l&&i<=m)
                    l=i;
        }
        opl(min(l-1,c-2));
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define opl(n) cout<<n<<endl
#define op(n) cout<<n<<" "
#define pb(n) push_back(n)
#define all(n) (n).begin(),(n).end()
#define ios  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main(){
    ios;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n<3){
            opl(0);
            continue;
        }
        int i,l(INT_MIN),m(INT_MIN),c(0);
        for(int x=0;x<n;x++){
            cin>>i;
            if(i>=1)
                c++;
                if(i>=m){
                    l=m;
                    m=i;
                }
                else if(i>l&&i<m)
                    l=i;
        }
        opl(min(l-1,c-2));
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define opl(n) cout<<n<<endl
#define op(n) cout<<n<<" "
#define pb(n) push_back(n)
#define all(n) (n).begin(),(n).end()
#define ios  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main(){
    ios;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n<3){
            opl(0);
            continue;
        }
        int i,l(INT_MIN),m(INT_MIN),c(0);
        for(int x=0;x<n;x++){
            cin>>i;
            if(i>=1)
                c++;
                if(i>=m){
                    l=m;
                    m=i;
                }
                else if(i>l&&i<m)
                    l=i;
        }
        opl(max(0,min(l-1,c-2)));
    }
    return 0;
}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <stdio.h>
#include<iterator>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const ll INF = 1ll << 64;
int q;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		vector<int>v(n);
		for (int i = 0; i < n; i++)
			scanf("%d", &v[i]);
		sort(v.rbegin(), v.rend());
		int x = v[1];
		x--;
		if (x == 0) {
			puts("0");
			return 0;
		}
		if (n - x >= x)
			printf("%d\n", x);
		else {
			while (n - x < x)
				x--;
			printf("%d\n", x);
		}
	}
	return 0;
}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <stdio.h>
#include<iterator>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const ll INF = 1ll << 64;
int q;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		vector<int>v(n);
		for (int i = 0; i < n; i++)
			scanf("%d", &v[i]);
		sort(v.rbegin(), v.rend());
		int x = v[1];
		x--;
		if (x == 0) {
			puts("0");
			return 0;
		}
		if (n - x >= x)
			printf("%d\n", x);
		else
			printf("%d\n",x - 1);
	}
	return 0;
}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <stdio.h>
#include<iterator>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const ll INF = 1ll << 64;
int q;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		vector<int>v(n);
		for (int i = 0; i < n; i++)
			scanf("%d", &v[i]);
		sort(v.rbegin(), v.rend());
		int x = v[1];
		x--;
		cout << min(x, n - 2);
	}
	return 0;
}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <stdio.h>
#include<iterator>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const ll INF = 1ll << 64;
int q;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		vector<int>v(n);
		for (int i = 0; i < n; i++)
			scanf("%d", &v[i]);
		sort(v.rbegin(), v.rend());
		int x = v[1];
		x--;
		cout << min(x, n - 2);
		}
	}
	return 0;
}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <stdio.h>
#include<iterator>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const ll INF = 1ll << 64;
int q;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		vector<int>v(n);
		for (int i = 0; i < n; i++)
			cin >> v[i];
		sort(v.rbegin(), v.rend());
		int x = v[1], z = n - 2;
		x--;
		cout << min(x, z) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    ii sz, arr[10000] = {0}, ans = 0;
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1] - 1, sz - 2);
    cout << ans << '\n';
  }
// end
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    ii sz, arr[10000] = {0}, ans = 0;
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1], sz - 2);
    cout << ans << '\n';
// end
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
  }


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q, sz, arr[10000], ans;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    ans = 0;
    memset(arr, 0, 1005);
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1] - 1, sz - 2);
    cout << ans << '\n';
  }
// end
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q, sz, arr[100005], ans;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    ans = 0;
    memset(arr, 0, 100005);
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1] - 1, sz - 2);
    cout << ans << '\n';
  }
// end
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q, sz, arr[10000], ans;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    arr = {0}, ans = 0;
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1] - 1, sz - 2);
    cout << ans << '\n';
  }
// end
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    ii sz, arr[10000] = {0}, ans = 0;
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1], sz - 2);
    cout << ans << '\n';
  }
// end
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q, sz, arr[10000], ans;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    arr[10000] = {0}, ans = 0;
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1] - 1, sz - 2);
    cout << ans << '\n';
  }
// end
#ifndef ONLINE_JUDGE
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oprecision cout.precision(30);
#define ii int
#define ss string
#define ll long long
#define cc char
#define ff float
#define dd double
#define bb bool
#define forn(sz) for (int i = 1; i <= sz; i++)
#define forni(sz, i) for (int i = 1; i <= sz; i++)
#define pb push_back
using namespace std;
ii q;
ii main() {
  speed;
  oprecision;
  /*#ifndef ONLINE_JUDGE
    freopen("outd.txt", "rt", stdin);
  #endif*/
  // code
  cin >> q;
  while (q--) {
    ii sz, arr[10000] = {0}, ans = 0;
    cin >> sz;
    forn(sz) cin >> arr[i];
    sort(arr + 1, arr + sz + 1);
    ans = min(arr[sz - 1], sz - 2);
    cout << ans << '\n';
// end
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
 ios_base :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while(t--)
  {
    int n, i, mx = -1, mx2 = 0, ans, in;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
      cin >> in;
      if(in > mx) mx2 = mx, mx = in;
    }
    ans = min(n - 2, mx2 - 1);
    cout << ans << endl;
  }
}


include <bits/stdc++.h>
using namespace std;
int main ()
{
 ios_base :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while(t--)
  {
    int n, i, mx = -1, mx2 = 0, ans, in;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
      cin >> in;
      if(in > mx) mx2 = mx, mx = in;
    }
    ans = min(n - 2, mx2 - 1);
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
 ios_base :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while(t--)
  {
    int n, i, mx = -1, mx2 = 0, ans, in;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
      cin >> in;
      if(in >= mx) mx2 = mx, mx = in;
    }
    ans = min(n - 2, mx2 - 1);
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
 ios_base :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while(t--)
  {
    int n, i, mx = -1, mx2 = 0, ans, in;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
      cin >> in;
      if(in >= mx) mx2 = mx, mx = in;
      else mx2 = max(mx2, in);
    }
    ans = min(n - 2, mx2 - 1);
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main ()
{
 ios_base :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while(t--)
  {
    int n, i, mx = -1, mx2 = 0, ans, in;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
      cin >> in;
      if(in > mx) mx = in;
      else mx2 = max(mx2, in);
    }
    ans = min(n - 2, mx2 - 1);
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
	int t;
	int n;
	int num;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i = 0; i < n; i++)
		{
			cin>>a[i];
		}
		sort(a, a+n);
		if(a[n-2] < n-1)
		{
			cout<<a[n-2]-1<<endl;
		}
		else
			cout<<n-2<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[10010];
int max1, max2;
int main()
{
	int t;
	int n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		max1 = 1;
		max2 = 1;
		for(int i = 0; i < n; i++)
		{
			cin>>t;
			if(t >= max1)
			{
				max2 = max1;
				max1 = t;
			}
		}
		if(max2>n-1)
			max2 = n-1;
		if(max2 < 1)
			max2 = 1;
		cout<<max2-1<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int max1, max2;
int main()
{
	int t;
	int n;
	int num;
	cin>>t;
	while(t--)
	{
		cin>>n;
		max1 = 1;
		max2 = 1;
		for(int i = 0; i < n; i++)
		{
			cin>>num;
			if(num >= max1)
			{
				max2 = max1;
				max1 = num;
			}
		}
		if(max2>n-1)
			max2 = n-1;
		if(max2 < 1)
			max2 = 1;
		cout<<max2-1<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[10010];
int main()
{
	int t;
	int n;
	int num;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i = 0; i < n; i++)
		{
			cin>>a[i];
		}
		sort(a, a+n);
		if(a[n-2] < n-1)
		{
			cout<<a[n-2]-1<<endl;
		}
		else
			cout<<n-2<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define itn int
#define pp pair<ll,ll>
#define fi first
#define se second
#define sc(a) scanf("%d" , &a)
#define scl(a) scanf("%lld" , &a)
#define lg 18
#define all(v)        ((v).begin()), ((v).end())
#define sz(v)       ((int)((v).size()))
#define clr(v, d)     memset(v, d, sizeof(v))
#define fo(i, j, n)     for(int i=(j);i<(int)(n);++i)
#define fod(i, j, n)     for(int i=(j);i>=(int)(n);--i)
#define foa(v)       fo(i, 0, sz(v)) fo(j, 0, sz(v[i]))
#define fov(i, v)     fo(i, 1, sz(v))
#define lp(i, cnt)      fo(i, 0, cnt)
#define lpi(i, s, cnt)    fo(i, s, cnt)
#define P(x)        cout<<#x<<" = { "<<x<<" }\n"
#define pb          push_back
#define MP          make_pair
#define fastIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int MAX = 1e9+7;
const int MIN = -1e9+7;
using namespace std;

int main() {

int t;
cin>>t;

while(t--){

    int n;
    cin>>n;

    int a[n+5];
    fo(i,0,n)
    cin>>a[i];

    sort(a,a+n);
    bool q = 0;

    int k = n-2;
    for(int i=n-1;i>=0;i--)
    if(a[i]>=k+1 && a[i-1]>=k+1)
    {
        cout<<k<<endl;
        q=1;
        break;
    }
    else
    k--;

    if(q) continue;
    else
    cout<<0<<endl;
}

	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define itn int
#define pp pair<ll,ll>
#define fi first
#define se second
#define sc(a) scanf("%d" , &a)
#define scl(a) scanf("%lld" , &a)
#define lg 18
#define all(v)        ((v).begin()), ((v).end())
#define sz(v)       ((int)((v).size()))
#define clr(v, d)     memset(v, d, sizeof(v))
#define fo(i, j, n)     for(int i=(j);i<(int)(n);++i)
#define fod(i, j, n)     for(int i=(j);i>=(int)(n);--i)
#define foa(v)       fo(i, 0, sz(v)) fo(j, 0, sz(v[i]))
#define fov(i, v)     fo(i, 1, sz(v))
#define lp(i, cnt)      fo(i, 0, cnt)
#define lpi(i, s, cnt)    fo(i, s, cnt)
#define P(x)        cout<<#x<<" = { "<<x<<" }\n"
#define pb          push_back
#define MP          make_pair
#define fastIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int MAX = 1e9+7;
const int MIN = -1e9+7;
using namespace std;

int main() {

int t;
cin>>t;

while(t--){

    int n;
    cin>>n;

    int a[n+5];
    fo(i,0,n)
    cin>>a[i];

    if(n==2){
    cout<<0<<endl;
        continue;
    }

    sort(a,a+n);
    bool q = 0;

    int k = n-2;
    for(int i=n-2;i>0;i--){
    if(a[n-1]>=k+1 && a[i]>=k+1)
    {
        cout<<k<<endl;
        q=1;
        break;
    }
    k--;
    }

    if(q) continue;
    else
    cout<<0<<endl;
}

	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define itn int
#define pp pair<ll,ll>
#define fi first
#define se second
#define sc(a) scanf("%d" , &a)
#define scl(a) scanf("%lld" , &a)
#define lg 18
#define all(v)        ((v).begin()), ((v).end())
#define sz(v)       ((int)((v).size()))
#define clr(v, d)     memset(v, d, sizeof(v))
#define fo(i, j, n)     for(int i=(j);i<(int)(n);++i)
#define fod(i, j, n)     for(int i=(j);i>=(int)(n);--i)
#define foa(v)       fo(i, 0, sz(v)) fo(j, 0, sz(v[i]))
#define fov(i, v)     fo(i, 1, sz(v))
#define lp(i, cnt)      fo(i, 0, cnt)
#define lpi(i, s, cnt)    fo(i, s, cnt)
#define P(x)        cout<<#x<<" = { "<<x<<" }\n"
#define pb          push_back
#define MP          make_pair
#define fastIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int MAX = 1e9+7;
const int MIN = -1e9+7;
using namespace std;

int main() {

int t;
cin>>t;

while(t--){

    int n;
    cin>>n;

    int a[n+5];
    fo(i,0,n)
    cin>>a[i];

    if(n==2){
    cout<<0<<endl;
        continue;
    }

    sort(a,a+n);
    bool q = 0;

    int k = n-2;
    for(int i=n-2;i>0;i--){
    if(a[n-1]>=k+1 && a[n-2]>=k+1)
    {
        cout<<k<<endl;
        q=1;
        break;
    }
    k--;
    }

    if(q) continue;
    else
    cout<<0<<endl;
}

	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define itn int
#define pp pair<ll,ll>
#define fi first
#define se second
#define sc(a) scanf("%d" , &a)
#define scl(a) scanf("%lld" , &a)
#define lg 18
#define all(v)        ((v).begin()), ((v).end())
#define sz(v)       ((int)((v).size()))
#define clr(v, d)     memset(v, d, sizeof(v))
#define fo(i, j, n)     for(int i=(j);i<(int)(n);++i)
#define fod(i, j, n)     for(int i=(j);i>=(int)(n);--i)
#define foa(v)       fo(i, 0, sz(v)) fo(j, 0, sz(v[i]))
#define fov(i, v)     fo(i, 1, sz(v))
#define lp(i, cnt)      fo(i, 0, cnt)
#define lpi(i, s, cnt)    fo(i, s, cnt)
#define P(x)        cout<<#x<<" = { "<<x<<" }\n"
#define pb          push_back
#define MP          make_pair
#define fastIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int MAX = 1e9+7;
const int MIN = -1e9+7;
using namespace std;

int main() {

int t;
cin>>t;

while(t--){

    int n;
    cin>>n;

    int a[n+5];
    fo(i,0,n)
    cin>>a[i];

    if(n==2){
    cout<<0<<endl;
        continue;
    }

    sort(a,a+n);
    bool q = 0;

    int k = n-2;
    for(int i=n-1;i>0;i--){
    if(a[i]>=k+1 && a[i-1]>=k+1)
    {
        cout<<k<<endl;
        q=1;
        break;
    }
    k--;
    }

    if(q) continue;
    else
    cout<<0<<endl;
}

	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define itn int
#define pp pair<ll,ll>
#define fi first
#define se second
#define sc(a) scanf("%d" , &a)
#define scl(a) scanf("%lld" , &a)
#define lg 18
#define all(v)        ((v).begin()), ((v).end())
#define sz(v)       ((int)((v).size()))
#define clr(v, d)     memset(v, d, sizeof(v))
#define fo(i, j, n)     for(int i=(j);i<(int)(n);++i)
#define fod(i, j, n)     for(int i=(j);i>=(int)(n);--i)
#define foa(v)       fo(i, 0, sz(v)) fo(j, 0, sz(v[i]))
#define fov(i, v)     fo(i, 1, sz(v))
#define lp(i, cnt)      fo(i, 0, cnt)
#define lpi(i, s, cnt)    fo(i, s, cnt)
#define P(x)        cout<<#x<<" = { "<<x<<" }\n"
#define pb          push_back
#define MP          make_pair
#define fastIO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int MAX = 1e9+7;
const int MIN = -1e9+7;
using namespace std;

int main() {

int t;
cin>>t;

while(t--){

    int n;
    cin>>n;

    int a[n+5];
    fo(i,0,n)
    cin>>a[i];

    sort(a,a+n);
    bool q = 0;

    int k = n-2;
    for(int i=n-1;i>=0;i++)
    if(a[i]>=k+1 && a[i-1]>=k+1)
    {
        cout<<k<<endl;
        q=1;
        break;
    }
    else
    k--;

    if(q) continue;
    else
    cout<<0<<endl;
}

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.clear();
  v.resize(n);
  fr(i,0,n)
  {
    cin>>v[i];
  }
  int ans = 0;
  sort(all(v));
  if(n>=3 and v[n-2]>1)
    {
      ans=min(n-2,v[n-2]-1);
    }
    cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  sort(all(v));
  int ans = 0;
  for(int i = 1;i<n-1;i++)
  {
    if(v[i] > i && v[i+1] > i)
    {
      ans = i;
    }
  }
  cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  sort(all(v));
  reverse(all(v));
  for(int i = n-1;i>=0;i--)
  {
    if(v[i] == 0)
    {
      v.pop_back();
    }
  }
  n = (int)v.size();
  reverse(all(v));
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  int ans = 0;
  for(int i = 1;i<n-1;i++)
  {
    if(v[i] > i && v[i+1] > i)
    {
      ans = i;
    }
  }
  cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  v.clear();
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  sort(all(v));
  int ans = 0;
  for(int i = 1;i<n-1;i++)
  {
    if(v[i] > i && v[i+1] > i)
    {
      ans = i;
    }
  }
  cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  int ans = 0;
  sort(all(v));
  if(n>=3 and v[n-2]>1)
    {
      ans=min(n-2,v[n-2]-1);
    }
    cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  sort(all(v));
  int ans = 0;
  int no  = 0;
  if(v[0] >= 1)
  {
    no = 1;
  }
  for(int i = 1;i<n-1;i++)
  {
    if(v[i] > no && v[i+1] > no && no > 0)
    {
      ans = no;
    }
    if(v[i] > 0)
    {
      no++;
    }
  }
  cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  sort(all(v));
  int ans = 0;
  for(int i = n-1;i>1;i--)
  {
    if(v[i] > i-1 && v[i-1] > i-1)
    {
      ans = i-1;
      break;
    }
  }
  cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define len length
#define mp make_pair
#define pq priority_queue
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define forn(i,n) for (int i = 0; i < int(n); ++i)
#define take(v) for(int i=0;i<(int)v.size();i++){cin>>v[i];}
#define printvec(v) for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define printds(x) while(!x.empty()){cout<<x.top()<<" ";x.pop();}
#define input(n) int n;cin>>n;
#define PI 3.141592653589793238463
#define lnput(n) long long int n;cin>>n;
#define fr(i,s,e) for(int i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
vector<int> v;
void solve()
{
  input(n);
  v.resize(n);
  if(n < 3)
  {
    cout<<0<<endl;
    return;
  }
  fr(i,0,n)
  {
    cin>>v[i];
  }
  sort(all(v));
  int ans = 0;
  int no  = 0;
  if(v[0] >= 1)
  {
    no = 1;
  }
  for(int i = 1;i<n-1;i++)
  {
    if(v[i] > no && v[i+1] > no)
    {
      ans = no;
    }
    if(v[i] > 0)
    {
      no++;
    }
  }
  cout<<ans<<endl;
  return;
}
int main()
{
  speed;
  int t ;
  cin>>t;
  while(t--)
  {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;

	while(t--)
	{
		int n;
		cin>>n;

		int arr[n];
		int i;
		for(i=0;i<n;i++)
		{
			cin>>arr[i];
		}

		sort(arr,arr+n);
		int x,y;

		x=arr[n-1];
		y=arr[n-2];

		cout<<min(min(x-1,y-1),n-2)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;

	while(t--)
	{
		int n;
		cin>>n;

		int arr[n];
		int i;
		for(i=0;i<n;i++)
		{
			cin>>arr[i];
		}

		sort(arr,arr+n);

		int a,b;

		a=arr[n-1];
		b=arr[n-2];

		i=n-3;
		int k=0;
		while(i>=0)
		{
			if(arr[i]< a && arr[i] < b)
			{
				k++;
			}

			i--;
		}

		cout<<min(min(a-1,b-1)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;

	while(t--)
	{
		int n;
		cin>>n;

		int arr[n];
		int i;
		for(i=0;i<n;i++)
		{
			cin>>arr[i];
		}

		sort(arr,arr+n);

		int a,b;

		a=arr[n-1];
		b=arr[n-2];

		i=n-3;
		int k=0;
		while(i>=0)
		{
			if(arr[i]< a && arr[i] < b)
			{
				k++;
			}

			i--;
		}

		cout<<min(min(a-1,b-1),k)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;

	while(t--)
	{
		int n;
		cin>>n;

		int arr[n];
		int i;
		for(i=0;i<n;i++)
		{
			cin>>arr[i];
		}

		sort(arr,arr+n);

		int a,b;

		a=arr[n-1];
		b=arr[n-2];

		i=n-3;
		int k=0;
		while(i>=0)
		{
			if(arr[i]< a && arr[i] < b)
			{
				k++;
			}

			i--;
		}

		cout<<k<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;

	while(t--)
	{
		int n;
		cin>>n;

		int arr[n];
		int i;
		for(i=0;i<n;i++)
		{
			cin>>arr[i];
		}

		i=n-3;
		int k=0;
		while(i>=0)
		{
			if(arr[i]< a && arr[i] < b)
			{
				k++;
			}

			i--;
		}

		cout<<min(min(a-1,b-1),k)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;

	while(t--)
	{
		int n;
		cin>>n;

		int arr[n];
		int i;
		for(i=0;i<n;i++)
		{
			cin>>arr[i];
		}

		sort(arr,arr+n);

		int a,b;

		a=arr[n-1];
		b=arr[n-2];

		i=n-3;
		int k=0;
		while(i>=0)
		{
			if(arr[i]< a && arr[i] < b)
			{
				k++;
			}

			i--;
		}

		cout<<min(min(a,b),k)<<endl;
	}
}


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

int main() {

	ll t,i,n,max;;
	cin>>t;
	while(t--)
	{

        	 cin>>n;
        	 ll a[n],x[100001]={0},max=0,high=0,MAX=0,j;
        	 for(i=0;i<n;i++)
            {  cin>>a[i];
        	   x[a[i]]++;
        	   if(high<a[i])
        	   high=a[i];
            }

        	sort(a,a+n);
        	 ll sum=0,temp;
        	   for(i=0;i<n-1;i++)
        	   {
        	       sum++;
        	       x[a[i]]=0;
        	       temp=0;
        	       for(j=sum+1;j<=high;j++)
        	       {
        	          temp+=x[j];
        	          if(temp>=2)
        	          { max=sum;
        	            break;
        	          }
        	       }
        	       if(MAX<max)
        	       MAX=max;
        	   }

           cout<<MAX<<endl;

	}
	return 0;
}


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

int main() {

	ll t,i,n,max;;
	cin>>t;
	while(t--)
	{

        	 cin>>n;
        	 ll a[n],x[100001]={0},max=0,high=0,MAX=0,j;
        	 for(i=0;i<n;i++)
        	 {
        	     cin>>a[i];
        	    x[a[i]]++;
        	    if(high<a[i])
        	    high=a[i];
        	 }

        	   for(i=1;i<=high-1;i++)
        	   {
        	       ll sum=0;
        	       for(j=x[i]+1;j<=high;j++)
        	       {
        	           if(x[j]>=1)
        	         {  sum+=x[j];
        	            if(sum>=2)
        	           { max=x[i];
        	             break;
        	           }
        	         }
                   }

                   if(MAX<max)
                   MAX=max;
        	    }
           cout<<MAX<<endl;

	}
	return 0;
}


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

int main() {

	ll t,i,n,max;;
	cin>>t;
	while(t--)
	{

        	 cin>>n;
        	 ll a[n],x[100001]={0},max,high=0;
        	 for(i=0;i<n;i++)
        	 {
        	     cin>>a[i];
        	    x[a[i]]++;
        	    if(high<a[i])
        	    high=a[i];
        	 }
        	   ll maxbeg[high+1],maxend[high+1];
        	   max=x[1];
        	   maxbeg[1]=x[1];
        	 for(i=2;i<=high;i++)
        	 {
        	     if(x[i]>max)
        	     max=x[i];

        	     maxbeg[i]=max;
        	 }
        	 max=x[high];
        	 maxbeg[high]=max;
        	 for(i=high-1;i>=1;i--)
        	 {
        	     if(x[i]>max)
        	     max=x[i];

        	     maxend[i]=max;
        	 }
        	 if(maxbeg[2]>=2)
        	 max=maxbeg[1];
        	 else
        	 max=0;
        	 //cout<<max<<" ";
        	 for(i=2;i<=high-1;i++)
        	 {
        	     if(maxbeg[i]>max && maxend[i+1]>=2)
        	      max=maxbeg[i];
        	   //   cout<<maxbeg[i]<<" ";
        	 }
        	 //cout<<endl;
        	 cout<<max<<endl;
   }
	return 0;
}


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

int main() {

	ll t,i,n,max;;
	cin>>t;
	while(t--)
	{

        	 cin>>n;
        	 ll a[n],x[100001]={0},max=0,high=0,MAX=0,j;
        	 for(i=0;i<n;i++)
            {  cin>>a[i];
        	   x[a[i]]++;
        	   if(high<a[i])
        	   high=a[i];
            }

        	sort(a,a+n);
        	 ll sum=0,temp;
        	   for(i=0;i<n-1;i++)
        	   {
        	       sum++;
        	       x[a[i]]--;
        	       temp=0;
        	       for(j=sum+1;j<=high;j++)
        	       {
        	          temp+=x[j];
        	          if(temp>=2)
        	          { max=sum;
        	            break;
        	          }
        	       }
        	       if(MAX<max)
        	       MAX=max;
        	   }

           cout<<MAX<<endl;

	}
	return 0;
}


import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.util.*;

public class Main {
	//////
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	//////

	public static void main(String[] args) throws IOException {

		FastReader scan = new FastReader();

		int N=scan.nextInt();
		int[] A=new int[N];
		int[] B=new int[N];
		for(int i=0;i<N;i++){
			A[i]=scan.nextInt();
		}
		for(int i=0;i<N;i++){
			B[i]=scan.nextInt();
		}
		drink[] O=new drink[N];
		for(int i=0;i<N;i++){
			O[i]=new drink(A[i],B[i]);
		}

		Arrays.sort(O,new Comparator<drink>(){

			public int compare(drink O1,drink O2){
				return O1.b-O2.a;
			}
		});

		int sum=0;
		for(int i=0;i<N-2;i++){
			sum+=O[i].a;
		}

		if(sum<=O[N-1].b+O[N-2].b-O[N-1].a-O[N-2].a){
			System.out.println("YES");
		}else{
			System.out.println("NO");
		}

	}

}

class drink{

	int a;
	int b;

	drink(int a,int b){
		this.a=a;
		this.b=b;
	}
}


import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.util.*;

public class Main {
	//////
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	//////

	public static void main(String[] args) throws IOException {

		FastReader scan = new FastReader();

		int N=scan.nextInt();
		int[] A=new int[N];
		int[] B=new int[N];
		for(int i=0;i<N;i++){
			A[i]=scan.nextInt();
		}
		for(int i=0;i<N;i++){
			B[i]=scan.nextInt();
		}
		Drink[] O=new Drink[N];
		for(int i=0;i<N;i++){
			O[i]=new Drink(A[i],B[i]);
		}

		Arrays.sort(O,new Comparator<Drink>(){

			public int compare(Drink O1,Drink O2){
				return O1.b-O2.a;
			}

		});

		long sum=0;
		for(int i=0;i<N-2;i++){
			sum+=O[i].a;
		}

		if(sum<=O[N-1].b+O[N-2].b-O[N-1].a-O[N-2].a){
			System.out.println("YES");
		}else{
			System.out.println("NO");
		}

	}

}

class Drink{

	int a;
	int b;

	Drink(int a,int b){
		this.a=a;
		this.b=b;
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        inclass in = new inclass(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AGreed solver = new AGreed();
        solver.solve(1, in, out);
        out.close();
    }

    static class AGreed {
        public void solve(int testNumber, inclass in, PrintWriter out) {
            int n = in.nextInt();
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            Integer arr[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            Arrays.sort(arr);
            if (arr[n - 1] + arr[n - 2] >= sum) {
                out.println("YES");
            } else {
                out.println("NO");
            }
        }

    }

    static class inclass {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private inclass.SpaceCharFilter filter;

        public inclass(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        inclass in = new inclass(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AGreed solver = new AGreed();
        solver.solve(1, in, out);
        out.close();
    }

    static class AGreed {
        public void solve(int testNumber, inclass in, PrintWriter out) {
            int n = in.nextInt();
            long sum = 0;
            for (int i = 0; i < n; i++) {
                sum += (long) in.nextInt();
            }
            Long arr[] = new Long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = (long) in.nextInt();
            }
            Arrays.sort(arr);
            if (arr[n - 1] + arr[n - 2] >= sum) {
                out.println("YES");
            } else {
                out.println("NO");
            }
        }

    }

    static class inclass {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private inclass.SpaceCharFilter filter;

        public inclass(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        inclass in = new inclass(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AGreed solver = new AGreed();
        solver.solve(1, in, out);
        out.close();
    }

    static class AGreed {
        public void solve(int testNumber, inclass in, PrintWriter out) {
            int n = in.nextInt();
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            Integer arr[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            Arrays.sort(arr);
            if (arr[n - 1] == 0 || sum == 0) {
                out.println("NO");
                return;
            }
            if (arr[n - 1] + arr[n - 2] >= sum) {
                out.println("YES");
            } else {
                out.println("NO");
            }
        }

    }

    static class inclass {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private inclass.SpaceCharFilter filter;

        public inclass(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        inclass in = new inclass(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AGreed solver = new AGreed();
        solver.solve(1, in, out);
        out.close();
    }

    static class AGreed {
        public void solve(int testNumber, inclass in, PrintWriter out) {
            int n = in.nextInt();
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            Integer arr[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            Arrays.sort(arr);
            if (arr[n - 1] >= sum && arr[n - 2] != 0) {
                out.println("YES");
                return;
            } else if (arr[n - 1] >= sum && arr[n - 2] == 0) {
                out.println("NO");
                return;
            }
            if (arr[n - 1] + arr[n - 2] >= sum) {
                out.println("YES");
            } else {
                out.println("NO");
            }
        }

    }

    static class inclass {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private inclass.SpaceCharFilter filter;

        public inclass(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.util.Arrays;
import java.util.Scanner;

public class falling {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] vol = new long[n];
		long[] cap = new long[n];
		for(int i = 0; i < n; i++)
			vol[i] = scan.nextLong();
		for(int i = 0; i < n; i++)
			cap[i] = scan.nextLong();
		Arrays.sort(cap);
		long max = cap[cap.length - 2] + cap[cap.length - 1];
		long tot = 0;
		for(long i : vol)
			tot += i;
		System.out.println(tot <= max ? "yes" : "no");
	}
}


import java.util.Scanner;
import java.util.Arrays;
public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] volume = new int[n];
int[] cap = new int[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextInt();
for(int i = 0; i < n; i++)
cap[i] = scan.nextInt();
Arrays.sort(cap);
int max = cap[cap.length - 2] + cap[cap.length - 1];
int total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;

public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] volume = new int[n];
int[] cap = new int[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextInt();
for(int i = 0; i < n; i++)
cap[i] = scan.nextInt();
Arrays.sort(volume);
int max = volume[volume.length - 2] + volume[volume.length - 1];
int total = 0;
for(int i : cap)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;
public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
long[] volume = new long[n];
long[] cap = new long[n];
for(long i = 0; i < n; i++)
volume[i] = scan.nextLong();
for(long i = 0; i < n; i++)
cap[i] = scan.nextLong();
Arrays.sort(cap);
long max = cap[cap.length - 2] + cap[cap.length - 1];
long total = 0;
for(long i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;

public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] volume = new int[n];
int[] cap = new int[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextInt();
for(int i = 0; i < n; i++)
cap[i] = scan.nextInt();
Arrays.sort(volume);
int max = volume[volume.length - 2] + volume[volume.length - 1];
int total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;
public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
long[] volume = new long[n];
long[] cap = new long[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextInt();
for(int i = 0; i < n; i++)
cap[i] = scan.nextInt();
Arrays.sort(cap);
long max = cap[cap.length - 2] + cap[cap.length - 1];
long total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;
public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
long[] volume = new long[n];
int[] cap = new int[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextInt();
for(int i = 0; i < n; i++)
cap[i] = scan.nextInt();
Arrays.sort(cap);
int max = cap[cap.length - 2] + cap[cap.length - 1];
int total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;

public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] volume = new int[n];
int[] cap = new int[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextInt();
for(int i = 0; i < n; i++)
cap[i] = scan.nextInt();
Arrays.sort(volume);
int max = cap[cap.length - 2] + cap[cap.length - 1];
int total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;
public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long[] volume = new long[n];
long[] cap = new long[n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextLong();
for(int i = 0; i < n; i++)
cap[i] = scan.nextLong();
Arrays.sort(cap);
long max = cap[cap.length - 2] + cap[cap.length - 1];
long total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;
import java.util.Arrays;
public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextInt();
long[] volume = new long[(int) n];
long[] cap = new long[(int) n];
for(int i = 0; i < n; i++)
volume[i] = scan.nextLong();
for(int i = 0; i < n; i++)
cap[i] = scan.nextLong();
Arrays.sort(cap);
long max = cap[cap.length - 2] + cap[cap.length - 1];
long total = 0;
for(int i : volume)
total += i;
if(total <= max)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Dell
 */
public class Cf {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[] a = new int[n];
        int[] b = new int[n];

        int sum=0;
        for(int i = 0; i < n; i++)
        {
            a[i] = sc.nextInt();
            sum += a[i];
        }

        for(int i = 0; i < n; i++)
        {
            b[i] = sc.nextInt();
        }

        Arrays.sort(b);

        int max1 = b[n-1];
        int max2 = b[n-2];

        int cap = max1 + max2;
        String msg = "";
        if(cap >= sum)
        {
            msg = "YES";
        }
        else
        {
            msg = "NO";
        }

        System.out.println(msg);

    }

}


public class Cf {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[] a = new int[n];
        int[] b = new int[n];

        int sum=0;
        for(int i = 0; i < n; i++)
        {
            a[i] = sc.nextInt();
            sum += a[i];
        }

        for(int i = 0; i < n; i++)
        {
            b[i] = sc.nextInt();
        }

        Arrays.sort(b);

        int max1 = b[n-1];
        int max2 = b[n-2];

        int cap = max1 + max2;
        String msg = "";
        if(cap >= sum)
        {
            msg = "YES";
        }
        else
        {
            msg = "NO";
        }

        System.out.println(msg);

    }

}


import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Dell
 */
public class Cf {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[] a = new int[n];
        int[] b = new int[n];

        long sum=0;
        for(int i = 0; i < n; i++)
        {
            a[i] = sc.nextInt();
            sum += a[i];
        }

        for(int i = 0; i < n; i++)
        {
            b[i] = sc.nextInt();
        }

        Arrays.sort(b);

        int max1 = b[n-1];
        int max2 = b[n-2];

        int cap = max1 + max2;

        String msg = "";
        if(cap >= sum)
        {
            msg = "YES";
        }
        else
        {
            msg = "NO";
        }

        System.out.println(msg);

    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.lang.management.MemoryType;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Function;

public class Main {
    static final int INF = Integer.MAX_VALUE;

    static int mergeSort(int[] a,int [] c, int begin, int end)
    {
        int inversion=0;
        if(begin < end)
        {
            inversion=0;
            int mid = (begin + end) >>1;
            inversion+= mergeSort(a,c, begin, mid);
            inversion+=mergeSort(a, c,mid + 1, end);
            inversion+=merge(a,c, begin, mid, end);
        }
        return inversion;
    }

    static int  merge(int[] a,int[]c, int b, int mid, int e)
    {
        int n1 = mid - b + 1;
        int n2 = e - mid;
        int[] L = new int[n1+1], R = new int[n2+1];
        int[] L1 = new int[n1+1], R1 = new int[n2+1];

        //inversion
        int inversion=0;

        for(int i = 0; i < n1; i++) {
            L[i] = a[b + i];
            L1[i] = c[b + i];
            //  L2[i] = w[b + i];
        }
        for(int i = 0; i < n2; i++)  {
            R[i] = a[mid + 1 + i];
            R1[i] = c[mid + 1 + i];
            //R2[i] = w[mid + 1 + i];
        }
        L[n1] = R[n2] = INF;

        for(int k = b, i = 0, j = 0; k <= e; k++)
            if(L[i] <= R[j]){
                a[k] = L[i];
                c[k] = L1[i];
                ++i;
                //  w[k]=L2[i++];
            }

            else
            {
                a[k] = R[j];
                c[k] = R1[j];
                //w[k] = R2[j++];
                ++j;

                inversion=inversion+(n1-i);

            }
        return inversion;

    }

    static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    static long a,b,c,k;
    static int bs(Long []a,long e,int i){
        int  r=i,l=0,mid=0,ans=-1;
        while (r>=l){
            mid=(r+l)>>1;
            if (a[mid]>e){
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        return ans;
    }

    public int peakIndexInMountainArray(int[] A) {
        int ans=-1;
        for (int i = 0; i < A.length-1; i++) {
            if (A[i]>A[i+1]){
                ans=i;
            }
        }
        return ans;
    }

    public static void main (String[]args) throws IOException {
        Scanner in = new Scanner(System.in);
        try(PrintWriter or = new PrintWriter(System.out)) {

         int n = in.nextInt();
         int [][]a= new int[n][];
            for (int i = 0; i < n; i++) {
                int x=in.nextInt();
                int y=in.nextInt();
                a[i]=new int[]{x,y,y-x};
            }
            Arrays.sort(a,(x,y)->{return y[2]-x[2];});
            long c=a[0][1]+a[1][1];
            long s=0;
            for (int i = 0; i < n; i++) {
                s+=(long)a[i][0];
            }
            or.println(c>=s?"YES":"NO");

        }
    }

    static int getlen(int r,int l,int a){
        return (r-l+1+1)/(a+1);
    }

    static class Scanner {

        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++) {
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec) {
                        f *= 10;
                    }
                }
            }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}

class Pair implements Comparable<Pair> {
    int first, second;

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public int compareTo(Pair o) {
        return first - o.first;
    }

}

class Tempo {

    int num;
    int index;

    public Tempo(int num, int index) {
        this.num = num;
        this.index = index;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.lang.management.MemoryType;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Function;

public class Main {
    static final int INF = Integer.MAX_VALUE;

    static int mergeSort(int[] a,int [] c, int begin, int end)
    {
        int inversion=0;
        if(begin < end)
        {
            inversion=0;
            int mid = (begin + end) >>1;
            inversion+= mergeSort(a,c, begin, mid);
            inversion+=mergeSort(a, c,mid + 1, end);
            inversion+=merge(a,c, begin, mid, end);
        }
        return inversion;
    }

    static int  merge(int[] a,int[]c, int b, int mid, int e)
    {
        int n1 = mid - b + 1;
        int n2 = e - mid;
        int[] L = new int[n1+1], R = new int[n2+1];
        int[] L1 = new int[n1+1], R1 = new int[n2+1];

        //inversion
        int inversion=0;

        for(int i = 0; i < n1; i++) {
            L[i] = a[b + i];
            L1[i] = c[b + i];
            //  L2[i] = w[b + i];
        }
        for(int i = 0; i < n2; i++)  {
            R[i] = a[mid + 1 + i];
            R1[i] = c[mid + 1 + i];
            //R2[i] = w[mid + 1 + i];
        }
        L[n1] = R[n2] = INF;

        for(int k = b, i = 0, j = 0; k <= e; k++)
            if(L[i] <= R[j]){
                a[k] = L[i];
                c[k] = L1[i];
                ++i;
                //  w[k]=L2[i++];
            }

            else
            {
                a[k] = R[j];
                c[k] = R1[j];
                //w[k] = R2[j++];
                ++j;

                inversion=inversion+(n1-i);

            }
        return inversion;

    }

    static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    static long a,b,c,k;
    static int bs(Long []a,long e,int i){
        int  r=i,l=0,mid=0,ans=-1;
        while (r>=l){
            mid=(r+l)>>1;
            if (a[mid]>e){
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        return ans;
    }

    public int peakIndexInMountainArray(int[] A) {
        int ans=-1;
        for (int i = 0; i < A.length-1; i++) {
            if (A[i]>A[i+1]){
                ans=i;
            }
        }
        return ans;
    }

    public static void main (String[]args) throws IOException {
        Scanner in = new Scanner(System.in);
        try(PrintWriter or = new PrintWriter(System.out)) {

         int n = in.nextInt();
         int [][]a= new int[n][];
         int [] f= new  int[n];
            for (int i = 0; i < n; i++) {
                f[i]=in.nextInt();
            }
            for (int i = 0; i < n; i++) {
                int x=in.nextInt();
                a[i]=new int[]{f[i],x,x-f[i]};
            }
            Arrays.sort(a,(x,y)->{return y[1]-x[1];});
            long c=a[0][1]+a[1][1];
           // for (int [] r:a)or.println(r[0]+" "+r[1]+" "+r[2]);
            long s=0;
            for (int i = 0; i < n; i++) {
                s+=(long)a[i][0];
            }
            or.println(c>=s?"YES":"NO");

        }
    }

    static int getlen(int r,int l,int a){
        return (r-l+1+1)/(a+1);
    }

    static class Scanner {

        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++) {
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec) {
                        f *= 10;
                    }
                }
            }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}

class Pair implements Comparable<Pair> {
    int first, second;

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public int compareTo(Pair o) {
        return first - o.first;
    }

}

class Tempo {

    int num;
    int index;

    public Tempo(int num, int index) {
        this.num = num;
        this.index = index;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.lang.management.MemoryType;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Function;

public class Main {
    static final int INF = Integer.MAX_VALUE;

    static int mergeSort(int[] a,int [] c, int begin, int end)
    {
        int inversion=0;
        if(begin < end)
        {
            inversion=0;
            int mid = (begin + end) >>1;
            inversion+= mergeSort(a,c, begin, mid);
            inversion+=mergeSort(a, c,mid + 1, end);
            inversion+=merge(a,c, begin, mid, end);
        }
        return inversion;
    }

    static int  merge(int[] a,int[]c, int b, int mid, int e)
    {
        int n1 = mid - b + 1;
        int n2 = e - mid;
        int[] L = new int[n1+1], R = new int[n2+1];
        int[] L1 = new int[n1+1], R1 = new int[n2+1];

        //inversion
        int inversion=0;

        for(int i = 0; i < n1; i++) {
            L[i] = a[b + i];
            L1[i] = c[b + i];
            //  L2[i] = w[b + i];
        }
        for(int i = 0; i < n2; i++)  {
            R[i] = a[mid + 1 + i];
            R1[i] = c[mid + 1 + i];
            //R2[i] = w[mid + 1 + i];
        }
        L[n1] = R[n2] = INF;

        for(int k = b, i = 0, j = 0; k <= e; k++)
            if(L[i] <= R[j]){
                a[k] = L[i];
                c[k] = L1[i];
                ++i;
                //  w[k]=L2[i++];
            }

            else
            {
                a[k] = R[j];
                c[k] = R1[j];
                //w[k] = R2[j++];
                ++j;

                inversion=inversion+(n1-i);

            }
        return inversion;

    }

    static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    static long a,b,c,k;
    static int bs(Long []a,long e,int i){
        int  r=i,l=0,mid=0,ans=-1;
        while (r>=l){
            mid=(r+l)>>1;
            if (a[mid]>e){
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        return ans;
    }

    public int peakIndexInMountainArray(int[] A) {
        int ans=-1;
        for (int i = 0; i < A.length-1; i++) {
            if (A[i]>A[i+1]){
                ans=i;
            }
        }
        return ans;
    }

    public static void main (String[]args) throws IOException {
        Scanner in = new Scanner(System.in);
        try(PrintWriter or = new PrintWriter(System.out)) {

         int n = in.nextInt();
         int [][]a= new int[n][];
         int [] f= new  int[n];
            for (int i = 0; i < n; i++) {
                f[i]=in.nextInt();
            }
            for (int i = 0; i < n; i++) {
                int x=in.nextInt();
                a[i]=new int[]{f[i],x,x-f[i]};
            }
            Arrays.sort(a,(x,y)->{return y[2]-x[2];});
            long c=a[0][1]+a[1][1];
           // for (int [] r:a)or.println(r[0]+" "+r[1]+" "+r[2]);
            long s=0;
            for (int i = 0; i < n; i++) {
                s+=(long)a[i][0];
            }
            or.println(c>=s?"YES":"NO");

        }
    }

    static int getlen(int r,int l,int a){
        return (r-l+1+1)/(a+1);
    }

    static class Scanner {

        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++) {
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec) {
                        f *= 10;
                    }
                }
            }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}

class Pair implements Comparable<Pair> {
    int first, second;

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public int compareTo(Pair o) {
        return first - o.first;
    }

}

class Tempo {

    int num;
    int index;

    public Tempo(int num, int index) {
        this.num = num;
        this.index = index;
    }
}


//package com.company;
import java.lang.String;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		long cnt=0;
		for(int i=0;i<n;i++){
			cnt+=in.nextInt();
		}
		for(int i=0;i<n;i++){
			a[i]=in.nextInt();
		}

		Arrays.sort(a);
		if(a[n-1]+a[n-2]>=cnt){
			System.out.println("YES");
		}
		else{
			System.out.println("NO");
		}
    }
}


//package com.company;
import java.lang.String;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.*;
import java.util.*;

public class A882 {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[] a=new int[n];
        long cnt=0;
        for(int i=0;i<n;i++){
            cnt+=in.nextInt();
        }
        for(int i=0;i<n;i++){
            a[i]=in.nextInt();
        }

        Arrays.sort(a);
        if(a[n-1]+a[n-2]>=cnt){
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }
    }
}


package com.company;
import java.lang.String;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		long cnt=0;
		for(int i=0;i<n;i++){
			long x=in.nextInt();
			cnt+=x;
		}
		for(int i=0;i<n;i++){
			a[i]=in.nextInt();
		}

		Arrays.sort(a);
		if(a[n-1]+a[n-2]>=cnt){
			System.out.println("YES");
		}
		else{
			System.out.println("NO");
		}

    }
}


package com.company;
import java.lang.String;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) {

		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		long cnt=0;
		for(int i=0;i<n;i++){
			long x=in.nextInt();
			cnt+=x;
		}
		for(int i=0;i<n;i++){
			a[i]=in.nextInt();
		}

		Arrays.sort(a);
		if(a[n-1]+a[n-2]>=cnt){
			System.out.println("YES");
		}
		else{
			System.out.println("NO");
		}

    }
}


package com.company;
import java.lang.String;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.*;
import java.util.*;

public class A882 {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[] a=new int[n];
        long cnt=0;
        for(int i=0;i<n;i++){
            cnt+=in.nextInt();
        }
        for(int i=0;i<n;i++){
            a[i]=in.nextInt();
        }

        Arrays.sort(a);
        if(a[n-1]+a[n-2]>=cnt){
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }
    }
}


package com.company;
import java.lang.String;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {

		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		long cnt=0;
		for(int i=0;i<n;i++){
			long x=in.nextInt();
			cnt+=x;
		}
		for(int i=0;i<n;i++){
			a[i]=in.nextInt();
		}

		Arrays.sort(a);
		if(a[n-1]+a[n-2]>=cnt){
			System.out.println("YES");
		}
		else{
			System.out.println("NO");
		}

    }
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Problem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] remainings = new int[n];
        List<Integer> capacities = new ArrayList<>();
        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            remainings[i] = scanner.nextInt();
        }

        long total = Arrays.stream(remainings).sum();

        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            capacities.add(scanner.nextInt());
        }
        Integer max1 = capacities.stream().max(Integer::compareTo).get();
        capacities.remove(max1);
        Integer max2 = capacities.stream().max(Integer::compareTo).get();

        System.out.println((max1 + max2 - total) >= 0 ? "YES" : "NO");
    }

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Problem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] remainings = new int[n];
        List<Integer> capacities = new ArrayList<>();
        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            remainings[i] = scanner.nextInt();
        }

        double total = Arrays.stream(remainings).sum();

        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            capacities.add(scanner.nextInt());
        }
        Integer max1 = capacities.stream().max(Integer::compareTo).get();
        capacities.remove(max1);
        Integer max2 = capacities.stream().max(Integer::compareTo).get();

        System.out.println((max1 + max2 - total) >= 0 ? "YES" : "NO");
    }

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Problem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long[] remainings = new long[n];
        List<Long> capacities = new ArrayList<>();
        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            remainings[i] = scanner.nextLong();
        }

        long total = Arrays.stream(remainings).sum();

        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            capacities.add(scanner.nextLong());
        }
        Long max1 = capacities.stream().max(Long::compareTo).get();
        capacities.remove(max1);
        Long max2 = capacities.stream().max(Long::compareTo).get();
        System.out.println((max1 + max2 - total) >= 0 ? "YES" : "NO");
    }
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Problem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] remainings = new int[n];
        List<Integer> capacities = new ArrayList<>();
        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            remainings[i] = scanner.nextInt();
        }

        int total = Arrays.stream(remainings).sum();

        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            capacities.add(scanner.nextInt());
        }
        Integer max1 = capacities.stream().max(Integer::compareTo).get();
        capacities.remove(max1);
        Integer max2 = capacities.stream().max(Integer::compareTo).get();

        System.out.println((max1 + max2 - total) >= 0 ? "YES" : "NO");
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class Problem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] remainings = new int[n];
        int[] capacities = new int[n];
        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            remainings[i] = scanner.nextInt();
        }

        int total = Arrays.stream(remainings).sum();

        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            capacities[i] = scanner.nextInt();
        }
        int max1 = Arrays.stream(capacities).max().getAsInt();
        int max2 = Arrays.stream(capacities).filter(e -> e != max1).max().getAsInt();

        System.out.println((max1 + max2 - total) >=0 ? "YES" : "NO");
    }

}


import java.util.Scanner;

public class Greed {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),i;
        long sum_remain=0;
        long tab_cap[]=new long[n];
        for(i=0; i<n; i++){
            sum_remain+=sc.nextInt();
        }
        for(i=0; i<n; i++){
            tab_cap[i]=sc.nextInt();
        }
        long max1=tab_cap[0];
        long max2=tab_cap[1];
        for(i=2; i<n; i++){
            if(tab_cap[i]>max1){
                max2=max1;
                max1=tab_cap[i];

            }else if(tab_cap[i]>max2){
                max2=tab_cap[i];
            }
        }

        if(sum_remain<=(max1+max2)){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }
}


import java.util.Scanner;

public class Greed {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum_remain=0, i;
        int tab_cap[]=new int[n];
        for(i=0; i<n; i++){
            sum_remain+=sc.nextInt();
        }
        for(i=0; i<n; i++){
            tab_cap[i]=sc.nextInt();
        }
        int max1=tab_cap[0];
        int max2=tab_cap[1];
        for(i=2; i<n; i++){
            if(tab_cap[i]>=max1){
                max2=max1;
                max1=tab_cap[i];

            }else if(tab_cap[i]>max2){
                max2=tab_cap[i];
            }
        }

        if(sum_remain<=(max1+max2)){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }
}


import java.util.Scanner;

public class Greed {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum_remain=0, i;
        int tab_cap[]=new int[n];
        for(i=0; i<n; i++){
            sum_remain+=sc.nextInt();
        }
        for(i=0; i<n; i++){
            tab_cap[i]=sc.nextInt();
        }
        int max1=tab_cap[0];
        int max2=tab_cap[1];
        for(i=2; i<n; i++){
            if(tab_cap[i]>max1){
                max2=max1;
                max1=tab_cap[i];

            }else if(tab_cap[i]>max2){
                max2=tab_cap[i];
            }
        }

        if(sum_remain<=(max1+max2)){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }
}


import java.util.Scanner;

public class Greed {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum_remain=0, i;
        int tab_cap[]=new int[n];
        for(i=0; i<n; i++){
            sum_remain+=sc.nextInt();
        }
        for(i=0; i<n; i++){
            tab_cap[i]=sc.nextInt();
        }
        int max1=tab_cap[0];
        int max2=tab_cap[1];
        for(i=2; i<n; i++){
            if(tab_cap[i]>max1){
                max1=tab_cap[i];
            }else if(tab_cap[i]>max2){
                max2=tab_cap[i];
            }
        }

        if(sum_remain<=(max1+max2)){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }
}


import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		long g1 = 0, g2 = 0, remaining = 0;
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		for (int i = 0; i < n; i++) {
			int a = input.nextInt();
			remaining += a;
		}
		for (int i = 0; i < n; i++) {
			int b = input.nextInt();
			if (b > g1) {
				g2 = g1;
				g1 = b;
			} else if (b > g2) {
				g2 = b;
			}
		}
		if (g1 + g2 >= remaining)
			System.out.println("Yes");
		else
			System.out.println("No");
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		int g1 = 0, g2 = 0, remaining = 0;
		;
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		for (int i = 0; i < n; i++) {
			int a = input.nextInt();
			remaining += a;
		}
		for (int i = 0; i < n; i++) {
			int b = input.nextInt();
			if (b > g1) {
				g2 = g1;
				g1 = b;
			} else if (b > g2) {
				g2 = b;
			}
		}
		if (g1 + g2 >= remaining)
			System.out.println("Yes");
		else
			System.out.println("No");
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		int g1 = 0, g2 = 0, remaining = 0;
		;
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		for (int i = 0; i < n; i++) {
			int a = input.nextInt();
			remaining += a;
		}
		for (int i = 0; i < n; i++) {
			int b = input.nextInt();
			if (b > g1) {
				g2 = g1;
				g1 = b;
			} else if (b > g2 && g1 > b) {
				g2 = b;
			}
		}
		if (g1 + g2 >= remaining)
			System.out.println("Yes");
		else
			System.out.println("No");
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		int g1 = 0, g2 = 0, remaining = 0;
		;
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		for (int i = 0; i < n; i++) {
			int a = input.nextInt();
			remaining += a;
		}
		for (int i = 0; i < n; i++) {
			int b = input.nextInt();
			if (b > g1) {
				g2 = g1;
				g1 = b;
			} else if (b > g2 && g1 >= b) {
				g2 = b;
			}
		}
		if (g1 + g2 >= remaining)
			System.out.println("Yes");
		else
			System.out.println("No");
	}
}


import java.util.*;
import java.io.*;

public class Main
{
   static  class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(InputStream f) {
            try {
                br = new BufferedReader(new InputStreamReader(f));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

    }
    public static void main(String args[])
    {
         InputStream in=System.in;
       //  OutputStream out=System.out;
        FastScanner fs=new FastScanner(in);
        PrintWriter out=new PrintWriter(System.out);

         int n=fs.nextInt();
        int arr[]=new int[n];
        long rem=0;
        for(int i=0;i<n;i++)
        {
        arr[i]=fs.nextInt();
        rem=rem+arr[i];
        }
        arr[0]=fs.nextInt();
        long max=arr[0];
        long max1=0;
         for(int i=1;i<n;i++)
        {
        arr[i]=fs.nextInt();
         if(arr[i]>max)
           { max1=max;
            max=arr[i];
        }
        if(arr[i]<max&&arr[i]>max1)
            max1=arr[i];

        }
        max=max+max1;
        if(max>=rem)
            out.println("YES");
        else
            out.println("NO");

    //   for(int i=0;i<n;i++)
      // System.out.println(arr[i]);
    out.close();
    }

}


import java.util.*;
import java.io.*;

public class Main
{
   static  class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(InputStream f) {
            try {
                br = new BufferedReader(new InputStreamReader(f));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

    }
    public static void main(String args[])
    {
         InputStream in=System.in;
       //  OutputStream out=System.out;
        FastScanner fs=new FastScanner(in);
        PrintWriter out=new PrintWriter(System.out);

         int n=fs.nextInt();
        int arr[]=new int[n];
        long rem=0;
        for(int i=0;i<n;i++)
        {
        arr[i]=fs.nextInt();
        rem=rem+arr[i];
        }
        arr[0]=fs.nextInt();
        long max=arr[0];
        long max1=0;
         for(int i=1;i<n;i++)
        {
        arr[i]=fs.nextInt();
         if(arr[i]>=max)
           { max1=max;
            max=arr[i];
        }
        if(arr[i]<max&&arr[i]>max1)
            max1=arr[i];

        }
        max=max+max1;
        if(max>=rem)
            out.println("YES");
        else
            out.println("NO");

    //   for(int i=0;i<n;i++)
      // System.out.println(arr[i]);
    out.close();
    }

}