Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define MAX 1000000
#define ll long long
#define db double
#define str string
#define pb push_back
#define For(i, s, e) for (ll i = (s); i < (e); i++)
#define Forrev(i, s, e) for (ll i = (s); i >= (e); i--)
#define all(v) v.begin(), v.end()

#define vll vector<ll>
#define vs vector<string>
#define mapll map<ll, ll>
#define pll pair<ll, ll>
#define initialise(a, x) memset(a, x, sizeof(a))
#define maxheap priority_queue<ll>
#define minheap priority_queue<ll, vector<ll>, greater<ll>>

#define ff first
#define ss second
#define endl "\n"
#define mp make_pair
const ll mod = 1e9 + 7;

ll takemod(ll a) { return ((a % mod) + mod) % mod; }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll fast_exp(ll base, ll expo) {
  ll res = 1;
  while (expo > 0) {
    if (expo & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    expo >>= 1;
  }
  return res;
}

ll modinv(ll a) { return takemod(fast_exp(takemod(a), mod - 2)); }

signed main() {
  fast_cin();
#ifndef ONLINE_JUDGE
  freopen("C:/IO/in.txt", "r", stdin);
  freopen("C:/IO/out.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  if (k > ((n - 1) * (n - 2)) / 2) {
    cout << -1 << endl;
    return 0;
  }

  ll j = 1;
  while (((j + 1) * j) / 2 <= k) {
    j++;
  }
  vector<pll> p1;
  ll count = 0, prev = 0;
  j = 2;
  For(i, 2, n + 1) {
    if (count + prev <= k) {
      j = i + 1;
      p1.pb(mp(1, i));
      count += prev;
      prev++;
    } else {
      break;
    }

    // cout<<count<<endl;
  }
  // cout<<count<<j<<endl;
  if (count < k) {
    p1.pb(mp(1, j));
    ll diff = k - count;

    For(i, 2 + diff, j) { p1.pb(mp(i, j)); }
    j++;
  }
  if (j <= n) {
    For(i, j, n + 1) { For(l, 1, i) p1.pb(mp(l, i)); }
  }
  cout << p1.size() << endl;
  for (pll a : p1)
    cout << a.ff << " " << a.ss << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define MAX 1000000
#define ll long long
#define db double
#define str string
#define pb push_back
#define For(i, s, e) for (ll i = (s); i < (e); i++)
#define Forrev(i, s, e) for (ll i = (s); i >= (e); i--)
#define all(v) v.begin(), v.end()

#define vll vector<ll>
#define vs vector<string>
#define mapll map<ll, ll>
#define pll pair<ll, ll>
#define initialise(a, x) memset(a, x, sizeof(a))
#define maxheap priority_queue<ll>
#define minheap priority_queue<ll, vector<ll>, greater<ll>>

#define ff first
#define ss second
#define endl "\n"
#define mp make_pair
const ll mod = 1e9 + 7;

ll takemod(ll a) { return ((a % mod) + mod) % mod; }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll fast_exp(ll base, ll expo) {
  ll res = 1;
  while (expo > 0) {
    if (expo & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    expo >>= 1;
  }
  return res;
}

ll modinv(ll a) { return takemod(fast_exp(takemod(a), mod - 2)); }

signed main() {
  fast_cin();
  // #ifndef ONLINE_JUDGE
  // freopen("C:/IO/in.txt", "r", stdin);
  // freopen("C:/IO/out.txt", "w", stdout);
  // #endif
  ll n, k;
  cin >> n >> k;
  if (k > ((n - 1) * (n - 2)) / 2) {
    cout << -1 << endl;
    return 0;
  }

  ll j = 1;
  while (((j + 1) * j) / 2 <= k) {
    j++;
  }
  vector<pll> p1;
  ll count = 0, prev = 0;
  j = 2;
  For(i, 2, n + 1) {
    if (count + prev <= k) {
      j = i + 1;
      p1.pb(mp(1, i));
      count += prev;
      prev++;
    } else {
      break;
    }

    // cout<<count<<endl;
  }
  // cout<<count<<j<<endl;
  if (count < k) {
    p1.pb(mp(1, j));
    ll diff = k - count;

    For(i, 2 + diff, j) { p1.pb(mp(i, j)); }
    j++;
  }
  if (j <= n) {
    For(i, j, n + 1) { For(l, 1, i) p1.pb(mp(l, i)); }
  }
  cout << p1.size() << endl;
  for (pll a : p1)
    cout << a.ff << " " << a.ss << " ";
  cout << endl;
  return 0;
}