Language: C++

/*
        _            _                  _            _       _     _         _   _          _          _             _        
       / /\         / /\               / /\         / /\    / /\  /\ \      /\_\/\_\ _     /\ \       /\ \     _    /\ \      
      / /  \       / /  \             / /  \       / / /   / / /  \ \ \    / / / / //\_\   \ \ \     /  \ \   /\_\ /  \ \     
     / / /\ \__   / / /\ \           / / /\ \__   / /_/   / / /   /\ \_\  /\ \/ \ \/ / /   /\ \_\   / /\ \ \_/ / // /\ \_\    
    / / /\ \___\ / / /\ \ \         / / /\ \___\ / /\ \__/ / /   / /\/_/ /  \____\__/ /   / /\/_/  / / /\ \___/ // / /\/_/    
    \ \ \ \/___// / /  \ \ \        \ \ \ \/___// /\ \___\/ /   / / /   / /\/________/   / / /    / / /  \/____// / / ______  
     \ \ \     / / /___/ /\ \        \ \ \     / / /\/___/ /   / / /   / / /\/_// / /   / / /    / / /    / / // / / /\_____\ 
 _    \ \ \   / / /_____/ /\ \   _    \ \ \   / / /   / / /   / / /   / / /    / / /   / / /    / / /    / / // / /  \/____ / 
/_/\__/ / /  / /_________/\ \ \ /_/\__/ / /  / / /   / / /___/ / /__ / / /    / / /___/ / /__  / / /    / / // / /_____/ / /  
\ \/___/ /  / / /_       __\ \_\\ \/___/ /  / / /   / / //\__\/_/___\\/_/    / / //\__\/_/___\/ / /    / / // / /______\/ /   
 \_____\/   \_\___\     /____/_/ \_____\/   \/_/    \/_/ \/_________/        \/_/ \/_________/\/_/     \/_/ \/___________/    
                                                                                                                              
*/
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for(int i = 0;i < (n);++i)

#define endl '\n'
#define pb emplace_back
#define mkp make_pair
#define p_q priority_queue
#define INF 1000000000
#define PI 3.1415926
#define coutdb cout << fixed << setprecision(10)

const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
const int dx_8[]={1,1,0,-1,-1,-1,0,1}, dy_8[]={0,-1,-1,-1,0,1,1,1};
const long long MOD = 1e9+7;

// typedef long long ll;
#define int long long

template <typename T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <typename T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template <typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template <typename T> inline T nHr(T a,T b){return nCr(a+b-1,b);}

signed main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K; cin >> N >> K;
  
  if(K > nCr(N-1, 2)){
    cout << -1 << endl;
    return 0;
  }
  
  int c[110]; c[0] = c[1] = 0;
  for(int i = 2; i < 110; i++)
    c[i] = nCr(i, 2ll);
  int vsum = 0, cnt[110] = {};
  int all = 0, sum = 0;
  for(int i = 109; i >= 2; i--){
    vsum += (K / c[i]) * (i+1);
    cnt[i] = K/c[i];
    all += cnt[i]; sum += (K/c[i]) * i;
    K %= c[i];
  }
  if(vsum > N){
    cout << -1 << endl;
    return 0;
  }
  cout << sum << endl;
  int v = 1;
  for(int i = 2; i < 110; i++){
    REP(j, cnt[i]){
      for(int k = v+1; k <= v+i; k++){
        cout << v << ' ' << k << endl;
      }
      v = v+i+1;
    }
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:46:13: error: no matching function for call to 'nCr(long long int, int)'
   46 |   if(K > nCr(N-1, 2)){
      |          ~~~^~~~~~~~
a.cc:37:32: note: candidate: 'template<class T> T nCr(T, T)'
   37 | template <typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
      |                                ^~~
a.cc:37:32: note:   template argument deduction/substitution failed:
a.cc:46:13: note:   deduced conflicting types for parameter 'T' ('long long int' and 'int')
   46 |   if(K > nCr(N-1, 2)){
      |          ~~~^~~~~~~~