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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

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

  int N;
  cin >> N;
  pair<int, int> X[501];
  int kotae[250001];
  rep1(i, N) {
    cin >> X[i].first;
    X[i].second = i;
    kotae[X[i].first] = i;
  }

  sort(X + 1, X + N + 1);

  queue<pair<int, int>> que;
  rep1(i, N) { rep(j, X[i].second - 1) que.push(mp(X[i].second, X[i].first)); }

  int OK = 1;
  rep1(i, N * N) {
    if (kotae[i] == 0) {
      if (que.size() == 0) {
        OK = 0;
        break;
      }
      auto q = que.front();
      que.pop();
      if (q.second < i) {
        OK = 0;
        break;
      }
      kotae[i] = q.first;
    } else {
      rep(j, N - kotae[i]) que.push(mp(kotae[i], 300000));
    }
  }

  if (OK) {
    co("Yes");
    rep1(i, N * N) co(kotae[i]);
  } else
    co("No");

  Would you please return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

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

  int N;
  cin >> N;
  pair<int, int> X[501];
  int kotae[250001] = {};
  rep1(i, N) {
    cin >> X[i].first;
    X[i].second = i;
    kotae[X[i].first] = i;
  }

  sort(X + 1, X + N + 1);

  queue<pair<int, int>> que;
  rep1(i, N) { rep(j, X[i].second - 1) que.push(mp(X[i].second, X[i].first)); }

  int OK = 1;
  rep1(i, N * N) {
    if (kotae[i] == 0) {
      if (que.size() == 0) {
        OK = 0;
        break;
      }
      auto q = que.front();
      que.pop();
      if (q.second < i) {
        OK = 0;
        break;
      }
      kotae[i] = q.first;
    } else {
      rep(j, N - kotae[i]) que.push(mp(kotae[i], 300000));
    }
  }

  if (OK) {
    co("Yes");
    rep1(i, N * N) co(kotae[i]);
  } else
    co("No");

  Would you please return 0;
}
Problem p03841 (C++)

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

int N;
int X[505], Cnt[505], Num[505], Ans[505];

int main() {
  int i, j, k;
  scanf("%d", &N);
  for (i = 1; i <= N; i++) {
    scanf("%d", &X[i]);
    Ans[X[i]] = i;
  }
  for (i = 1, j = 0; i <= N * N; i++)
    if (Ans[i])
      Num[++j] = Ans[i];
  for (i = 1; i <= N; i++)
    Cnt[i] = i - 1;
  for (i = 1; i <= N * N; i++)
    if (!Ans[i])
      for (j = 1; j <= N; j++)
        if (Cnt[Num[j]]) {
          Cnt[Ans[i] = Num[j]]--;
          break;
        }
  for (i = 1; i <= N; i++)
    Cnt[i] = N - i;
  for (i = N * N; i; i--)
    if (!Ans[i])
      for (j = N; j; j--)
        if (Cnt[Num[j]]) {
          Cnt[Ans[i] = Num[j]]--;
          break;
        }
  for (i = 1; i <= N; i++)
    for (j = 1, k = 0; j <= N * N; j++) {
      k += (Ans[j] == i);
      if (k == i) {
        if (j ^ X[i]) {
          puts("No");
          return 0;
        }
        break;
      }
    }
  puts("Yes");
  for (i = 1; i <= N * N; i++)
    printf("%d ", Ans[i]);
  return 0;
}

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

int N;
int X[505], Cnt[505], Num[505], Ans[505 * 505];

int main() {
  int i, j, k;
  scanf("%d", &N);
  for (i = 1; i <= N; i++) {
    scanf("%d", &X[i]);
    Ans[X[i]] = i;
  }
  for (i = 1, j = 0; i <= N * N; i++)
    if (Ans[i])
      Num[++j] = Ans[i];
  for (i = 1; i <= N; i++)
    Cnt[i] = i - 1;
  for (i = 1; i <= N * N; i++)
    if (!Ans[i])
      for (j = 1; j <= N; j++)
        if (Cnt[Num[j]]) {
          Cnt[Ans[i] = Num[j]]--;
          break;
        }
  for (i = 1; i <= N; i++)
    Cnt[i] = N - i;
  for (i = N * N; i; i--)
    if (!Ans[i])
      for (j = N; j; j--)
        if (Cnt[Num[j]]) {
          Cnt[Ans[i] = Num[j]]--;
          break;
        }
  for (i = 1; i <= N; i++)
    for (j = 1, k = 0; j <= N * N; j++) {
      k += (Ans[j] == i);
      if (k == i) {
        if (j ^ X[i]) {
          puts("No");
          return 0;
        }
        break;
      }
    }
  puts("Yes");
  for (i = 1; i <= N * N; i++)
    printf("%d ", Ans[i]);
  return 0;
}
Problem p03841 (C++)

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

const int MAXN = 1e6 + 10;
int N;
int A[MAXN];
int x[MAXN], rk[MAXN];
int cnt[MAXN];

int main() {
  register int i, j = 0;
  scanf("%d", &N);
  for (i = 1; i <= N; ++i)
    scanf("%d", x + i), rk[i] = i, cnt[i] = N;
  sort(rk + 1, rk + N + 1, [&](int a, int b) { return x[a] < x[b]; });
  for (i = 1; i <= N; ++i) {
    while (cnt[rk[i]] > N - rk[i] + 1) {
      --cnt[rk[i]];
      while (j <= N * N && A[j + 1])
        ++j;
      if (j > N * N)
        return puts("No"), 0;
      A[++j] = rk[i];
    }
    if (A[x[rk[i]]])
      return puts("No"), 0;
    A[x[rk[i]]] = rk[i];
    --cnt[rk[i]];
  }
  i = 1;
  for (; j <= N * N; ++j) {
    if (A[j])
      continue;
    if (cnt[rk[i]] > 0)
      --cnt[rk[i]], A[j] = rk[i];
    else {
      while (cnt[rk[i]] == 0)
        ++i;
      --cnt[rk[i]];
      A[j] = rk[i];
    }
    if (j <= x[rk[i]])
      return puts("No"), 0;
    if (i > N)
      return puts("No"), 0;
  }
  puts("Yes");
  for (i = 1; i <= N * N; ++i)
    printf("%d%c", A[i], " \n"[i == N * N]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
using namespace std;

const int MAXN = 1e6 + 10;
int N;
int A[MAXN];
int x[MAXN], rk[MAXN];
int cnt[MAXN];

int main() {
  register int i, j = 0;
  scanf("%d", &N);
  for (i = 1; i <= N; ++i)
    scanf("%d", x + i), rk[i] = i, cnt[i] = N;
  if (N == 1)
    return puts(x[1] == 1 ? "Yes\n1" : "No"), 0;
  sort(rk + 1, rk + N + 1, [&](int a, int b) { return x[a] < x[b]; });
  for (i = 1; i <= N; ++i) {
    while (cnt[rk[i]] > N - rk[i] + 1) {
      --cnt[rk[i]];
      while (j <= N * N && A[j + 1])
        ++j;
      if (j > N * N)
        return puts("No"), 0;
      A[++j] = rk[i];
    }
    if (A[x[rk[i]]])
      return puts("No"), 0;
    A[x[rk[i]]] = rk[i];
    --cnt[rk[i]];
  }
  i = 1;
  for (; j <= N * N; ++j) {
    if (A[j])
      continue;
    if (cnt[rk[i]] > 0)
      --cnt[rk[i]], A[j] = rk[i];
    else {
      while (cnt[rk[i]] == 0)
        ++i;
      --cnt[rk[i]];
      A[j] = rk[i];
    }
    if (j <= x[rk[i]])
      return puts("No"), 0;
    if (i > N)
      return puts("No"), 0;
  }
  puts("Yes");
  for (i = 1; i <= N * N; ++i)
    printf("%d%c", A[i], " \n"[i == N * N]);
  return 0;
}
Problem p03841 (C++)

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

const int N = 505;

int n, x[N], rem[N], a[N * N];
vector<int> vec;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    a[--x[i]] = i;
    rem[i] = i - 1;
  }
  for (int i = 0; i < n * n; i++)
    if (a[i])
      vec.push_back(a[i]);
  int p = 0;
  for (int num : vec)
    while (rem[num]--) {
      while (a[p])
        p++;
      if (p > x[num])
        break;
      a[p] = num;
    }
  for (int i = 1; i <= n; i++)
    rem[i] = n - i;
  for (int num : vec)
    while (rem[num]--) {
      while (a[p] || p < x[num])
        p++;
      a[p] = num;
    }
  for (int i = 0; i < n * n; i++)
    if (a[i] == 0) {
      cout << "No\n";
      return 0;
    }
  cout << "Yes\n";
  for (int i = 0; i < n * n; i++)
    cout << a[i] << ' ';
}


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

const int N = 505;

int n, x[N], rem[N], a[N * N];
vector<int> vec;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    a[--x[i]] = i;
    rem[i] = i - 1;
  }
  for (int i = 0; i < n * n; i++)
    if (a[i])
      vec.push_back(a[i]);
  int p = 0;
  for (int num : vec)
    while (rem[num]--) {
      while (a[p])
        p++;
      if (p > x[num])
        break;
      a[p] = num;
    }
  for (int i = 1; i <= n; i++)
    rem[i] = n - i;
  for (int num : vec)
    while (rem[num]--) {
      while (a[p] || p < x[num])
        p++;
      if (p >= n * n)
        break;
      a[p] = num;
    }
  for (int i = 0; i < n * n; i++)
    if (a[i] == 0) {
      cout << "No\n";
      return 0;
    }
  cout << "Yes\n";
  for (int i = 0; i < n * n; i++)
    cout << a[i] << ' ';
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
const int N = 2501;
int n, a, b[N], c[N], cnt;
queue<int> q, qu;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    b[a] = i;
    c[a] = n - i + 1;
  }
  for (int i = 1; i <= n * n; ++i) {
    if (!b[i]) {
      ++cnt;
      continue;
    }
    if (cnt < b[i] - 1) {
      cout << "No";
      return 0;
    }
    cnt -= b[i] - 1;
    for (int j = 1; j < b[i]; ++j)
      q.push(b[i]);
  }
  cnt = 0;
  for (int i = n * n; i >= 1; --i) {
    if (!c[i]) {
      ++cnt;
      continue;
    }
    if (cnt < c[i] - 1) {
      cout << "No";
      return 0;
    }
    cnt -= c[i] - 1;
    for (int j = 1; j < c[i]; ++j)
      qu.push(n - c[i] + 1);
  }
  for (int i = 1; i <= n * n && sz(q) != 0; ++i) {
    if (!b[i]) {
      b[i] = q.front();
      q.pop();
    }
  }
  for (int i = n * n; i >= 1 && sz(qu) != 0; --i) {
    if (!b[i]) {
      b[i] = qu.front();
      qu.pop();
    }
  }
  cout << "Yes\n";
  for (int i = 1; i <= n * n; ++i)
    cout << b[i] << " ";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
const int N = 1000000;
int n, a, b[N], c[N], cnt;
queue<int> q, qu;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    b[a] = i;
    c[a] = n - i + 1;
  }
  for (int i = 1; i <= n * n; ++i) {
    if (!b[i]) {
      ++cnt;
      continue;
    }
    if (cnt < b[i] - 1) {
      cout << "No";
      return 0;
    }
    cnt -= b[i] - 1;
    for (int j = 1; j < b[i]; ++j)
      q.push(b[i]);
  }
  cnt = 0;
  for (int i = n * n; i >= 1; --i) {
    if (!c[i]) {
      ++cnt;
      continue;
    }
    if (cnt < c[i] - 1) {
      cout << "No";
      return 0;
    }
    cnt -= c[i] - 1;
    for (int j = 1; j < c[i]; ++j)
      qu.push(n - c[i] + 1);
  }
  for (int i = 1; i <= n * n && sz(q) != 0; ++i) {
    if (!b[i]) {
      b[i] = q.front();
      q.pop();
    }
  }
  for (int i = n * n; i >= 1 && sz(qu) != 0; --i) {
    if (!b[i]) {
      b[i] = qu.front();
      qu.pop();
    }
  }
  cout << "Yes\n";
  for (int i = 1; i <= n * n; ++i)
    cout << b[i] << " ";
  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MN = 505;

int ans[MN * MN];
ii x[MN];

int main() {
  if (fopen("in", "r"))
    freopen("in", "r", stdin), freopen("out", "w", stdout);
  ios_base::sync_with_stdio(0), cin.tie(0);

  int N;
  cin >> N;
  set<int> S;
  for (int i = 0; i < N * N; ++i)
    S.insert(i);
  for (int i = 0; i < N; ++i) {
    cin >> x[i].f;
    S.erase(--x[i].f), ans[x[i].f] = x[i].s = i;
  }
  sort(x, x + N);
  for (int i = 0; i < N; ++i) {
    int j = 0;
    while (S.size() && *begin(S) < x[i].f && j < x[i].s)
      ans[*begin(S)] = x[i].s, S.erase(begin(S)), ++j;
    if (j < x[i].s) {
      cout << "No";
      return 0;
    }
  }
  for (int i = 0; i < N; ++i) {
    int j = 0;
    auto it = S.upper_bound(x[i].f);
    while (S.size() && j < N - x[i].s - 1)
      ans[*it] = x[i].s, it = S.erase(it), ++j;
    if (j < N - x[i].s - 1) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\n";
  for (int i = 0; i < N * N; ++i)
    cout << ans[i] + 1 << ' ';
}

Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MN = 505;

int ans[MN * MN];
ii x[MN];

int main() {
  if (fopen("in", "r"))
    freopen("in", "r", stdin), freopen("out", "w", stdout);
  ios_base::sync_with_stdio(0), cin.tie(0);

  int N;
  cin >> N;
  set<int> S;
  for (int i = 0; i < N * N; ++i)
    S.insert(i);
  for (int i = 0; i < N; ++i) {
    cin >> x[i].f;
    S.erase(--x[i].f), ans[x[i].f] = x[i].s = i;
  }
  sort(x, x + N);
  for (int i = 0; i < N; ++i) {
    int j = 0;
    while (S.size() && *begin(S) < x[i].f && j < x[i].s)
      ans[*begin(S)] = x[i].s, S.erase(begin(S)), ++j;
    if (j < x[i].s) {
      cout << "No";
      return 0;
    }
  }
  for (int i = 0; i < N; ++i) {
    int j = 0;
    auto it = S.upper_bound(x[i].f);
    while (it != end(S) && j < N - x[i].s - 1)
      ans[*it] = x[i].s, it = S.erase(it), ++j;
    if (j < N - x[i].s - 1) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\n";
  for (int i = 0; i < N * N; ++i)
    cout << ans[i] + 1 << ' ';
}
Problem p03841 (C++)

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

using namespace std;

typedef pair<int, int> P;
bool used[500];

int main() {
  int N;
  int ans[250000];
  P x[500];
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> x[i].first;
    x[i].first--;
    x[i].second = i + 1;
    used[x[i].first] = true;
    ans[x[i].first] = i + 1;
  }
  sort(x, x + N);
  int idx = 0;
  for (int i = 0; i < N; i++) {
    // cout << x[i].first << ' ' << x[i].second << endl;
    int n = x[i].second;
    int cnt = 0;
    while (cnt < n - 1) {
      if (idx > x[i].first) {
        cout << "No" << endl;
        return 0;
      }
      if (!used[idx]) {
        cnt++;
        ans[idx] = n;
      }
      idx++;
    }
  }
  idx = N * N - 1;
  for (int i = N - 1; i >= 0; i--) {
    // cout << x[i].first << ' ' << x[i].second << endl;
    int n = N - x[i].second;
    // cout << n << endl;
    int cnt = 0;
    while (cnt < n) {
      if (idx < x[i].first) {
        cout << "No" << endl;
        return 0;
      }
      if (!used[idx]) {
        cnt++;
        ans[idx] = x[i].second;
      }
      idx--;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N * N; i++) {
    cout << ans[i];
    if (i < N * N - 1)
      cout << ' ';
    else
      cout << '\n';
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<int, int> P;
bool used[250000];

int main() {
  int N;
  int ans[250000];
  P x[500];
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> x[i].first;
    x[i].first--;
    x[i].second = i + 1;
    used[x[i].first] = true;
    ans[x[i].first] = i + 1;
  }
  sort(x, x + N);
  int idx = 0;
  for (int i = 0; i < N; i++) {
    // cout << x[i].first << ' ' << x[i].second << endl;
    int n = x[i].second;
    int cnt = 0;
    while (cnt < n - 1) {
      if (idx > x[i].first) {
        cout << "No" << endl;
        return 0;
      }
      if (!used[idx]) {
        cnt++;
        ans[idx] = n;
      }
      idx++;
    }
  }
  idx = N * N - 1;
  for (int i = N - 1; i >= 0; i--) {
    // cout << x[i].first << ' ' << x[i].second << endl;
    int n = N - x[i].second;
    // cout << n << endl;
    int cnt = 0;
    while (cnt < n) {
      if (idx < x[i].first) {
        cout << "No" << endl;
        return 0;
      }
      if (!used[idx]) {
        cnt++;
        ans[idx] = x[i].second;
      }
      idx--;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N * N; i++) {
    cout << ans[i];
    if (i < N * N - 1)
      cout << ' ';
    else
      cout << '\n';
  }
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define sq(x) ((x) * (x))

const int mxn = 500;
int n;
int a[sq(mxn)];
pi p[mxn];

void die() {
  cout << "No" << endl;
  exit(0);
}

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

  cin >> n;

  memset(a, -1, sizeof(a));
  for (int i = 0; i < n; i++) {
    cin >> p[i].f;
    a[--p[i].f] = p[i].s = i;
  }

  sort(p, p + n);

  int x = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < p[i].s; j++) {
      while (~a[x])
        x++;
      a[x] = p[i].s;
    }
    if (x > p[i].f)
      die();
  }

  for (int i = 0; i < n; i++) {
    while (~a[x])
      x++;
    if (x < p[i].f)
      die();
    for (int j = 0; j < n - p[i].s - 1; j++) {
      while (~a[x])
        x++;
      a[x] = p[i].s;
    }
  }

  cout << "Yes" << endl;
  cout << a[0] + 1;
  for (int i = 1; i < sq(n); i++)
    cout << " " << a[i] + 1;
  cout << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
#define sq(x) ((x) * (x))

const int mxn = 501;
int n;
int a[sq(mxn)];
pi p[mxn];

void die() {
  cout << "No" << endl;
  exit(0);
}

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

  cin >> n;

  memset(a, -1, sizeof(a));
  for (int i = 0; i < n; i++) {
    cin >> p[i].f;
    a[--p[i].f] = p[i].s = i;
  }

  sort(p, p + n);

  int x = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < p[i].s; j++) {
      while (~a[x])
        x++;
      a[x] = p[i].s;
    }
    if (x > p[i].f)
      die();
  }

  for (int i = 0; i < n; i++) {
    while (~a[x])
      x++;
    if (x < p[i].f)
      die();
    for (int j = 0; j < n - p[i].s - 1; j++) {
      while (~a[x])
        x++;
      a[x] = p[i].s;
    }
  }

  cout << "Yes" << endl;
  cout << a[0] + 1;
  for (int i = 1; i < sq(n); i++)
    cout << " " << a[i] + 1;
  cout << endl;

  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

const int N = 555;

int n, x[N], a[N];
int ptr = 1;

int go(int times, int who) {
  int first = -1;
  for (int i = 0; i < times; i++) {
    while (a[ptr] != -1)
      ptr++;
    a[ptr] = who;
    if (first == -1)
      first = ptr;
  }
  return first;
}

int main() {
  scanf("%d", &n);
  vector<pair<int, int>> pos;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i]);
    pos.push_back({x[i], i});
  }
  fill(a + 1, a + n * n + 1, -1);
  sort(pos.begin(), pos.end());
  for (auto p : pos) {
    int where = p.first, who = p.second;
    go(who - 1, who);
    if (a[where] != -1) {
      // printf("%d %d\n", where, who);
      puts("No");
      return 0;
    }
    a[where] = who;
  }
  for (auto p : pos) {
    int where = p.first, who = p.second;
    if (n - who && go(n - who, who) < where) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (int i = 1; i <= n * n; i++) {
    printf("%d%c", a[i], "\n "[i < n * n]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

const int N = 555;

int n, x[N], a[N * N];
int ptr = 1;

int go(int times, int who) {
  int first = -1;
  for (int i = 0; i < times; i++) {
    while (a[ptr] != -1)
      ptr++;
    a[ptr] = who;
    if (first == -1)
      first = ptr;
  }
  return first;
}

int main() {
  scanf("%d", &n);
  vector<pair<int, int>> pos;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i]);
    pos.push_back({x[i], i});
  }
  fill(a + 1, a + n * n + 1, -1);
  sort(pos.begin(), pos.end());
  for (auto p : pos) {
    int where = p.first, who = p.second;
    go(who - 1, who);
    if (a[where] != -1) {
      // printf("%d %d\n", where, who);
      puts("No");
      return 0;
    }
    a[where] = who;
  }
  for (auto p : pos) {
    int where = p.first, who = p.second;
    if (n - who && go(n - who, who) < where) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (int i = 1; i <= n * n; i++) {
    printf("%d%c", a[i], "\n "[i < n * n]);
  }
  return 0;
}
Problem p03841 (C++)

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

using namespace std;

int i, j, n, x[510], a[300000], b[300000], y[510];
pair<int, int> p[510];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x[i];
    a[x[i]] = i;
    b[x[i]] = 1;
    y[i] = n - i;
    p[i] = make_pair(x[i], i);
  }

  for (i = 1; i <= n * n; i++) {
    if (b[i] == 1) {
      int cnt = a[i] - 1;
      for (j = 1; j <= i; j++) {
        if (cnt == 0) {
          break;
        }
        if (b[j] == 0) {
          a[j] = a[i];
          b[j] = 1;
          cnt--;
        }
        if (j == i) {
          cout << "No" << endl;
          return 0;
        }
      }
    }
  }
  sort(p, p + n);
  for (i = 1; i <= n * n; i++) {
    if (b[i] == 0) {
      for (j = 1; j <= n; j++) {
        if (y[p[j].second] != 0) {
          y[p[j].second]--;
          a[i] = p[j].second;
          b[i] = 1;
          break;
        }
      }
    }
  }
  for (i = 1; i <= n * n; i++) {
    b[i] = 0;
  }
  for (i = 1; i <= n * n; i++) {
    b[a[i]]++;
    if (b[a[i]] == a[i]) {
      if (i != x[i]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  cout << "Yes" << endl;
  for (i = 1; i <= n * n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>

using namespace std;

int i, j, n, x[510], a[300000], b[300000], y[510];
pair<int, int> p[510];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x[i];
    a[x[i]] = i;
    b[x[i]] = 1;
    y[i] = n - i;
    p[i] = make_pair(x[i], i);
  }

  for (i = 1; i <= n * n; i++) {
    if (b[i] == 1) {
      int cnt = a[i] - 1;
      for (j = 1; j <= i; j++) {
        if (cnt == 0) {
          break;
        }
        if (b[j] == 0) {
          a[j] = a[i];
          b[j] = 1;
          cnt--;
        }
        if (j == i) {
          cout << "No" << endl;
          return 0;
        }
      }
    }
  }
  sort(p, p + n);
  for (i = 1; i <= n * n; i++) {
    if (b[i] == 0) {
      for (j = 1; j <= n; j++) {
        if (y[p[j].second] != 0) {
          y[p[j].second]--;
          a[i] = p[j].second;
          b[i] = 1;
          break;
        }
      }
    }
  }
  for (i = 1; i <= n * n; i++) {
    b[i] = 0;
  }
  for (i = 1; i <= n * n; i++) {
    b[a[i]]++;
    if (b[a[i]] == a[i]) {
      if (i != x[a[i]]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  cout << "Yes" << endl;
  for (i = 1; i <= n * n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
const int maxn = 250010;
using namespace std;
int a[maxn], b[maxn], n;
priority_queue<pair<int, int>> p;
int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &b[i]), a[b[i]] = i;
    rep(j, 1, i - 1) p.push(make_pair(-(b[i] - 1), i));
  }
  rep(i, 1, n * n) {
    if (a[i]) {
      rep(j, a[i] + 1, n) p.push(make_pair(-(n * n + 1), a[i]));
    } else {
      if (p.top().first > -i) {
        printf("No");
        return 0;
      }
      a[i] = p.top().second;
      p.pop();
    }
  }
  printf("Yes\n");
  rep(i, 1, n * n) printf("%d ", a[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
const int maxn = 250010;
using namespace std;
int a[maxn], b[maxn], n;
priority_queue<pair<int, int>> p;
int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &b[i]), a[b[i]] = i;
    rep(j, 1, i - 1) p.push(make_pair(-(b[i] - 1), i));
  }
  rep(i, 1, n * n) {
    if (a[i]) {
      rep(j, a[i] + 1, n) p.push(make_pair(-(n * n + 1), a[i]));
    } else {
      if (p.empty() || p.top().first > -i) {
        printf("No");
        return 0;
      }
      a[i] = p.top().second;
      p.pop();
    }
  }
  printf("Yes\n");
  rep(i, 1, n * n) printf("%d ", a[i]);
  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
pair<int, int> x[505];
int ans[505], c[505];
int main() {
  int n, cur = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i].first);
    x[i].second = i;
    ans[x[i].first] = i;
  }
  sort(x + 1, x + n + 1);
  for (int i = 1; i <= n; i++) {
    int cnt = x[i].second - 1;
    while (cnt--) {
      while (ans[cur])
        cur++;
      ans[cur] = x[i].second;
    }
  }
  for (int i = 1; i <= n; i++) {
    int cnt = n - x[i].second;
    while (cnt--) {
      while (ans[cur])
        cur++;
      ans[cur] = x[i].second;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    c[ans[i]]++;
    if (c[ans[i]] == ans[i]) {
      for (int j = 1; j <= n; j++) {
        if (x[j].second == ans[i] && x[j].first != i) {
          printf("No");
          return 0;
        }
      }
    }
  }
  printf("Yes\n");
  for (int i = 1; i <= n * n; i++)
    printf("%d ", ans[i]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
pair<int, int> x[505];
int ans[250005], c[505];
int main() {
  int n, cur = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i].first);
    x[i].second = i;
    ans[x[i].first] = i;
  }
  sort(x + 1, x + n + 1);
  for (int i = 1; i <= n; i++) {
    int cnt = x[i].second - 1;
    while (cnt--) {
      while (ans[cur])
        cur++;
      ans[cur] = x[i].second;
    }
  }
  for (int i = 1; i <= n; i++) {
    int cnt = n - x[i].second;
    while (cnt--) {
      while (ans[cur])
        cur++;
      ans[cur] = x[i].second;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    c[ans[i]]++;
    if (c[ans[i]] == ans[i]) {
      for (int j = 1; j <= n; j++) {
        if (x[j].second == ans[i] && x[j].first != i) {
          printf("No");
          return 0;
        }
      }
    }
  }
  printf("Yes\n");
  for (int i = 1; i <= n * n; i++)
    printf("%d ", ans[i]);
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int)x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define all(x) x.begin(), x.end()
#define ull unsigned long long

using namespace std;

const int N = 505;

int n;
int x[N];
vector<int> v;
int L[N], R[N];
int a[N];

int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", x + i);
    a[x[i]] = i;
    L[i] = i - 1;
    R[i] = n - i;
  }
  rep(i, 1, n * n) {
    if (a[i]) {
      v.pb(a[i]);
      if (L[a[i]] != 0)
        return printf("No\n"), 0;
      continue;
    }
    // 1. L
    int best = -1;
    rep(j, 1, n) if (i < x[j] && (best == -1 || x[j] < x[best]) && L[j] > 0)
        best = j;
    if (best != -1) {
      v.pb(best);
      L[best]--;
      continue;
    }
    // 2. R
    rep(j, 1, n) if (x[j] < i && R[j]) {
      v.pb(j);
      R[j]--;
      break;
    }
    if (ss(v) != i)
      return printf("No\n"), 0;
  }
  printf("Yes\n");
  for (auto it : v)
    printf("%d ", it);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int)x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define all(x) x.begin(), x.end()
#define ull unsigned long long

using namespace std;

const int N = 505 * 505;

int n;
int x[N];
vector<int> v;
int L[N], R[N];
int a[N];

int main() {
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", x + i);
    a[x[i]] = i;
    L[i] = i - 1;
    R[i] = n - i;
  }
  rep(i, 1, n * n) {
    if (a[i]) {
      v.pb(a[i]);
      if (L[a[i]] != 0)
        return printf("No\n"), 0;
      continue;
    }
    // 1. L
    int best = -1;
    rep(j, 1, n) if (i < x[j] && (best == -1 || x[j] < x[best]) && L[j] > 0)
        best = j;
    if (best != -1) {
      v.pb(best);
      L[best]--;
      continue;
    }
    // 2. R
    rep(j, 1, n) if (x[j] < i && R[j]) {
      v.pb(j);
      R[j]--;
      break;
    }
    if (ss(v) != i)
      return printf("No\n"), 0;
  }
  printf("Yes\n");
  for (auto it : v)
    printf("%d ", it);

  return 0;
}
Problem p03841 (C++)

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

#define se second
#define fi first
#define mk(a, b) make_pair(a, b)

int N, X[100010];
int ans[100010] = {0};
vector<pair<int, int>> V;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", X + i);
  for (int i = 0; i < N; i++)
    ans[X[i] - 1] = 1 + i;
  for (int i = 0; i < N; i++)
    V.push_back(mk(X[i], i));
  sort(V.begin(), V.end());
  vector<int> mem, mem2;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < V[i].se; j++)
      mem.push_back(V[i].se + 1);
    for (int j = 0; j < N - V[i].se - 1; j++)
      mem2.push_back(V[i].se + 1);
  }
  reverse(mem2.begin(), mem2.end());

  int cnt = 0;
  for (int i = 0; i < N * N; i++) {
    if (cnt != mem.size() && ans[i] == 0) {
      ans[i] = mem[cnt];
      cnt++;
    }
  }
  cnt = 0;
  for (int i = N * N - 1; i >= 0; i--) {
    if (cnt != mem2.size() && ans[i] == 0) {
      ans[i] = mem2[cnt];
      cnt++;
    }
  }
  for (int i = 1; i <= N; i++) {
    cnt = 0;
    for (int j = 0; j < N * N; j++) {
      if (X[i - 1] == j + 1) {
        if (cnt != i - 1) {
          printf("No\n");
          return 0;
        }
      }
      if (ans[j] == i)
        cnt++;
    }
  }
  printf("Yes\n");
  for (int i = 0; i < N * N; i++) {
    printf("%d", ans[i]);
    if (i != N * N - 1)
      printf(" ");
  }
  printf("\n");
  return 0;
}


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

#define se second
#define fi first
#define mk(a, b) make_pair(a, b)

int N, X[300010];
int ans[300010] = {0};
vector<pair<int, int>> V;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", X + i);
  for (int i = 0; i < N; i++)
    ans[X[i] - 1] = 1 + i;
  for (int i = 0; i < N; i++)
    V.push_back(mk(X[i], i));
  sort(V.begin(), V.end());
  vector<int> mem, mem2;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < V[i].se; j++)
      mem.push_back(V[i].se + 1);
    for (int j = 0; j < N - V[i].se - 1; j++)
      mem2.push_back(V[i].se + 1);
  }
  reverse(mem2.begin(), mem2.end());

  int cnt = 0;
  for (int i = 0; i < N * N; i++) {
    if (cnt != mem.size() && ans[i] == 0) {
      ans[i] = mem[cnt];
      cnt++;
    }
  }
  cnt = 0;
  for (int i = N * N - 1; i >= 0; i--) {
    if (cnt != mem2.size() && ans[i] == 0) {
      ans[i] = mem2[cnt];
      cnt++;
    }
  }
  for (int i = 1; i <= N; i++) {
    cnt = 0;
    for (int j = 0; j < N * N; j++) {
      if (X[i - 1] == j + 1) {
        if (cnt != i - 1) {
          printf("No\n");
          return 0;
        }
      }
      if (ans[j] == i)
        cnt++;
    }
  }
  printf("Yes\n");
  for (int i = 0; i < N * N; i++) {
    printf("%d", ans[i]);
    if (i != N * N - 1)
      printf(" ");
  }
  printf("\n");
  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define maxn 501
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair
#define mod 1000000007

int n, m, k, x, y, z;
vector<pair<int, int>> a;
int b[maxn];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    a.pb(mp(x, i));
  }
  sort(a.begin(), a.end());
  int l = 0;
  rep(i, n) {
    int pos = a[i].first;
    int num = a[i].second;
    if (b[pos] > 0) {
      cout << "No";
      return;
    }
    b[pos] = num;
    rep(_, num - 1) {
      while (b[++l] > 0)
        ;
      b[l] = num;
    }
    if (l > pos) {
      cout << "No";
      return;
    }
  }
  int r = n * n + 1;
  reverse(a.begin(), a.end());
  for (auto p : a) {
    int pos = p.first;
    int num = p.second;
    rep(_, n - num) {
      while (r >= 0 && b[--r] > 0) {
      }
      if (r == 0) {
        cout << "No";
        return;
      }
      b[r] = num;
    }
    // cout << pos << ' ' << num << ' ' << n - num << ' ' << r << endl;
    if (r < pos) {
      cout << "No";
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n * n; ++i) {
    cout << b[i] << ' ';
  }
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  solve();
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define maxn 501
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair
#define mod 1000000007

int n, m, k, x, y, z;
vector<pair<int, int>> a;
int b[maxn * maxn];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    a.pb(mp(x, i));
  }
  sort(a.begin(), a.end());
  int l = 0;
  rep(i, n) {
    int pos = a[i].first;
    int num = a[i].second;
    if (b[pos] > 0) {
      cout << "No";
      return;
    }
    b[pos] = num;
    rep(_, num - 1) {
      while (b[++l] > 0)
        ;
      b[l] = num;
    }
    if (l > pos) {
      cout << "No";
      return;
    }
  }
  int r = n * n + 1;
  reverse(a.begin(), a.end());
  for (auto p : a) {
    int pos = p.first;
    int num = p.second;
    rep(_, n - num) {
      while (r >= 0 && b[--r] > 0) {
      }
      if (r == 0) {
        cout << "No";
        return;
      }
      b[r] = num;
    }
    // cout << pos << ' ' << num << ' ' << n - num << ' ' << r << endl;
    if (r < pos) {
      cout << "No";
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n * n; ++i) {
    cout << b[i] << ' ';
  }
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  solve();
}
Problem p03841 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }

  vector<pair<int, int>> xidx(n);
  for (int i = 0; i < n; i++) {
    xidx[i] = make_pair(x[i], i + 1);
  }
  sort(xidx.begin(), xidx.end());

  vector<int> a(n * n, 0);
  for (int i = 0; i < n; i++) {
    a[x[i]] = i + 1;
  }

  int pos = 0;
  for (int loop = 0; loop < 2; loop++) {
    for (int idx = 0; idx < n; idx++) {
      int cnt = (loop == 0 ? xidx[idx].second - 1 : n - xidx[idx].second);
      for (int i = 0; i < cnt; i++) {
        if (a[pos] == 0)
          a[pos] = xidx[idx].second;
        else
          i--;
        pos++;
      }
    }
  }

  vector<int> cnt(n * n, 0);
  for (int i = 0; i < 2 * n; i++) {
    cnt[a[i] - 1]++;
    if (cnt[a[i] - 1] == a[i] && x[a[i] - 1] != i) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n * n; i++) {
    cout << a[i] << (i + 1 == n * n ? "\n" : " ");
  }

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }

  vector<pair<int, int>> xidx(n);
  for (int i = 0; i < n; i++) {
    xidx[i] = make_pair(x[i], i + 1);
  }
  sort(xidx.begin(), xidx.end());

  vector<int> a(n * n, 0);
  for (int i = 0; i < n; i++) {
    a[x[i]] = i + 1;
  }

  int pos = 0;
  for (int loop = 0; loop < 2; loop++) {
    for (int idx = 0; idx < n; idx++) {
      int cnt = (loop == 0 ? xidx[idx].second - 1 : n - xidx[idx].second);
      for (int i = 0; i < cnt; i++) {
        if (a[pos] == 0)
          a[pos] = xidx[idx].second;
        else
          i--;
        pos++;
      }
    }
  }

  vector<int> cnt(n * n, 0);
  for (int i = 0; i < n * n; i++) {
    cnt[a[i] - 1]++;
    if (cnt[a[i] - 1] == a[i] && x[a[i] - 1] != i) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n * n; i++) {
    cout << a[i] << (i + 1 == n * n ? "\n" : " ");
  }

  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
// I SELL YOU...!
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(18);
}
signed main() {
  init_io();
  ll n;
  cin >> n;
  vector<ll> ans(n * n, -1), x(n), need(n);
  vector<P> px;
  queue<ll> que;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
    px.push_back(P(x[i], i));
    ans[x[i]] = i;
    need[i] = i;
  }
  sort(px.begin(), px.end());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < px[i].second; j++) {
      que.push(px[i].second);
    }
  }
  for (int i = 0; i < n * n; i++) {
    if (ans[i] == -1) {
      ans[i] = que.front();
      need[que.front()]--;
      que.pop();
    } else {
      if (need[ans[i]] != 0) {
        cout << "No\n";
        return 0;
      } else {
        for (int j = 0; j < n - 1 - ans[i]; j++) {
          que.push(ans[i]);
        }
      }
    }
  }
  cout << "Yes\n";
  for (int i = 0; i < n * n; i++) {
    cout << ans[i] + 1;
    if (i == n * n - 1)
      cout << endl;
    else
      cout << " ";
  }
}


Fixed submission:
// I SELL YOU...!
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(18);
}
signed main() {
  init_io();
  ll n;
  cin >> n;
  vector<ll> ans(n * n, -1), x(n), need(n);
  vector<P> px;
  queue<ll> que;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
    px.push_back(P(x[i], i));
    ans[x[i]] = i;
    need[i] = i;
  }
  sort(px.begin(), px.end());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < px[i].second; j++) {
      que.push(px[i].second);
    }
  }
  for (int i = 0; i < n * n; i++) {
    if (ans[i] == -1) {
      if (que.empty()) {
        cout << "No\n";
        return 0;
      }
      ans[i] = que.front();
      need[que.front()]--;
      que.pop();
    } else {
      if (need[ans[i]] != 0) {
        cout << "No\n";
        return 0;
      } else {
        for (int j = 0; j < n - 1 - ans[i]; j++) {
          que.push(ans[i]);
        }
      }
    }
  }
  cout << "Yes\n";
  for (int i = 0; i < n * n; i++) {
    cout << ans[i] + 1;
    if (i == n * n - 1)
      cout << endl;
    else
      cout << " ";
  }
}
Problem p03841 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;

  bool possible = true;
  vector<vector<int>> v(n);
  vector<int> ret(n * n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    v[i] = {a, i + 1};
    if (ret[a] != 0)
      possible = false;
    ret[a] = i + 1;
  }
  sort(v.begin(), v.end());

  vector<int> count(n + 1);
  for (int i = 0; i < n; i++)
    count[i + 1] = i;
  vector<int> extra;

  int index = 0;
  for (int i = 0; i < n * n; i++) {
    if (ret[i] != 0) {
      if (count[ret[i]] != 0)
        possible = false;
      for (int j = 0; j < n - ret[i]; j++)
        extra.push_back(ret[i]);
      continue;
    }
    while (index < n && count[v[index][1]] == 0)
      index++;
    if (index < n)
      ret[i] = v[index][1], count[v[index][1]]--;
    else
      ret[i] = extra.back(), extra.pop_back();
  }

  if (possible) {
    cout << "Yes" << endl;
    for (int i = 0; i < n * n; i++) {
      if (i > 0)
        cout << " ";
      cout << ret[i];
    }
    cout << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;

  bool possible = true;
  vector<vector<int>> v(n);
  vector<int> ret(n * n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    v[i] = {a, i + 1};
    if (ret[a] != 0)
      possible = false;
    ret[a] = i + 1;
  }
  sort(v.begin(), v.end());

  vector<int> count(n + 1);
  for (int i = 0; i < n; i++)
    count[i + 1] = i;
  vector<int> extra;

  int index = 0;
  for (int i = 0; i < n * n; i++) {
    if (ret[i] != 0) {
      if (count[ret[i]] != 0)
        possible = false;
      for (int j = 0; j < n - ret[i]; j++)
        extra.push_back(ret[i]);
      continue;
    }
    while (index < n && count[v[index][1]] == 0)
      index++;
    if (index < n)
      ret[i] = v[index][1], count[v[index][1]]--;
    else {
      if (extra.empty())
        possible = false;
      else
        ret[i] = extra.back(), extra.pop_back();
    }
  }

  if (possible) {
    cout << "Yes" << endl;
    for (int i = 0; i < n * n; i++) {
      if (i > 0)
        cout << " ";
      cout << ret[i];
    }
    cout << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
typedef long long ll;

int main(void) {
  ll i, n, now = 0;
  scanf("%lld", &n);
  ll x[n], be[n * n], af[n * n], ans[n * n], cnt[n], tmp[n * n];
  for (i = 0; i < n * n; ++i)
    be[i] = af[i] = 0, ans[i] = tmp[i] = -1;
  for (i = 0; i < n; ++i) {
    scanf("%lld", &x[i]);
    be[--x[i]] = i + 1;
    af[x[i]] = n - i;
    ans[x[i]] = tmp[x[i]] = i;
    cnt[i] = 0;
  }
  if (be[0] > 1) {
    printf("No");
    return 0;
  }
  for (i = 1; i < n * n; ++i) {
    be[i] += be[i - 1];
    if (be[i] > i + 1) {
      printf("No");
      return 0;
    }
  }
  now = 0;
  for (i = n * n - 1; i >= 0; --i) {
    now += af[i];
    if (now > n * n - i) {
      printf("No");
      return 0;
    }
  }
  now = be[n * n - 1];
  std::queue<ll> q;
  for (i = n * n - 1; i >= 0; --i) {
    if (ans[i] != -1) {
      if (ans[i])
        q.push(ans[i]);
      now--;
    } else if (!q.empty() && now == i + 1) {
      ans[i] = q.front();
      cnt[q.front()]++;
      if (cnt[q.front()] != q.front())
        q.push(q.front());
      q.pop();
      now--;
    }
  }
  for (i = 0; i < n; ++i)
    cnt[i] = 0;
  for (i = 0; i < n * n; ++i) {
    if (tmp[i] != -1) {
      if (tmp[i] != i - 1)
        q.push(tmp[i]);
    } else if (ans[i] == -1) {
      ans[i] = q.front();
      cnt[q.front()]++;
      if (cnt[q.front()] != n - q.front() - 1)
        q.push(q.front());
      q.pop();
    }
  }
  printf("Yes\n");
  for (i = 0; i < n * n; ++i)
    printf("%lld ", ans[i] + 1);
  return 0;
}

Fixed submission:
#include <queue>
#include <stdio.h>
typedef long long ll;

int main(void) {
  ll i, n, now = 0;
  scanf("%lld", &n);
  ll x[n], be[n * n], af[n * n], ans[n * n], cnt[n], tmp[n * n];
  for (i = 0; i < n * n; ++i)
    be[i] = af[i] = 0, ans[i] = tmp[i] = -1;
  for (i = 0; i < n; ++i) {
    scanf("%lld", &x[i]);
    be[--x[i]] = i + 1;
    af[x[i]] = n - i;
    ans[x[i]] = tmp[x[i]] = i;
    cnt[i] = 0;
  }
  if (be[0] > 1) {
    printf("No");
    return 0;
  }
  for (i = 1; i < n * n; ++i) {
    be[i] += be[i - 1];
    if (be[i] > i + 1) {
      printf("No");
      return 0;
    }
  }
  now = 0;
  for (i = n * n - 1; i >= 0; --i) {
    now += af[i];
    if (now > n * n - i) {
      printf("No");
      return 0;
    }
  }
  now = be[n * n - 1];
  std::queue<ll> q;
  for (i = n * n - 1; i >= 0; --i) {
    if (ans[i] != -1) {
      if (ans[i])
        q.push(ans[i]);
      now--;
    } else if (!q.empty() && now == i + 1) {
      ans[i] = q.front();
      cnt[q.front()]++;
      if (cnt[q.front()] != q.front())
        q.push(q.front());
      q.pop();
      now--;
    }
  }
  for (i = 0; i < n; ++i)
    cnt[i] = 0;
  for (i = 0; i < n * n; ++i) {
    if (tmp[i] != -1) {
      if (tmp[i] != n - 1)
        q.push(tmp[i]);
    } else if (ans[i] == -1) {
      ans[i] = q.front();
      cnt[q.front()]++;
      if (cnt[q.front()] != n - q.front() - 1)
        q.push(q.front());
      q.pop();
    }
  }
  printf("Yes\n");
  for (i = 0; i < n * n; ++i)
    printf("%lld ", ans[i] + 1);
  return 0;
}
Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int x[600], a[600], n, A[200000];
int compare(int k1, int k2) { return x[k1] < x[k2]; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &x[i]), a[i] = i;
  sort(a + 1, a + n + 1, compare);
  for (int i = 1; i < n; i++)
    if (x[a[i]] == x[a[i + 1]]) {
      printf("No\n");
      return 0;
    }
  int now = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < a[i]; j++) {
      while (now < x[a[i]] && A[now])
        now++;
      A[now] = a[i];
      now++;
    }
    //	cout<<i<<" "<<now<<endl;
    if (now > x[a[i]]) {
      printf("No\n");
      return 0;
    }
    A[x[a[i]]] = a[i];
  }
  now = 1;
  for (int i = 1; i <= n; i++) {
    while (A[now])
      now++;
    if (now < x[a[i]]) {
      printf("No\n");
      return 0;
    }
    for (int j = 1; j <= n - a[i]; j++) {
      while (A[now])
        now++;
      A[now] = a[i];
      now++;
    }
  }
  printf("Yes\n");
  for (int i = 1; i <= n * n; i++)
    printf("%d ", A[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int x[600], a[600], n, A[300000];
int compare(int k1, int k2) { return x[k1] < x[k2]; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &x[i]), a[i] = i;
  sort(a + 1, a + n + 1, compare);
  for (int i = 1; i < n; i++)
    if (x[a[i]] == x[a[i + 1]]) {
      printf("No\n");
      return 0;
    }
  int now = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < a[i]; j++) {
      while (now < x[a[i]] && A[now])
        now++;
      A[now] = a[i];
      now++;
    }
    //	cout<<i<<" "<<now<<endl;
    if (now > x[a[i]]) {
      printf("No\n");
      return 0;
    }
    A[x[a[i]]] = a[i];
  }
  now = 1;
  for (int i = 1; i <= n; i++) {
    while (A[now])
      now++;
    if (now < x[a[i]]) {
      printf("No\n");
      return 0;
    }
    for (int j = 1; j <= n - a[i]; j++) {
      while (A[now])
        now++;
      A[now] = a[i];
      now++;
    }
  }
  printf("Yes\n");
  for (int i = 1; i <= n * n; i++)
    printf("%d ", A[i]);
  return 0;
}