Problem p03249 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 100005;

template <class T> void read(T &x) {
  int sgn = 1;
  char ch;
  x = 0;
  for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    ch = getchar(), sgn = -1;
  for (; '0' <= ch && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= sgn;
}

template <class T> void write(T x) {
  if (x < 0)
    putchar('-'), write(-x);
  else if (x < 10)
    putchar(x + '0');
  else
    write(x / 10), putchar(x % 10 + '0');
}

int n, dep[N], sz[N];
bool flag = true;

struct node {
  int id;
  long long dist;
  bool operator<(node rhs) const { return dist < rhs.dist; }
} vec[N];
long long d[N];
vector<pair<int, int>> tree;

int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    vec[i].id = i, read(vec[i].dist);

  sort(vec + 1, vec + n + 1);
  for (int i = 1; i <= n; i++)
    d[i] = vec[i].dist, sz[i] = 1;

  for (int i = n; i > 1; i--) {
    int pos = lower_bound(d + 1, d + i, d[i] - (n - sz[i]) + sz[i]) - d;
    if (pos >= i || d[pos] != d[i] - (n - sz[i]) + sz[i]) {
      flag = false;
      break;
    }
    tree.push_back(make_pair(vec[i].id, vec[pos].id));
    sz[pos] += sz[i];
  }

  dep[vec[1].id] = 0;
  for (int i = n - 2; i >= 0; i--)
    dep[tree[i].first] = dep[tree[i].second] + 1;

  for (int i = 1; i <= n; i++)
    d[1] -= dep[i];
  if (d[1])
    flag = false;

  if (!flag)
    write(-1), putchar('\n');
  else {
    for (int i = 0; i < tree.size(); i++) {
      write(tree[i].first), putchar(' ');
      write(tree[i].second), putchar('\n');
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 100005;

template <class T> void read(T &x) {
  int sgn = 1;
  char ch;
  x = 0;
  for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    ch = getchar(), sgn = -1;
  for (; '0' <= ch && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= sgn;
}

template <class T> void write(T x) {
  if (x < 0)
    putchar('-'), write(-x);
  else if (x < 10)
    putchar(x + '0');
  else
    write(x / 10), putchar(x % 10 + '0');
}

int n, dep[N], sz[N];
bool flag = true;

struct node {
  int id;
  long long dist;
  bool operator<(node rhs) const { return dist < rhs.dist; }
} vec[N];
long long d[N];
vector<pair<int, int>> tree;

int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    vec[i].id = i, read(vec[i].dist);

  sort(vec + 1, vec + n + 1);
  for (int i = 1; i <= n; i++)
    d[i] = vec[i].dist, sz[i] = 1;

  for (int i = n; i > 1; i--) {
    int pos = lower_bound(d + 1, d + i, d[i] - (n - sz[i]) + sz[i]) - d;
    if (pos >= i || d[pos] != d[i] - (n - sz[i]) + sz[i]) {
      flag = false;
      break;
    }
    tree.push_back(make_pair(vec[i].id, vec[pos].id));
    sz[pos] += sz[i];
  }

  if (flag) {
    dep[vec[1].id] = 0;
    for (int i = n - 2; i >= 0; i--)
      dep[tree[i].first] = dep[tree[i].second] + 1;
    for (int i = 1; i <= n; i++)
      d[1] -= dep[i];
    if (d[1])
      flag = false;
  }
  if (!flag)
    write(-1), putchar('\n');
  else {
    for (int i = 0; i < tree.size(); i++) {
      write(tree[i].first), putchar(' ');
      write(tree[i].second), putchar('\n');
    }
  }
  return 0;
}