prithivMLmods/Coder-Stat huggingface dataset:
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Rint register int
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 100003;
template <typename T> inline void read(T &x) {
  int ch = getchar();
  x = 0;
  bool flag = false;
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flag = true;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (flag)
    x = -x;
}
pii a[N];
int n;
LL sum;
int main() {
  read(n);
  for (Rint i = 1; i <= n; i++)
    read(a[i].second), read(a[i].first);
  sort(a + 1, a + n + 1);
  for (Rint i = 1; i <= n; i++) {
    sum += a[i].second;
    if (sum > a[i].first) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
}

Fixed submission:
#include <bits/stdc++.h>
#define Rint register int
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 200003;
template <typename T> inline void read(T &x) {
  int ch = getchar();
  x = 0;
  bool flag = false;
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flag = true;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (flag)
    x = -x;
}
pii a[N];
int n;
LL sum;
int main() {
  read(n);
  for (Rint i = 1; i <= n; i++)
    read(a[i].second), read(a[i].first);
  sort(a + 1, a + n + 1);
  for (Rint i = 1; i <= n; i++) {
    sum += a[i].second;
    if (sum > a[i].first) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
}
Problem p02996 (Python)

Buggy submission (status: Runtime Error):
import sys

input = sys.stdin.readline


def main():
    AB = []
    n = int(input())
    for i in range(n):
        a, b = map(int, input().split())
        AB.append([a, b])
    AB = sorted(AB, key=lambda x: (x[1], -x[0]))
    time = 0
    for span, dead in zip(AB):
        time += span
        if time > dead:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

input = sys.stdin.readline


def main():
    AB = []
    n = int(input())
    for i in range(n):
        a, b = map(int, input().split())
        AB.append([a, b])
    AB = sorted(AB, key=lambda x: (x[1], -x[0]))
    time = 0
    for span, dead in AB:
        time += span
        if time > dead:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    main()
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int lim = 1e5 + 5;
pair<int, int> jobs[lim];

bool comp(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second < b.second;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> jobs[i].first >> jobs[i].second;
  sort(jobs, jobs + n, comp);

  int l = 0;
  for (int i = 0; i < n; i++) {
    if (l + jobs[i].first > jobs[i].second) {
      cout << "No";
      return 0;
    }
    l += jobs[i].first;
  }

  cout << "Yes";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int lim = 2e5 + 5;
pair<int, int> jobs[lim];

bool comp(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second < b.second;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> jobs[i].first >> jobs[i].second;
  sort(jobs, jobs + n, comp);

  int l = 0;
  for (int i = 0; i < n; i++) {
    if (l + jobs[i].first > jobs[i].second) {
      cout << "No";
      return 0;
    }
    l += jobs[i].first;
  }

  cout << "Yes";
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include <bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
// #define mp make_pair
#define pb push_back
#define ls rt << 1, l, m
#define rs rt << 1 | 1, m + 1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<int, pii>
#define puu pair<ULL, ULL>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fopen                                                                  \
  freopen("in.txt", "r", stdin);                                               \
  freopen("out.txt", "w", stout);
// head

const int N = 1e5 + 5;
pii a[N];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d %d", &a[i].se, &a[i].fi);
  int now = 0;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (now + a[i].se > a[i].fi)
      return 0 * puts("No\n");
    now += a[i].se;
  }
  printf("Yes\n");
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include <bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
// #define mp make_pair
#define pb push_back
#define ls rt << 1, l, m
#define rs rt << 1 | 1, m + 1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<int, pii>
#define puu pair<ULL, ULL>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fopen                                                                  \
  freopen("in.txt", "r", stdin);                                               \
  freopen("out.txt", "w", stout);
// head

const int N = 2e5 + 5;
pii a[N];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d %d", &a[i].se, &a[i].fi);
  int now = 0;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (now + a[i].se > a[i].fi)
      return 0 * puts("No\n");
    now += a[i].se;
  }
  printf("Yes\n");
  return 0;
}
Problem p02996 (C++)

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

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define sq(x) ((x) * (x))
#define bit(x) bitset<32>(x)
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v) sort(v.begin(), v.end(), greater<llint>())
#define debug(x) cout << #x << " " << (x) << endl
#define ci cin
#define co cout
#define en endl
#define ln cout << '\n'
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007

typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<llint, llint> pll;
typedef vector<int> vi;
typedef vector<llint> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

template <typename Tn> Tn __lcm(Tn a, Tn b) { return a / __gcd(a, b) * b; }

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int n;

struct ab {
  int a, b;
  ab() {}
  ab(int a, int b) : a(a), b(b) {}

  bool operator<(const ab &a) const { return b < a.b; }
};

int main() {
  ci >> n;

  vector<ab> v;

  rep(i, 0, n) {
    int a, b;
    ci >> a >> b;

    v.pb(ab(a, b));
  }

  vsort(v);

  int t = 0;

  rep(i, 0, n) {

    if (v[i].b - (t + v[i].a) < 0) {
      co << "No" << en;
      return 0;
    }

    int remain_power = v[i].b - (t + v[i].a);

    rep(j, i + 1, n) {
      if (v[j].a >= remain_power) {
        v[j].a -= remain_power;
        break;
      } else {
        remain_power -= v[j].a;
        v[j].a = 0;
      }
    }

    t = v[i].b;
  }

  co << "Yes" << en;

  return 0;
}


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

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define sq(x) ((x) * (x))
#define bit(x) bitset<32>(x)
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v) sort(v.begin(), v.end(), greater<llint>())
#define debug(x) cout << #x << " " << (x) << endl
#define ci cin
#define co cout
#define en endl
#define ln cout << '\n'
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007

typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<llint, llint> pll;
typedef vector<int> vi;
typedef vector<llint> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

template <typename Tn> Tn __lcm(Tn a, Tn b) { return a / __gcd(a, b) * b; }

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int n;

struct ab {
  int a, b;
  ab() {}
  ab(int a, int b) : a(a), b(b) {}

  bool operator<(const ab &a) const { return b < a.b; }
};

int main() {
  ci >> n;

  vector<ab> v;

  rep(i, 0, n) {
    int a, b;
    ci >> a >> b;

    v.pb(ab(a, b));
  }

  vsort(v);

  int t = 0;

  rep(i, 0, n) {

    if (v[i].b - (t + v[i].a) < 0) {
      co << "No" << en;
      return 0;
    }

    t += v[i].a;
  }

  co << "Yes" << en;

  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, num;
pair<int, int> p[100010];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].second >> p[i].first;
  sort(p, p + n);
  for (int i = 0; i < n;) {
    int j = i;
    while (p[i].first == p[j].first)
      num += p[j++].second;
    if (num > p[i].first) {
      cout << "No" << endl;
      return 0;
    }
    i = j;
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, num;
pair<int, int> p[200010];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].second >> p[i].first;
  sort(p, p + n);
  for (int i = 0; i < n;) {
    int j = i;
    while (p[i].first == p[j].first)
      num += p[j++].second;
    if (num > p[i].first) {
      cout << "No" << endl;
      return 0;
    }
    i = j;
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

#define rep(i, m, n) for (int i = m; i < n; i++)
#define ll long long
#define MOD 1000000007

bool pair_comp_eq(pair<ll, ll> a, pair<ll, ll> b) {
  return a.second <= b.second;
}

int main(int argc, char *argv[]) {
  int n;
  vector<pair<ll, ll>> v;
  ll a, b;
  cin >> n;
  rep(i, 0, n) {
    cin >> a >> b;
    v.push_back(pair<ll, ll>(a, b));
  }
  sort(v.begin(), v.end(), pair_comp_eq);

  ll t = 0;
  rep(i, 0, n) {
    t += v[i].first;
    if (t > v[i].second) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

#define rep(i, m, n) for (int i = m; i < n; i++)
#define ll long long
#define MOD 1000000007

bool pair_comp_eq(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second) {
    return a.first <= b.first;
  } else {
    return a.second <= b.second;
  }
}

int main(int argc, char *argv[]) {
  int n;
  vector<pair<ll, ll>> v;
  ll a, b;
  cin >> n;
  rep(i, 0, n) {
    cin >> a >> b;
    v.push_back(pair<ll, ll>(a, b));
  }
  sort(v.begin(), v.end(), pair_comp_eq);

  ll t = 0;
  rep(i, 0, n) {
    t += v[i].first;
    if (t > v[i].second) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):

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

struct Pair {
  int A;
  int B;

  bool operator<(const Pair &X) const { return B == X.B ? A > X.A : B < X.B; }
};

Pair X[100010];

int main() {
  int i;
  int N;
  long long iSum;

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;

  for (i = 0; i < N; i++) {
    cin >> X[i].A;
    cin >> X[i].B;
  }

  sort(X, X + N);

  iSum = 0;
  for (i = 0; i < N; i++) {
    iSum += X[i].A;
    if (iSum > X[i].B) {
      cout << "No" << endl;

      return 0;
    }
  }

  cout << "Yes" << endl;

  return 0;
}


Fixed submission:

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

struct Pair {
  int A;
  int B;

  bool operator<(const Pair &X) const { return B == X.B ? A > X.A : B < X.B; }
};

Pair X[200010];

int main() {
  int i;
  int N;
  long long iSum;

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;

  for (i = 0; i < N; i++) {
    cin >> X[i].A;
    cin >> X[i].B;
  }

  sort(X, X + N);

  iSum = 0;
  for (i = 0; i < N; i++) {
    iSum += X[i].A;
    if (iSum > X[i].B) {
      cout << "No" << endl;

      return 0;
    }
  }

  cout << "Yes" << endl;

  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
pair<int, int> X[100100];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> X[i].second >> X[i].first;
  sort(X, X + N);
  int Time = 0;
  for (int i = 0; i < N; i++) {
    Time += X[i].second;
    if (Time > X[i].first) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
pair<int, int> X[200200];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> X[i].second >> X[i].first;
  sort(X, X + N);
  int Time = 0;
  for (int i = 0; i < N; i++) {
    Time += X[i].second;
    if (Time > X[i].first) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}
Problem p02996 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  return a.second < b.second;
}

int N;

vector<pair<long long, long long>> v;

int main() {
  cin >> N;

  for (int i = 0; i < N; ++i)
    cin >> v[i].first >> v[i].second;

  sort(v.begin(), v.end(), compare);

  bool ok = true;
  long long sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += v[i].first;
    if (sum > v[i].second)
      ok = false;
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  return a.second < b.second;
}

int N;

vector<pair<long long, long long>> v;

int main() {
  cin >> N;

  v.resize(N);

  for (int i = 0; i < N; ++i)
    cin >> v[i].first >> v[i].second;

  sort(v.begin(), v.end(), compare);

  bool ok = true;
  long long sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += v[i].first;
    if (sum > v[i].second)
      ok = false;
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02997 (C++)

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

using namespace std;

const ll mod = 1000000007;
const ll inf = 1e18;

int main() {

  ll n, k;
  cin >> n >> k;

  ll m = 0;
  vector<vector<ll>> v;

  if (k > (n - 2) * (n - 1) / 2)
    cout << -1 << endl;
  else {
    queue<vector<ll>> G;
    for (int i = 2; i < n; i++) {
      for (int j = i + 1; j < n; j++)
        G.push({i, j});
    }
    for (int i = 2; i <= n; i++) {
      v.push_back({1, i});
      m++;
    }
    for (int i = 0; i < (n - 1) * (n - 2) / 2 - k; i++) {
      vector<ll> u = G.front();
      G.pop();
      v.push_back(u);
      m++;
    }
    cout << m << endl;
    for (int i = 0; i < m; i++)
      cout << v[i][0] << " " << v[i][1] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int

using namespace std;

const ll mod = 1000000007;
const ll inf = 1e18;

int main() {

  ll n, k;
  cin >> n >> k;

  ll m = 0;
  vector<vector<ll>> v;

  if (k > (n - 2) * (n - 1) / 2)
    cout << -1 << endl;
  else {
    queue<vector<ll>> G;
    for (int i = 2; i < n; i++) {
      for (int j = i + 1; j <= n; j++)
        G.push({i, j});
    }
    for (int i = 2; i <= n; i++) {
      v.push_back({1, i});
      m++;
    }
    for (int i = 0; i < (n - 1) * (n - 2) / 2 - k; i++) {
      vector<ll> u = G.front();
      G.pop();
      v.push_back(u);
      m++;
    }
    cout << m << endl;
    for (int i = 0; i < m; i++)
      cout << v[i][0] << " " << v[i][1] << endl;
  }
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 105;
int n, k, ans, t, la = 2;
int ansn, Ans1[MAXN], Ans2[MAXN];
void Print() {
  printf("%d\n", ansn);
  for (int i = 1; i <= ansn; i++)
    printf("%d %d\n", Ans1[i], Ans2[i]);
}
int main() {
  scanf("%d%d", &n, &k);
  if ((n - 1) * (n - 2) / 2 < k) {
    printf("-1\n");
    return 0;
  }
  for (int i = 2; i <= n; i++) {
    ansn++;
    Ans1[ansn] = 1;
    Ans2[ansn] = i;
  }
  t = (n - 1) * (n - 2) / 2 - k;
  for (int i = 2; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      if (t == 0) {
        Print();
        return 0;
      }
      t--;
      ansn++;
      Ans1[ansn] = i;
      Ans2[ansn] = j;
    }
  Print();
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 10005;
int n, k, ans, t, la = 2;
int ansn, Ans1[MAXN], Ans2[MAXN];
void Print() {
  printf("%d\n", ansn);
  for (int i = 1; i <= ansn; i++)
    printf("%d %d\n", Ans1[i], Ans2[i]);
}
int main() {
  scanf("%d%d", &n, &k);
  if ((n - 1) * (n - 2) / 2 < k) {
    printf("-1\n");
    return 0;
  }
  for (int i = 2; i <= n; i++) {
    ansn++;
    Ans1[ansn] = 1;
    Ans2[ansn] = i;
  }
  t = (n - 1) * (n - 2) / 2 - k;
  for (int i = 2; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      if (t == 0) {
        Print();
        return 0;
      }
      t--;
      ansn++;
      Ans1[ansn] = i;
      Ans2[ansn] = j;
    }
  Print();
}
Problem p02997 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll int b[200005];
ll int a[200005];
bool compare(const pair<ll int, int> p1, const pair<ll int, int> p2) {
  if (b[p1.second] == b[p2.second]) {
    return p1.first < p2.first;
  }
  return b[p1.second] < b[p2.second];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    if (k > ((n - 1) * (n - 2)) / 2) {
      cout << -1 << endl;
    } else {
      cout << (n * n - n) / 2 - k << endl;
      int req = (n * n - n) / 2 - k;
      for (int i = 1; i <= n - 1; i++) {
        cout << n << " " << i << endl;
      }
      req -= n - 1;

      bool b = true;
      int r = 1;
      while (b) {
        for (int i = 1; i <= n - 1 - r; i++) {
          if (req == 0) {
            b = false;
          } else {
            cout << i << " " << i + r << endl;
            req -= 1;
          }
        }
        r++;
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll int b[200005];
ll int a[200005];
bool compare(const pair<ll int, int> p1, const pair<ll int, int> p2) {
  if (b[p1.second] == b[p2.second]) {
    return p1.first < p2.first;
  }
  return b[p1.second] < b[p2.second];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    if (k > ((n - 1) * (n - 2)) / 2) {
      cout << -1 << endl;
    } else {
      cout << (n * n - n) / 2 - k << endl;
      int req = (n * n - n) / 2 - k;
      for (int i = 1; i <= n - 1; i++) {
        cout << n << " " << i << endl;
      }
      req -= n - 1;

      bool b = true;
      int r = 1;
      while (b && r <= n - 2) {
        for (int i = 1; i <= n - 1 - r; i++) {
          if (req == 0) {
            b = false;
          } else {
            cout << i << " " << i + r << endl;
            req -= 1;
          }
        }
        r++;
      }
    }
  }
}
Problem p02997 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a) for (int(i) = 0; (i) < (a); ++(i))
#define Rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define ALLIN(v) rep(i, v.size()) cin >> v[i]
#define all(v) (v).begin(), (v).end()
#define bet(a, x, b) a <= x &&x < b
#define fi first
#define se second
using namespace std;

using ll = long long;
using ull = unsigned long long;
using arr = vector<int>;
using mat = vector<arr>;
using pi = pair<int, int>;
template <typename T> using vec = vector<T>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using rpq = priority_queue<T, vec<T>, greater<T>>;

int main(void) {
  int N, K;
  cin >> N >> K;

  int able = (N - 1) * (N - 2) / 2;
  if (able < K) {
    cout << -1;
    return 0;
  }

  int M = N - 1 + able - K;
  cout << M << endl;
  int rem = M - N + 1, now = 2;
  for (int i = 2; i <= N; ++i) {
    cout << 1 << " " << i << endl;
  }
  while (rem > 0) {
    if (rem >= N - now) {
      for (int i = now + 1; i <= N; ++i) {
        cout << now << " " << i << endl;
      }
      rem -= N - now;
    } else {
      for (int i = now + 1; i <= i + rem; ++i) {
        cout << now << " " << i << endl;
      }
      rem = 0;
    }
    ++now;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a) for (int(i) = 0; (i) < (a); ++(i))
#define Rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define ALLIN(v) rep(i, v.size()) cin >> v[i]
#define all(v) (v).begin(), (v).end()
#define bet(a, x, b) a <= x &&x < b
#define fi first
#define se second
using namespace std;

using ll = long long;
using ull = unsigned long long;
using arr = vector<int>;
using mat = vector<arr>;
using pi = pair<int, int>;
template <typename T> using vec = vector<T>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using rpq = priority_queue<T, vec<T>, greater<T>>;

int main(void) {
  int N, K;
  cin >> N >> K;

  int able = (N - 1) * (N - 2) / 2;
  if (able < K) {
    cout << -1;
    return 0;
  }

  int M = N - 1 + able - K;
  cout << M << endl;
  int rem = M - N + 1, now = 2;
  for (int i = 2; i <= N; ++i) {
    cout << 1 << " " << i << endl;
  }
  while (rem > 0) {
    if (rem >= N - now) {
      for (int i = now + 1; i <= N; ++i) {
        cout << now << " " << i << endl;
      }
      rem -= N - now;
    } else {
      for (int i = now + 1; i <= now + rem; ++i) {
        cout << now << " " << i << endl;
      }
      rem = 0;
    }
    ++now;
  }
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

signed main() {
  cin >> N >> K;
  int k = (N - 1) * (N - 2) / 2;
  if (K > k) {
    cout << -1 << endl;
  } else {
    cout << N - 1 + k - K << endl;
    REP(i, N - 1) { cout << 1 << " " << i + 2 << endl; }
    int cnt = k - K;
    REP(i, N - 1)
    FOR(j, i + 1, N - 1) {
      if (cnt-- == 0)
        return 1;
      cout << i + 2 << " " << j + 2 << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

signed main() {
  cin >> N >> K;
  int k = (N - 1) * (N - 2) / 2;
  if (K > k) {
    cout << -1 << endl;
  } else {
    cout << N - 1 + k - K << endl;
    REP(i, N - 1) { cout << 1 << " " << i + 2 << endl; }
    int cnt = k - K;
    REP(i, N - 1)
    FOR(j, i + 1, N - 1) {
      if (cnt-- == 0)
        return 0;
      cout << i + 2 << " " << j + 2 << endl;
    }
  }
}
Problem p02997 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

int main() {

  int N, K;
  std::cin >> N >> K;
  int max = N * (N - 1) / 2 - (N - 1);
  int ans = -1;
  if (max < K) {
    std::cout << -1 << std::endl;
    return 0;
  }
  int M = N - 1 + max - K;
  std::cout << M << std::endl;
  for (int i = 2; i <= N; ++i)
    std::cout << 1 << " " << i << "\n";
  for (int i = N - 1;;) {
    for (int j = 2; j <= N; ++j) {
      for (int k = j + 1; k <= N; ++k, ++i) {
        if (i >= M)
          return 0;
        std::cout << j << " " << k << "\n";
      }
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"

int main() {

  int N, K;
  std::cin >> N >> K;
  int max = N * (N - 1) / 2 - (N - 1);
  int ans = -1;
  if (max < K) {
    std::cout << -1 << std::endl;
    return 0;
  }
  int M = N - 1 + max - K;
  std::cout << M << std::endl;
  for (int i = 2; i <= N; ++i)
    std::cout << 1 << " " << i << "\n";
  for (int i = N - 1; i < M;) {
    for (int j = 2; j < N && i < M; ++j) {
      for (int k = j + 1; k <= N && i < M; ++k, ++i) {
        std::cout << j << " " << k << "\n";
      }
    }
  }
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);

int main() {
  int n, k;
  cin >> n >> k;
  int mx = (n - 1) * (n - 2) / 2;
  if (mx < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<P> ans;
  rep(i, n - 1) { ans.push_back(P(i + 1, n)); }
  int add = mx - k;
  vector<P> edge;
  rep(i, n - 1) rep(j, i) ans.push_back(P(i + 1, j + 1));
  rep(i, add) ans.push_back(edge[i]);
  cout << ans.size() << endl;
  rep(i, ans.size()) { cout << ans[i].first << " " << ans[i].second << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);

int main() {
  int n, k;
  cin >> n >> k;
  int mx = (n - 1) * (n - 2) / 2;
  if (mx < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<P> ans;
  rep(i, n - 1) { ans.push_back(P(i + 1, n)); }
  int add = mx - k;
  vector<P> edge;
  rep(i, n - 1) rep(j, i) edge.push_back(P(i + 1, j + 1));
  rep(i, add) ans.push_back(edge[i]);
  cout << ans.size() << endl;
  rep(i, ans.size()) { cout << ans[i].first << " " << ans[i].second << endl; }
  return 0;
}
Problem p02997 (Python)

Buggy submission (status: Runtime Error):
from itertools import combinations

n, k = map(int, input().split())
star = []
max_k = (n - 1) * (n - 2) // 2
delta = max_k - k
cnt = 0
if k <= max_k:
    ans = []
    for i in range(2, n + 1):
        star.append((1, i))
        cnt += 1

    if delta > 0:
        kumiawase = list(combinations(range(2, n), 2))
        for j in range(delta):
            kumi = kumiawase[j]
            star.append(kumi)
            cnt += 1
    print(cnt)
    for pair in star:
        print(*pair, sep=" ")
else:
    print(-1)


Fixed submission:
from itertools import combinations

n, k = map(int, input().split())
star = []
max_k = (n - 1) * (n - 2) // 2
delta = max_k - k
cnt = 0
if k <= max_k:
    ans = []
    for i in range(2, n + 1):
        star.append((1, i))
        cnt += 1

    if delta > 0:
        kumiawase = list(combinations(range(2, n + 1), 2))
        for j in range(delta):
            kumi = kumiawase[j]
            star.append(kumi)
            cnt += 1
    print(cnt)
    for pair in star:
        print(*pair, sep=" ")
else:
    print(-1)
Problem p02997 (Python)

Buggy submission (status: Runtime Error):
n, k = map(int, input().split())

if k <= (n - 1) * (n - 2) // 2:
    raise Exception
else:
    print(-1)


Fixed submission:
n, k = map(int, input().split())

if k <= (n - 1) * (n - 2) // 2:
    g = [0] * (n * n)
    for i in range(n * n):
        u = i // n
        v = i % n
        if u < v:
            g[i] = 1

    i = 0
    for _ in range(k):
        while g[i] == 0 or i % n == n - 1:
            i += 1
        g[i] = 0

    cnt = sum(g)
    print(cnt)
    for i, e in enumerate(g):
        if e:
            u = i // n + 1
            v = i % n + 1
            print(u, v)

else:
    print(-1)
Problem p02997 (C++)

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 100;
int N, K;

vector<string> ans;

void pushAns(int a, int b) { ans.push_back(to_string(a) + " " + to_string(b)); }

void makeG(int base, int pos) {
  FOR(n, pos, N + 1) {
    FOR(m, base, n) { pushAns(n, m); }
  }
}

int S[MAX_N];

signed main() {
  cin >> N >> K;
  if (K == 0) {
    makeG(1, 1);
    cout << ans.size() << endl;
    REP(n, ans.size()) { cout << ans[n] << endl; }
    return 0;
  }

  int sumS = (N - 1) * (N - 2) / 2;

  if (K > sumS) {
    cout << -1 << endl;
    return 0;
  }

  FOR(n, 2, N + 1) {
    S[n] = n - 2;
    pushAns(1, n);
  }

  for (int n = N; 3 <= n; n--) {
    int p = 2;
    while (sumS > K && S[n] > 0) {
      S[n]--;
      sumS--;
      pushAns(n, p);
      p++;
    }
  }

  cout << ans.size() << endl;
  REP(n, ans.size()) { cout << ans[n] << endl; }

  return 0;
}

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 105;
int N, K;

vector<string> ans;

void pushAns(int a, int b) { ans.push_back(to_string(a) + " " + to_string(b)); }

void makeG(int base, int pos) {
  FOR(n, pos, N + 1) {
    FOR(m, base, n) { pushAns(n, m); }
  }
}

int S[MAX_N];

signed main() {
  cin >> N >> K;
  if (K == 0) {
    makeG(1, 1);
    cout << ans.size() << endl;
    REP(n, ans.size()) { cout << ans[n] << endl; }
    return 0;
  }

  int sumS = (N - 1) * (N - 2) / 2;

  if (K > sumS) {
    cout << -1 << endl;
    return 0;
  }

  FOR(n, 2, N + 1) {
    S[n] = n - 2;
    pushAns(1, n);
  }

  for (int n = N; 3 <= n; n--) {
    int p = 2;
    while (sumS > K && S[n] > 0) {
      S[n]--;
      sumS--;
      pushAns(n, p);
      p++;
    }
  }

  cout << ans.size() << endl;
  REP(n, ans.size()) { cout << ans[n] << endl; }

  return 0;
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
#define mp make_pair

double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); }   // 内積
double cross_product(xy_t a, xy_t b) { return (conj(a) * b).imag(); } // 外積
xy_t projection(xy_t p, xy_t b) {
  return b * dot_product(p, b) / norm(b);
} // 投影
// 対図形

int main() {
  int n, k;
  cin >> n >> k;
  int maxk = (n - 1) * (n - 2) / 2;
  if (maxk < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<pair<int, int>> edges;
  rep(i, n - 1) { edges.push_back(mp(i + 1, n)); }
  vector<pair<int, int>> temp;
  rep(i, n - 1) {
    if (n == 2)
      continue;
    for (int j = i + 1; j < n - 2; j++) {
      temp.push_back(mp(i + 1, j + 1));
    }
  }
  rep(i, maxk - k) { edges.push_back(temp[i]); }
  cout << edges.size() << endl;
  rep(i, edges.size()) {
    cout << get<0>(edges[i]) << " " << get<1>(edges[i]) << endl;
  }
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
#define mp make_pair

double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); }   // 内積
double cross_product(xy_t a, xy_t b) { return (conj(a) * b).imag(); } // 外積
xy_t projection(xy_t p, xy_t b) {
  return b * dot_product(p, b) / norm(b);
} // 投影
// 対図形

int main() {
  int n, k;
  cin >> n >> k;
  int maxk = (n - 1) * (n - 2) / 2;
  if (maxk < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<pair<int, int>> edges;
  rep(i, n - 1) { edges.push_back(mp(i + 1, n)); }
  vector<pair<int, int>> temp;
  rep(i, n - 1) {
    if (n == 2)
      continue;
    for (int j = i + 1; j < n - 1; j++) {
      temp.push_back(mp(i + 1, j + 1));
    }
  }
  rep(i, maxk - k) { edges.push_back(temp[i]); }
  cout << edges.size() << endl;
  rep(i, edges.size()) {
    cout << get<0>(edges[i]) << " " << get<1>(edges[i]) << endl;
  }
  return 0;
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int mx = (n - 1) * (n - 2) / 2;
  if (mx < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<P> ans;
  rep(i, n - 1) ans.push_back(P(i + 1, n));
  int add = mx - k;
  vector<P> edge;
  rep(i, n - 1) rep(j, i) ans.push_back(P(i + 1, j + 1));
  rep(i, add) ans.push_back(edge[i]);
  cout << ans.size() << endl;
  rep(i, ans.size()) { cout << ans[i].first << ' ' << ans[i].second << endl; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int mx = (n - 1) * (n - 2) / 2;
  if (mx < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<P> ans;
  rep(i, n - 1) ans.push_back(P(i + 1, n));
  int add = mx - k;
  vector<P> edge;
  rep(i, n - 1) rep(j, i) edge.push_back(P(i + 1, j + 1));
  rep(i, add) ans.push_back(edge[i]);
  cout << ans.size() << endl;
  rep(i, ans.size()) { cout << ans[i].first << ' ' << ans[i].second << endl; }
  return 0;
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n, k;
  cin >> n >> k;
  int num = (n - 1) * (n - 2);
  num /= 2;
  if (k > num) {
    cout << -1 << endl;
    return 0;
  }

  // 辺の数
  int m = (n - 1) + (num - k);

  pair<int, int> p[m];
  for (int i = 1; i < n; i++) {
    p[i - 1].first = 1;
    p[i - 1].second = i + 1;
  }

  int idx = n - 1;
  for (int i = 1; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      p[idx].first = i + 1;
      p[idx].second = j + 1;
      idx++;
      if (idx == m) {
        cout << m << endl;
        for (int l = 0; l < m; l++) {
          cout << p[l].first << ' ' << p[l].second << endl;
        }
        return 0;
      }
    }
  }
}

Fixed submission:
#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n, k;
  cin >> n >> k;
  int num = (n - 1) * (n - 2);
  num /= 2;
  if (k > num) {
    cout << -1 << endl;
    return 0;
  }

  // 辺の数
  int m = (n - 1) + (num - k);

  pair<int, int> p[m];
  for (int i = 1; i < n; i++) {
    p[i - 1].first = 1;
    p[i - 1].second = i + 1;
  }

  int idx = n - 1;
  if (idx == m) {
    cout << m << endl;
    for (int l = 0; l < m; l++) {
      cout << p[l].first << ' ' << p[l].second << endl;
    }
    return 0;
  }
  for (int i = 1; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      p[idx].first = i + 1;
      p[idx].second = j + 1;
      idx++;
      if (idx == m) {
        cout << m << endl;
        for (int l = 0; l < m; l++) {
          cout << p[l].first << ' ' << p[l].second << endl;
        }
        return 0;
      }
    }
  }
}
Problem p02997 (C++)

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

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << ": " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << "| ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int n, k;

int32_t main() {
  boost;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // code
  cin >> n >> k;
  int x = ((n - 1) * (n - 2)) / 2;

  if (k > x) {
    cout << -1;
    return 0;
  }

  set<pair<int, int>> ans;

  int diff = (n * (n - 1)) / 2 - k;

  // trace(diff);

  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      int a = i, b = j;

      if (a > b)
        swap(a, b);

      ans.insert({a, b});

      diff -= 1;

      if (diff == 0)
        break;
    }
    if (diff == 0)
      break;
  }

  cout << ans.size() << "\n";

  for (auto i : ans) {
    cout << i.first << " " << i.second << "\n";
  }
  return 0;
}


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

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << ": " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << "| ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int n, k;

int32_t main() {
  boost;

  // code
  cin >> n >> k;
  int x = ((n - 1) * (n - 2)) / 2;

  if (k > x) {
    cout << -1;
    return 0;
  }

  set<pair<int, int>> ans;

  int diff = (n * (n - 1)) / 2 - k;

  // trace(diff);

  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      int a = i, b = j;

      if (a > b)
        swap(a, b);

      ans.insert({a, b});

      diff -= 1;

      if (diff == 0)
        break;
    }
    if (diff == 0)
      break;
  }

  cout << ans.size() << "\n";

  for (auto i : ans) {
    cout << i.first << " " << i.second << "\n";
  }
  return 0;
}
Problem p02997 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())

if K > (N - 1)(N - 2) // 2:
    print(-1)
else:
    M = N * (N - 1) // 2 - K
    cnt = 0
    ans = []
    for i in range(N - 1):
        for j in range(i + 1, N):
            ans.append((i + 1, j + 1))
            cnt += 1
            if M == cnt:
                break
        else:
            continue
        break
    print(M)
    for edge in ans:
        print(*edge)


Fixed submission:
N, K = map(int, input().split())

if K > (N - 1) * (N - 2) // 2:
    print(-1)
else:
    M = N * (N - 1) // 2 - K
    cnt = 0
    ans = []
    for i in range(N - 1):
        for j in range(i + 1, N):
            ans.append((i + 1, j + 1))
            cnt += 1
            if M == cnt:
                break
        else:
            continue
        break
    print(M)
    for edge in ans:
        print(*edge)
Problem p02997 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  ll lim = n * (n - 1) / 2 - (n - 1);
  cout << lim - k + n - 1 << endl;
  for (ll i = 2; i <= n; i++) {
    cout << 1 << " " << i << endl;
  }
  ll i = 2, j = 3;
  ll cnt = lim - k;
  while (cnt--) {
    cout << i << " " << j << endl;
    if (j < n)
      j++;
    else
      i++, j = i + 1;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  ll lim = n * (n - 1) / 2 - (n - 1);
  if (lim < k) {
    cout << -1 << endl;
    return 0;
  }
  cout << lim - k + n - 1 << endl;
  for (ll i = 2; i <= n; i++) {
    cout << 1 << " " << i << endl;
  }
  ll i = 2, j = 3;
  ll cnt = lim - k;
  while (cnt--) {
    cout << i << " " << j << endl;
    if (j < n)
      j++;
    else
      i++, j = i + 1;
  }
}
Problem p02997 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

int main() {
  int n, k;
  cin >> n >> k;
  int maxi = (n - 2) * (n - 1) / 2;
  if (k > maxi) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> u, v;
  for (int i = 2; i <= n; i++) {
    u.push_back(1);
    v.push_back(i);
  }
  int nn = maxi - k;
  int from = 2;
  while (1) {
    bool flg = 0;
    for (int to = from + 1; to <= n; to++) {
      u.push_back(from);
      v.push_back(to);
      nn--;
      if (nn == 0) {
        flg = 1;
        break;
      }
    }
    from++;
    if (flg)
      break;
  }
  int m = u.size();
  cout << m << endl;
  rep(i, m) { cout << u[i] << " " << v[i] << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

int main() {
  int n, k;
  cin >> n >> k;
  int maxi = (n - 2) * (n - 1) / 2;
  if (k > maxi) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> u, v;
  for (int i = 2; i <= n; i++) {
    u.push_back(1);
    v.push_back(i);
  }
  int nn = maxi - k;
  int from = 2;
  while (nn > 0) {
    bool flg = 0;
    for (int to = from + 1; to <= n; to++) {
      u.push_back(from);
      v.push_back(to);
      nn--;
      if (nn == 0) {
        flg = 1;
        break;
      }
    }
    from++;
    if (flg)
      break;
  }
  int m = u.size();
  cout << m << endl;
  rep(i, m) { cout << u[i] << " " << v[i] << endl; }
  return 0;
}
Problem p02997 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll N, K;
  cin >> N >> K;
  if (K > ((N - 1) * (N - 2)) / 2)
    return -1;
  vector<pair<int, int>> ret;
  for (int i = 2; i <= N; i++) {
    ret.push_back(make_pair(1, i));
  }
  int cnt = ((N - 1) * (N - 2)) / 2 - K;
  for (int i = 2; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      if (cnt) {
        ret.push_back(make_pair(i, j));
        cnt--;
      } else
        break;
    }
    if (!cnt)
      break;
  }

  cout << ret.size() << endl;

  for (int i = 0; i < ret.size(); i++) {
    cout << ret[i].first << ' ' << ret[i].second << endl;
  }
}

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

int main() {
  ll N, K;
  cin >> N >> K;
  if (N == 2) {
    if (K == 0)
      cout << 1 << endl << 1 << ' ' << 2 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  if (K > ((N - 1) * (N - 2)) / 2) {
    cout << -1 << endl;
    return 0;
  }

  vector<pair<int, int>> ret;
  for (int i = 2; i <= N; i++) {
    ret.push_back(make_pair(1, i));
  }
  int cnt = ((N - 1) * (N - 2)) / 2 - K;
  for (int i = 2; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      if (cnt) {
        ret.push_back(make_pair(i, j));
        cnt--;
      } else
        break;
    }
    if (!cnt)
      break;
  }

  cout << ret.size() << endl;

  for (int i = 0; i < ret.size(); i++) {
    cout << ret[i].first << ' ' << ret[i].second << endl;
  }
}