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 <iostream>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
#include <set>
using namespace std;

#define REP(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
#define ALL(c) begin(c), end(c)
#define SIZE(c) int((c).size())
#define PB push_back
#define F first
#define S second
#define MOD 1000000007

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


long long int ipow(long long int e, long long int a) {
    if (e == 0) return 1;
    if (e == 1) return a;
    if (e & 1) return ((ipow(e - 1, a) * a) % 1000000007);
    long long v = ipow(e / 2, a);
    return (v * v) % 1000000007;
}

int main() {
	ll fact[300000];
	ll invfact[300000];

	fact[0] = 1; fact[1] = 1; invfact[0] = 1; invfact[1] = 1;

	for(int i = 2; i < 300000; i++) {
		fact[i] = i * fact[i-1];
		fact[i] %= MOD;
	}

	for(int i = 2; i < 300000; i++) {
		invfact[i] = ipow(1000000005, fact[i]);
	}

	ll N, X, Y;
	ll overmin = 999999999999999;
	cin >> N >> X >> Y;

	vector< vector<ll> > storage;

	for(int i = 0; i < N+1; i++) {
		vector<ll> empty;
		storage.push_back(empty);
	}

	for(int i = 0; i < N; i++) {
		ll c, w;
		cin >> c >> w;
		storage[c].push_back(w);
		if(w < overmin) {
			overmin = w;
		}
	}

	vector<ll> combinations;
	ll totalcomb = 0;

	for(int i = 0; i < N+1; i++) {
		if(storage[i].size() > 0) {
			sort(storage[i].begin(), storage[i].end());
			if(overmin + storage[i][0] <= Y) {
				ll current = 0;
				while(current != storage[i].size() - 1 && ( (storage[i][0] + storage[i][current + 1] <= X) || (storage[i][current + 1] + overmin <= Y) )) {
					current++;
				}
				combinations.push_back(current + 1);
				totalcomb += (current + 1);
			}
		} 
	}

	ll answer = fact[totalcomb];
	for(int i = 0; i < combinations.size(); i++) {
		answer *= invfact[combinations[i]];
		answer %= MOD;
	}
	cout << answer << endl;
}