Problem:
Score : 700 points

Problem Statement

Given are positive integers N and K.

Determine if the 3N integers K, K+1, ..., K+3N-1 can be partitioned into N triples (a_1,b_1,c_1), ..., (a_N,b_N,c_N) so that the condition below is satisfied. Any of the integers K, K+1, ..., K+3N-1 must appear in exactly one of those triples.

For every integer i from 1 to N, a_i + b_i \leq c_i holds.

If the answer is yes, construct one such partition.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq 10^9

Input

Input is given from Standard Input in the following format:

N K

Output

If it is impossible to partition the integers satisfying the condition, print -1. If it is possible, print N triples in the following format:

a_1 b_1 c_1
:
a_N b_N c_N

Sample Input 1

1 1

Sample Output 1

1 2 3

Sample Input 2

3 3

Sample Output 2

-1

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

#include <bits/stdc++.h>

using namespace std;

template <class T>
struct Triple {
	T a, b, c;

	Triple() : a(0), b(0), c(0) {

	}

	Triple(const T & a, const T & b, const T & c) : a(a), b(b), c(c) {
		
	}
};

vector<Triple<int> > ans;

bool answer(const int & N, const int & K) {
	if (N < K * 2 - 1)
		return false;

	for (int i = 0; i < N; i++)
		ans.emplace_back(K + i, K + N * 2 - i - 1, K + N * 2 + i);
	int x = K - 1;
	int y = (x + 1) / 2, z = x / 2;
	for (int i = 0; i < y; i++)
		swap(ans[i].b, ans[y * 2 - i - 1].b);
	for (int i = 0; i < z; i++)
		swap(ans[y * 2 + i].b, ans[y * 2 + z * 2 - i].b);
	for (int i = 0; i <= x * 2; i++)
		ans[i].c = ans[i].a + ans[i].b;
	return true;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	int N, K;
	
	cin >> N >> K;

	if (answer(N, K)) {
		for (const auto & t : ans)
			cout << t.a << ' ' << t.b << ' ' << t.c << '\n';
	} else
		cout << "-1" << '\n';

	return 0;
}