#include <cstdio>
#include <deque>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#include <algorithm>
#include <cmath>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifndef LOCAL
#define cerr if(0)cerr
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long double ld;

const int INF = 1000001000;
const long long INFll = 1ll * INF * INF;
const long double ldINF = 1e+018;
const long double EPS = 0.000000001;
const int sdx[4] = {-1, 0, 1, 0};
const int sdy[4] = {0, 1, 0, -1};

template<typename T>
ostream& operator<<(ostream& out, pair<T, T>& a) {
    out << a.F << " " << a.S;
    return out;
}

template<typename T>
istream& operator>>(istream& in, pair<T, T>& a) {
    in >> a.F >> a.S;
    return in;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for (int i = 0; i < a.size(); ++i)
        in >> a[i];
    return in;
}

template<typename T>
ostream& operator<<(ostream& out, vector<T> a) {
    for (int i = 0; i < a.size(); ++i)
        if (i == a.size() - 1)
            out << a[i];
        else
            out << a[i] << " ";
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector <int> a(n);
    cin >> a;
    sort(a.begin(), a.end());
    ll ans = 0;
    for (int i = 0, j = n - 1; i < n; i++, j--)
        ans += ((1ll * a[i] * a[j]) % 10007ll);
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <deque>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#include <algorithm>
#include <cmath>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifndef LOCAL
#define cerr if(0)cerr
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long double ld;

const int INF = 1000001000;
const long long INFll = 1ll * INF * INF;
const long double ldINF = 1e+018;
const long double EPS = 0.000000001;
const int sdx[4] = {-1, 0, 1, 0};
const int sdy[4] = {0, 1, 0, -1};

template<typename T>
ostream& operator<<(ostream& out, pair<T, T>& a) {
    out << a.F << " " << a.S;
    return out;
}

template<typename T>
istream& operator>>(istream& in, pair<T, T>& a) {
    in >> a.F >> a.S;
    return in;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for (int i = 0; i < a.size(); ++i)
        in >> a[i];
    return in;
}

template<typename T>
ostream& operator<<(ostream& out, vector<T> a) {
    for (int i = 0; i < a.size(); ++i)
        if (i == a.size() - 1)
            out << a[i];
        else
            out << a[i] << " ";
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector <int> a(n);
    cin >> a;
    sort(a.begin(), a.end());
    ll ans = 0;
    for (int i = 0, j = n - 1; i < n; i++, j--)
        ans += ((a[i] * a[j]) % 10007);
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const long long int REM = 10007;

long long int n , res = 0;
long long int a[MAXN], b[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++){
    cin >> a[i];
    a[i] %= REM;
  }
  sort(a , a+n);
  for(int i = 0 ; i < n ; i++)
    b[i] = (a[i] * a[n-i-1]) % REM;
  for(int i = 0 ; i < n ; i++){
    res += b[i];
    res %= REM;
  }
  cout << res % REM << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const long long int REM = 10007;

long long int n , res = 0;
long long int a[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++){
    cin >> a[i];
    a[i] %= REM;
  }

  sort(a , a+n);
  for(int i = 0 ; i < n ; i++){
    res += ((a[i] * a[n-i-1]) % REM);
    res %= REM;
  }

  cout << res << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const int REM = 10007;

int n , res = 0;
int a[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++){
    cin >> a[i];
    a[i] %= REM;
  }
  sort(a , a+n);
  for(int i = 0 ; i < n ; i++)
    res += (a[i] * a[n-i-1]);
  cout << res % REM << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const int REM = 10007;

int n , res = 0;
int a[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++){
    cin >> a[i];
    a[i] %= REM;
  }
  sort(a , a+n);
  for(int i = 0 ; i < n ; i++){
    res += (a[i] * a[n-i-1]);
    res %= REM;
  }
  cout << res % REM << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const long long int REM = 10007;

long long int n , res = 0;
long long int a[MAXN];

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

  sort(a , a+n);
  for(int i = 0 ; i < n ; i++){
    res += (a[i] * a[n-i-1]);
    res %= REM;
  }

  cout << res << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const int REM = 10007;

long long int n , res = 0;
long long int a[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++){
    cin >> a[i];
    a[i] %= REM;
  }
  sort(a , a+n);
  for(int i = 0 ; i < n ; i++){
    res += ((a[i] * a[n-i-1]) % REM);
    res %= REM;
  }
  cout << res % REM << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;
const int REM = 10007;

int n , res = 0;
int a[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++)
    cin >> a[i];
  sort(a , a+n);
  for(int i = 0 ; i < n ; i++)
    res += (a[i] * a[n-i-1]);
  cout << res % REM << endl;

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;

int n , res = 0;
int a[MAXN];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; i++)
    cin >> a[i];
  sort(a , a+n);
  for(int i = 0 ; i < n ; i++)
    res += (a[i] * a[n-i-1]);
  cout << res << endl;

  return 0;
}


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

int main()
{
    long long  i,n;
    cin>>n;
    long long arra[1000001];
    for(i=0;i<n;i++)
    {
        cin>>arra[i];

    }
    sort(arra,arra+n);
    long long sum=0;

    for(i=0;i<n;i++)
    {
        sum+=arra[i]*arra[n-i-1];
    }

    cout<<(sum%10007);
    return 0;
}


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

int main()
{
    int i,n;
    cin>>n;
    int arra[n];
    for(i=0;i<n;i++)
    {
        cin>>arra[i];

    }
    sort(arra,arra+n);
    long long int sum=0;

    for(i=0;i<n;i++)
    {
        sum+=arra[i]*arra[n-i-1];
    }

    cout<<(sum%10007);
    return 0;
}


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

int main()
{
    int i,n;
    cin>>n;
    int arra[n],arrb[n];
    for(i=0;i<n;i++)
    {
        cin>>arra[i];
        arrb[i]=arra[i];
    }
    sort(arrb,arrb+n);
    long int sum=0;

    for(i=0;i<n;i++)
    {
        sum+=arra[i]*arrb[i];
    }

    cout<<(sum%10007);
    return 0;
}


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

int main()
{
    long int i,n;
    cin>>n;
    long int arra[n];
    for(i=0;i<n;i++)
    {
        cin>>arra[i];

    }
    sort(arra,arra+n);
    long long int sum=0;

    for(i=0;i<n;i++)
    {
        sum+=arra[i]*arra[n-i-1];
    }

    cout<<(sum%10007);
    return 0;
}


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

int main()
{
    int i,n;
    cin>>n;
    int arra[n],arrb[n];
    for(i=0;i<n;i++)
    {
        cin>>arra[i];
        arrb[i]=arra[i];
    }
    sort(arrb,arrb+n);
    sort(arra,arra+n);
    long int sum=0;

    for(i=0;i<n;i++)
    {
        sum+=arra[i]*arrb[n-i-1];
    }

    cout<<(sum%10007);
    return 0;
}


#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
		int n;

		cin >> n;

		multiset<int> input;

		for(int i=0; i<n; i++)
		{
			int in;
			cin >> in;
			input.insert(in);

		}

		auto it = input.begin();
        auto rev_it = input.rbegin();

        long long int ans = 0;

        while(it != input.end())
            {
            ans += ((*it) * 1LL* (*rev_it));

            ans %= 10007;
            it++;
            rev_it++;
        }

		cout << ans << endl;

	return 0;
}


#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
		int n;

		cin >> n;

		set<int> input;

		for(int i=0; i<n; i++)
		{
			int in;
			cin >> in;
			input.insert(in);

		}

		auto it = input.begin();
        auto rev_it = input.rbegin();

        long long int ans = 0;

        while(it != input.end())
            {
            ans += ((*it) * (*rev_it));

            it++;
            rev_it++;
        }

		cout << ans % 10007 << endl;

	return 0;
}


#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
		int n;

		cin >> n;

		set<int> input;

		for(int i=0; i<n; i++)
		{
			int in;
			cin >> in;
			input.insert(in);

		}

		auto it = input.begin();
        auto rev_it = input.rbegin();

        long long int ans = 0;

        while(it != input.end())
            {
            ans += ((*it) * 1LL* (*rev_it));

            ans %= 10007;
            it++;
            rev_it++;
        }

		cout << ans << endl;

	return 0;
}


#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
		int n;

		cin >> n;

		set<int> input;

		for(int i=0; i<n; i++)
		{
			int in;
			cin >> in;
			input.insert(in);

		}

		auto it = input.begin();
        auto rev_it = input.rbegin();

        long long int ans = 0;

        while(it != input.end())
            {
            ans += ((*it) * (*rev_it));

            ans %= 10007;
            it++;
            rev_it++;
        }

		cout << ans << endl;

	return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;

#define fill(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const ll INF = 1e18;
const int mod = 1e4+7;
const int N = 1e5+10;
int a[N];
int main(){
  fast;int n;cin >> n;
  FOR(i,0,n-1)cin >> a[i];
  ll ans = 0;
  FOR(i,0,n-1){
  	ans += (1LL * a[i] * a[n-1-i])% mod;
  	if(ans >= mod)ans -= mod;
  }
  cout << ans << "\n";
  return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;

#define fill(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const ll INF = 1e18;
const int mod = 1e4+7;
const int N = 1e5+10;
int a[N];
int main(){
  fast;int n;cin >> n;
  FOR(i,0,n-1)cin >> a[i];
  ll ans = 0;
  FOR(i,0,n-1){
  	ans += (1LL * a[i] * a[n-1-i])% mod;
  }
  cout << ans << "\n";
  return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;

#define fill(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const ll INF = 1e18;
const int mod = 1e4+7;
const int N = 1e5+10;
ll a[N];
int main(){
  fast;int n;cin >> n;
  FOR(i,0,n-1)cin >> a[i];
  ll ans = 0;
  FOR(i,0,n-1){
  	ans += (1LL * a[i] * a[n-1-i])% mod;
  	if(ans >= mod)ans -= mod;
  }
  cout << ans%mod << "\n";
  return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;

#define fill(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const ll INF = 1e18;
const int mod = 1e4+7;
const int N = 1e5+10;
ll a[N];
int main(){
  fast;int n;cin >> n;
  FOR(i,0,n-1)cin >> a[i];
  ll ans = 0;sort(a,a+n);
  FOR(i,0,n-1){
  	ans += (1LL * a[i] * a[n-1-i])% mod;
  	if(ans >= mod)ans -= mod;
  }
  cout << ans%mod << "\n";
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL      long long
#define LD      long double
#define PB		push_back
#define LOOP(i,n)  for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
const int mod = 10007;

int main()
{
	int n;
	cin>>n;
	LL ans=0;
	int a[n+1];
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	int pos=n;
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+a[i]*a[pos]);
		ans%=mod;
		pos--;
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL      long long
#define LD      long double
#define PB		push_back
#define LOOP(i,n)  for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
const int mod = 10007;

int main()
{
	LL n;
	cin>>n;
	LL ans=0;
	LL a[n+2];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	int pos=n;
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		ans+=a[i]*a[pos];
		ans%=mod;
		pos--;
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL      long long
#define LD      long double
#define PB		push_back
#define LOOP(i,n)  for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
const int mod = 10007;

int main()
{
	int n;
	cin>>n;
	LL ans=0;
	int a[n+1];
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	int pos=n;
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+a[i]*a[pos])%mod;
		pos--;
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL      long long
#define LD      long double
#define PB		push_back
#define LOOP(i,n)  for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
const int mod = 10007;

int main()
{
	LL n;
	cin>>n;
	LL ans=0;
	int a[n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	int pos=n;
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		ans+=a[i]*a[pos];
		ans%=mod;
		pos--;
	}
	cout<<ans;
	return 0;
}


/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم**/

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<fstream>
#include<numeric>
#include<iterator>

using namespace std;

#define MXN 100009
#define MXE
#define MXQ
#define MOD
#define INF
#define lli long long int
#define lu unsigned long
#define llu unsigned long long int
#define PI acos(0.0)
#define pb push_back
#define ppb pop_back
#define pfs printf("*")
#define nl printf("\n")
#define pf1 printf("%d",x)
#define pf2 printf("%d %d\n",x,y)
#define pf3 printf("%d %d %d\n",x,y,z)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sf1(a) scanf("%d",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf1ll(a) scanf("%lld",&a)
#define sf2ll(a,b) scanf("%lld %lld",&a,&b)
#define takell(a) scanf("%I64d", &a)
#define rep1(i,n) for(int i=0;i<n;i++)
#define rep2(i,n) for(int i=1;i<=n;i++)
#define rep3(n,i) for(int i=n;i>=0;i--)
#define rep(a,n) for(int i=a;i<=n;i++)

/***************************** MAIN PROGRAM STARTS HARE ***************************/
lli arr[MXN],arr1[MXN];
int main()
{
    lli n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        arr1[i]=arr[i];
    }
    reverse(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        lli a;
        a=((arr[i]%10007)*(arr1[i]%1007))&10007;
        sum=sum+a;

    }
    cout<<sum<<endl;

    return 0;
}


/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم**/

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<fstream>
#include<numeric>
#include<iterator>

using namespace std;

#define MXN 100009
#define MXE
#define MXQ
#define MOD
#define INF
#define lli long long int
#define lu unsigned long
#define llu unsigned long long int
#define PI acos(0.0)
#define pb push_back
#define ppb pop_back
#define pfs printf("*")
#define nl printf("\n")
#define pf1 printf("%d",x)
#define pf2 printf("%d %d\n",x,y)
#define pf3 printf("%d %d %d\n",x,y,z)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sf1(a) scanf("%d",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf1ll(a) scanf("%lld",&a)
#define sf2ll(a,b) scanf("%lld %lld",&a,&b)
#define takell(a) scanf("%I64d", &a)
#define rep1(i,n) for(int i=0;i<n;i++)
#define rep2(i,n) for(int i=1;i<=n;i++)
#define rep3(n,i) for(int i=n;i>=0;i--)
#define rep(a,n) for(int i=a;i<=n;i++)

/***************************** MAIN PROGRAM STARTS HARE ***************************/
lli arr[MXN],arr1[MXN];
int main()
{
    lli n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        arr1[i]=arr[i];
    }
    reverse(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        lli a;
        a=(((arr[i]%10007)%10007)*((arr1[i]%1007)%1007))%10007;
        sum=sum+a;

    }
    cout<<sum<<endl;

    return 0;
}


/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم**/

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<fstream>
#include<numeric>
#include<iterator>

using namespace std;

#define MXN 100009
#define MXE
#define MXQ
#define MOD
#define INF
#define lli long long int
#define lu unsigned long
#define llu unsigned long long int
#define PI acos(0.0)
#define pb push_back
#define ppb pop_back
#define pfs printf("*")
#define nl printf("\n")
#define pf1 printf("%d",x)
#define pf2 printf("%d %d\n",x,y)
#define pf3 printf("%d %d %d\n",x,y,z)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sf1(a) scanf("%d",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf1ll(a) scanf("%lld",&a)
#define sf2ll(a,b) scanf("%lld %lld",&a,&b)
#define takell(a) scanf("%I64d", &a)
#define rep1(i,n) for(int i=0;i<n;i++)
#define rep2(i,n) for(int i=1;i<=n;i++)
#define rep3(n,i) for(int i=n;i>=0;i--)
#define rep(a,n) for(int i=a;i<=n;i++)

/***************************** MAIN PROGRAM STARTS HARE ***************************/
lli arr[MXN],arr1[MXN];
int main()
{
    lli n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        arr1[i]=arr[i];
    }
    reverse(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        lli a;
        sum=(sum+(arr[i]*arr1[i]))%10007;

    }
    cout<<sum<<endl;

    return 0;
}


/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم**/

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<fstream>
#include<numeric>
#include<iterator>

using namespace std;

#define MXN 100009
#define MXE
#define MXQ
#define MOD
#define INF
#define lli long long int
#define lu unsigned long
#define llu unsigned long long int
#define PI acos(0.0)
#define pb push_back
#define ppb pop_back
#define pfs printf("*")
#define nl printf("\n")
#define pf1 printf("%d",x)
#define pf2 printf("%d %d\n",x,y)
#define pf3 printf("%d %d %d\n",x,y,z)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sf1(a) scanf("%d",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf1ll(a) scanf("%lld",&a)
#define sf2ll(a,b) scanf("%lld %lld",&a,&b)
#define takell(a) scanf("%I64d", &a)
#define rep1(i,n) for(int i=0;i<n;i++)
#define rep2(i,n) for(int i=1;i<=n;i++)
#define rep3(n,i) for(int i=n;i>=0;i--)
#define rep(a,n) for(int i=a;i<=n;i++)

/***************************** MAIN PROGRAM STARTS HARE ***************************/
lli arr[MXN],arr1[MXN];
int main()
{
    lli n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        arr1[i]=arr[i];
    }
    reverse(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        lli a;
        a=(arr[i]%10007*arr1[i]%10007)%10007;
        sum=sum+a;

    }
    cout<<sum<<endl;

    return 0;
}


/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم**/

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<fstream>
#include<numeric>
#include<iterator>

using namespace std;

#define MXN 100009
#define MXE
#define MXQ
#define MOD
#define INF
#define lli long long int
#define lu unsigned long
#define llu unsigned long long int
#define PI acos(0.0)
#define pb push_back
#define ppb pop_back
#define pfs printf("*")
#define nl printf("\n")
#define pf1 printf("%d",x)
#define pf2 printf("%d %d\n",x,y)
#define pf3 printf("%d %d %d\n",x,y,z)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sf1(a) scanf("%d",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf1ll(a) scanf("%lld",&a)
#define sf2ll(a,b) scanf("%lld %lld",&a,&b)
#define takell(a) scanf("%I64d", &a)
#define rep1(i,n) for(int i=0;i<n;i++)
#define rep2(i,n) for(int i=1;i<=n;i++)
#define rep3(n,i) for(int i=n;i>=0;i--)
#define rep(a,n) for(int i=a;i<=n;i++)

/***************************** MAIN PROGRAM STARTS HARE ***************************/
lli arr[MXN],arr1[MXN];
int main()
{
    lli n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        arr1[i]=arr[i];
    }
    reverse(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        lli a;
        a=(((arr[i]%10007)%10007)*((arr1[i]%10007)%10007))%10007;
        sum=sum+a;

    }
    cout<<sum<<endl;

    return 0;
}


/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم**/

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<fstream>
#include<numeric>
#include<iterator>

using namespace std;

#define MXN 100009
#define MXE
#define MXQ
#define MOD
#define INF
#define lli long long int
#define lu unsigned long
#define llu unsigned long long int
#define PI acos(0.0)
#define pb push_back
#define ppb pop_back
#define pfs printf("*")
#define nl printf("\n")
#define pf1 printf("%d",x)
#define pf2 printf("%d %d\n",x,y)
#define pf3 printf("%d %d %d\n",x,y,z)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sf1(a) scanf("%d",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf1ll(a) scanf("%lld",&a)
#define sf2ll(a,b) scanf("%lld %lld",&a,&b)
#define takell(a) scanf("%I64d", &a)
#define rep1(i,n) for(int i=0;i<n;i++)
#define rep2(i,n) for(int i=1;i<=n;i++)
#define rep3(n,i) for(int i=n;i>=0;i--)
#define rep(a,n) for(int i=a;i<=n;i++)

/***************************** MAIN PROGRAM STARTS HARE ***************************/
lli arr[MXN],arr1[MXN];
int main()
{
    lli n,sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        arr1[i]=arr[i];
    }
    reverse(arr,arr+n);

    for(int i=0;i<n;i++)
    {
        lli a;
        a=(arr[i]*arr1[i])%10007;
        sum=sum+a;

    }
    cout<<sum<<endl;

    return 0;
}


/**========================================================**/
/**  In the name of Allah , most Gracious , most Merciful  **/
/**========================================================**/

                 /**=======================**/
                 /** RKSAZID,CSE,KUET,2K14 **/
                 /**=======================**/

#include<bits/stdc++.h>

/** ===================== <Sort-Cut> ===================== **/

#define ll            long long int
#define db            cout<<"Allahu Akbar"<<endl
#define pb(a)         push_back(a)
#define sf(t)         scanf("%d",&t)
#define sff(a,b)      scanf("%d %d",&a,&b)
#define sfff(a,b,c)   scanf("%d %d %d",&a,&b,&c)
#define sfll(a)       scanf("%lld",&a)
#define pf            printf
#define sc            scanf
#define uu            first
#define vv            second
#define mod           1000000007
#define CAS           int t;sf(t)
#define Cloop         for(int M = 1;M<=t;M++)
#define Cprint        pf("Case %d: ",M)
#define all(a)        a.begin(),a.end()
#define ms(a,b)       memset(a, b, sizeof(a))
#define REP(O,a,n)    for(int O = a ; O<n ; O++)
#define CIN           ios_base::sync_with_stdio(0); cin.tie(0)
#define pi            2*acos(0.0)
#define insin(a,b)    asin(b/(double)a)
#define intan(a,b)    atan(b/(double)a)
#define Mx            100000
using namespace std;

/** ==================== <Main Code> ==================== **/

vector<int>v,v2(100005);

int main(){
    int n;
    int k;
    ll sum = 0;
    sf(n);
    REP(j,0,n){
        sf(k);
        v.pb(k);
    }
    sort(all(v));
    int f = n-1;
   // for(int i = 0;i<n;i++)cout<<v[i]<< " ";
    REP(i,0,n){
        sum+=  (v[i]*v[f--])%10007;
      //  cout<<v2[i]<<endl;
    }
   // REP(k,0,n)sum = (sum+v2[k])%10007;
    pf("%I64d\n",sum%10007);
    return 0;
}


/**========================================================**/
/**  In the name of Allah , most Gracious , most Merciful  **/
/**========================================================**/

                 /**=======================**/
                 /** RKSAZID,CSE,KUET,2K14 **/
                 /**=======================**/

#include<bits/stdc++.h>

/** ===================== <Sort-Cut> ===================== **/

#define ll            long long int
#define db            cout<<"Allahu Akbar"<<endl
#define pb(a)         push_back(a)
#define sf(t)         scanf("%d",&t)
#define sff(a,b)      scanf("%d %d",&a,&b)
#define sfff(a,b,c)   scanf("%d %d %d",&a,&b,&c)
#define sfll(a)       scanf("%lld",&a)
#define pf            printf
#define sc            scanf
#define uu            first
#define vv            second
#define mod           1000000007
#define CAS           int t;sf(t)
#define Cloop         for(int M = 1;M<=t;M++)
#define Cprint        pf("Case %d: ",M)
#define all(a)        a.begin(),a.end()
#define ms(a,b)       memset(a, b, sizeof(a))
#define REP(O,a,n)    for(int O = a ; O<n ; O++)
#define CIN           ios_base::sync_with_stdio(0); cin.tie(0)
#define pi            2*acos(0.0)
#define insin(a,b)    asin(b/(double)a)
#define intan(a,b)    atan(b/(double)a)
#define Mx            100000
using namespace std;

/** ==================== <Main Code> ==================== **/

vector<int>v,v2(100005);

int main(){
    int n;
    int k;
    ll sum = 0;
    sf(n);
    REP(j,0,n){
        sf(k);
        v.pb(k);
    }
    sort(all(v));
   // for(int i = 0;i<n;i++)cout<<v[i]<< " ";
    REP(i,0,n){
        sum+=  (v[i]*v[n-i-1])%10007;
      //  cout<<v2[i]<<endl;
    }
   // REP(k,0,n)sum = (sum+v2[k])%10007;
    pf("%d\n",sum%10007);
    return 0;
}


/**========================================================**/
/**  In the name of Allah , most Gracious , most Merciful  **/
/**========================================================**/

                 /**=======================**/
                 /** RKSAZID,CSE,KUET,2K14 **/
                 /**=======================**/

#include<bits/stdc++.h>

/** ===================== <Sort-Cut> ===================== **/

#define ll            long long int
#define db            cout<<"Allahu Akbar"<<endl
#define pb(a)         push_back(a)
#define sf(t)         scanf("%d",&t)
#define sff(a,b)      scanf("%d %d",&a,&b)
#define sfff(a,b,c)   scanf("%d %d %d",&a,&b,&c)
#define sfll(a)       scanf("%lld",&a)
#define pf            printf
#define sc            scanf
#define uu            first
#define vv            second
#define mod           1000000007
#define CAS           int t;sf(t)
#define Cloop         for(int M = 1;M<=t;M++)
#define Cprint        pf("Case %d: ",M)
#define all(a)        a.begin(),a.end()
#define ms(a,b)       memset(a, b, sizeof(a))
#define REP(O,a,n)    for(int O = a ; O<n ; O++)
#define CIN           ios_base::sync_with_stdio(0); cin.tie(0)
#define pi            2*acos(0.0)
#define insin(a,b)    asin(b/(double)a)
#define intan(a,b)    atan(b/(double)a)
#define Mx            100000
using namespace std;

/** ==================== <Main Code> ==================== **/

vector<int>v,v2(100005);

int main(){
    int n;
    int k;
    ll sum = 0;
    sf(n);
    REP(j,0,n){
        sf(k);
        v.pb(k);
    }
    sort(all(v));
   // for(int i = 0;i<n;i++)cout<<v[i]<< " ";
    REP(i,0,n){
        v2[i] = (v[i]*v[n-i-1])%10007;
      //  cout<<v2[i]<<endl;
    }
    REP(k,0,n)sum = (sum+v2[k])%10007;
    pf("%d\n",sum%10007);
    return 0;
}


/**========================================================**/
/**  In the name of Allah , most Gracious , most Merciful  **/
/**========================================================**/

                 /**=======================**/
                 /** RKSAZID,CSE,KUET,2K14 **/
                 /**=======================**/

#include<bits/stdc++.h>

/** ===================== <Sort-Cut> ===================== **/

#define ll            long long int
#define db            cout<<"Allahu Akbar"<<endl
#define pb(a)         push_back(a)
#define sf(t)         scanf("%d",&t)
#define sff(a,b)      scanf("%d %d",&a,&b)
#define sfff(a,b,c)   scanf("%d %d %d",&a,&b,&c)
#define sfll(a)       scanf("%lld",&a)
#define pf            printf
#define sc            scanf
#define uu            first
#define vv            second
#define mod           1000000007
#define CAS           int t;sf(t)
#define Cloop         for(int M = 1;M<=t;M++)
#define Cprint        pf("Case %d: ",M)
#define all(a)        a.begin(),a.end()
#define ms(a,b)       memset(a, b, sizeof(a))
#define REP(O,a,n)    for(int O = a ; O<n ; O++)
#define CIN           ios_base::sync_with_stdio(0); cin.tie(0)
#define pi            2*acos(0.0)
#define insin(a,b)    asin(b/(double)a)
#define intan(a,b)    atan(b/(double)a)
#define Mx            100000
using namespace std;

/** ==================== <Main Code> ==================== **/

vector<int>v,v2(100005);

int main(){
    int n;
    int k;
    ll sum = 0;
    sf(n);
    REP(j,0,n){
        sf(k);
        v.pb(k);
    }
    sort(all(v));
   // for(int i = 0;i<n;i++)cout<<v[i]<< " ";
    REP(i,0,n){
        v2[i] = v[i]*v[n-i-1];
      //  cout<<v2[i]<<endl;
    }
    REP(k,0,n)sum = (sum+v2[k])%10007;
    pf("%d\n",sum%10007);
    return 0;
}


/**========================================================**/
/**  In the name of Allah , most Gracious , most Merciful  **/
/**========================================================**/

                 /**=======================**/
                 /** RKSAZID,CSE,KUET,2K14 **/
                 /**=======================**/

#include<bits/stdc++.h>

/** ===================== <Sort-Cut> ===================== **/

#define ll            long long int
#define db            cout<<"Allahu Akbar"<<endl
#define pb(a)         push_back(a)
#define sf(t)         scanf("%d",&t)
#define sff(a,b)      scanf("%d %d",&a,&b)
#define sfff(a,b,c)   scanf("%d %d %d",&a,&b,&c)
#define sfll(a)       scanf("%lld",&a)
#define pf            printf
#define sc            scanf
#define uu            first
#define vv            second
#define mod           1000000007
#define CAS           int t;sf(t)
#define Cloop         for(int M = 1;M<=t;M++)
#define Cprint        pf("Case %d: ",M)
#define all(a)        a.begin(),a.end()
#define ms(a,b)       memset(a, b, sizeof(a))
#define REP(O,a,n)    for(int O = a ; O<n ; O++)
#define CIN           ios_base::sync_with_stdio(0); cin.tie(0)
#define pi            2*acos(0.0)
#define insin(a,b)    asin(b/(double)a)
#define intan(a,b)    atan(b/(double)a)
#define Mx            100000
using namespace std;

/** ==================== <Main Code> ==================== **/

vector<int>v,v2(100005);

int main(){
    int n;
    int k;
    int sum = 0;
    sf(n);
    REP(j,0,n){
        sf(k);
        v.pb(k);
    }
    sort(all(v));
   // for(int i = 0;i<n;i++)cout<<v[i]<< " ";
    REP(i,0,n){
        v2[i] = v[i]*v[n-i-1];
      //  cout<<v2[i]<<endl;
    }
    REP(k,0,n)sum = (sum)%10007+v2[k];
    pf("%d\n",sum%10007);
    return 0;
}


/**========================================================**/
/**  In the name of Allah , most Gracious , most Merciful  **/
/**========================================================**/

                 /**=======================**/
                 /** RKSAZID,CSE,KUET,2K14 **/
                 /**=======================**/

#include<bits/stdc++.h>

/** ===================== <Sort-Cut> ===================== **/

#define ll            long long int
#define db            cout<<"Allahu Akbar"<<endl
#define pb(a)         push_back(a)
#define sf(t)         scanf("%d",&t)
#define sff(a,b)      scanf("%d %d",&a,&b)
#define sfff(a,b,c)   scanf("%d %d %d",&a,&b,&c)
#define sfll(a)       scanf("%lld",&a)
#define pf            printf
#define sc            scanf
#define uu            first
#define vv            second
#define mod           1000000007
#define CAS           int t;sf(t)
#define Cloop         for(int M = 1;M<=t;M++)
#define Cprint        pf("Case %d: ",M)
#define all(a)        a.begin(),a.end()
#define ms(a,b)       memset(a, b, sizeof(a))
#define REP(O,a,n)    for(int O = a ; O<n ; O++)
#define CIN           ios_base::sync_with_stdio(0); cin.tie(0)
#define pi            2*acos(0.0)
#define insin(a,b)    asin(b/(double)a)
#define intan(a,b)    atan(b/(double)a)
#define Mx            100000
using namespace std;

/** ==================== <Main Code> ==================== **/

vector<ll>v;

int main(){
    int n;
    int k;
    ll sum = 0;
    sf(n);
    REP(j,0,n){
        sf(k);
        v.pb(k);
    }
    sort(all(v));
    int f = n-1;
   // for(int i = 0;i<n;i++)cout<<v[i]<< " ";
    REP(i,0,n){
        sum+=  (v[i]*v[f--])%10007;
      //  cout<<v2[i]<<endl;
    }
   // REP(k,0,n)sum = (sum+v2[k])%10007;
    pf("%I64d\n",sum%10007);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	for(ll i=0; i<n; i++)
		res+=a[i]*a[n-1-i], res%=10007;
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	for(ll i=0; i<n; i++)
		res+=max(a[i],a[n-1-i]),res%=MOD;
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	for(ll i=0; i<n; i++)
		res+=max(a[i],a[n-1-i]),res/=MOD;
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	for(ll i=0; i<n; i++)
		res+=a[i]*a[n-1-i],res%=MOD;
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	if(n%2!=0)
		res+=a[n-1],n--;
	for(ll i=0; i<n; i+=2)
		res+=max(a[i],a[i+1])*2,res%=MOD;
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	for(ll i=0; i<n; i++)
		res+=max(a[i],a[n-1-i]);
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=1; i<=n; i++)
		cin>>a[i],res=max(res,a[i]*i);
	cout<<res%MOD<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	for(ll i=0; i<n; i++)
		res+=a[i]*a[n-1-i];
	cout<<res<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int N = 101;
const int MOD = 1e9 + 7;
const ll INF = ll(1e18);
ll n,a[100005],res;
int main(){
	cin>>n;
	for(ll i=0; i<n; i++)
		cin>>a[i];
	sort(a,a+n);
	cout<<(a[n-1]+a[n-1])%MOD<<endl;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10007

using namespace std;

ll a[111111],b[111111];

bool cmp(ll x,ll y){
    if(x>y)return true;
    else return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,i,ans=0;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(a,a+n);
    sort(b,b+n,cmp);
    for(i=0;i<n;i++){
        ans=((ans%mod)+((a[i]%mod)*(b[i]%mod))%mod)%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10007

using namespace std;

ll a[11111],b[11111];

bool cmp(ll x,ll y){
    if(x>y)return true;
    else return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,i,ans=0;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(a,a+n);
    sort(b,b+n,cmp);
    for(i=0;i<n;i++){
        ans=((ans%mod)+((a[i]%mod)*(b[i]%mod))%mod)%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10007

using namespace std;

ll a[11111],b[11111];

bool cmp(ll x,ll y){
    if(x>y)return true;
    else return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,i,ans=0;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b,b+n,cmp);
    for(i=0;i<n;i++){
        ans=((ans%mod)+((a[i]%mod)*(b[i]%mod))%mod)%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 10007

using namespace std;

ll a[11111],b[11111];

bool cmp(ll x,ll y){
    if(x>y)return true;
    else return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,i,ans=0;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b,b+n,cmp);
    ll k=(n/2);
    if(n%2==0){
        k--;
    }
    for(i=0;i<=k;i++){
        ans=((ans%mod)+((a[i]%mod)*(b[i]%mod))%mod)%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<int>v;
   vector<int>v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   uint64_t time_mini=0;
   for(int i=0;i<n;i++)
   {
       time_mini+=v[i]*v1[n-i-1];
   }
   cout<<time_mini%10007;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<long long >v;
   vector<long long >v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   unsigned long long time_mini=0;
   for(int i=0;i<n;i++)
   {
       time_mini+=v[i]*v1[n-i-1];
       time_mini%=10007;
   }
   cout<<time_mini;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<int>v;
   vector<int>v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   int time_mini=0;
   for(int i=0;i<n;i++)
   {
       long long y=(v[i]*v1[n-i-1])%10007;
       time_mini+=y;
   }
   cout<<time_mini;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<int>v;
   vector<int>v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   uint64_t time_mini=0;
   for(int i=0;i<n;i++)
   {
       time_mini+=v[i]*v1[n-i-1];
       time_mini%=10007;
   }
   cout<<time_mini;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<int>v;
   vector<int>v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   long long time_mini=0;
   for(int i=0;i<n;i++)
   {
       time_mini+=v[i]*v1[n-i-1];
       time_mini%=10007;
   }
   cout<<time_mini;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<int>v;
   vector<int>v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   uint64_t time_mini=0;
   for(int i=0;i<n;i++)
   {
       time_mini+=(v[i]*v1[n-i-1])%10007;
   }
   cout<<time_mini;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   vector<int>v;
   vector<int>v1;
   cin>>n;
   for(int i=0;i<n;i++)
   {
        int x;
        cin>>x;
       v.push_back(x);
       v1.push_back(x);

   }
   sort(v.begin(),v.end());
   sort(v1.begin(),v1.end());
   uint64_t time_mini=0;int oo=0;
   for(int i=0;i<n;i++)
   {
       time_mini=v[i]*v1[n-i-1];
       oo+=time_mini%10007;
   }
   cout<<oo;
}


//#define LOCAL 1

/*cf handle: visitr*/

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

#define DBG printf("*\n")
#define DBGV(var) std::cout << var << '\n'
#define NOSYNC std::ios_base::sync_with_stdio(false)
#define REMAX(a, b) (a) = std::max((a), (b))
#define REMIN(a, b) (a) = std::min((a), (b))
#define pi acos(-1)
#define inf (1 << 30)
#define linf (1llu << 60)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef pair<double, double> pdd;
typedef pair<int, double> pid;

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	int n;
	scanf("%d", &n);
	ll arr[n];
	for (int i=0; i<n; ++i)
		scanf("%I64d", arr + i);
	sort(arr, arr + n);
	int ans = 0;
	for (int i=0; i<n/2; ++i)
		ans = (ans + 2 * arr[i] * arr[n-1-i]) % 10007;
	if (n % 2 == 1)
		ans = (ans + arr[n/2] * arr[n/2]) % 10007;
	printf("%d", ans);
	return 0;
}


//#define LOCAL 1

/*cf handle: visitr*/

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

#define DBG printf("*\n")
#define DBGV(var) std::cout << var << '\n'
#define NOSYNC std::ios_base::sync_with_stdio(false)
#define REMAX(a, b) (a) = std::max((a), (b))
#define REMIN(a, b) (a) = std::min((a), (b))
#define pi acos(-1)
#define inf (1 << 30)
#define linf (1llu << 60)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef pair<double, double> pdd;
typedef pair<int, double> pid;

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	int n;
	scanf("%d", &n);
	ll arr[n];
	for (int i=0; i<n; ++i)
		scanf("%d", arr + i);
	sort(arr, arr + n);
	int ans = 0;
	for (int i=0; i<n/2; ++i)
		ans = (ans + 2 * arr[i] * arr[n-1-i]) % 10007;
	if (n % 2 == 1)
		ans = (ans + arr[n/2] * arr[n/2]) % 10007;
	printf("%d", ans);
	return 0;
}


#define LOCAL 1

/*cf handle: visitr*/

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

#define DBG printf("*\n")
#define DBGV(var) std::cout << var << '\n'
#define NOSYNC std::ios_base::sync_with_stdio(false)
#define REMAX(a, b) (a) = std::max((a), (b))
#define REMIN(a, b) (a) = std::min((a), (b))
#define pi acos(-1)
#define inf (1 << 30)
#define linf (1llu << 60)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef pair<double, double> pdd;
typedef pair<int, double> pid;

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	int n;
	scanf("%d", &n);
	ll arr[n];
	for (int i=0; i<n; ++i)
		scanf("%I64d", arr + i);
	sort(arr, arr + n);
	int ans = 0;
	for (int i=0; i<n/2; ++i)
		ans = (ans + 2 * arr[i] * arr[n-1-i]) % 10007;
	if (n % 2 == 1)
		ans = (ans + arr[n/2] * arr[n/2]) % 10007;
	printf("%d", ans);
	return 0;
}


//#define LOCAL 1

/*cf handle: visitr*/

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

#define DBG printf("*\n")
#define DBGV(var) std::cout << var << '\n'
#define NOSYNC std::ios_base::sync_with_stdio(false)
#define REMAX(a, b) (a) = std::max((a), (b))
#define REMIN(a, b) (a) = std::min((a), (b))
#define pi acos(-1)
#define inf (1 << 30)
#define linf (1llu << 60)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef pair<double, double> pdd;
typedef pair<int, double> pid;

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	int n;
	scanf("%d", &n);
	int arr[n];
	for (int i=0; i<n; ++i)
		scanf("%d", arr + i);
	sort(arr, arr + n);
	int ans = 0;
	for (int i=0; i<n/2; ++i)
		ans = (ans + 2 * arr[i] * arr[n-1-i]) % 10007;
	if (n % 2 == 1)
		ans = (ans + arr[n/2] * arr[n/2]) % 10007;
	printf("%d", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int MOD = 10007;
int n;
int a[100000+1000];

int main() {
    cin >> n;
    for( int i = 1; i <= n; i++ ) {
        cin >> a[i];
    }
    sort( a+1, a+n+1 );
    long long sum = 0;
    for( int i = 1; i <= n ; i++ ) {
        sum += ( a[i] * a[n-i+1] ) % MOD;
    }
    cout << sum % MOD << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int MOD = 10007;
int n;
int a[100000+1000];

int main() {
    cin >> n;
    for( int i = 1; i <= n; i++ ) {
        cin >> a[i];
    }
    sort( a+1, a+n+1 );
    int sum = 0;
    for( int i = 1; i <= n ; i++ ) {
        sum += ( a[i] * a[n-i+1] ) % MOD;
    }
    cout << sum % MOD << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int MOD = 10007;
int n;
int a[100000+1000];

int main() {
    cin >> n;
    for( int i = 1; i <= n; i++ ) {
        cin >> a[i];
    }
    sort( a+1, a+n+1 );
    long long sum = 0;
    for( int i = 1; i <= n ; i++ ) {
        sum += ( (a[i] % MOD) * (a[n-i+1] % MOD) ) % MOD;
    }
    cout << sum % MOD << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	 int b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	b[i]=a[i];
	}

	sort(a,a+n);
	sort(b,b+n);
	long long sum=0;
	int j=0;
	for(int i=n-1;i>=0;i--)
	     {
	     	sum=sum+(a[i]*b[j]);
	     	if(sum>=10007)
	     	     sum=sum%10007;
	     	j++;
	 }
	cout<<sum;
	     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	long long a[n];
	 long long b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	}
	sort(a,a+n);
	int j=0;
	for(int i=n-1;i>=0;i--)
	   {
	   	b[j]=a[i];
	   	j++;
	   }
	long long sum=0;
	for(int i=0;i<n;i++)
	     {
	     	sum=sum+(a[i]*b[i]);
	     	if(sum>=10007)
	   	    sum=sum%10007;
	 }
	cout<<sum;
	     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	 int b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	b[i]=a[i];
	}

	sort(a,a+n);
	sort(b,b+n);
	long long sum=0;
	int j=0;
	for(int i=n-1;i>=0;i--)
	     {
	     	sum=sum+(a[i]*b[j]);
	     	j++;
	     }
	     cout<<sum;
	     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	 int b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	}
	sort(a,a+n);
	int j=0;
	for(int i=n-1;i>=0;i--)
	   {
	   	b[j]=a[i];
	   	j++;
	   }
	long long sum=0;
	for(int i=n-1;i>=0;i--)
	     {
	     	sum=sum+(a[i]*b[i]);
	 }
	cout<<sum%10007;
	     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	 int b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	b[i]=a[i];
	}

	sort(a,a+n);
	long long sum=0;
	int j=0;
	for(int i=n-1;i>=0;i--)
	     {
	     	sum=sum+(a[i]*b[j]);
	     	j++;
	     }
	     cout<<sum;
	     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	 int b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	b[i]=a[i];
	}

	sort(a,a+n);
	sort(b,b+n);
	long long sum=0;
	int j=0;
	for(int i=n-1;i>=0;i--)
	     {
	     	sum=sum+(a[i]*b[j]);
	     	j++;
	     }
	     cout<<sum%10007;
	     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	 int b[n];
	for(int i=0;i<n;i++)
	{
	cin>>a[i];
	}
	sort(a,a+n);
	int j=0;
	for(int i=n-1;i>=0;i--)
	   {
	   	b[j]=a[i];
	   	j++;
	   }
	long long sum=0;
	for(int i=0;i<n;i++)
	     {
	     	sum=sum+(a[i]*b[i]);
	     	if(sum>=10007)
	   	    sum=sum%10007;
	 }
	cout<<sum;
	     return 0;
}


#include<iostream>
using namespace std;

int n,v[100001];
void merge_sort( int i, int m, int j)
{
    int w[100001],x,y,k,t;
    x=i;
    y=m+1;
    k=1;
    while(x<=m&&y<=j)
        if(v[x]<v[y])
            w[k++]=v[x++];
        else
            w[k++]=v[y++];
    while(x<=m)
        w[k++]=v[x++];
    while(y<=j)
        w[k++]=v[y++];
    t=i;
    for(k=1; k<=j-i+1; k++)
        v[t++]=w[k];
}
void dei(int i, int j)
{
    int m;
    if(i<j)
    {
        m=(i+j)/2;
        dei(i,m);
        dei(m+1,j);
        merge_sort(i,m,j);
    }
}
int main()
{
    int i, j, suma=0;
    cin>>n;
    for(i=1; i<=n; i++)
        cin>>v[i];
    dei(1,n);
    i=1;
    j=n;
    while(i<=n&&j>=1)
    {
        suma=(suma+(v[i]*v[j])%10007)%10007;
        i++;
        j--;
    }
    cout<<suma%10007;

}


#include<iostream>
using namespace std;

int n,v[100001];
void merge_sort( int i, int m, int j)
{
    int w[100001],x,y,k,t;
    x=i;
    y=m+1;
    k=1;
    while(x<=m&&y<=j)
        if(v[x]<v[y])
            w[k++]=v[x++];
        else
            w[k++]=v[y++];
    while(x<=m)
        w[k++]=v[x++];
    while(y<=j)
        w[k++]=v[y++];
    t=i;
    for(k=1; k<=j-i+1; k++)
        v[t++]=w[k];
}
void dei(int i, int j)
{
    int m;
    if(i<j)
    {
        m=(i+j)/2;
        dei(i,m);
        dei(m+1,j);
        merge_sort(i,m,j);
    }
}
int main()
{
    int i, j, suma=0;
    cin>>n;
    for(i=1; i<=n; i++)
        cin>>v[i];
        //sortez vectorul cu merge sort
    dei(1,n);
    i=1;
    j=n;
    //pornesc cu i de a inceputul vectorului si cu j de la final
    //si atata timp cat i nu a trecut de n iar j nu a scazut sub 1
    while(i<=n&&j>=1)
    {
        //adaug la fosta suma produsul dintre v[i] si v[j] si fac modulo 10007
        suma=(suma%10007+(v[i]%10007*v[j]%10007)%10007)%10007;
        //cresc i
        //scad j
        i++;
        j--;
    }
    cout<<suma%10007;

}


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

int main(){

	int n;

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

	sort(v.begin(), v.end());
	int i=0;
	int j= n-1;
	long int sum=0;
	while(i<=j){
		if(i!=j)sum=sum+(2*v[i]*v[j]);
		else sum=sum+(v[i]*v[j]);
		sum=sum%10007;
		i++;j--;
	}
	cout << sum;

}


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

int main(){

	int n;

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

	sort(v.begin(), v.end());
	int i=0;
	int j= n-1;
	int sum=0;
	while(i<=j){
		sum=sum+2*v[i]*v[j];
		sum=sum%10007;
		i++;j--;
	}
	cout << sum;

}


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

int main(){

	int n;

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

	sort(v.begin(), v.end());
	int i=0;
	int j= n-1;
	int sum=0;
	while(i<=j){
		sum=sum+(2*v[i]*v[j])%10007;
		sum=sum%10007;
		i++;j--;
	}
	cout << sum;

}


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

int main(){

	int n;

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

	sort(v.begin(), v.end());
	int i=0;
	int j= n-1;
	long long int sum=0;
	while(i<=j){
		if(i!=j){sum=sum+(2*v[i]*v[j]);}
		else {sum=sum+(v[i]*v[j]);}

		sum=sum%10007;
		i++;j--;
	}
	cout << sum;

}


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

int main(){

	int n;

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

	sort(v.begin(), v.end());
	int i=0;
	int j= n-1;
	int sum=0;
	while(i<=j){
		if(i!=j)sum=sum+(2*v[i]*v[j]);
		else sum=sum+(v[i]*v[j]);
		sum=sum%10007;
		i++;j--;
	}
	cout << sum;

}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const int mod =100007;
    int s=0;
    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]) % mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;

    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }

    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const long long mod =10007;

    long long s=0;

    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]);
        s%=mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const int mod =10007;
    long long s=0;
    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]);
        s%=mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const int mod =100007;
    long long s=0;
    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]);
        s % = mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <long long> a,b;

    for(int i=0;i<n;i++)
    {
        long long x;
        cin>>x;
        a.push_back(x);
    }

    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const long long mod =10007;

    long long s=0;

    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]);
        s%=mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const int mod =100007;
    long long s=0;
    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]) % mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const int mod =100007;
    long long s=0;
    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]);
        s%=mod;
    }

    cout<<s;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector <int> a,b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    b=a;
    reverse(b.begin(),b.end());

    const int mod =10007;
    long long s=0;
    for(int i=0;i<a.size();i++)
    {
        s+=(a[i]*b[i]);
        s%=mod;
    }

    cout<<s;

    return 0;
}
//


#include<iostream>
using namespace std;
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}
int main()
{
	int n,i;
	long long int sum = 0;
	cin>>n;
	int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	mergeSort(arr , 0 , n-1);
	for(i=0;i<n;i++)
	{
		sum = sum + (arr[i]*arr[n-1-i]);
	}
	cout<<(sum%(10007))<<endl;
	return 0;
}


#include<iostream>
using namespace std;
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}
int main()
{
	int n,i,sum = 0;
	cin>>n;
	int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	mergeSort(arr , 0 , n-1);
	for(i=0;i<n;i++)
	{
		sum = sum + (arr[i]*arr[n-1-i]);
	}
	cout<<(sum%(10007))<<endl;
	return 0;
}


#include<iostream>
using namespace std;
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}
int main()
{
	int n,i;
	long long sum = 0;
	cin>>n;
	int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	mergeSort(arr , 0 , n-1);
	for(i=0;i<n;i++)
	{
		sum = sum + (arr[i]*arr[n-1-i]);
	}
	cout<<(sum%(10007))<<endl;
	return 0;
}


#include<iostream>
using namespace std;
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}
int main()
{
	int n,i;
	unsigned long int sum = 0;
	cin>>n;
	int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	mergeSort(arr , 0 , n-1);
	for(i=0;i<n;i++)
	{
		sum = sum + (arr[i]*arr[n-1-i]);
	}
	cout<<(sum%(10007))<<endl;
	return 0;
}


#include<iostream>
using namespace std;
void merge(long long int arr[], long long int l, long long int m, long long int r)
{
    long int i, j, k;
    long int n1 = m - l + 1;
    long int n2 =  r - m;

    /* create temp arrays */
    long long int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(long long int arr[], long long int l, long long int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}
int main()
{
	int n,i;
	long long int sum = 0;
	cin>>n;
	long long int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	mergeSort(arr , 0 , n-1);
	for(i=0;i<n;i++)
	{
		sum = sum + (arr[i]*arr[n-1-i]);
	}
	cout<<(sum%10007)<<endl;
	return 0;
}


#include<iostream>
//#include<fstream>
using namespace std;
//ifstream fin("potion.in");
//ofstream fout("potion.out");
long long A[100005], n, x, s;

void citire()
{
    cin>>n;
    for(int i = 1; i <= n; ++i)
    {
        cin>>x;
        A[x]++;
    }
}

void parcurgere()
{
    int p = 1, u = 100000;

    while(x < n)
    {
        while(A[p] == 0) p++;
        x++;
        A[p] --;
        if(x < n) while(A[u] == 0) u--;
        A[u]--;
        x++;

        if(x > n)
        {
            s+= (p * p);
        }
        else s+= 2*(p * u);
        s%=10007;
    }
}

int main()
{
    citire();
    x = 0;
    parcurgere();
    cout<<s;
}


#include<bits/stdc++.h>
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define forn(i,n) for(int i = 0; i<n; i++)
#define forr(i,a,n) for(int i = a; i<n; i++)
using namespace std;
string s;
int n, sum[1000006], res, cierran, abren;
void solve(){
	cin >> n;
	cin >> s;
	if(n & 1) {cout << "-1\n"; return;}
	int j = 0;
	forr(i,1,n+1){
		sum[i] = sum[i-1] + (s[j+1] == ')' ? -1 : 1);
		s[j++] == ')'? cierran++ : abren++;
	}
	//forr(i,1,n+1) cout << sum[i] << ' '; cout << "\n";
	if(cierran != abren) {cout << "-1\n"; return;}
	else{
		int lon = 0;
		int i = 1;
		bool hayNegativo = false;
		while(i < n+1){
			hayNegativo = false;
			while(sum[i] < 0){
				hayNegativo = true;
				lon++;
				i++;
			}
			if(hayNegativo) {res += lon+1; lon=0;}
			while(sum[i] >= 0){
				i++;
				if(i >= n+1)
					break;
			}
		}
	}
	cout << res << '\n';
}
int main(){
	solve();
	return 0;
}


#include<bits/stdc++.h>
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define forn(i,n) for(int i = 0; i<n; i++)
#define forr(i,a,n) for(int i = a; i<n; i++)
using namespace std;
string s;
int n, sum[1000006], res;
void solve(){
	cin >> n;
	cin >> s;
	if(n & 1) {cout << "-1\n"; return;}
	int j = 0;
	forr(i,1,n+1){
		sum[i] = sum[i-1] + (s[j++] == ')' ? -1 : 1);
	}
	//forr(i,1,n+1) cout << sum[i] << ' '; cout << "\n";
	if(sum[n+1]) {cout << "-1\n"; return;}
	else{
		int lon = 0;
		int i = 1;
		bool hayNegativo = false;
		while(i < n+1){
			hayNegativo = false;
			while(sum[i] < 0){
				hayNegativo = true;
				lon++;
				i++;
			}
			if(hayNegativo) {res += lon+1; lon=0;}
			while(sum[i] >= 0){
				i++;
				if(i >= n+1)
					break;
			}
		}
	}
	cout << res << '\n';
}
int main(){
	solve();
	return 0;
}


#include<bits/stdc++.h>
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define forn(i,n) for(int i = 0; i<n; i++)
#define forr(i,a,n) for(int i = a; i<n; i++)
using namespace std;
string s;
int n, sum[1000006], res;
void solve(){
	cin >> n;
	cin >> s;
	if(n & 1) {cout << "-1\n"; return;}
	int j = 0;
	forr(i,1,n+1){
		sum[i] = sum[i-1] + (s[j++] == ')' ? -1 : 1);
	}
	//forr(i,1,n+1) cout << sum[i] << ' '; cout << "\n";
	if(sum[n+1]) cout << "-1\n";
	else{
		int lon = 0;
		int i = 1;
		bool hayNegativo = false;
		while(i < n+1){
			hayNegativo = false;
			while(sum[i] < 0){
				hayNegativo = true;
				lon++;
				i++;
			}
			if(hayNegativo) {res += lon+1; lon=0;}
			while(sum[i] >= 0){
				i++;
				if(i >= n+1)
					break;
			}
		}
	}
	cout << res << '\n';
}
int main(){
	solve();
	return 0;
}


#include<bits/stdc++.h>
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define forn(i,n) for(int i = 0; i<n; i++)
#define forr(i,a,n) for(int i = a; i<n; i++)
using namespace std;
string s;
int n, sum[1000006], res, cierran, abren;
void solve(){
	cin >> n;
	cin >> s;
	if(n & 1) {cout << "-1\n"; return;}
	int j = 0;
	forr(i,1,n+1){
		sum[i] = sum[i-1] + (s[j] == ')' ? -1 : 1);
		s[j++] == ')'? cierran++ : abren++;
	}
	//forr(i,1,n+1) cout << sum[i] << ' '; cout << "\n";
	if(cierran != abren) {cout << "-1\n"; return;}
	else{
		int lon = 0;
		int i = 1;
		bool hayNegativo = false;
		while(i < n+1){
			hayNegativo = false;
			while(sum[i] < 0){
				hayNegativo = true;
				lon++;
				i++;
			}
			if(hayNegativo) {res += lon+1; lon=0;}
			while(sum[i] >= 0){
				i++;
				if(i >= n+1)
					break;
			}
		}
	}
	cout << res << '\n';
}
int main(){
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn*5];
int b[maxn];
int sum[maxn*5];
long long n,m,k;
char s[maxn*5];
int vis[maxn*5];

int main(){
  	int n;
  	cin>>n;
  	cin>>s+1;
  	int cnt1=0;
  	int cnt2=0;
  	long long ans=0;
  	for(int i=1;i<=n;++i){
  		if(s[i]==')')a[i]=-1;
  		else a[i]=1;
  	}
  	for(int i=1;i<=n;++i){
  		sum[i]=sum[i-1]+a[i];
  	}
  	int pre=0;
  	int flag=0;
  	for(int i=1;i<=n;++i){
  		if(sum[i]==0&&a[i]==1){
  			if(i==n)flag=1;
  			ans+=i-pre;
  		}
  		if(sum[i]==0){
  			pre=i;
  		}
  	}
  	if(flag==0)cout<<"-1"<<endl;
  	else cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn*5];
int b[maxn];
int sum[maxn*5];
long long n,m,k;
char s[maxn*5];
int vis[maxn*5];

int main(){
  	int n;
  	cin>>n;
  	cin>>s+1;
  	int cnt1=0;
  	int cnt2=0;
  	long long ans=0;
  	for(int i=1;i<=n;++i){
  		if(s[i]==')')a[i]=-1;
  		else a[i]=1;
  	}
  	for(int i=1;i<=n;++i){
  		sum[i]=sum[i-1]+a[i];
  	}
  	int pre=0;
  	int flag=0;
  	int _sum=0;
  	for(int i=1;i<=n;++i){
  		if(a[i]==-1){
  			_sum--;
  			if(_sum==-1)pre=i;
  		}
  		else{
  			_sum++;
  			if(_sum==0)ans+=i-pre+1;
  		}
  	}
  	if(ans==0)cout<<"-1"<<endl;
  	else cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn*5];
int b[maxn];
int sum[maxn*5];
long long n,m,k;
char s[maxn*5];
int vis[maxn*5];

int main(){
  	int n;
  	cin>>n;
  	cin>>s+1;
  	int cnt1=0;
  	int cnt2=0;
  	long long ans=0;
  	for(int i=1;i<=n;++i){
  		if(s[i]==')')a[i]=-1;
  		else a[i]=1;
  	}
  	for(int i=1;i<=n;++i){
  		sum[i]=sum[i-1]+a[i];
  	}
  	int pre=0;
  	for(int i=1;i<=n;++i){
  		if(sum[i]==0&&a[i]==1){
  			ans+=i-pre;
  		}
  		if(sum[i]==0){
  			pre=i;
  		}
  	}
  	if(ans==0)cout<<"-1"<<endl;
  	else cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn*5];
int b[maxn];
int sum[maxn*5];
long long n,m,k;
char s[maxn*5];
int vis[maxn*5];

int main(){
  	int n;
  	cin>>n;
  	cin>>s+1;
  	int cnt1=0;
  	int cnt2=0;
  	long long ans=0;
  	for(int i=1;i<=n;++i){
  		if(s[i]==')')a[i]=-1;
  		else a[i]=1;
  	}
  	for(int i=1;i<=n;++i){
  		sum[i]=sum[i-1]+a[i];
  	}
  	int pre=0;
  	int flag=0;
  	int _sum=0;
  	for(int i=1;i<=n;++i){
  		if(a[i]==-1){
  			_sum--;
  			if(_sum==-1)pre=i;
  		}
  		else{
  			_sum++;
  			if(_sum==0)ans+=i-pre+1;
  		}
  	}
  	if(_sum!=0)cout<<"-1"<<endl;
  	else cout<<ans<<endl;
    return 0;
}


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

int n;

int main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  stack<char> s;
  cin>>n;
  int cnt=0, sol=0, nr=0;
  while(n--){
    char x;
    cin>>x;
    nr++;
    if(!s.empty()&&x==')'&&s.top()=='('){
      s.pop();
    }
    else s.push(x);
    if(x=='(') cnt++;
    else cnt--;
    if(cnt==0){
      if(!s.empty()){
        sol+=nr;
        while(!s.empty()) s.pop();
      }
      nr=0;
    }
  }
  if(cnt) cout<<-1<<"\n";
  else cout<<sol<<"\n";
  return 0;
}


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

int n;

int main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  stack<char> s;
  cin>>n;
  int cnt=0, sol=0, nr=0, nr2=0;
  while(n--){
    char x;
    cin>>x;
    nr++;
    if(!s.empty()&&x==')'&&s.top()=='('){
      s.pop();
      nr2+=2;
      if(nr2==nr) nr=0, nr2=0;
    }
    else s.push(x), nr2=0;
    if(x=='(') cnt++;
    else cnt--;
    if(cnt==0&&!s.empty()){
      sol+=nr;
      nr=0;
      while(!s.empty()) s.pop();
    }
  }
  if(cnt) cout<<-1<<"\n";
  else cout<<sol<<"\n";
  return 0;
}


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

int n;

int main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  stack<char> s;
  cin>>n;
  int cnt=0, sol=0;
  while(n--){
    char x;
    cin>>x;
    if(!s.empty()&&x==')'&&s.top()=='(') s.pop();
    else s.push(x);
    if(x=='(') cnt++;
    else cnt--;
    if(cnt==0&&!s.empty()){
      sol+=s.size();
      while(!s.empty()) s.pop();
    }
  }
  if(cnt) cout<<-1<<"\n";
  else cout<<sol<<"\n";
  return 0;
}


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

int n;

int main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  stack<char> s;
  cin>>n;
  int cnt=0, sol=0, nr=0;
  while(n--){
    char x;
    cin>>x;
    nr++;
    if(!s.empty()&&x==')'&&s.top()=='('){
      s.pop();
      nr+=2;
    }
    else s.push(x);
    if(x=='(') cnt++;
    else cnt--;
    if(cnt==0){
      if(!s.empty()){
        sol+=nr;
        while(!s.empty()) s.pop();
      }
      nr=0;
    }
  }
  if(cnt) cout<<-1<<"\n";
  else cout<<sol<<"\n";
  return 0;
}


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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	 ll n;cin>>n;
	 string s;cin>>s;
	 if(n%2!=0)cout<<"-1"<<endl;
	 else
	 {

	             ll c1=0,ans=0;
	             for(ll i=0;i<n;i++)
	             {
	                   if(s[i]=='(')c1++;
	                   else if(c1==0)ans++;
	                   else c1--;
	                  if(c1>0)ans+=c1-1,c1=1;
	             }
	             cout<<ans+c1<<endl;

	 }

	return 0;
}


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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	ll t=1;//cin>>t;
	while(t--)
	{
	     ll n;cin>>n;
	     string s;cin>>s;
	     ll c=0;
	     for(ll i=0;i<n;i++)
	     {
	           if(s[i]=='(')c++;
	           else c--;
	     }
	     if(c!=0)
	     {
	           cout<<"-1"<<endl;continue;
	     }
	     c=0;ll x=0;ll flag=0;
	     ll ans=0;
	     for(ll i=0;i<n;i++)
	     {
	           if(s[i]=='(')c++;
	           else c--;

	           x++;
	           if(c<0)flag=1;
	           if(c==0)
	           {
	                if(flag==1) ans+=x;
	                 x=0;flag=0;
	           }
	     }
	      cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n, db, cnt;
string s;
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        if (s[i]=='(') cnt++;
        else cnt--;
        if (cnt<0 || (cnt==0 && s[i]=='(')) db++;
    }
    if (cnt!=0) cout << -1 << "\n";
    else cout << db << "\n";
    return 0;
}
//copied


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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	 ll n;cin>>n;
	 string s;cin>>s;
	 if(n%2!=0)cout<<"-1"<<endl;
	 else
	 {
	       stack<char>s1;
	       for(ll i=0;i<n;i++)
	       {
	             if(s1.size()==0&&s[i]=='(')
	             s1.push(s[i]);
	             else if(s1.size()!=0&&s[i]==')')
	             s1.pop();
	       }
	       if(s1.size()==0)cout<<"0"<<endl;
	       else
	       {
	             ll c1=0,ans=0;
	             for(ll i=0;i<n;i++)
	             {
	                   if(s[i]=='(')c1++;
	                   else if(c1==0)ans++;
	                   else c1--;
	                  if(c1>0)ans+=c1-1,c1=1;
	             }
	             cout<<ans+c1<<endl;
	       }
	 }

	return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    int n,l=0,r=0,cnt=0;cin>>n;
    string s;cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }

    if(l!=r)return cout<<-1,0;

    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(st.empty())st.push(s[i]);
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }if(st.empty())return cout<<0,0;

     for(int i=0;i<s.size();i+=2){
        if(s[i]=='('&&s[i+1]==')')continue;
        else cnt+=2;
     }cout<<cnt;

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    int n,l=0,r=0,cnt=0;cin>>n;
    string s;cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }if(l!=r && s.size()%2)return cout<<-1,0;

    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(st.empty())st.push(s[i]);
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }

    if(st.empty())return cout<<0,0;

    for(int i=0,j=s.size()-1;i<j&&j>i;i++,j--){
      if(i==j-1 && s[i]==')'&&s[j]=='(')continue;
      if(s[i]=='('&&s[j]==')')continue;
      else cnt+=2;
    }
    int cnt1=0;
    for(int i=0;i<s.size();i+=2){
        if(s[i]=='('&&s[i+1]==')')continue;
        else cnt1+=2;
     }

     cout<<min(cnt1,cnt);

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    int n;cin>>n;
    string s;cin>>s;
    int l=0,r=0;
    int arr[n];

    memset(arr,0,sizeof arr);
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }
    if(l!=r)return cout<<-1,0;

    for(int i=0;i<s.size();i++){
        if(s[i]==')')arr[i]--;
        else arr[i]++;
    }
    vector<int>v;
    for(int i=1;i<n;i++)arr[i]+=arr[i-1];
    for(int i=0;i<n;i++)if(!arr[i])v.push_back(i);
    int cnt=0;
    for(auto x : v){

        int check=1;
        for(int i=x-1;i>=0;i--){
            if(arr[i]<0){
                check++;
            }else break;
        }if(check>1)cnt+=check;
    }

    cout<<cnt<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    int n,l=0,r=0,cnt=0;cin>>n;
    string s;cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }

    if(l!=r || s.size()%2)return cout<<-1,0;

    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(st.empty())st.push(s[i]);
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }

    if(st.empty())return cout<<0,0;

    for(int i=0,j=s.size()-1;i<j&&j>i;i++,j--){
      if(i==j-1 && s[i]==')'&&s[j]=='(')continue;
      else cnt+=2;
      if(s[i]=='('&&s[j]==')')continue;
      else cnt+=2;
    }

    int cnt1=0;
    for(int i=0;i<s.size();i+=2){
        if(s[i]=='('&&s[i+1]==')')continue;
        else cnt1+=2;
     }

     cout<<min(cnt1,cnt);

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    ll n,l=0,r=0,cnt=0;cin>>n;
    string s;cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }

    if(l!=r)return cout<<-1,0;

    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(st.empty())st.push(s[i]);
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }if(st.empty())return cout<<0,0;

     for(int i=0;i<s.size();i+=2){
        if(s[i]=='('&&s[i+1]==')')continue;
        else cnt+=2;
     }cout<<cnt;

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    int n;cin>>n;
    string s;cin>>s;
    int l=0,r=0;
    int arr[n];

    memset(arr,0,sizeof arr);
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }
    if(l!=r)return cout<<-1,0;

    for(int i=0;i<s.size();i++){
        if(s[i]==')')arr[i]--;
        else arr[i]++;
    }
    vector<int>v;
    for(int i=1;i<n;i++)arr[i]+=arr[i-1];
    for(int i=0;i<n;i++)if(!arr[i])v.push_back(i);
    int cnt=0;
    for(auto x : v){

        int check=1;
        for(int i=x-1;i>=0;i--){
            if(arr[i]<0)check++;
            else break;
        }

        cnt+=check;
    }cout<<cnt-1<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    int n,l=0,r=0,cnt=0;cin>>n;
    string s;cin>>s;

    if(s.size()%2)return cout<<-1,0;
    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(st.empty())st.push(s[i]);
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }

    if(st.empty())return cout<<0,0;

     for(int i=0;i<s.size();i+=2){
        if(s[i]=='('&&s[i+1]==')')continue;
        else cnt+=2;
     }

     cout<<cnt;

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int solve(string s)
{
    stack<char>st;
    int idx=0,cnt=0;
    for(int i=0; i<s.size(); i++)
    {
        if(st.empty()){st.push(s[i]);idx=i;}
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }

    if(st.empty())return cout<<0,0;
    for(int i=idx; i<s.size(); i+=2)
    {
        if(s[i]=='('&&s[i+1]==')')continue;
        else cnt+=2;
    }return cnt;
}

int main()
{
    ROCK;
    ll n,l=0,r=0;cin>>n;
    string s;cin>>s;
    for(int i=0; i<s.size(); i++)
    {
        if(s[i]=='(')l++;
        else r++;
    }if(l!=r||s.size()%2)return cout<<-1,0;
    string x=s;
    reverse(x.begin(),x.end());
    for(int i=0;i<x.size();i++){
        if(x[i]==')')x[i]='(';
        else x[i]=')';
    }cout<<min(solve(x),solve(s))<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
int main()
{
    ROCK;
    ll n,l=0,r=0,cnt=0;cin>>n;
    string s;cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='(')l++;
        else r++;
    }

    if(l!=r)return cout<<-1,0;

    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(st.empty())st.push(s[i]);
        else if(st.size()&&st.top()=='('&&s[i]==')')st.pop();
        else st.push(s[i]);
    }

    if(st.empty())return cout<<0,0;
    cout<<(st.size()-1)*2<<endl;

    return 0;
}


/* Author:bhagatsunny96
         JGEC
	   template__
	lets_code_it_ */
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long int
#define ld long double
#define fi first
#define se second
#define mod 1000000007
#define test long long int t; cin>>t; while(t-->0)
#define fIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define loop(n) for(long long int i=0;i<n;i++)
#define loopr(n) for(long long int i=n-1;i>=0;i--)
#define lrp(a,b) for(long long int i=a;i<=b;i++)
#define lrn(a,b) for(long long int i=b;i>=a;i--)
#define endl '\n'
#define vec vector<ll>v;
#define imap map<ll,ll>mip;
#define umap unordered_map<ll,ll>ump;
#define sett set<ll>st;
using namespace std;
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
 {
    return (a*b)/__gcd(a, b);
 }
 ll power(ll x, unsigned ll y)
 {
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
 }
 bool isPrime(ll num)
 {
    bool flag=true;
    for(ll i =2;i<=num/2;i++)
    {
       if(num%i==0)
       {
          flag=false;
          break;
       }
    }
    return flag;
}
bool isPalin(ll num)
{
    bool flag=false;
    ll digit,rev=0,n=num;
    do
     {
         digit = num % 10;
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);
     if(rev==n)
     flag=true;
     return flag;
}
ll sumofdigits(ll n)
{
    ll c=0;
    while(n>0)
    {
        c++;
        n/=10;
    }
    return c;
}
bool sortinrev(const pair<int,int>&a,const pair<int,int>&b)
{
    return (a.first > b.first);
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
string sortString(string &str)
{
   sort(str.begin(), str.end());
   return str;
}
void countSort(ll arr[], ll n, ll exp)
{
	ll output[n];
	ll i, count[10] = {0};
	for (i = 0; i < n; i++)
		count[ (arr[i]/exp)%10 ]++;
	for (i = 1; i < 10; i++)
		count[i] += count[i - 1];
	for (i = n - 1; i >= 0; i--)
	{
		output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
		count[ (arr[i]/exp)%10 ]--;
	}
	for (i = 0; i < n; i++)
		arr[i] = output[i];
}
void radixsort(ll arr[], ll n)
{
    ll mx = arr[0];
	for (ll i = 1; i < n; i++)
		if (arr[i] > mx)
			mx = arr[i];
	ll m =mx;
	for (ll exp = 1; m/exp > 0; exp *= 10)
		countSort(arr, n, exp);
}
void print(ll arr[], ll n)
{
    for (ll i = 0; i < n; i++)
        cout << arr[i] << " ";
}
int main()
{
    fIO
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll ans=0;
    if(n%2!=0)
    cout<<-1<<endl;
    else
    {
        ll cnt=0;
        ll p=-1;
        ll ans=0;
        loop(n)
        {
            if(s[0]==')')
            {
                s[0]='(';
                ans++;
            }
            if(s[i]=='(')
            {
                cnt++;
            }
            else
            {
                p=i;
            }
            if(p!=-1 && cnt>1)
            {
                for(ll j=p+1;j<n;j++)
                {
                    if(s[j]=='(' && cnt>0)
                    {
                        ans++;
                        cnt--;
                        s[j]=')';
                    }
                    else
                    {
                        cnt--;
                    }
                }
            }
        }
        cout<<ans*2<<endl;
    }
	return 0;
}


/* Author:bhagatsunny96
         JGEC
	   template__
	lets_code_it_ */
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long int
#define ld long double
#define fi first
#define se second
#define mod 1000000007
#define test long long int t; cin>>t; while(t-->0)
#define fIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define loop(n) for(long long int i=0;i<n;i++)
#define loopr(n) for(long long int i=n-1;i>=0;i--)
#define lrp(a,b) for(long long int i=a;i<=b;i++)
#define lrn(a,b) for(long long int i=b;i>=a;i--)
#define endl '\n'
#define vec vector<ll>v;
#define imap map<ll,ll>mip;
#define umap unordered_map<ll,ll>ump;
#define sett set<ll>st;
using namespace std;
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
 {
    return (a*b)/__gcd(a, b);
 }
 ll power(ll x, unsigned ll y)
 {
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
 }
 bool isPrime(ll num)
 {
    bool flag=true;
    for(ll i =2;i<=num/2;i++)
    {
       if(num%i==0)
       {
          flag=false;
          break;
       }
    }
    return flag;
}
bool isPalin(ll num)
{
    bool flag=false;
    ll digit,rev=0,n=num;
    do
     {
         digit = num % 10;
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);
     if(rev==n)
     flag=true;
     return flag;
}
ll sumofdigits(ll n)
{
    ll c=0;
    while(n>0)
    {
        c++;
        n/=10;
    }
    return c;
}
bool sortinrev(const pair<int,int>&a,const pair<int,int>&b)
{
    return (a.first > b.first);
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
string sortString(string &str)
{
   sort(str.begin(), str.end());
   return str;
}
void countSort(ll arr[], ll n, ll exp)
{
	ll output[n];
	ll i, count[10] = {0};
	for (i = 0; i < n; i++)
		count[ (arr[i]/exp)%10 ]++;
	for (i = 1; i < 10; i++)
		count[i] += count[i - 1];
	for (i = n - 1; i >= 0; i--)
	{
		output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
		count[ (arr[i]/exp)%10 ]--;
	}
	for (i = 0; i < n; i++)
		arr[i] = output[i];
}
void radixsort(ll arr[], ll n)
{
    ll mx = arr[0];
	for (ll i = 1; i < n; i++)
		if (arr[i] > mx)
			mx = arr[i];
	ll m =mx;
	for (ll exp = 1; m/exp > 0; exp *= 10)
		countSort(arr, n, exp);
}
void print(ll arr[], ll n)
{
    for (ll i = 0; i < n; i++)
        cout << arr[i] << " ";
}
int main()
{
    fIO
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll ans=0;
    if(n%2!=0)
    cout<<-1<<endl;
    else
    {
        ll cnt=0;
        ll p=-1;
        ll ans=0;
        loop(n)
        {
            if(s[0]==')')
            {
                s[0]='(';
                ans++;
            }
            if(s[i]=='(')
            {
                cnt++;
            }
            else
            {
                p=i;
            }
            if(p!=-1 && cnt>=1)
            {
                for(ll j=p+1;j<n;j++)
                {
                    if(s[j]=='(' && cnt>=0)
                    {
                        ans++;
                        cnt--;
                        s[j]=')';
                    }
                    else
                    {
                        cnt--;
                    }
                }
            }
        }
        cout<<ans*2<<endl;
    }
	return 0;
}


/* Author:bhagatsunny96
         JGEC
	   template__
	lets_code_it_ */
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long int
#define ld long double
#define fi first
#define se second
#define mod 1000000007
#define test long long int t; cin>>t; while(t-->0)
#define fIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define loop(n) for(long long int i=0;i<n;i++)
#define loopr(n) for(long long int i=n-1;i>=0;i--)
#define lrp(a,b) for(long long int i=a;i<=b;i++)
#define lrn(a,b) for(long long int i=b;i>=a;i--)
#define endl '\n'
#define vec vector<ll>v;
#define imap map<ll,ll>mip;
#define umap unordered_map<ll,ll>ump;
#define sett set<ll>st;
using namespace std;
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
 {
    return (a*b)/__gcd(a, b);
 }
 ll power(ll x, unsigned ll y)
 {
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
 }
 bool isPrime(ll num)
 {
    bool flag=true;
    for(ll i =2;i<=num/2;i++)
    {
       if(num%i==0)
       {
          flag=false;
          break;
       }
    }
    return flag;
}
bool isPalin(ll num)
{
    bool flag=false;
    ll digit,rev=0,n=num;
    do
     {
         digit = num % 10;
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);
     if(rev==n)
     flag=true;
     return flag;
}
ll sumofdigits(ll n)
{
    ll c=0;
    while(n>0)
    {
        c++;
        n/=10;
    }
    return c;
}
bool sortinrev(const pair<int,int>&a,const pair<int,int>&b)
{
    return (a.first > b.first);
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
string sortString(string &str)
{
   sort(str.begin(), str.end());
   return str;
}
void countSort(ll arr[], ll n, ll exp)
{
	ll output[n];
	ll i, count[10] = {0};
	for (i = 0; i < n; i++)
		count[ (arr[i]/exp)%10 ]++;
	for (i = 1; i < 10; i++)
		count[i] += count[i - 1];
	for (i = n - 1; i >= 0; i--)
	{
		output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
		count[ (arr[i]/exp)%10 ]--;
	}
	for (i = 0; i < n; i++)
		arr[i] = output[i];
}
void radixsort(ll arr[], ll n)
{
    ll mx = arr[0];
	for (ll i = 1; i < n; i++)
		if (arr[i] > mx)
			mx = arr[i];
	ll m =mx;
	for (ll exp = 1; m/exp > 0; exp *= 10)
		countSort(arr, n, exp);
}
void print(ll arr[], ll n)
{
    for (ll i = 0; i < n; i++)
        cout << arr[i] << " ";
}
int main()
{
    fIO
    ll n;
    cin>>n;
    string s;
    cin>>s;
        list<char>sl;
        ll cnt=0;
        ll p=-1;
        ll ans=0;
        loop(n)
        {
            if(s[i]==')')
            {
                if(sl.size()!=0)
                {
                    if(sl.back()=='(')
                    sl.pop_front();
                    else
                    sl.push_back(')');
                }
                else
                sl.push_back(')');
            }
            else
            {
                if(sl.size()!=0)
                {
                    if(sl.back()=='(')
                    {
                        sl.push_back('(');
                    }
                    else
                    {
                        sl.pop_front();
                        ans+=2;
                    }
                }
                else
                {
                    sl.push_back('(');
                }
            }
        }
        if(sl.size()!=0)
        cout<<-1<<endl;
        else
        cout<<ans<<endl;
	return 0;
}