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



Sample input:
1 1


Sample output:
1 2 3

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = (N-1)//2
    print(ans)
    return

def examB():
    N = I()
    D = LI()
    d = Counter(D)
    loop = max(d.keys())
    if D[0]!=0 or d[0]!=1:
        print(0)
        return
#    print(d,loop)
    ans = 1; cur = 1
    for i in range(loop+1):
        if d[i]==0:
            print(0)
            return
        for _ in range(d[i]):
            ans *= cur
            ans %= mod2
        cur = d[i]
    print(ans)
    return

def examC():
    class UnionFind():
        def __init__(self, n):
            self.parent = [-1 for _ in range(n)]
            # 正==子: 根の頂点番号 / 負==根: 連結頂点数

        def find(self, x):
            # 要素xが属するグループの根を返す
            if self.parent[x] < 0:
                return x
            else:
                self.parent[x] = self.find(self.parent[x])
                return self.parent[x]

        def unite(self, x, y):
            # 要素xが属するグループと要素yが属するグループとを併合する
            x, y = self.find(x), self.find(y)
            if x == y:
                return False
            else:
                if self.size(x) < self.size(y):
                    x, y = y, x
                self.parent[x] += self.parent[y]
                self.parent[y] = x

        def same(self, x, y):
            # 要素x, yが同じグループに属するかどうかを返す
            return self.find(x) == self.find(y)

        def size(self, x):
            # 要素xが属するグループのサイズ（要素数）を返す
            x = self.find(x)
            return -self.parent[x]

        def is_root(self, x):
            # すべての根の要素をリストで返す
            return self.parent[x] < 0

        def members(self, x):
            # 要素xが属するグループに属する要素をリストで返す
            root = self.find(x)
            return [i for i in range(self.n) if self.find(i) == root]

        def group_count(self):
            # グループの数を返す
            return len(self.roots())

        def all_group_members(self):
            # {ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す
            return {r: self.members(r) for r in self.roots()}
    N = I()
    A = LI()
    B = LI()
    indices = [i for i in range(len(B))]
    sorted_indices = sorted(indices, key=lambda i: B[i])
    sorted_B = [B[i] for i in sorted_indices]
    sorted_A = sorted(A)
#    print(sorted_B); print(sorted_indices)
    d = defaultdict(int)
#    print(sortA,sortB)
    for i in range(N):
        d[sorted_A[i]] = sorted_B[i]
        if sorted_A[i]>sorted_B[i]:
            print("No")
            return
    for i in range(N-1):
        if sorted_A[i+1]<=sorted_B[i]:
            print("Yes")
            return
    uf = UnionFind(N)
    indices_A = [i for i in range(len(A))]
    sorted_indices_A = sorted(indices_A, key=lambda i: A[i])
    for i in range(N):
        uf.unite(sorted_indices[i],sorted_indices_A[i])
    if uf.size(0)<N:
        print("Yes")
        return
    print("No")
    return

def examD():
    class Dijkstra(object):
        """
        construct: O(ElogV)
        """

        def __init__(self, edges, start=0):
            """
            :param list of list of list of int edges:
            :param int start=0:
            """
            self.__dist = [inf] * len(edges)
            self.__dist[start] = 0
            self.__calculate(edges, start)

        @property
        def dist(self):
            return self.__dist

        def __calculate(self, edges, start):
            Q = [(0, start)]  # (dist,vertex)
            while (Q):
                dist, v = heapq.heappop(Q)
                if self.dist[v] < dist: continue  # 候補として挙がったd,vだが、他に短いのがある
                for u, cost in edges[v]:
                    if self.dist[u] > self.dist[v] + cost:
                        self.__dist[u] = self.dist[v] + cost
                        heapq.heappush(Q, (self.dist[u], u))
    N, M = LI()
    V = [[]for _ in range(N)]
    for _ in range(M):
        l, r, c = LI()
        l -=1; r -=1
        V[l].append((r,c))
    for i in range(N-1):
        V[i+1].append((i,0))
#    print(V)
    D = Dijkstra(V)
    ans = D.dist[N-1]
    if ans==inf:
        ans = -1
    print(ans)
    return

def examE():
    N, K = LI()
    ans = [[]for _ in range(N)]
    A1 = [K+i for i in range(N)]
    A2 = [K+N+i for i in range(N)]
    A3 = [K+2*N+i for i in range(N)]
    for i in range(N):
        a = A1[-((N+1)//2)+i]
        if N%2==1 or i<N//2:
            b = A2[N-1-2*i]
        else:
            b = A2[N- 2 * i-2]
        c = A3[-1-i]
        if a+b>c:
            print(-1)
            return
        ans[i] = [a,b,c]
    for v in ans:
        print(" ".join(map(str,v)))
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examE()

"""

"""

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n < 2*k-1:
  print(-1)
else:
  A1 = [k+i for i in range(n)]
  A2 = [k+n+i for i in range(n)]
  A3 = [k+2*n+i for i in range(n)]
  l = (n+1)//2
  for i in range(l):
    print(A1[l-1-i], A2[i*2], A3[i])
  for i in range(n-l):
    print(A1[n-1-i], A2[i*2+1], A3[l+i])

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
l=[0]*3*N
for i in range(3*N):
    z=(i//(2*N))*(3*((i%2)*(N//2-(i%N)//2-1)+(1-i%2)*(-(i%N)//2-N%2)))
    #print(i+K,z)
#    print(i+K,(i//N+3*(i%N)+(i//N)*3*((N+1)//2)))
    l[(i//N+3*(i%N)+(i//N)*3*((N+1)//2)+z)%(3*N)] = i+K
s=("{} "*3+"\n")*N
print(-1 if 2*K-1>N else s.format(*l))
#print(%(*l))

Submission status: Accepted

Solution code:
N,K=map(int,input().split());print(-1 if 2*K-1>N else"\n".join("%d %d %d"%(((N+1)//2-k-1)%N+K,N+2*k%N+K+(2*k>=N)*(N+1)%2,2*N+k+K)for k in range(N)))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
for i in range(N):
    j = (N+(N+1)//2-i-1)%N
    k = N+(N+2*i)%N+(2*i>=N)*(N+1)%2
    if 2*K-1>N:
        print(-1)
        break
    print(j+K, k+K, 2*N+i+K)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
results = []
for i in range(N//2):
    results.append(((N+1)//2+i+1, 2*N-2*i-1, (N+1)//2+2*N-i))
for i in range((N+1)//2):
    results.append((i+1, 2*N-2*i, 2*N-i+1))
results = results[::-1]
for i, (x, y, z) in enumerate(results):
    if x+y+2*K-2 > 2*N+i+K:
        print(-1)
        break
    print(x + K - 1, y + K - 1, 2*N+i+K)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

if 2 * k - 1 > n:
  print(-1)
else:
  half_n = n // 2
  if n % 2 == 0:
    for i in range(half_n):
      a = k + i
      b = k + n + i + half_n
      c = k + 2 * n + 2 * i
      print(a, b, c)
      a += half_n
      b -= half_n
      c += 1
      print(a, b, c)
  else:
    for i in range(half_n):
      a = k + i
      b = k + n + i + half_n
      c = k + 2 * n + 2 * i
      print(a, b, c)
      a += half_n + 1
      b -= half_n
      c += 1
      print(a, b, c)
    a = k + half_n
    b = k + n + 2 * half_n
    c = k + 2 * n + 2 * half_n
    print(a, b, c)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

if 2 * k > n + 1:
    print(-1)
    exit(0)

ans = []
for i in range((n+1)//2):
    ans.append((k+i, k+n+i+n//2, k+2*n+2*i))
for i in range(n//2):
    ans.append((k+(n+1)//2+i, k+n+i, k+2*n+2*i+1))


for x in ans:
    print(*x)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if 2*k > n+1:
	print(-1)
else:
	for i in range(n//2):
		print((2*k+n-2)//2 - i, k+n+1+2*i, k+2*n+i)
	for i in range(n-n//2):
		print(k+n-1-i, k+n+2*i, k+2*n+n//2+i)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n, k = [int(item) for item in input().split()]
if n - 2 * k + 1 < 0:
    print(-1)
    exit()
for i in range((n + 1) // 2):
    a = k + 2*i
    b = k + n + ((n+1) // 2) - 1 - i
    c = k + 2 * n + i
    print(a, b, c)
for i in range(n // 2):
    a = k + 1 + 2*i
    b = k + 2*n - 1 - i
    c = k + 2*n + ((n+1) // 2) + i
    print(a, b, c)

Submission status: Accepted

Solution code:
from operator import itemgetter


n, k = map(int, input().split())

li_a = [i+k for i in range(n)]
li_b = [i+k+n for i in range(n)]
li_c = [i+k+2*n for i in range(n)]
ans = []

for i in range(n):
   ans.append((li_a[n//2-i], li_b[n-1-i], li_a[n//2-i] + li_b[n-1-i]))
ans = sorted(ans, key = itemgetter(2))

res = []
for i in range(n):
    if ans[i][0] + ans[i][1] <= li_c[i]:
        res.append((ans[i][0], ans[i][1], li_c[i]))
    else:
        print(-1)
        break
else:
    for i in res:
        print(*i)

Submission status: Accepted

Solution code:
def gn(n,k):
    k -= 1
    r = k+2*n+n//2 - (1 + 2*k + 2*n)
    if r < 0:
        print(-1)
        return
    print(k+1, 2*n+k, 3*n+k)
    n-=1
    k+=1
    for i in range(1, n+1):
        x = i
        y = n+(i-((n+1)//2)+n)%n+1
        print(k+x, k+y, 2*k+x+y+r)
def kn(n,k):
    k -= 1
    r = k+2*n+(n+1)//2 - (1 + 2*k + 2*n)
    if r < 0:
        print(-1)
        return
    for i in range(1, n+1):
        x = i
        y = n+(i-((n+1)//2)+n)%n+1
        print(k+x, k+y, 2*k+x+y+r)

N, K = map(int, input().split())
if N%2:
    kn(N,K)
else:
    gn(N,K)

Submission status: Accepted

Solution code:
import os
import sys
from collections import deque

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

# 解説AC
N, K = list(map(int, sys.stdin.readline().split()))
if 2 * K > N + 1:
    print(-1)
    exit()

ab = []
ai = 0
bi = N - 1
for _ in range(N):
    a = K + ai
    b = K + N + bi
    ab.append((a + b, a, b))
    ai = (ai + 2) % N
    bi = (bi - 1) % N
    if ai == 0:
        # もう選んだ
        ai += 1

ab.sort()
for c, (_, a, b) in zip(range(K + 2*N, K + 3*N), ab):
    print(a, b, c)

Submission status: Accepted

Solution code:
import sys
n,k=map(int,input().split())
if 2*k-1>n:
  print(-1)
  sys.exit()
a=[i for i in range(k,k+n)]
A=[]
for i in range(len(a)):
  if i%2==0:
    A.append(a[i])
for i in range(len(a)):
  if i%2==1:
    A.append(a[i])
b1=[i for i in range(k+n,k+n+(n+1)//2)]
b2=[i for i in range(k+n+(n+1)//2,k+2*n)]
b1.reverse()
b2.reverse()
B=b1+b2
C=[i for i in range(k+2*n,k+3*n)]
for i in range(n):
  print(A[i],B[i],C[i])

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if 2*k-1>n:
  print(-1)
else:
  a=[i for i in range(k,k+n)]
  A=[]
  for i in range(len(a)):
    if i%2==0:
      A.append(a[i])
  for i in range(len(a)):
    if i%2==1:
      A.append(a[i])
  b1=[i for i in range(k+n,k+n+(n+1)//2)]
  b2=[i for i in range(k+n+(n+1)//2,k+2*n)]
  b1.reverse()
  b2.reverse()
  B=b1+b2
  C=[i for i in range(k+2*n,k+3*n)]
  for i in range(n):
    print(A[i],B[i],C[i])

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n, k = map(int, input().split())
    if 2 * k - n > 1:
        print(-1)
        exit()
    a = k + n // 2
    b = k + n
    c = k + 2 * n
    ab = []
    for i in range(n):
        ab.append([a, b])
        a, b = a + 1, b + 1
        if a == k + n: a = k
    ab.sort(key=lambda x: x[0] + x[1])
    for a, b in ab:
        print(a, b, c)
        c += 1

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    if(2*k>n+1):
        print(-1)
        return
    
    h=(2*k+3*n-1)//2
    for i in range((n+1)//2):
        print(k+2*i,h-i,k+2*n+i)
    for i in range(n//2):
        print(k+1+i*2,k+2*n-1-i,k+2*n+((n-1)//2)+i+1)
resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    if(2*k>n+1):
        print(-1)
        return
    
    h=(2*k+3*n-1)//2
    for i in range(0,(n+1)//2):
        print(k+2*i,h-i,k+2*n+i)
    for i in range(0,n//2):
        print(k+1+i*2,k+2*n-1-i,k+2*n+((n-1)//2)+i+1)
resolve()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())

ans = []
n = (N+1)//2
if N%2==1:
    for i in range(1,n+1):
        a = i
        b = 4*n-2*i
        c = a+b+n-1
        a += K-1
        b += K-1
        c += K-1
        ans.append([a,b,c])
    for i in range(1,n):
        a = n+i
        b = 4*n-1-2*i
        c = a+b+n-1
        a += K-1
        b += K-1
        c += K-1
        ans.append([a,b,c])
else:
    for i in range(1,n+1):
        a = i
        b = 4*n+2-2*i
        c = a+b+n-1
        a += K-1
        b += K-1
        c += K-1
        ans.append([a,b,c])
        a = n+i
        b = 4*n+1-2*i
        c = a+b+n
        a += K-1
        b += K-1
        c += K-1
        ans.append([a,b,c])

if K>n:
    print(-1)
else:
    for a,b,c in ans:
        print(a,b,c)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N < 2*K - 1:
    print(-1)
else:
    A = [a for a in range(K, K+N, 2)]
    A += [a for a in range(K+1, K+N, 2)]
    B = [b for b in range(K+2*N-N//2-1, K+N-1, -1)]
    B += [b for b in range(K+2*N-1, K+2*N-N//2-1, -1)]
    C = [c for c in range(K+2*N, K+3*N)]
    [print(*p) for p in list(map(list, zip(A, B, C)))]

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
if n % 2 == 0:
  if 2*k > n:
    print(-1)
  else:
    for i in range(n//2):
      print(k+2*i, k+(3*n)//2-1-i, k+2*n+i)
    for i in range(n//2):
      print(k+1+2*i, k+2*n-1-i, k+(5*n)//2+i)
else:
  if 2*k-1 > n:
    print(-1)
  else:
    for i in range((n+1)//2):
      print(k+2*i, k+(3*n-1)//2-i, k+2*n+i)
    for i in range((n-1)//2):
      print(k+1+2*i, k+2*n-1-i, k+(5*n+1)//2+i)

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, K = getlist()
	if 2 * K - 1 > N:
		print(-1)
	else:
		if N % 2 == 1:
			ans = [[0, 0, 0] for i in range(N)]
			for i in range(N):
				ans[i][2] = K + 2 * N + i
			for i in range(N):
				ans[(2 * i) % N][(2 * i) // N] = K + i
				ans[(2 * i + 1) % N][(2 * i + 1) // N] = K + N + i
		else:
			ans = [[0, 0, 0] for i in range(N)]
			for i in range(N):
				ans[i][2] = K + 2 * N + i
			n = N // 2
			for i in range(n):
				ans[2 * i][0] = K + i
				ans[2 * i + 1][0] = K + N + i
			for i in range(n, N):
				ans[2 * i - N][1] = K + N + i
				ans[2 * i + 1 - N][1] = K + i
		for i in range(N):
			print(" ".join(list(map(str, ans[i]))))

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if n%2==0:
    if (2+k-1)+(n//2*3+k-1)>2*n+1+k-1:
        print(-1)
    else:
        cc=3*n
        bb=3*n//2+1
        for aa in range(n-1,0,-2):
            print(aa+k-1,bb+k-1,cc+k-1)
            bb+=1
            cc-=1
        bb=n+1
        for aa in range(n,0,-2):
            print(aa+k-1,bb+k-1,cc+k-1)
            bb+=1
            cc-=1
else:
    if (2+k-1)+((n*3-1)//2+k-1)>2*n+1+k-1:
        print(-1)
    else:
        cc=3*n
        bb=(3*n+1)//2
        for aa in range(n,0,-2):
            print(aa+k-1,bb+k-1,cc+k-1)
            bb+=1
            cc-=1
        bb=n+1
        for aa in range(n-1,0,-2):
            print(aa+k-1,bb+k-1,cc+k-1)
            bb+=1
            cc-=1

Submission status: Accepted

Solution code:
import sys

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

if 2*K-1 > N:
    print (-1)
    sys.exit()

ans = []

nc = K+2*N + (N+1) // 2
na = K+1
nb = K + 2*N -1

for i in range(N // 2):


    ans.append([na,nb,nc])
    
    na += 2
    nb -= 1
    nc += 1

na = K
nc = K + 2*N
for i in range((N+1) // 2):


    ans.append([na,nb,nc])


    na += 2
    nb -= 1
    nc += 1

for i in ans:

    print (i[0],i[1],i[2])

Submission status: Accepted

Solution code:
N,K = map(int,input().split())

ans = [[K+i] for i in range(N)]

m = (N+1)//2
n = K+N
for i in range(m,N):
    ans[i].append(n)
    n += 1
for i in range(m):
    ans[i].append(n)
    n += 1

ans.sort(key=lambda x: x[0]+x[1])
for i in range(N):
    if sum(ans[i]) > n:
        print(-1)
        exit()
    ans[i].append(n)
    n += 1

for row in ans:
    print(*row)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s1, s2, s3, s4, s5, s6 = k, k+n-1, k+n, k+2*n-1, k+2*n, k+3*n-1
if s1 + s4 <= (s5 + s6)/2:
  for i in range(n//2+n%2):
    a = s1 + i*2
    b = s4 - i
    c = s5 + n//2 + i
    print(a, b, c)
  for i in range(n//2):
    a = s1 + 1 + i*2
    b = s4 - (n+1)//2 - i
    c = s5 + i
    print(a, b, c)
else:
  print(-1)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = []
B = []
C = [2*N+i+K for i in range(N)]
s = 2 * K + 3 * N // 2 + (1 if N % 2 == 0 else 0)
for i in range((N+1)//2):
    A.append(K+i*2)
    B.append(s-K-i-(2 if N % 2 == 0 else 0))
for i in range(N//2):
    A.append(K+1+i*2)
    B.append(s-K-i+N//2-(2 if N % 2 == 0 else 0))

for i in range(N):
    if A[i] + B[i] > C[i]:
        print(-1)
        break
else:
    for i in range(N):
        print(A[i], B[i], C[i])

Submission status: Accepted

Solution code:
def solve(n, k):
    s1 = n * (k + (k + 2 * n - 1))
    s2 = n * ((k + 2 * n) + (k + 3 * n - 1)) // 2
    res = []
    if s1 > s2:
        return None
    if n % 2 == 1:
        m = ((n + 1) // 4) * 2
        for i in range(m):
            a = k + n - 1 - i
            b = k + n + i + (m - 1 - 2 * i)
            c = k + 2 * n + (n - 1) // 2 + (m - 1 - 2 * i)
            res.append([a, b, c])
            # print(1, a, b, c)
        for i in range(n - m):
            a = k + n - 1 - m - i
            b = k + 2 * n - 1 - i
            c = k + 2 * n + (n - 1) // 2 + 2 * ((n - m) // 2 - i)
            res.append([a, b, c])
            # print(2, a, b, c)
        return res
    else:
        m = n // 2
        for i in range(m):
            a = k + n - 1 - i
            b = k + n + i + (m - 1 - 2 * i)
            c = k + 2 * n + m + (m - 2 - 2 * i)
            res.append([a, b, c])
            # print(1, a, b, c)
        for i in range(n - m):
            a = k + n - 1 - m - i
            b = k + 2 * n - 1 - i
            c = k + 3 * n - 1 - 2 * i
            res.append([a, b, c])
            # print(2, a, b, c)
        return res


def main():
    n, k = map(int, input().split())
    res = solve(n, k)
    if res is None:
        print(-1)
    else:
        for r in res:
            print(*r)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:



def solve(N,K):
    if 2*K > N+1:
        print(-1)
        return

    def it():

        for i in range((N+1)//2):
            yield (K+i, K+N+i+N//2, K+N*2+i*2)
        for i in range(N//2):
            yield (K+(N+1)//2 + i, K+N+i, K+N*2+i*2+1)

    for a,b,c in it():
        print(a,b,c)

N,K = map(int,input().split())
solve(N,K)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    if k == 1:
        for i in range(n):
            print(i+1,i+1+n,i+1+2*n)
        return
    a = [i+k for i in range(n)]
    b = [i+n+k for i in range(n)]
    c = [i+2*n+k for i in range(n)]
    if n%2 == 0:
        ans = [[a[i],b[(i+n-k)%n],c[2*i]] if 2*i < n else [a[i],b[(i+n-k)%n],c[(2*i+1)%n]] for i in range(n)]
    else:
        ans = [[a[i],b[(i+n-k)%n],c[(2*i)%n]] for i in range(n)]
    for a,b,c in ans:
        if a+b > c:
            print(-1)
            return
    for a in ans:
        print(*a)
    return
def test():
    def change(m):
        res = []
        for i in range(3*n):
            res.append(m%n)
            m //= n
        return res

    def check(a):
        return a[0]+a[1] <= a[2]
    n,k = LI()
    l = [k+i for i in range(3*n)]
    print(n**(3*n))
    f = [change(i) for i in range(n**(3*n))]
    for k in f[::-1]:
        if all([k.count(i) == n for i in range(n)]):
            a = [[l[i] for i in range(3*n) if k[i] == j] for j in range(n)]
            if all([check(i) for i in a]):
                print(a)
#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n,k = [int(i) for i in readline().split()]

if 2*k > n+1:
    print(-1)
else:
    if n%2:
        K = (n+1)//2
        S = K + 2*n
    else:
        K = (n)//2
        S = K + 2*n-2
        
    #print(K,n,S)
    ans = []
    L = n-2
    num = n
    for i in range(2*K+n-1,K+n-1,-1):
        ans.append([i,S-i,S])
        S += 1
        num -= 1        
    
    if n%2==0:
        num -= 1
    for i in range(num):
        if n%2: P = K+2*n-1-i
        else: P = K+2*n-1-i-1
        ans.append([P,S-P,S])
        S += 1
    
    if n%2==0:
        ans.append([K+n-1,K+n-2,2*K+2*n-3+n//2])
        K-=1

    #for i in ans:
    #    print(sum(i))
    #print(ans)
    res = 0
    if n%2==0: res += 1
    
    for i in ans:
        a,b,c = i
        print(a-(K-k),b-(K-k),c-(K-k)+res)

Submission status: Accepted

Solution code:
import math
N, K = map(int,input().split())
if (2*N)*(K+ K+2*N-1) > N*(K+2*N + K+3*N-1):
    print(-1)
    exit()
flag = [1]*(2*N)
for i in range(math.floor(N/2),N):
    a = K + i
    b = K + 2*N -1 -(i-math.floor(N/2))*2
    c = K + 3*N -1 -(i-math.floor(N/2))
    print(a,b,c)
for i in range(math.floor(N/2)):
    a = K + i
    b = K + 2*N -2 -i*2
    c = K + 3*N -1 -i-math.ceil(N/2)
    print(a,b,c)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=[k+i for i in range(n)]
b=[n+i for i in a]
c=[n+i for i in b]
if sum(a)+sum(b)>sum(c):print(-1);exit()
i=n-1
j=b.index(2*n)
print(k+n-1,2*n,k+3*n-1)
bs={2*n}
for k in range(n-1-(n%2==0),0,-1):
  i-=2
  j+=1
  if i<0:i=n+i
  if j==n:j=0
  if i==n-1:
    i-=1
    j+=1
  bs.add(b[j])
  print(a[i],b[j],c[k-n%2])
if n%2==0:
  for i in b:
    if not i in bs:bb=i
  print(a[0],bb,c[0])

Submission status: Accepted

Solution code:
def swap(x):
    s = len(x)//2
    out = []
    if s>=1:
        for i in range(s):
            out.append([x[i][0],x[-1-i][1]])
            out.append([x[-1-i][0],x[i][1]])
        if len(x)%2==1:
            out.append([x[s][0],x[s][1]])
    else:
        out.append(x[0])
    return out

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

if K == 1:
    for i in range(N):
        print(K+i,K+2*N-i-1,K+2*N+i)
elif K <= (N+1)/2:
    ans=[]
    for i in range(N):
        ans.append([K+i,K+2*N-i-1])
    if N%2 == 0:
        s=N//2
        ans_1 = ans[:s+1]
        ans_2 = ans[s+1:-1]
        ans_3 = ans[-1]
        ans_1_new = swap(ans_1)
        ans_2_new = swap(ans_2)
        ans_new = ans_1_new + ans_2_new + [ans_3]
    else:
        s=(N-1)//2
        ans_1 = ans[:s+1]
        ans_2 = ans[s+1:]
        ans_1_new = swap(ans_1)
        ans_2_new = swap(ans_2)
        ans_new = ans_1_new + ans_2_new
    ans = []
    for elem in ans_new:
        ans.append([elem[0],elem[1],elem[0]+elem[1]])
    ans = sorted(ans, key=lambda x: x[2])
    for i in range(N):
        ans[i][2] = K + 2*N + i
        print(ans[i][0],ans[i][1],ans[i][2])
else:
    print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def solve():
    N, K = map(int,input().split())
    if K > (N+1)/2:
        return '-1'

    L = [[K,K+N,K+2*N] for i in range(N)]
    for i in range(N):
        if N%2:
            j = (N-1-2*i)%N
            k = (2*i)%N
        else:
            j = (N-1-2*i)%N-i//(N//2)
            k = (1+2*i)%N-i//(N//2)
        L[i][0] += i
        L[j][1] += i
        L[k][2] += i
    res = '\n'.join(' '.join(map(str, l)) for l in L)
    return res

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
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:
    #1-indexed
    def __init__(self, A, B, issum = False):
        #Aに初期状態の要素をすべて入れる,Bは値域のリスト
        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)}
        # 1-indexed
        return L2, C
    
    def leng(self):
        #今入っている個数を取得
        return self.count(self.X[-1])
    
    def count(self, v):
        #v(Bの元)以下の個数を取得
        i = self.comp[v]
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    
    def less(self, v):
        #v(Bの元である必要はない)未満の個数を取得
        i = bl(self.X, v)
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    
    def leq(self, v):
        #v(Bの元である必要はない)以下の個数を取得
        i = br(self.X, v)
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, v, x):
        #vをx個入れる,負のxで取り出す,iの個数以上取り出すとエラーを出さずにバグる
        i = self.comp[v]
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def get(self, i):
        # 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):
        #累積和がv以下となる最大のindexを返す
        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):
        #sumを扱いたいときにaddの代わりに使う
        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):
        #v(Bの元)以下のsumを取得
        i = self.comp[v]
        s = 0
        while i > 0:
            s += self.sumtree[i]
            i -= i & -i
        return s
    
    def getsum(self, i):
        #i番目までのsumを取得
        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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)
        #print(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)
        #print(wlist)
        for i in range(n):
            print(wlist[i][1], wlist[i][2], k+2*n+i)
    else:
        print(-1)

Submission status: Accepted

Solution code:
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)