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 (Python):
import sys
readline = sys.stdin.readline        

from itertools import accumulate
from collections import Counter
from bisect import bisect as br, bisect_left as bl
class PMS:
    
    def __init__(self, A, B, issum = False):
        
        self.X, self.comp = self.compress(B)
        self.size = len(self.X)
        self.tree = [0] * (self.size + 1)
        self.p = 2**(self.size.bit_length() - 1)
        self.dep = self.size.bit_length()
        
        CA = Counter(A)
        S = [0] + list(accumulate([CA[self.X[i]] for i in range(self.size)]))
        for i in range(1, 1+self.size):
            self.tree[i] = S[i] - S[i - (i&-i)]
        if issum:
            self.sumtree = [0] * (self.size + 1)
            Ssum = [0] + list(accumulate([CA[self.X[i]]*self.X[i] for i in range(self.size)]))
            for i in range(1, 1+self.size):
                self.sumtree[i] = Ssum[i] - Ssum[i - (i&-i)]
    
    def compress(self, L):
        
        L2 = list(set(L))
        L2.sort()
        C = {v : k for k, v in enumerate(L2, 1)}
        
        return L2, C
    
    def leng(self):
        
        return self.count(self.X[-1])
    
    def count(self, v):
        
        i = self.comp[v]
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    
    def less(self, v):
        
        i = bl(self.X, v)
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    
    def leq(self, v):
        
        i = br(self.X, v)
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, v, x):
        
        i = self.comp[v]
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def get(self, i):
        
        if i <= 0:
            return -1
        s = 0
        k = self.p
        for _ in range(self.dep):
            if s + k <= self.size and self.tree[s+k] < i:
                s += k
                i -= self.tree[s]
            k //= 2
        return self.X[s]
    
    def gets(self, v):
        
        v1 = v
        s = 0
        k = self.p
        for _ in range(self.dep):
            if s + k <= self.size and self.sumtree[s+k] < v:
                s += k
                v -= self.sumtree[s]
            k //= 2
        if s == self.size:
            return self.leng()
        return self.count(self.X[s]) + (v1 - self.countsum(self.X[s]))//self.X[s]
    
    def addsum(self, i, x):
        
        self.add(i, x)
        x *= i
        i = self.comp[i]
        while i <= self.size:
            self.sumtree[i] += x
            i += i & -i
    
    def countsum(self, v):
        
        i = self.comp[v]
        s = 0
        while i > 0:
            s += self.sumtree[i]
            i -= i & -i
        return s
    
    def getsum(self, i):
        
        x = self.get(i)
        return self.countsum(x) - x*(self.count(x) - i)


N, K = map(int, readline().split())
C = list(range(K+2*N, K+3*N))
A = list(range(K, K+N))
B = list(range(K+N, K+2*N))
Ans = []
ans = 1

BB = PMS(B, B)
if sum(A) + sum(B) > sum(C):
    ans = -1
else:
    Ao = [A[i] for i in range(N) if A[i]%2]
    Ae = [A[i] for i in range(N) if not A[i]%2]
    if len(Ao) < len(Ae):
        AA = Ae + Ao
    else:
        AA = Ao + Ae
    
    for a, c in zip(AA, C):
        bx = c - a
        k = BB.leq(bx)
        if k == 0:
            ans = -1
            break
        b = BB.get(k)
        Ans.append((a, b, c))
        BB.add(b, -1)

if ans != -1:
    print('\n'.join('{} {} {}'.format(*an) for an in Ans))
else:
    print(ans)

Solution (Python):
N, K = map(int, input().split())
Ans0 = [0] * N
Ans1 = [0] * N
Ans2 = list(range(K+2*N, K+3*N))
n = K
for i in range(0, N, 2):
    Ans0[i] = n
    n += 1
for i in range(1, N, 2):
    Ans0[i] = n
    n += 1
for i in range(1, N, 2):
    Ans1[i] = n
    n += 1
for i in range(0, N, 2):
    Ans1[i] = n
    n += 1
Ans = []
for a, b, c in zip(Ans0, Ans1, Ans2):
    if a+b > c:
        print(-1)
        exit()
    Ans.append("{} {} {}".format(a, b, c))
print("\n".join(Ans))

Solution (Python):
n, k = (int(i) for i in input().split())

if n%2==1:
    if k>(n+1)/2:
        print(-1)
    else:
        wlist=[]
        for i in range(n):
            if i<=(n/2)-1:
                wlist.append([k-i+2*n, k+i, k+2*(n-i)-2])
            else:
                wlist.append([k+3*n-i, k+i, k+n+2*(n-i)-2])
        wlist=sorted(wlist)
        
        for i in range(n):
            print(wlist[i][1], wlist[i][2], k+2*n+i)
else:
    if k<=(n//2):
        wlist=[]
        for i in range(n):
            if i<n/2:
                wlist.append([k-i+2*n+1, k+i, k+2*(n-i)-2])
            else:
                wlist.append([k+3*n-i+1, k+i, k+n+2*(n-i)-1])
        wlist=sorted(wlist)
        
        for i in range(n):
            print(wlist[i][1], wlist[i][2], k+2*n+i)
    else:
        print(-1)

Solution (Python):
N, K = map(int, input().split())
Ans0 = [0] * N
Ans1 = [0] * N
Ans2 = list(range(K+2*N, K+3*N))
n = K
for i in range(0, N, 2):
    Ans0[i] = n
    n += 1
for i in range(1, N, 2):
    Ans0[i] = n
    n += 1
for i in range(1, N, 2):
    Ans1[i] = n
    n += 1
for i in range(0, N, 2):
    Ans1[i] = n
    n += 1
Ans = []
for a, b, c in zip(Ans0, Ans1, Ans2):
    if a+b > c:
        print(-1)
        exit()
    Ans.append("{} {} {}".format(a, b, c))
print("\n".join(Ans))

Solution (Python):
import sys
input = sys.stdin.readline

N,K=map(int,input().split())
ANS=[]

if N%2==1:

    for i in range(N//2+1):
        ANS.append((K+i,K+2*N-1-2*i,K+3*N-1-i-N//2))

    for i in range(N//2):
        ANS.append((K+N//2+1+i,K+2*N-2-2*i,K+3*N-1-i))

    for x,y,z in ANS:
        if x+y>z:
            print(-1)
            sys.exit()

    for x,y,z in ANS:
        print(x,y,z)


else:

    for i in range(N//2):
        ANS.append((K+i,K+2*N-1-2*i,K+3*N-1-i-N//2))

    for i in range(N//2):
        ANS.append((K+N//2+i,K+2*N-2-2*i,K+3*N-1-i))


    for x,y,z in ANS:
        if x+y>z:
            print(-1)
            sys.exit()

    for x,y,z in ANS:
        print(x,y,z)

Solution (Python):

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N,K = inpl()


sab = [[i,i,0] for i in range(N)]

cnt = N
for i in range(N//2, N):
    sab[i][2] = cnt
    sab[i][0] += cnt
    cnt += 1

for i in range(N//2):
    sab[i][2] = cnt
    sab[i][0] += cnt
    cnt += 1

sab.sort()

C = N*2
ans = []
for s,a,b in sab:
    if s+K <= C:
        tmpl = [a+K,b+K,C+K]
        ans.append(' '.join(map(str,tmpl)))
    else:
        print(-1)
        sys.exit()
    C += 1

print('\n'.join(ans))

Solution (Python):
n, k = list(map(int, input().split()))
if n % 2 == 1:
    if k + n // 2 + k + 2 * n - 1 > k + 3 * n - 1:
        print(-1)
    else:
        small = list(range(k, k + n))
        large = list(range(k + n, k + 2 * n))
        large = large[n // 2:] + large[:n // 2]
        total = list(range(k + 2 * n, k + 3 * n))
        total = total[::2] + total[1::2]
        for a, b, c in zip(small, large, total):
            print(a, b, c)
else:
    
    if k + (n - 1) // 2 + k + 2 * n - 1 + 1 > k + 3 * n - 1:
        print(-1)
    else:
        small = list(range(k, k + n))
        large = list(range(k + n, k + 2 * n))
        total = list(range(k + 2 * n, k + 3 * n))
        for a, b, c in zip(small[:n // 2], large[n // 2:], total[::2]):
            print(a, b, c)
        for a, b, c in zip(small[n // 2:], large[:n // 2], total[1::2]):
            print(a, b, c)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,K = map(int,read().split())

def solve_even(N,K):
    L = N//2
    x = np.arange(K,K+N)
    y = np.concatenate([np.arange(K+L+N,K+L+N+L),np.arange(K+N,K+L+N)])
    add = N-(K+L)
    z = x + y + add
    z[L:] += 1
    answer = np.vstack([x,y,z]).T
    print('\n'.join(' '.join(row) for row in answer.astype(str)))

def solve_odd(N,K):
    L = N//2
    x = np.arange(K,K+N)
    y = np.concatenate([np.arange(K+L+N,K+L+N+L+1),np.arange(K+N,K+L+N)])
    add = N-(K+L)
    z = x + y + add
    answer = np.vstack([x,y,z]).T
    print('\n'.join(' '.join(row) for row in answer.astype(str)))

def solve(N,K):
    if K+K > N+1:
        print(-1)
        return
    if N&1:
        solve_odd(N,K)
    else:
        solve_even(N,K)

solve(N,K)