Language: C++

#include <vector>
#include <algorithm>

#define llint long long int

int output(std::pair<int, int> sep, int now_count,
           const std::vector<std::pair<int, int> >& back,
           std::vector<std::pair<int, int> >& edges){
  int used = 0;
  if(sep.second == -1){
    for(int i=1; i<sep.first; i++){
      edges.push_back(std::make_pair(now_count, now_count+i));
    }
    return sep.first;
  }else if(sep.second == 0){
    used += output(back[sep.first], now_count, back, edges);
  }else{
    used += output(back[sep.first], now_count, back, edges);
    used += output(back[sep.second], now_count+used, back, edges);
  }
  return used;
}

int main(void){

  int n, k;
  std::cin >> n >> k;

  std::vector<int> dp(n+1, -1);
  std::vector<std::pair<int, int> > back(n+1, std::make_pair(-1, -1));
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = 0;

  for(int i=3; i<=n; i++){
    llint combi = (i-1)*(i-2)/2;
    if(combi <= k){
      dp[i] = combi;
      back[i] = std::make_pair(i, -1);
    }else{
      for(int j=1; j<i; j++){

        llint temp = dp[j]+dp[i-j];
        if(dp[i] < temp && temp <= k){
          dp[i] = temp;
          if(i-j>j) back[i] = std::make_pair(i-j, j);
          else      back[i] = std::make_pair(j, i-j);
        }
      }
    }
    if(i!=n && dp[i] == k ){
      dp[n] = dp[i];
      back[n] = std::make_pair(i, 0);
      break;
    }
  }
  

  if(dp[n] != k){
    std::cout << "-1" << std::endl;
    return 0;
  }

  std::vector<std::pair<int, int> > edges;
  output(back[n], 1, back, edges);

  std::cout << edges.size() << std::endl;
  for(int i=0; i<edges.size(); i++){
    std::cout << edges[i].first << " " << edges[i].second << std::endl;
  }

  return 0;
}




Compiler output:
a.cc: In function 'int main()':
a.cc:27:8: error: 'cin' is not a member of 'std'
   27 |   std::cin >> n >> k;
      |        ^~~
a.cc:3:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    2 | #include <algorithm>
  +++ |+#include <iostream>
    3 | 
a.cc:60:10: error: 'cout' is not a member of 'std'
   60 |     std::cout << "-1" << std::endl;
      |          ^~~~
a.cc:60:10: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:60:31: error: 'endl' is not a member of 'std'
   60 |     std::cout << "-1" << std::endl;
      |                               ^~~~
a.cc:3:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    2 | #include <algorithm>
  +++ |+#include <ostream>
    3 | 
a.cc:67:8: error: 'cout' is not a member of 'std'
   67 |   std::cout << edges.size() << std::endl;
      |        ^~~~
a.cc:67:8: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:67:37: error: 'endl' is not a member of 'std'
   67 |   std::cout << edges.size() << std::endl;
      |                                     ^~~~
a.cc:67:37: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:69:10: error: 'cout' is not a member of 'std'
   69 |     std::cout << edges[i].first << " " << edges[i].second << std::endl;
      |          ^~~~
a.cc:69:10: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:69:67: error: 'endl' is not a member of 'std'
   69 |     std::cout << edges[i].first << " " << edges[i].second << std::endl;
      |                                                                   ^~~~
a.cc:69:67: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'