Problem p02997 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define vi vector<int>
#define vii vector<pair<int, int>>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i > b; i--)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define PI 3.14159265359
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace __gnu_pbds;
typedef tree<pii,null_type,less
<pii>,rb_tree_tag,tree_order_statistics_node_update>ind_set;
*/
///*X.find_by_order("index")
/// X.order_of_key("key")

#define deb(x) cerr << #x << " : " << x << "\n";
#ifndef local
#define deb(x) ;
#endif
///----------------------------------------------------------------------------------

int n, k;

void solve() {
  cin >> n >> k;

  set<pii> adj;

  rep(i, 2, n + 1) adj.insert({1, i});

  if (k > (n - 1) * (n - 2) / 2) {
    while (1)
      ;
    cout << -1;
    return;
  }

  int cur = (n - 1) * (n - 2) / 2;
  if (cur == k) {
    cout << adj.size() << "\n";
    for (auto x : adj)
      cout << x.ff << " " << x.ss << "\n";
    return;
  }
  rep(i, 2, n + 1) {
    rep(j, i + 1, n + 1) {

      cur--;
      adj.insert({i, j});
      if (cur == k) {
        cout << adj.size() << "\n";
        for (auto x : adj)
          cout << x.ff << " " << x.ss << "\n";
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef local
/// freopen("input.txt","r",stdin);
/// freopen("output.txt","w",stdout);
#endif

  clock_t qwertyuiop = clock();
  int t = 1;
  /// cin>>t;
  rep(a, 0, t) {
    /// cout<<"Case #"<<a+1<<": ";
    solve();
  }
#ifdef local
  {
    qwertyuiop = clock() - qwertyuiop;
    double time_taken = ((double)qwertyuiop) / CLOCKS_PER_SEC;
    cerr << fixed << setprecision(5) << "\n\nTime Taken : " << time_taken
         << " seconds\n";
  }
#endif
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define vi vector<int>
#define vii vector<pair<int, int>>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i > b; i--)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define PI 3.14159265359
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace __gnu_pbds;
typedef tree<pii,null_type,less
<pii>,rb_tree_tag,tree_order_statistics_node_update>ind_set;
*/
///*X.find_by_order("index")
/// X.order_of_key("key")

#define deb(x) cerr << #x << " : " << x << "\n";
#ifndef local
#define deb(x) ;
#endif
///----------------------------------------------------------------------------------

int n, k;

void solve() {
  cin >> n >> k;

  set<pii> adj;

  rep(i, 2, n + 1) adj.insert({1, i});

  if (k > (n - 1) * (n - 2) / 2) {
    cout << -1;
    return;
  }

  int cur = (n - 1) * (n - 2) / 2;
  if (cur == k) {
    cout << adj.size() << "\n";
    for (auto x : adj)
      cout << x.ff << " " << x.ss << "\n";
    return;
  }
  rep(i, 2, n + 1) {
    rep(j, i + 1, n + 1) {

      cur--;
      adj.insert({i, j});
      if (cur == k) {
        cout << adj.size() << "\n";
        for (auto x : adj)
          cout << x.ff << " " << x.ss << "\n";
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef local
/// freopen("input.txt","r",stdin);
/// freopen("output.txt","w",stdout);
#endif

  clock_t qwertyuiop = clock();
  int t = 1;
  /// cin>>t;
  rep(a, 0, t) {
    /// cout<<"Case #"<<a+1<<": ";
    solve();
  }
#ifdef local
  {
    qwertyuiop = clock() - qwertyuiop;
    double time_taken = ((double)qwertyuiop) / CLOCKS_PER_SEC;
    cerr << fixed << setprecision(5) << "\n\nTime Taken : " << time_taken
         << " seconds\n";
  }
#endif
}