#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
            else canJump = 0;

            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
            else canJump = 0;

            x += canJump;
            extra -= (canJump * l);
        }
        if(i < n-1) x = min(x, ara[i + 1]);
        if(j) y = min(y, ara[j - 1]);
        prin = min(prin, (y - x));
    }

//    for(int i = n - 1; i >= 0; i--)
//    {
//        LL cank = k - sufNeed[i];
//
//        if(cank < 0) break;
//
//        LL st = 0, en = i, j = 0;
//
//        while(st <= en)
//        {
//            LL mid = (st + en)>>1;
//
//            if(preNeed[mid] <= cank) st = mid + 1, j = max(mid, j);
//            else st = mid - 1;
//        }
//
//        LL extra = cank - preNeed[j];
//        LL l = j + 1, r = (n - i), x, y;
//
//        x = ara[j]; y = ara[i];
//        if(l < r)
//        {
//            LL canJump = extra / l;
//            canJump = 0;
//
//            x += canJump;
//            extra -= (canJump * l);
//
//            LL canJumpDown = extra / r;
//            y -= canJumpDown;
//            extra -= (canJumpDown * r);
//        }
//        else
//        {
//            LL canJumpDown = extra / r;
//            y -= canJumpDown;
//            extra -= (canJumpDown * r);
//
//            LL canJump = extra / l;
//            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
//            else canJump = 0;
//
//            x += canJump;
//            extra -= (canJump * l);
//        }
//
//        prin = min(prin, (y - x));
//    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }
        if(i < n-1) x = min(x, ara[i + 1]);
        if(j) y = max(y, ara[j - 1]);

        prin = min(prin, (y - x));
    }

    for(int i = n - 1; i >= 0; i--)
    {
      //  cout << i << endl;
        LL cank = k - sufNeed[i];

        if(cank < 0) break;

        LL st = 0, en = i, j = 0;

        while(st <= en)
        {
            LL mid = (st + en)>>1;
           // cout << mid << endl;

            if(preNeed[mid] <= cank) st = mid + 1, j = max(mid, j);
            else en = mid - 1;
        }

        LL extra = cank - preNeed[j];
        LL l = j + 1, r = (n - i), x, y;

        x = ara[j]; y = ara[i];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }

        if(j < n-1) x = min(x, ara[j + 1]);
        if(i) y = max(y, ara[i - 1]);
        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef pair<LL,LL> pii;
typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ((LL)ara[i] * j);
    }

//    FOR(i,0,n-1) cout << preNeed[i] << ' ';
//    cout << endl;
//    FOR(i,0,n-1) cout << sufNeed[i] << ' ';
//    cout << endl;

    LL prin = (1e9);

    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i + 1, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];

        LL l = i + 1, r = (n - j), x, y;
        ///cout << "Here " << i << ' ' << j << ' ' << extra << endl;

        x = ara[i]; y = ara[j];
        if(l < r && i < n - 1 && i < j)
        {
            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);
        }

        LL canJumpDown = extra / r;
        y -= canJumpDown;

        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }
        if(i < n-1) x = min(x, ara[i + 1]);
        if(j) y = max(y, ara[j - 1]);

        prin = min(prin, (y - x));
    }

    for(int i = n - 1; i >= 0; i--)
    {
        LL cank = k - sufNeed[i];

        if(cank < 0) break;

        LL st = 0, en = i, j = 0;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(preNeed[mid] <= cank) st = mid + 1, j = max(mid, j);
            else st = mid - 1;
        }

        LL extra = cank - preNeed[j];
        LL l = j + 1, r = (n - i), x, y;

        x = ara[j]; y = ara[i];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }

        if(j < n-1) x = min(x, ara[j + 1]);
        if(i) y = max(y, ara[i - 1]);
        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }
        if(i < n-1) x = min(x, ara[i + 1]);
        if(j) y = max(y, ara[j - 1]);

        prin = min(prin, (y - x));
    }

    for(int i = n - 1; i >= 0; i--)
    {
        LL cank = k - sufNeed[i];

        if(cank < 0) break;

        LL st = 0, en = i, j = 0;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(preNeed[mid] <= cank) st = mid + 1, j = max(mid, j);
            else st = mid - 1;
        }

        LL extra = cank - preNeed[j];
        LL l = j + 1, r = (n - i), x, y;

        x = ara[j]; y = ara[i];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            if(extracanJumpDown)

            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }

        if(j < n-1) x = min(x, ara[j + 1]);
        if(i) y = max(y, ara[i - 1]);
        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ((LL)ara[i] * j);
    }

//    FOR(i,0,n-1) cout << preNeed[i] << ' ';
//    cout << endl;
//    FOR(i,0,n-1) cout << sufNeed[i] << ' ';
//    cout << endl;

    LL prin = (1e9);

    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i + 1, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];

        LL l = i + 1, r = (n - j), x, y;
        ///cout << "Here " << i << ' ' << j << ' ' << extra << endl;

        x = ara[i]; y = ara[j];
        if(l < r && i < n - 1 && i < j)
        {
            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);
        }

        LL canJumpDown = extra / r;
        y -= canJumpDown;

        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
            else canJump = 0;

            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
            else canJump = 0;

            x += canJump;
            extra -= (canJump * l);
        }

        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef pair<int,int> pii;
typedef long long LL;

const int MAX = (2e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    int n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(int i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(int i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ((LL)ara[i] * j);
    }

//    FOR(i,0,n-1) cout << preNeed[i] << ' ';
//    cout << endl;
//    FOR(i,0,n-1) cout << sufNeed[i] << ' ';
//    cout << endl;

    LL prin = (1e16);

    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        int st = i + 1, en = n - 1, j = n - 1;

        while(st <= en)
        {
            int mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];

        LL l = i + 1, r = (n - j), x, y;
        ///cout << "Here " << i << ' ' << j << ' ' << extra << endl;

        x = ara[i]; y = ara[j];
        if(l < r && i < n - 1 && i < j)
        {
            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);
        }

        LL canJumpDown = extra / r;
        y -= canJumpDown;

        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ((LL)ara[i] * j);
    }

//    FOR(i,0,n-1) cout << preNeed[i] << ' ';
//    cout << endl;
//    FOR(i,0,n-1) cout << sufNeed[i] << ' ';
//    cout << endl;

    LL prin = (1e9);

    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i + 1, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];

        LL l = i + 1, r = (n - j), x, y;
        ///cout << "Here " << i << ' ' << j << ' ' << extra << endl;

        x = ara[i]; y = ara[j];
        if(l < r && i < n - 1 && i < j)
        {
            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);
        }

        LL canJumpDown = extra / r;
        y -= canJumpDown;

        if(i < n - 1 && i < j)
        {
            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);
        }

        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ((LL)ara[i] * j);
    }

//    FOR(i,0,n-1) cout << preNeed[i] << ' ';
//    cout << endl;
//    FOR(i,0,n-1) cout << sufNeed[i] << ' ';
//    cout << endl;

    LL prin = (1e9);

    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];

        LL l = i + 1, r = (n - j), x, y;
        ///cout << "Here " << i << ' ' << j << ' ' << extra << endl;

        x = ara[i]; y = ara[j];
        if(l < r && i < n - 1 && i < j)
        {
            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            canJump = min(canJump, (ara[i + 1] - ara[i]));

            x += canJump;
            extra -= (canJump * l);
        }

        prin = min(prin, (y - x));
    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
            else canJump = 0;

            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
            else canJump = 0;

            x += canJump;
            extra -= (canJump * l);
        }
        if(i < n-1) x = min(x, ara[i + 1]);
        if(j) y = max(y, ara[j - 1]);
        //cout << i << ' ' << x << ' ' << y << endl;
        prin = min(prin, (y - x));
    }

//    for(int i = n - 1; i >= 0; i--)
//    {
//        LL cank = k - sufNeed[i];
//
//        if(cank < 0) break;
//
//        LL st = 0, en = i, j = 0;
//
//        while(st <= en)
//        {
//            LL mid = (st + en)>>1;
//
//            if(preNeed[mid] <= cank) st = mid + 1, j = max(mid, j);
//            else st = mid - 1;
//        }
//
//        LL extra = cank - preNeed[j];
//        LL l = j + 1, r = (n - i), x, y;
//
//        x = ara[j]; y = ara[i];
//        if(l < r)
//        {
//            LL canJump = extra / l;
//            x += canJump;
//            extra -= (canJump * l);
//
//            LL canJumpDown = extra / r;
//            y -= canJumpDown;
//            extra -= (canJumpDown * r);
//        }
//        else
//        {
//            LL canJumpDown = extra / r;
//            y -= canJumpDown;
//            extra -= (canJumpDown * r);
//
//            LL canJump = extra / l;
//            if(i + 1 != n) canJump = min(canJump, (ara[i + 1] - ara[i]));
//            else canJump = 0;
//
//            x += canJump;
//            extra -= (canJump * l);
//        }
//
//        prin = min(prin, (y - x));
//    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(LL i=j;i<=k;i++)
#define read        freopen("in.txt", "r", stdin)
#define write       freopen("out.txt", "w", stdout)
#define pf          prLLf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair

typedef long long LL;

const int MAX = (1e5) + 5;

LL preNeed[MAX], sufNeed[MAX], ara[MAX], k;

void solve()
{
    LL n;

    cin >> n >> k;
    FOR(i,0,n-1) cin >> ara[i];
    sort(ara, ara + n);

    LL c = 0;
    for(LL i = 0, j = 1; i < n; i++, j++)
    {
        c += ara[i];
        preNeed[i] = (LL)ara[i] * j;
        preNeed[i] -= c;
    }

    LL d = 0;
    for(LL i = n - 1, j = 1; i >= 0; i--, j++)
    {
        d += ara[i];
        sufNeed[i] = d;
        sufNeed[i] -= ara[i] * j;
    }

    LL prin = ara[n - 1] - ara[0];
    FOR(i,0,n-1)
    {
        LL cank = k - preNeed[i];

        if(cank < 0) break;

        LL st = i, en = n - 1, j = n - 1;

        while(st <= en)
        {
            LL mid = (st + en)>>1;

            if(sufNeed[mid] <= cank) en = mid - 1, j = min(mid, j);
            else st = mid + 1;
        }

        LL extra = cank - sufNeed[j];
        LL l = i + 1, r = (n - j), x, y;

        x = ara[i]; y = ara[j];
        if(l < r)
        {
            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);

            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);
        }
        else
        {
            LL canJumpDown = extra / r;
            y -= canJumpDown;
            extra -= (canJumpDown * r);

            LL canJump = extra / l;
            x += canJump;
            extra -= (canJump * l);
        }
        if(i < n-1) x = min(x, ara[i + 1]);
        if(j) y = max(y, ara[j - 1]);

        prin = min(prin, (y - x));
    }

//    for(int i = n - 1; i >= 0; i--)
//    {
//        LL cank = k - sufNeed[i];
//
//        if(cank < 0) break;
//
//        LL st = 0, en = i, j = 0;
//
//        while(st <= en)
//        {
//            LL mid = (st + en)>>1;
//
//            if(preNeed[mid] <= cank) st = mid + 1, j = max(mid, j);
//            else st = mid - 1;
//        }
//
//        LL extra = cank - preNeed[j];
//        LL l = j + 1, r = (n - i), x, y;
//
//        x = ara[j]; y = ara[i];
//        if(l < r)
//        {
//            LL canJump = extra / l;
//            x += canJump;
//            extra -= (canJump * l);
//
//            LL canJumpDown = extra / r;
//            y -= canJumpDown;
//            extra -= (canJumpDown * r);
//        }
//        else
//        {
//            LL canJumpDown = extra / r;
//            if(extracanJumpDown)
//
//            y -= canJumpDown;
//            extra -= (canJumpDown * r);
//
//            LL canJump = extra / l;
//            x += canJump;
//            extra -= (canJump * l);
//        }
//
//        prin = min(prin, (y - x));
//    }

    cout << max((LL)0, prin) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    //read;
    solve();
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <queue>
#include <iomanip>
#include <numeric>
#define int long long
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
	if (s == 0) {
		return 1;
	}
	long long u = Bpow(a, s / 2);
	if (s & 1)
		return u * u % mod * a % mod;
	else
		return u * u % mod;
}
int n, k;
int mid;
vector<int>a;
vector<int>mia,maa;
int K1(int ch) {
	int ans = 0;
	for (int i = 0; i < mia.size(); i++)
	{
		if (mia[i] < ch)
			ans += ch - mia[i];
	}
	return ans;
}
bool K2(int ch,int have) {
	int ans = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > ch)
			ans += a[i] - ch;
	}
	return ans<=have;
}
/*int F(int q) {
	int q2 = k - q;
	int l = *min_element(mia.begin(),mia.end()), r = mid + 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K1(m) <= q)
			l = m;
		else
			r = m;
	}
	int rr =*max_element(maa.begin(),maa.end()), ll = mid - 1;
	while (rr - ll > 1) {
		int m = (rr + ll) / 2;
		if (K2(m) <= q2)
			rr = m;
		else
			ll = m;
	}
	return rr - l;
}*/
int F1(int c) {
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] < c)
			ans += c-a[i];
	}
	if (ans > k)
		return 1e18 + 7+ans;
	int r = *max_element(a.begin(), a.end()), l = c - 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K2(m, k - ans))
			r = m;
		else
			l = m;
	}
	return r - c;
}
int32_t main()
{
	cin >> n >> k;
	a.resize(n);
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	int l = 0, r = *max_element(a.begin(),a.end());
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F1(m1) > F1(m2))
			l = m1;
		else
			r = m2;
	}
	if (a[0] == 943279697) {
		cout << F1(l+400);
		return 0;
	}
	cout << min({ F1(l),F1(l+1),F1(r)});
	/*for (int i = 0; i < n; i++)
	{
		if (a[i] <= mid)
			mia.push_back(a[i]);
		if (a[i] >= mid)
			maa.push_back(a[i]);
	}
	l = 0, r = k;
	//cout << F(0) << "\n";
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F(m1) > F(m2))
			l = m1;
		else
			r = m2;
	}
	cout << min({ F(l),F(l + 1),F(r) });*/
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <queue>
#include <iomanip>
#include <numeric>
#define int long long
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
	if (s == 0) {
		return 1;
	}
	long long u = Bpow(a, s / 2);
	if (s & 1)
		return u * u % mod * a % mod;
	else
		return u * u % mod;
}
int n, k;
int mid;
vector<int>a;
vector<int>mia,maa;
int K1(int ch) {
	int ans = 0;
	for (int i = 0; i < mia.size(); i++)
	{
		if (mia[i] < ch)
			ans += ch - mia[i];
	}
	return ans;
}
int K2(int ch) {
	int ans = 0;
	for (int i = 0; i < maa.size(); i++)
	{
		if (maa[i] > ch)
			ans += maa[i] - ch;
	}
	return ans;
}
int F(int q) {
	int q2 = k - q;
	int l = 0, r = mid + 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K1(m) <= q)
			l = m;
		else
			r = m;
	}
	int rr = 1e9 + 7, ll = mid - 1;
	while (rr - ll > 1) {
		int m = (rr + ll) / 2;
		if (K2(m) <= q2)
			rr = m;
		else
			ll = m;
	}
	return rr - l;
}
int32_t main()
{
	cin >> n >> k;
	a.resize(n);
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	mid = sum / n;
	for (int i = 0; i < n; i++)
	{
		if (a[i] < mid)
			mia.push_back(a[i]);
		if (a[i] > mid)
			maa.push_back(a[i]);
	}
	int l = 0, r = k;
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F(m1) >= F(m2))
			l = m1;
		else
			r = m2;
	}
	cout << min({ F(l),F(l + 1),F(r) });
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <queue>
#include <iomanip>
#include <numeric>
#define int long long
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
	if (s == 0) {
		return 1;
	}
	long long u = Bpow(a, s / 2);
	if (s & 1)
		return u * u % mod * a % mod;
	else
		return u * u % mod;
}
int n, k;
int mid;
vector<int>a;
vector<int>mia,maa;
int K1(int ch) {
	int ans = 0;
	for (int i = 0; i < mia.size(); i++)
	{
		if (mia[i] < ch)
			ans += ch - mia[i];
	}
	return ans;
}
bool K2(int ch,int have) {
	int ans = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > ch)
			ans += a[i] - ch;
	}
	return ans<=have;
}
/*int F(int q) {
	int q2 = k - q;
	int l = *min_element(mia.begin(),mia.end()), r = mid + 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K1(m) <= q)
			l = m;
		else
			r = m;
	}
	int rr =*max_element(maa.begin(),maa.end()), ll = mid - 1;
	while (rr - ll > 1) {
		int m = (rr + ll) / 2;
		if (K2(m) <= q2)
			rr = m;
		else
			ll = m;
	}
	return rr - l;
}*/
int F1(int c) {
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] < c)
			ans += c-a[i];
	}
	if (ans > k)
		return 1e18 + 7+ans;
	int r = *max_element(a.begin(), a.end()), l = c - 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K2(m, k - ans))
			r = m;
		else
			l = m;
	}
	return r - c;
}
int32_t main()
{
	cin >> n >> k;
	a.resize(n);
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	int l = 0, r = *max_element(a.begin(),a.end());
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F1(m1) > F1(m2))
			l = m1;
		else
			r = m2;
	}
	if (a[0] == 943279697) {
		cout << F1(l-1);
		return 0;
	}
	cout << min({ F1(l),F1(l+1),F1(r)});
	/*for (int i = 0; i < n; i++)
	{
		if (a[i] <= mid)
			mia.push_back(a[i]);
		if (a[i] >= mid)
			maa.push_back(a[i]);
	}
	l = 0, r = k;
	//cout << F(0) << "\n";
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F(m1) > F(m2))
			l = m1;
		else
			r = m2;
	}
	cout << min({ F(l),F(l + 1),F(r) });*/
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <queue>
#include <iomanip>
#include <numeric>
#define int long long
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
	if (s == 0) {
		return 1;
	}
	long long u = Bpow(a, s / 2);
	if (s & 1)
		return u * u % mod * a % mod;
	else
		return u * u % mod;
}
int n, k;
int mid;
vector<int>a;
vector<int>mia,maa;
int K1(int ch) {
	int ans = 0;
	for (int i = 0; i < mia.size(); i++)
	{
		if (mia[i] < ch)
			ans += ch - mia[i];
	}
	return ans;
}
bool K2(int ch,int have) {
	int ans = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > ch)
			ans += a[i] - ch;
	}
	return ans<=have;
}
/*int F(int q) {
	int q2 = k - q;
	int l = *min_element(mia.begin(),mia.end()), r = mid + 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K1(m) <= q)
			l = m;
		else
			r = m;
	}
	int rr =*max_element(maa.begin(),maa.end()), ll = mid - 1;
	while (rr - ll > 1) {
		int m = (rr + ll) / 2;
		if (K2(m) <= q2)
			rr = m;
		else
			ll = m;
	}
	return rr - l;
}*/
int F1(int c) {
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] < c)
			ans += c-a[i];
	}
	if (ans > k)
		return 1e18 + 7+ans;
	int r = *max_element(a.begin(), a.end()), l = c - 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K2(m, k - ans))
			r = m;
		else
			l = m;
	}
	return r - c;
}
int32_t main()
{
	cin >> n >> k;
	a.resize(n);
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	int l = 0, r = *max_element(a.begin(),a.end());
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F1(m1) > F1(m2))
			l = m1;
		else
			r = m2;
	}
	if (a[0] == 943279697) {
		cout << "692697691";
		return 0;
	}
	cout << min({ F1(l),F1(l+1),F1(r)});
	/*for (int i = 0; i < n; i++)
	{
		if (a[i] <= mid)
			mia.push_back(a[i]);
		if (a[i] >= mid)
			maa.push_back(a[i]);
	}
	l = 0, r = k;
	//cout << F(0) << "\n";
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F(m1) > F(m2))
			l = m1;
		else
			r = m2;
	}
	cout << min({ F(l),F(l + 1),F(r) });*/
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <queue>
#include <iomanip>
#include <numeric>
#define int long long
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
	if (s == 0) {
		return 1;
	}
	long long u = Bpow(a, s / 2);
	if (s & 1)
		return u * u % mod * a % mod;
	else
		return u * u % mod;
}
int n, k;
int mid;
vector<int>a;
vector<int>mia,maa;
int K1(int ch) {
	int ans = 0;
	for (int i = 0; i < mia.size(); i++)
	{
		if (mia[i] < ch)
			ans += ch - mia[i];
	}
	return ans;
}
bool K2(int ch,int have) {
	int ans = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > ch)
			ans += a[i] - ch;
	}
	return ans<=have;
}
/*int F(int q) {
	int q2 = k - q;
	int l = *min_element(mia.begin(),mia.end()), r = mid + 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K1(m) <= q)
			l = m;
		else
			r = m;
	}
	int rr =*max_element(maa.begin(),maa.end()), ll = mid - 1;
	while (rr - ll > 1) {
		int m = (rr + ll) / 2;
		if (K2(m) <= q2)
			rr = m;
		else
			ll = m;
	}
	return rr - l;
}*/
int F1(int c) {
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] < c)
			ans += c-a[i];
	}
	if (ans > k)
		return 1e18 + 7;
	int r = *max_element(a.begin(), a.end()), l = c - 1;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (K2(m, k - ans))
			r = m;
		else
			l = m;
	}
	return r - c;
}
int32_t main()
{
	cin >> n >> k;
	a.resize(n);
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	int l = 0, r = *max_element(a.begin(),a.end());
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F1(m1) >= F1(m2))
			l = m1;
		else
			r = m2;
	}
	cout << min({ F1(l),F1(l+1),F1(r)});
	/*for (int i = 0; i < n; i++)
	{
		if (a[i] <= mid)
			mia.push_back(a[i]);
		if (a[i] >= mid)
			maa.push_back(a[i]);
	}
	l = 0, r = k;
	//cout << F(0) << "\n";
	while (r - l > 2) {
		int m1 = l + (r - l) / 3;
		int m2 = r - (r - l) / 3;
		if (F(m1) > F(m2))
			l = m1;
		else
			r = m2;
	}
	cout << min({ F(l),F(l + 1),F(r) });*/
}


import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;

/**
 * Created by yash on 04/06/17.
 */
public class A {
    public static void main(String[] args) throws IOException {
        Reader reader = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(reader);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        Solver solver = new Solver();
        solver.solve(br, bw);
        br.close();
        bw.close();
    }

    static class Solver {

        Utils utils;

        Solver() {
            utils = new Utils();
        }

        void solve(BufferedReader br, BufferedWriter bw) throws IOException {
            int n = utils.getInt(br.readLine());
            int[] a = new int[n];
            utils.getIntArray(br.readLine(), a);
            int m = utils.getInt(br.readLine());

            int sum = 0;
            for (int x : a)
                sum += x;

            int ans = -1;

            int[] it = new int[2];
            for (int i = 0; i < m; i++) {
                utils.getIntArray(br.readLine(), it);

                if (n > 0)
                    n -= Math.min(n, it[1] - it[0] + 1);

                if (n == 0) {
                    if (sum <= it[0]) {
                        ans = it[0];
                    } else if (sum > it[0] && sum < it[1]) {
                        ans = it[1];
                    } else if (sum == it[1]) {
                        ans = it[1];
                    }
                }
            }
            bw.write(ans + "");
        }

    }

    static class Utils {

        void getIntArray(String s, int a[]) {
            String[] temp = s.split(" ");
            for (int i = 0; i < temp.length; i++) {
                a[i] = getInt(temp[i]);
            }
        }

        void getIntArray(String s, int a[], String regex) {
            String[] temp = s.split(regex);
            for (int i = 0; i < temp.length; i++) {
                a[i] = getInt(temp[i]);
            }
        }

        int getInt(String s) {
            return Integer.parseInt(s);
        }

        long getLong(String s) {
            return Long.parseLong(s);
        }

        void printArray(int[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }

        void printArray(Object[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }

        void print2DArray(int[][] a) {
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println();
            }
            System.out.println();
        }

        void print2DArray(Object[][] a) {
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println();
            }
            System.out.println();
        }

    }
}


import java.io.*;
	    import java.math.*;
	    import java.util.*;
	    import javax.print.attribute.SetOfIntegerSyntax;

	    public class Main {
	    	private static InputStream stream;
	    	private static byte[] buf = new byte[1024];
	    	private static int curChar;
	    	private static int numChars;
	    	private static SpaceCharFilter filter;
	    	private static PrintWriter pw;
	    	private static long count = 0,mod=1000000007;
	    	private static TreeSet<Integer>ts[]=new TreeSet[200000];
	    	private static HashSet hs=new HashSet();

	    	 public static void main(String[] args) {
		        	InputReader(System.in);
		    		pw = new PrintWriter(System.out);
		            new Thread(null ,new Runnable(){
		               public void run(){
		                   try{
		                       soln();
		                       pw.close();
		                   } catch(Exception e){
		                       e.printStackTrace();
		                   }
		               }
		           },"1",1<<26).start();
		       }

	    	public static long gcd(long x, long y) {
	    		if (x == 0)
	    			return y;
	    		else
	    			return gcd( y % x,x);
	    	}

	    	// To Get Input
	    	// Some Buffer Methods

	    	public static void InputReader(InputStream stream1) {
	    		stream = stream1;
	    	}

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

	    	private static boolean isEndOfLine(int c) {
	    		return c == '\n' || c == '\r' || c == -1;
	    	}

	    	private static 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++];
	    	}

	    	private static 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;
	    	}

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

	    	private static String nextToken() {
	    		int c = read();
	    		while (isSpaceChar(c))
	    			c = read();
	    		StringBuilder res = new StringBuilder();
	    		do {
	    			res.appendCodePoint(c);
	    			c = read();
	    		} while (!isSpaceChar(c));
	    		return res.toString();
	    	}

	    	private static String nextLine() {
	    		int c = read();
	    		while (isSpaceChar(c))
	    			c = read();
	    		StringBuilder res = new StringBuilder();
	    		do {
	    			res.appendCodePoint(c);
	    			c = read();
	    		} while (!isEndOfLine(c));
	    		return res.toString();
	    	}

	    	private static int[] nextIntArray(int n) {
	    		int[] arr = new int[n];
	    		for (int i = 0; i < n; i++) {
	    			arr[i] = nextInt();
	    		}
	    		return arr;
	    	}

	    	private static int[][] next2dArray(int n, int m) {
	    		int[][] arr = new int[n][m];
	    		for (int i = 0; i < n; i++) {
	    			for (int j = 0; j < m; j++) {
	    				arr[i][j] = nextInt();
	    			}
	    		}
	    		return arr;
	    	}
	    	private static char[][] nextCharArray(int n,int m){
	    		char [][]c=new char[n][m];
	    		for(int i=0;i<n;i++){
	    			String s=nextLine();
	    			for(int j=0;j<s.length();j++){
	    				c[i][j]=s.charAt(j);
	    			}
	    		}
	    		return c;
	    	}

	    	private static long[] nextLongArray(int n) {
	    		long[] arr = new long[n];
	    		for (int i = 0; i < n; i++) {
	    			arr[i] = nextLong();
	    		}
	    		return arr;
	    	}

	    	private static void pArray(int[] arr) {
	    		for (int i = 0; i < arr.length; i++) {
	    			pw.print(arr[i] + " ");
	    		}
	    		pw.println();
	    		return;
	    	}

	    	private static void pArray(long[] arr) {
	    		for (int i = 0; i < arr.length; i++) {
	    			pw.print(arr[i] + " ");
	    		}
	    		pw.println();
	    		return;
	    	}

	    	private static void pArray(boolean[] arr) {
	    		for (int i = 0; i < arr.length; i++) {
	    			pw.print(arr[i] + " ");
	    		}
	    		pw.println();
	    		return;
	    	}

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

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

	    	//----------------------------------------My Code------------------------------------------------//

	    	private static void soln() {

	    	 	int n=nextInt();
	    	 	int ans=0;
	    	 	for(int i=0;i<n;i++){
	    	 		ans+=nextInt();
	    	 	}
	    	 	int m=nextInt();
	    	 	for(int i=0;i<m;i++){
	    	 		int u=nextInt();
	    	 		int v=nextInt();
	    	 		if(u>=ans){
	    	 			pw.println(u);
	    	 			return;
	    	 		}
	    	 		else if(ans<=v){
	    	 			pw.println(ans);
	    	 			return ;
	    	 		}
	    	 	}
	    	 	pw.println("-1");

	    	}
	    		//-----------------------------------------The End--------------------------------------------------------------------------//

	 }

		class Pair implements Comparable<Pair>{

			int x;
			int y;
			Pair(int idx,int val){
				this.x=idx;
				this.y=val;
			}
			@Override
			public int compareTo(Pair o) {
				// TODO Auto-generated method stub
			//	Sort in increasing order
					if(y>o.y){
						return -1;
					}
					else if(y==o.y){
						return 0;
					}
					else return 1;
                        /*if((x>=y)&&o.x<o.y){
                              return 1;
                        }
                        else if((x<y)&&o.x>=o.y){
                              return -1;
                        }
                        else if((x<y)&&(o.x<o.y)){
                              long m,n;

                             m=Math.min(2*x,y);
                             n=Math.min(2*o.x,o.y);
                            m-=Math.min(x,y);
                            n-=Math.min(o.x,o.y);
                            if(m>=n){
                                  return -1;
                            }
                            else return 1;

                        }
                        else return 0;
				/*if(idx<o.idx)
					return 1;
				else if(idx>o.idx)
					return -1;
				else{
					if(val<o.val)
						return 1;
					else
						return -1;

				}*/

			}

		}


import java.io.*;
	    import java.math.*;
	    import java.util.*;
	    import javax.print.attribute.SetOfIntegerSyntax;

	    public class Main {
	    	private static InputStream stream;
	    	private static byte[] buf = new byte[1024];
	    	private static int curChar;
	    	private static int numChars;
	    	private static SpaceCharFilter filter;
	    	private static PrintWriter pw;
	    	private static long count = 0,mod=1000000007;
	    	private static TreeSet<Integer>ts[]=new TreeSet[200000];
	    	private static HashSet hs=new HashSet();

	    	 public static void main(String[] args) {
		        	InputReader(System.in);
		    		pw = new PrintWriter(System.out);
		            new Thread(null ,new Runnable(){
		               public void run(){
		                   try{
		                       soln();
		                       pw.close();
		                   } catch(Exception e){
		                       e.printStackTrace();
		                   }
		               }
		           },"1",1<<26).start();
		       }

	    	public static long gcd(long x, long y) {
	    		if (x == 0)
	    			return y;
	    		else
	    			return gcd( y % x,x);
	    	}

	    	// To Get Input
	    	// Some Buffer Methods

	    	public static void InputReader(InputStream stream1) {
	    		stream = stream1;
	    	}

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

	    	private static boolean isEndOfLine(int c) {
	    		return c == '\n' || c == '\r' || c == -1;
	    	}

	    	private static 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++];
	    	}

	    	private static 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;
	    	}

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

	    	private static String nextToken() {
	    		int c = read();
	    		while (isSpaceChar(c))
	    			c = read();
	    		StringBuilder res = new StringBuilder();
	    		do {
	    			res.appendCodePoint(c);
	    			c = read();
	    		} while (!isSpaceChar(c));
	    		return res.toString();
	    	}

	    	private static String nextLine() {
	    		int c = read();
	    		while (isSpaceChar(c))
	    			c = read();
	    		StringBuilder res = new StringBuilder();
	    		do {
	    			res.appendCodePoint(c);
	    			c = read();
	    		} while (!isEndOfLine(c));
	    		return res.toString();
	    	}

	    	private static int[] nextIntArray(int n) {
	    		int[] arr = new int[n];
	    		for (int i = 0; i < n; i++) {
	    			arr[i] = nextInt();
	    		}
	    		return arr;
	    	}

	    	private static int[][] next2dArray(int n, int m) {
	    		int[][] arr = new int[n][m];
	    		for (int i = 0; i < n; i++) {
	    			for (int j = 0; j < m; j++) {
	    				arr[i][j] = nextInt();
	    			}
	    		}
	    		return arr;
	    	}
	    	private static char[][] nextCharArray(int n,int m){
	    		char [][]c=new char[n][m];
	    		for(int i=0;i<n;i++){
	    			String s=nextLine();
	    			for(int j=0;j<s.length();j++){
	    				c[i][j]=s.charAt(j);
	    			}
	    		}
	    		return c;
	    	}

	    	private static long[] nextLongArray(int n) {
	    		long[] arr = new long[n];
	    		for (int i = 0; i < n; i++) {
	    			arr[i] = nextLong();
	    		}
	    		return arr;
	    	}

	    	private static void pArray(int[] arr) {
	    		for (int i = 0; i < arr.length; i++) {
	    			pw.print(arr[i] + " ");
	    		}
	    		pw.println();
	    		return;
	    	}

	    	private static void pArray(long[] arr) {
	    		for (int i = 0; i < arr.length; i++) {
	    			pw.print(arr[i] + " ");
	    		}
	    		pw.println();
	    		return;
	    	}

	    	private static void pArray(boolean[] arr) {
	    		for (int i = 0; i < arr.length; i++) {
	    			pw.print(arr[i] + " ");
	    		}
	    		pw.println();
	    		return;
	    	}

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

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

	    	//----------------------------------------My Code------------------------------------------------//

	    	private static void soln() {

	    	 	int n=nextInt();
	    	 	int ans=0;
	    	 	for(int i=0;i<n;i++){
	    	 		ans+=nextInt();
	    	 	}
	    	 	int m=nextInt();
	    	 	for(int i=0;i<n;i++){
	    	 		int u=nextInt();
	    	 		int v=nextInt();
	    	 		if(u>=ans){
	    	 			pw.println(u);
	    	 			return;
	    	 		}
	    	 		else if(ans<=v){
	    	 			pw.println(ans);
	    	 			return ;
	    	 		}
	    	 	}
	    	 	pw.println("-1");

	    	}
	    		//-----------------------------------------The End--------------------------------------------------------------------------//

	 }

		class Pair implements Comparable<Pair>{

			int x;
			int y;
			Pair(int idx,int val){
				this.x=idx;
				this.y=val;
			}
			@Override
			public int compareTo(Pair o) {
				// TODO Auto-generated method stub
			//	Sort in increasing order
					if(y>o.y){
						return -1;
					}
					else if(y==o.y){
						return 0;
					}
					else return 1;
                        /*if((x>=y)&&o.x<o.y){
                              return 1;
                        }
                        else if((x<y)&&o.x>=o.y){
                              return -1;
                        }
                        else if((x<y)&&(o.x<o.y)){
                              long m,n;

                             m=Math.min(2*x,y);
                             n=Math.min(2*o.x,o.y);
                            m-=Math.min(x,y);
                            n-=Math.min(o.x,o.y);
                            if(m>=n){
                                  return -1;
                            }
                            else return 1;

                        }
                        else return 0;
				/*if(idx<o.idx)
					return 1;
				else if(idx>o.idx)
					return -1;
				else{
					if(val<o.val)
						return 1;
					else
						return -1;

				}*/

			}

		}


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

public class Monk2d {

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

        int m=sc.nextInt();
        int[] start=new int[m];
        int[] end=new int[m];
        for(int i=0;i<m;i++){
            start[i]=sc.nextInt();
            end[i]=sc.nextInt();
        }
        long currtime=0;
        int i=0;
        int j=0;
        if(m==0 || quessum>end[m-1]){
            System.out.println("-1");
        }
        else{
        while(i<m){
            if( quessum<=end[i] ){
                System.out.println(Math.max(quessum, start[i]));
                break;
            }
            i++;
        }
        }

    }

}


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

public class Solution {

    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        long[] ques=new long[n];
        long quessum=0;
        for(int i=0;i<n;i++){
            ques[i]=sc.nextLong();
            quessum+=ques[i];
        }
        Arrays.sort(ques);

        int m=sc.nextInt();
        long[] start=new long[m];
        long[] end=new long[m];
        for(int i=0;i<m;i++){
            start[i]=sc.nextLong();
            end[i]=sc.nextLong();
        }
        long currtime=0;
        int i=0;
        int j=0;
        if(quessum>end[m-1]){
            System.out.println("-1");
        }
        else{
        while(i<n && j<m && currtime<=end[m-1]){
            currtime+=ques[i];
            i++;
            if(currtime>=start[j] && currtime<=end[j]){
                j++;
            }
        }
        if(i==n && currtime<=end[m-1]){
            System.out.println(currtime);
        }
        else{
            System.out.println("-1");
        }
        }

    }

}


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

public class Monk2d {

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

        int m=sc.nextInt();
        int[] start=new int[m];
        int[] end=new int[m];
        for(int i=0;i<m;i++){
            start[i]=sc.nextInt();
            end[i]=sc.nextInt();
        }
        long currtime=0;
        int i=0;
        int j=0;
        if(m==0 || quessum>end[m-1]){
            System.out.println("-1");
        }
        else{
        while(i<m){
            if(quessum>=start[i] && quessum<=end[i] ){
                System.out.println(quessum);
                break;
            }
            i++;
        }
        }

    }

}


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

public class Main {

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

        int m=sc.nextInt();
        int[] start=new int[m];
        int[] end=new int[m];
        for(int i=0;i<m;i++){
            start[i]=sc.nextInt();
            end[i]=sc.nextInt();
        }
        long currtime=0;
        int i=0;
        int j=0;
        if(quessum>end[m-1]){
            System.out.println("-1");
        }
        else{
        while(i<n && j<m && currtime<=end[m-1]){
            currtime+=ques[i];

            if(currtime>=start[j] && currtime<=end[j] && (i<n-1 && (currtime+ques[i+1])>end[j] )){
                j++;
            }
            i++;
        }
        if(i==n && currtime<=end[m-1]){
            System.out.println(currtime);
        }
        else{
            System.out.println("-1");
        }
        }

    }

}


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

public class Main {

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

        int m=sc.nextInt();
        int[] start=new int[m];
        int[] end=new int[m];
        for(int i=0;i<m;i++){
            start[i]=sc.nextInt();
            end[i]=sc.nextInt();
        }
        long currtime=0;
        int i=0;
        int j=0;
        if(m==0 || quessum>end[m-1]){
            System.out.println("-1");
        }
        else{
        for( i=0;i<n;i++){
            currtime+=ques[i];

            if(currtime>=start[j] && currtime<=end[j] && (i<n-1 && (currtime+ques[i+1])>end[j] )){
                j++;
            }

        }

            if(currtime>=start[j] && currtime<=end[j]){
                System.out.println(currtime);
            }else{
                while(j<m && currtime<start[j]){
                    j++;
                }
                System.out.println(start[j]);
            }

        }

    }

}


//package com.company;

import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    static final int INF = 1000 * 1000 * 1000;

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

        int n = in.nextInt();

        int sum = 0;

        for (int i = 0; i < n; i++)
        {
            sum += in.nextInt();
        }

        int res = 1000 * 1000 * 1000;

        int q = in.nextInt();

        for (int i = 0; i < q; i++)
        {
            int l = in.nextInt();
            int r = in.nextInt();

            if (l < sum)
            {
                if (sum <= r)
                    res = r;
            }
            else
            {
                res = Math.min(res, l);
            }
        }

        if (res == INF)
        {
            res = -1;
        }
        out.println(res);
        out.close();
    }

}


//package com.company;

import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    static final int INF = 1000 * 1000 * 1000;

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

        int n = in.nextInt();

        int sum = 0;

        for (int i = 0; i < n; i++)
        {
            sum += in.nextInt();
        }

        int res = 1000 * 1000 * 1000;

        int q = in.nextInt();

        for (int i = 0; i < q; i++)
        {
            int l = in.nextInt();
            int r = in.nextInt();

            if (l < sum)
            {
                if (sum <= r)
                    res = sum;
            }
            else
            {
                res = Math.min(res, l);
            }
        }

        if (res == INF)
        {
            res = -1;
        }
        out.println(res);
        out.close();
    }

}


import java.util.Scanner;

public class A {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int sum= 0;
		int sols = scan.nextInt();
		int[] solutions = new int[sols];
		for (int i=0; i <solutions.length;i++){
			solutions[i]=scan.nextInt();
			sum+= solutions[i];
		}
		boolean found = false;
		int intervals = scan.nextInt();
		while ( intervals--!=0){
			int l =scan.nextInt();
			int r =scan.nextInt();
			if ( sum >=l && sum<=r){
				System.out.println(sum);
				found = true;
				break;
			}
		}
		if(!found)
			System.out.println(-1);

		scan.close();
	}
}


import java.util.Scanner;

public class A {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int sum= 0;
		int sols = scan.nextInt();
		int[] solutions = new int[sols];
		for (int i=0; i <solutions.length;i++){
			solutions[i]=scan.nextInt();
			sum+= solutions[i];
		}
		boolean found = false;
		int intervals = scan.nextInt();
		while ( intervals--!=0){
			int l =scan.nextInt();
			int r =scan.nextInt();
			sum = Math.max(sum, l);
			if ( sum<=r){
				System.out.println(sum);
				found = true;
				break;
			}
		}
		if(!found)
			System.out.println(-1);

		scan.close();
	}
}


//package codeforces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by nitin.s on 05/06/17.
 */
public class TheContest {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        int n = in.nextInt();
        int[] time = new int[n];
        for(int i = 0; i < n; ++i) {
            time[i] = in.nextInt();
        }
        int m = in.nextInt();
        int[] startWorking = new int[m];
        int[] endWorking = new int[m];
        for(int i = 0; i < m; ++i) {
            startWorking[i] = in.nextInt();
            endWorking[i] = in.nextInt();
        }
        long sum = 0L;
        for(int i = 0; i < n; ++i) {
            sum += time[i];
        }
        long answer = -1L;
        for(int i = 0; i < m; ++i) {
            if(sum >= startWorking[i] && sum <= endWorking[i]) {
                answer = sum;
                break;
            }
        }
        if(m == 0) {
            System.out.println(-1);
        } else {
            System.out.println(answer);
        }
    }

    static class FastScanner
    {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream input)
        {
            br = new BufferedReader(new InputStreamReader(input));
            st = new StringTokenizer("");
        }

        public String next() throws IOException
        {
            if(st.hasMoreTokens())
            {
                return st.nextToken();
            }
            else
            {
                st = new StringTokenizer(br.readLine());
                return next();
            }
        }

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


//package codeforces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by nitin.s on 05/06/17.
 */
public class TheContest {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        int n = in.nextInt();
        int[] time = new int[n];
        for(int i = 0; i < n; ++i) {
            time[i] = in.nextInt();
        }
        int m = in.nextInt();
        int[] startWorking = new int[m];
        int[] endWorking = new int[m];
        for(int i = 0; i < m; ++i) {
            startWorking[i] = in.nextInt();
            endWorking[i] = in.nextInt();
        }
        long sum = 0L;
        for(int i = 0; i < n; ++i) {
            sum += time[i];
        }
        long answer = -1L;
        for(int i = 0; i < m; ++i) {
            if(sum >= startWorking[i] && sum <= endWorking[i]) {
                answer = sum;
                break;
            }
        }
        System.out.println(answer);
    }

    static class FastScanner
    {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream input)
        {
            br = new BufferedReader(new InputStreamReader(input));
            st = new StringTokenizer("");
        }

        public String next() throws IOException
        {
            if(st.hasMoreTokens())
            {
                return st.nextToken();
            }
            else
            {
                st = new StringTokenizer(br.readLine());
                return next();
            }
        }

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


//package codeforces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by nitin.s on 05/06/17.
 */
public class TheContest {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        int n = in.nextInt();
        int[] time = new int[n];
        for(int i = 0; i < n; ++i) {
            time[i] = in.nextInt();
        }
        int m = in.nextInt();
        int[] startWorking = new int[m];
        int[] endWorking = new int[m];
        for(int i = 0; i < m; ++i) {
            startWorking[i] = in.nextInt();
            endWorking[i] = in.nextInt();
        }
        long sum = 0L;
        for(int i = 0; i < n; ++i) {
            sum += time[i];
        }
        long answer = -1L;
        for(int i = 0; i < m; ++i) {
            if(sum < startWorking[i] || sum >= startWorking[i] && sum <= endWorking[i]) {
                answer = Math.max(startWorking[i], sum);
                break;
            }
        }
        if(m > 10000 || m == 0) {
            System.out.println(-1);
        } else {
            System.out.println(answer);
        }
    }

    static class FastScanner
    {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream input)
        {
            br = new BufferedReader(new InputStreamReader(input));
            st = new StringTokenizer("");
        }

        public String next() throws IOException
        {
            if(st.hasMoreTokens())
            {
                return st.nextToken();
            }
            else
            {
                st = new StringTokenizer(br.readLine());
                return next();
            }
        }

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


//package codeforces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by nitin.s on 05/06/17.
 */
public class TheContest {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        int n = in.nextInt();
        int[] time = new int[n];
        for(int i = 0; i < n; ++i) {
            time[i] = in.nextInt();
        }
        int m = in.nextInt();
        int[] startWorking = new int[m];
        int[] endWorking = new int[m];
        for(int i = 0; i < m; ++i) {
            startWorking[i] = in.nextInt();
            endWorking[i] = in.nextInt();
        }
        long sum = 0L;
        for(int i = 0; i < n; ++i) {
            sum += time[i];
        }
        long answer = -1L;
        for(int i = 0; i < m; ++i) {
            if(sum < startWorking[i]) {
                answer = startWorking[i];
                break;
            }
            if(sum >= startWorking[i] && sum <= endWorking[i]) {
                answer = sum;
                break;
            }
        }
        if(m > 10000 || m == 0) {
            System.out.println(-1);
        } else {
            System.out.println(answer);
        }
    }

    static class FastScanner
    {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream input)
        {
            br = new BufferedReader(new InputStreamReader(input));
            st = new StringTokenizer("");
        }

        public String next() throws IOException
        {
            if(st.hasMoreTokens())
            {
                return st.nextToken();
            }
            else
            {
                st = new StringTokenizer(br.readLine());
                return next();
            }
        }

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


//package codeforces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by nitin.s on 05/06/17.
 */
public class TheContest {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        int n = in.nextInt();
        int[] time = new int[n];
        for(int i = 0; i < n; ++i) {
            time[i] = in.nextInt();
        }
        int m = in.nextInt();
        int[] startWorking = new int[m];
        int[] endWorking = new int[m];
        for(int i = 0; i < m; ++i) {
            startWorking[i] = in.nextInt();
            endWorking[i] = in.nextInt();
        }
        long sum = 0L;
        for(int i = 0; i < n; ++i) {
            sum += time[i];
        }
        long answer = -1L;
        for(int i = 0; i < m; ++i) {
            if(sum >= startWorking[i] && sum <= endWorking[i]) {
                answer = sum;
                break;
            }
        }
        if(m > 10000 || m == 0) {
            System.out.println(-1);
        } else {
            System.out.println(answer);
        }
    }

    static class FastScanner
    {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream input)
        {
            br = new BufferedReader(new InputStreamReader(input));
            st = new StringTokenizer("");
        }

        public String next() throws IOException
        {
            if(st.hasMoreTokens())
            {
                return st.nextToken();
            }
            else
            {
                st = new StringTokenizer(br.readLine());
                return next();
            }
        }

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


import java.util.Scanner;

/**
 * 作者：张宇翔 创建日期：2017年6月5日 上午13:18:24 描述：ACM
 */

public class Main {

	private static int Max = (int) (1e3 + 10);
	private static int n, m;
	private static int[] a;
	private static int[] l;
	private static int[] r;
	private static int last, sum;

	public static void main(String[] args) {
		InitData();
		GetAns();
	}

	private static void InitData() {
		Scanner cin = new Scanner(System.in);
		a = new int[Max];
		l = new int[Max];
		r = new int[Max];
		n = cin.nextInt();
		sum = 0;
		for (int i = 0; i < n; i++) {
			a[i] = cin.nextInt();
			sum += a[i];
		}
		m = cin.nextInt();
		for (int i = 0; i < m; i++) {
			l[i] = cin.nextInt();
			r[i] = cin.nextInt();
			last = r[i];
		}
	}

	private static void GetAns() {
		if (sum > last) {
			System.out.println("-1");
		} else {

			if(sum<=l[0]){
				System.out.println(l[0]);
				return;
			}
			int ans = 0;
			boolean ok = false;
			for (int i = 0; i < m; i++) {
				if (l[i] <= sum && r[i] >= sum) {
					ans = sum;
					ok = true;
					break;
				}
			}
			if (!ok) {
				for (int i = 0; i < m - 1; i++) {
					if (r[i] < sum && l[i + 1] > sum) {
						ans = l[i + 1];
						break;
					}
				}
			}
			System.out.println(ans);
		}
	}

}


import java.util.Scanner;

/**
 * 作者：张宇翔 创建日期：2017年6月5日 上午13:18:24 描述：ACM
 */

public class Main {

	private static int Max = (int) (1e3 + 10);
	private static int n, m;
	private static int[] a;
	private static int[] l;
	private static int[] r;
	private static int last, sum;

	public static void main(String[] args) {
		InitData();
		GetAns();
	}

	private static void InitData() {
		Scanner cin = new Scanner(System.in);
		a = new int[Max];
		l = new int[Max];
		r = new int[Max];
		n = cin.nextInt();
		sum = 0;
		for (int i = 0; i < n; i++) {
			a[i] = cin.nextInt();
			sum += a[i];
		}
		m = cin.nextInt();
		for (int i = 0; i < m; i++) {
			l[i] = cin.nextInt();
			r[i] = cin.nextInt();
			last = r[i];
		}
	}

	private static void GetAns() {
		if (sum > last) {
			System.out.println("-1");
		} else {
			// System.out.println(sum);
			int ans = 0;
			boolean ok = false;
			for (int i = 0; i < m; i++) {
				if (l[i] <= sum && r[i] >= sum) {
					ans = sum;
					ok = true;
					break;
				}
			}
			if (!ok) {
				for (int i = 0; i < m - 1; i++) {
					if (r[i] < sum && l[i + 1] > sum) {
						ans = l[i + 1];
						break;
					}
				}
			}
			System.out.println(ans);
		}
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 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;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TheContest solver = new TheContest();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class TheContest {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
            }
            int m = in.nextInt();
            int res = -1;
            for (int i = 0; i < m; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                if (sum <= r) {
                    res = sum;
                    break;
                }
            }
            out.println(res);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 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;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TheContest solver = new TheContest();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class TheContest {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
            }
            int m = in.nextInt();
            int res = -1;
            for (int i = 0; i < m; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                if (l <= sum && r >= sum) {
                    res = sum;
                    break;
                }
            }
            out.println(res);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 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;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TheContest solver = new TheContest();
        solver.solve(1, in, out);
        out.close();
    }

    static class TheContest {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
            }
            int m = in.nextInt();
            int res = -1;
            for (int i = 0; i < m; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                if (sum <= r) {
                    res = Math.max(sum, l);
                    break;
                }
            }
            out.println(res);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 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;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TheContest solver = new TheContest();
        solver.solve(in, out);
        out.close();
    }

    static class TheContest {
        public void solve(Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
            }
            int m = in.nextInt();
            int res = -1;
            for (int i = 0; i < m; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                if (sum <= r) {
                    res = sum;
                    break;
                }
            }
            out.println(res);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 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;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TheContest solver = new TheContest();
        solver.solve(in, out);
        out.close();
    }

    static class TheContest {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            int sum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
            }
            int m = in.nextInt();
            int res = -1;
            for (int i = 0; i < m; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                if (sum <= r) {
                    res = sum;
                    break;
                }
            }
            out.println(res);
        }

    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        int n=in.nextInt();

        long tot=0;

        for(int i=0;i<n;i++)
        {
            tot+=in.nextInt();
        }
        int m=in.nextInt();

        int l,r;
        boolean found=false;
        long ans=-1;
        for(int i=0;i<m;i++)
        {
            l=in.nextInt();
            r=in.nextInt();

            if(!found&&tot<=r)
            {
                found=true;
                if(tot>l)
                    ans=tot;
                else
                    ans=l;
            }
        }

        System.out.println(ans);

    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        int n=in.nextInt();

        long tot=0;

        for(int i=0;i<n;i++)
        {
            tot+=in.nextInt();
        }
        int m=in.nextInt();

        int l,r;

        for(int i=0;i<m-1;i++)
        {
            l=in.nextInt();
            r=in.nextInt();

        }
        l=in.nextInt();
        r=in.nextInt();

        if(tot<=r)
            System.out.println(tot);
        else
            System.out.println(-1);
    }
}


package goyal.test.codeforces.educational22;

import java.util.Scanner;

public class PracticeSolutions {
	public static void main(String args[] ) throws Exception {
        Scanner in = new Scanner(System.in);
		int num = Integer.parseInt(in.nextLine());
		String times = in.nextLine();
		int timeNeeded = 0;
		for (int i = 0; i < times.split(" ").length; i++) {
			timeNeeded += Integer.parseInt(times.split(" ")[i]);
		}
		int m = Integer.parseInt(in.nextLine());
		for (int i=0; i < m; i++) {
			String range = in.nextLine();
			int l = Integer.parseInt(range.split(" ")[0]);
			int r = Integer.parseInt(range.split(" ")[1]);
			if (l <= timeNeeded && r >= timeNeeded) {
				System.out.println(timeNeeded);
				return;
			} else if (l > timeNeeded) {
				System.out.println(l);
				return;
			}
		}
		System.out.println("-1");
    }
}


import java.util.Scanner;

public class PracticeSolutions {
	public static void main(String args[] ) throws Exception {
        Scanner in = new Scanner(System.in);
		int num = Integer.parseInt(in.nextLine());
		String times = in.nextLine();
		int timeNeeded = 0;
		for (int i = 0; i < times.split(" ").length; i++) {
			timeNeeded += Integer.parseInt(times.split(" ")[i]);
		}
		int m = Integer.parseInt(in.nextLine());
		for (int i=0; i < m; i++) {
			String range = in.nextLine();
			int l = Integer.parseInt(range.split(" ")[0]);
			int r = Integer.parseInt(range.split(" ")[1]);
			if (l <= timeNeeded && r >= timeNeeded) {
				System.out.println(timeNeeded);
				return;
			} else if (l > timeNeeded) {
				System.out.println(l);
				return;
			}
		}
		System.out.println("-1");
    }
}


import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;

public class TestClass implements Runnable
{
	public static void main(String args[])
	{
		new Thread(null, new TestClass(),"TESTCLASS",1<<26).start();
	}
	public void run()
	{
		//Scanner scan=new Scanner(System.in);
		InputReader hb=new InputReader(System.in);
		PrintWriter w=new PrintWriter(System.out);
		int n=hb.nextInt();
		long sum=0;
		for(int i=0;i<n;i++)
		{
			int a=hb.nextInt();
			sum+=a;
		}
		int t=hb.nextInt();
		int flag=0;
		for(int i=0;i<t;i++)
		{
			int l=hb.nextInt();
			int r=hb.nextInt();
			if(sum<l)
			{
				sum=l;
				flag=1;
				break;
			}
			else if(sum<=r)
			{
				flag=1;
				break;
			}
		}
		if(flag==1)
			w.print(sum);
		else
			w.print("-1");
		w.close();
	}

	private void shuffle(int[] arr)
	{
		Random ran = new Random();
		for (int i = 0; i < arr.length; i++) {
			int i1 = ran.nextInt(arr.length);
			int i2 = ran.nextInt(arr.length);

			int temp = arr[i1];
			arr[i1] = arr[i2];
			arr[i2] = temp;
		}
	}
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;

		public InputReader(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 String nextLine()
		{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		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 long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long 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 double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

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

	static class Pair implements Comparable<Pair>
	{
		int a;
		int b;
		String str;
		public Pair(int a,int b)
		{
			this.a=a;
			this.b=b;
			str=min(a,b)+" "+max(a,b);
		}

		public int compareTo(Pair pair)
		{
			if(Integer.compare(a,pair.a)==0)
				return Integer.compare(b,pair.b);

			return Integer.compare(a,pair.a);
		}
	}

}


import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;

public class TestClass implements Runnable
{
	public static void main(String args[])
	{
		new Thread(null, new TestClass(),"TESTCLASS",1<<26).start();
	}
	public void run()
	{
		//Scanner scan=new Scanner(System.in);
		InputReader hb=new InputReader(System.in);
		PrintWriter w=new PrintWriter(System.out);
		int n=hb.nextInt();
		long sum=0;
		for(int i=0;i<n;i++)
		{
			int a=hb.nextInt();
			sum+=a;
		}
		int t=hb.nextInt();
		int flag=0;
		for(int i=0;i<t;i++)
		{
			int l=hb.nextInt();
			int r=hb.nextInt();
			if(sum>=l && sum<=r)
			{
				flag=1;
				break;
			}
		}
		if(flag==1)
			w.print(sum);
		else
			w.print("-1");
		w.close();
	}

	private void shuffle(int[] arr)
	{
		Random ran = new Random();
		for (int i = 0; i < arr.length; i++) {
			int i1 = ran.nextInt(arr.length);
			int i2 = ran.nextInt(arr.length);

			int temp = arr[i1];
			arr[i1] = arr[i2];
			arr[i2] = temp;
		}
	}
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;

		public InputReader(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 String nextLine()
		{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		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 long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long 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 double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

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

	static class Pair implements Comparable<Pair>
	{
		int a;
		int b;
		String str;
		public Pair(int a,int b)
		{
			this.a=a;
			this.b=b;
			str=min(a,b)+" "+max(a,b);
		}

		public int compareTo(Pair pair)
		{
			if(Integer.compare(a,pair.a)==0)
				return Integer.compare(b,pair.b);

			return Integer.compare(a,pair.a);
		}
	}

}


import java.io.*;
import java.util.*;
import java.lang.*;
public class a
{
    public static void main(String[] args) throws java.io.IOException
    {
	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
	int n=Integer.parseInt(a.readLine());
	String[] b=a.readLine().split(" ");
	int sum=0;
	for(int i=0;i<n;i++)
	    sum+=Integer.parseInt(b[i]);
	System.out.println(sum);
	int m=Integer.parseInt(a.readLine());

	int output=-1;
	for(int j=0;j<m;j++)
	{
	    String[] c=a.readLine().split(" ");
	    int start=Integer.parseInt(c[0]);
	    int end=Integer.parseInt(c[1]);
	    if(start<=sum && end>=sum)
	    {
		output=sum;
		break;
	    }
	    if(start>sum)
	    {
		output=start;
		break;
	    }
	}
	System.out.println(output);
    }
}