Problem:
Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<map>
#include<queue>
#include<bitset>
#define mk make_pair
#define fi first
#define nd second
#define pii pair<int,int>
#define pb push_back
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
inline ll read() {ll x = 0; char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {if(ch == '-') w = -1;
ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
const int N = 3e5;
struct T {
	int c, w;
}a[N];
int n, x, y;
vector<pii>b[N];
pii t1;
int fa[N], siz[N];
ll inv[N], fac[N];
const ll p = 1e9 + 7;
ll ksm(ll x, ll y) {
	ll res = 1;
	for(; y; y >>= 1, x = x * x % p)
		if(y & 1) res = res * x%p;
	return res;
}
ll ans[N];
//ll c1 = 0, c2 = 0;
int main() {
	n = read(), x = read(), y = read();
	inv[0] = fac[0]= 1;
	for(int i = 1; i <= n; ++i) {
		fac[i] = fac[i - 1] * i % p;
		inv[i] = ksm(fac[i], p - 2);
		ans[i] = 1;
	}
	for(int i = 1 ; i <= n; ++i) {
		a[i].c = read(), a[i].w = read();
		b[a[i].c].pb(mk(a[i].w, i));
	}
	t1.fi =1e9 + 99;
	for(int i = 1; i <= n; ++i) if(b[i].size()) {
		sort(b[i].begin(),b[i].end());
		siz[i] =1;
		for(int j = 1; j < b[i].size(); ++j)
			if(b[i][0].fi + b[i][j].fi <= x) fa[b[i][j].nd] = b[i][0].nd, ++siz[i];
	/*	if(b[i][0].fi < t1.fi) t2 = t1, t1 = b[i][0];
		else if(b[i][0].fi < t2.fi) t2 = b[i][0];*/
		if(b[i][0].fi < t1.fi) t1 = b[i][0];
	}
//	printf("%d %d %d %d\n",t1.fi,t1.nd,t2.fi,t2.nd);
//	printf("(%d,%d)\n",t1.fi,t1.nd);

	//printf("c=%d\n",a[t1.nd].c);
	for(int i = 1; i <= n; ++i) if(b[i].size() && i != a[t1.nd].c) {
	/*	if(a[t1.nd].c != i) {
			if(b[i][0].fi+ t1.fi <= y) fa[b[i][0].nd] = t1.nd, ans[t1.nd] = ans[t1.nd] * inv[siz[i]], c1 += siz[i];
		} else {
			if(b[i][0].fi + t2.fi <= y) fa[b[i][0].nd] = t2.nd, ans[t2.nd] = ans[t2.nd] * inv[siz[i]], c2 += siz[i];
		}*/
	//	printf("i=%d %d %d\n",i,b[i][0].fi + t1.fi, b[i][0].nd);
		if(b[i][0].fi+ t1.fi <= y) fa[b[i][0].nd] = t1.nd, ans[t1.nd] = ans[t1.nd] * inv[siz[i]] % p, siz[a[t1.nd].c] += siz[i];
	}
	/*for(int i = 1; i <= n; ++i)
		printf("%d:%d %d (%d,%d)\n", i, fa[i],siz[i],fac[i],inv[i]);*/
	ll res = 1;
	for(int i = 1; i <= n; ++i) if(b[i].size() && fa[b[i][0].nd] == 0) {
		//printf("ans=%lld\n",ans[b[i][0].nd]);
		res = res * fac[siz[i]] % p * ans[b[i][0].nd] % p;
	}
		
	writeln(res);
//	writeln(fac[c1] * ans[t1.nd] % p * fac[c2] % p  * ans[t2.nd] % p);
}