Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int gcd(int a, int b) {
  int c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
typedef long long ll;

ll M = 1000000007;

vector<ll> fac(300001);  // n!(mod M)
vector<ll> ifac(300001); // k!^{M-2} (mod M)

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}
ll comb(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % M;
  return tmp * fac[a] % M;
}
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a) {
  long long b = M, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= M;
  if (u < 0)
    u += M;
  return u;
}
vector<vector<ll>> mul(vector<vector<ll>> a, vector<vector<ll>> b, int n) {
  int i, j, k, t;
  vector<vector<ll>> c(n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      t = 0;
      for (k = 0; k < n; k++)
        t = (t + a[i][k] * b[k][j] % M) % M;
      c[i].push_back(t);
    }
  }
  return c;
}
int main() {
  int n, x, i, count, ans[333333], p[555], t, r[555];
  cin >> n;
  vector<pair<int, int>> v;
  for (i = 1; i <= n * n; i++)
    ans[i] = 0;
  for (i = 1; i <= n; i++) {
    cin >> x;
    v.push_back(make_pair(x, i));
    p[i] = n - 1;
  }
  sort(v.begin(), v.end());
  t = 1;
  for (auto &e : v) {
    ans[e.first] = e.second;
    count = e.second - 1;
    r[e.second] = e.first;
    for (; t < e.first && count > 0; t++) {
      if (ans[t] == 0) {
        ans[t] = e.second;
        count--;
        p[e.second]--;
      }
    }
    if (count > 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (auto &e : v) {
    while (ans[t] == 0)
      t++;
    if (t < e.first) {
      cout << "No" << endl;
      return 0;
    }
    for (; p[e.second] > 0; t++) {
      if (ans[t] == 0) {
        ans[t] = e.second;
        p[e.second]--;
      }
    }
  }
  cout << "Yes" << endl;
  for (i = 1; i <= n * n; i++)
    cout << ans[i] << " ";
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int gcd(int a, int b) {
  int c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
typedef long long ll;

ll M = 1000000007;

vector<ll> fac(300001);  // n!(mod M)
vector<ll> ifac(300001); // k!^{M-2} (mod M)

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}
ll comb(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % M;
  return tmp * fac[a] % M;
}
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a) {
  long long b = M, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= M;
  if (u < 0)
    u += M;
  return u;
}
vector<vector<ll>> mul(vector<vector<ll>> a, vector<vector<ll>> b, int n) {
  int i, j, k, t;
  vector<vector<ll>> c(n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      t = 0;
      for (k = 0; k < n; k++)
        t = (t + a[i][k] * b[k][j] % M) % M;
      c[i].push_back(t);
    }
  }
  return c;
}
int main() {
  int n, x, i, count, ans[333333], p[555], t, r[555];
  cin >> n;
  vector<pair<int, int>> v;
  for (i = 1; i <= n * n; i++)
    ans[i] = 0;
  for (i = 1; i <= n; i++) {
    cin >> x;
    v.push_back(make_pair(x, i));
    p[i] = n - 1;
  }
  sort(v.begin(), v.end());
  t = 1;
  for (auto &e : v) {
    ans[e.first] = e.second;
    count = e.second - 1;
    r[e.second] = e.first;
    for (; t < e.first && count > 0; t++) {
      if (ans[t] == 0) {
        ans[t] = e.second;
        count--;
        p[e.second]--;
      }
    }
    if (count > 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (auto &e : v) {
    while (ans[t] != 0)
      t++;
    if (t < e.first) {
      cout << "No" << endl;
      return 0;
    }
    for (; p[e.second] > 0; t++) {
      if (ans[t] == 0) {
        ans[t] = e.second;
        p[e.second]--;
      }
    }
  }
  cout << "Yes" << endl;
  for (i = 1; i <= n * n; i++)
    cout << ans[i] << " ";
}