Problem p03841 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define M 505
#define XFZSB                                                                  \
  {                                                                            \
    printf("No\n");                                                            \
    return 0;                                                                  \
  }
#define YXQAK                                                                  \
  { printf("Yes\n"); }
using namespace std;

struct node {
  int id, x;
  node() { id = x = 0; }
  node(int iid, int nnum) {
    id = iid;
    x = nnum;
  }
  friend bool operator<(node a, node b) { return a.x < b.x; }
} a[M];
int num[M * M] = {0}, now = 1, sum[M * M][M] = {0};
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = node(i, x);
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    num[a[i].x] = a[i].id;
    int ci = sum[now][a[i].id] + 1;
    while (now <= a[i].x && ci < a[i].id) {
      if (!num[now]) {
        num[now] = a[i].id;
        ci++;
      }
      for (int j = 1; j <= n; j++)
        sum[now][j] = sum[now - 1][j] + (num[now] == j);
      now++;
    }
    // if(sum[a[i].x][a[i].id]!=a[i].id) XFZSB
  }
  for (int i = 1; i <= n; i++) {
    int ci = sum[now - 1][a[i].id] + (now <= a[i].x);
    while (now <= n * n && ci < n) {
      if (!num[now]) {
        num[now] = a[i].id;
        ci++;
      }
      for (int j = 1; j <= n; j++)
        sum[now][j] = sum[now - 1][j] + (num[now] == j);
      now++;
    }
  }
  for (int j = 1; j <= n; j++)
    sum[n * n][j] = sum[n * n - 1][j] + (num[now] == j);
  for (int i = 1; i <= n; i++)
    if (sum[a[i].x][a[i].id] != a[i].id)
      XFZSB;
  YXQAK
  for (int i = 1; i <= n * n; i++)
    printf("%d ", num[i]);
}

Fixed submission:
#include <bits/stdc++.h>
#define M 505
#define XFZSB                                                                  \
  {                                                                            \
    printf("No\n");                                                            \
    return 0;                                                                  \
  }
#define YXQAK                                                                  \
  { printf("Yes\n"); }
using namespace std;

struct node {
  int id, x;
  node() { id = x = 0; }
  node(int iid, int nnum) {
    id = iid;
    x = nnum;
  }
  friend bool operator<(node a, node b) { return a.x < b.x; }
} a[M];
int num[M * M] = {0}, now = 1;
short sum[M * M][M] = {0};
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = node(i, x);
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    num[a[i].x] = a[i].id;
    int ci = sum[now][a[i].id] + 1;
    while (now <= a[i].x && ci < a[i].id) {
      if (!num[now]) {
        num[now] = a[i].id;
        ci++;
      }
      for (int j = 1; j <= n; j++)
        sum[now][j] = sum[now - 1][j] + (num[now] == j);
      now++;
    }
    // if(sum[a[i].x][a[i].id]!=a[i].id) XFZSB
  }
  for (int i = 1; i <= n; i++) {
    int ci = sum[now - 1][a[i].id] + (now <= a[i].x);
    while (now <= n * n && ci < n) {
      if (!num[now]) {
        num[now] = a[i].id;
        ci++;
      }
      for (int j = 1; j <= n; j++)
        sum[now][j] = sum[now - 1][j] + (num[now] == j);
      now++;
    }
  }
  for (int j = 1; j <= n; j++)
    sum[n * n][j] = sum[n * n - 1][j] + (num[now] == j);
  for (int i = 1; i <= n; i++)
    if (sum[a[i].x][a[i].id] != a[i].id)
      XFZSB;
  YXQAK
  for (int i = 1; i <= n * n; i++)
    printf("%d ", num[i]);
}