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 Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_e
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn solve(n: i64, k: i64) -> Option<Vec<(i64, i64, i64)>> {
    let mut ans = vec![];
    let le = (k+1)/2;
    for i in 0..n {
        if k+2*n+i*2 > 3*n-1+k {
            break;
        }
        if 2*n+i >= 2*n+k {
            break;
        }
        ans.push((k+i, 2*n+i, k+2*n+i*2));
    }
    // debug!(ans);
    let le = ans.len();
    let ni = n as usize;
    let mut used = vec![0; 3*ni];
    for i in 0..le {
        let num = ans[i].0 - k;
        if num < 0 || num >= 2*n {
            return None;
        }
        used[num as usize] += 1;
        let num = ans[i].1 - k;
        if num < 0 || num >= 2*n {
            return None;
        }
        used[num as usize] += 1;
        used[2*ni+i*2] = 1;
    }

    let mut left = vec![];
    for i in 0..2*ni {
        if used[i] >= 2 {
            return None;
        }
        if used[i] == 0 {
            left.push(i);
        }
    }
    let mut right = vec![];
    for i in 2*ni..3*ni {
        if used[i] == 0 {
            right.push(i);
        }
    }
    let ln = left.len();
    for w in 0..ln/2 {
        let a = left[w] as i64;
        let b = left[w+ln/2] as i64;
        let c = right[w] as i64;
        ans.push((a+k, b+k, c+k));
    }
    let n = ans.len();
    for i in 0..n {
        if ans[i].0 + ans[i].1 > ans[i].2 {
            return None;
        }
    }
    return Some(ans);
}

fn main() {
    input! {
        n: i64, k: i64
    };
    if let Some(a) = solve(n, k) {
        for i in 0..(n as usize) {
            println!("{} {} {}", a[i].0, a[i].1, a[i].2);
        }
    } else {
        println!("-1");
    }
}