Problem p00279 (C++) — buggy submission, verdict: Runtime Error

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

namespace geometry {
// ???????????§??????

typedef complex<double> Point;

struct Segment {
  Point p1, p2;
  Segment(const Point &p1 = Point(), const Point &p2 = Point())
      : p1(p1), p2(p2) {}
};

struct Circle {
  Point p;
  double r;
  Circle(const Point &p = Point(), double r = 0.0) : p(p), r(r) {}
};

typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;

// ??\\??????

inline istream &operator>>(istream &is, Point &p) {
  double x, y;
  is >> x >> y;
  p.real(x), p.imag(y);
  return is;
}

inline istream &operator>>(istream &is, Segment &s) {
  return is >> s.p1 >> s.p2;
}

inline istream &operator>>(istream &is, Circle &c) { return is >> c.p >> c.r; }

inline ostream &operator<<(ostream &os, Segment &s) {
  return os << "{" << s.p1 << ", " << s.p2 << "}";
}

inline ostream &operator<<(ostream &os, Circle &c) {
  return os << "{" << c.p << ", " << c.r << "}";
}

inline ostream &operator<<(ostream &os, Polygon &g) {
  os << "{";
  bool f = false;
  for (auto &p : g) {
    if (f)
      os << ", ";
    os << p;
    f = true;
  }
  return os << "}";
}

// ?????°, ??????

const double PI = acos(-1);
const double EPS = 1e-8;
const double INF = 1e16;
const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;
const int OUT = 0;
const int ON = 1;
const int IN = 2;

inline double square(double a) { return a * a; }
inline bool equal(double a, double b) { return abs(a - b) < EPS; }
inline bool equalVector(const Vector &a, const Vector &b) {
  return equal(a.real(), b.real()) && equal(a.imag(), b.imag());
}
inline double norm(const Point &a) {
  return square(a.real()) + square(a.imag());
}
inline double dot(const Point &a, const Point &b) {
  return (conj(a) * b).real();
}
inline double cross(const Point &a, const Point &b) {
  return (conj(a) * b).imag();
}

// ????????¢??° (x ??§?¨?, y ??§?¨?, ????§?)
// ????§?????????????????????? Point rp ?????£??\\????????????

bool cmpx(const Point &a, const Point &b) {
  if (!equal(a.real(), b.real()))
    return a.real() < b.real();
  return b.imag() < b.imag();
}

bool cmpy(const Point &a, const Point &b) {
  if (!equal(a.imag(), b.imag()))
    return a.imag() < b.imag();
  return a.real() < b.real();
}

Point rp;
bool cmparg(const Point &a, const Point &b) {
  double rada = arg(a - rp);
  if (rada < 0.0)
    rada += 2 * PI;
  double radb = arg(b - rp);
  if (radb < 0.0)
    radb += 2 * PI;
  if (!equal(rada, radb))
    return rada < radb;
  return norm(a) < norm(b);
}

// ??´???, ??????

bool orthgonal(const Vector &a, const Vector &b) {
  return equal(dot(a, b), 0.0);
}

bool parallel(const Vector &a, const Vector &b) {
  return equal(cross(a, b), 0.0);
}

// ????°?, ?°???±

Point project(const Segment &s, const Point &p) {
  Vector base = s.p2 - s.p1;
  double r = dot(p - s.p1, base) / norm(base);
  return s.p1 + base * r;
}

Point reflect(const Segment &s, const Point &p) {
  return p + (project(s, p) - p) * 2.0;
}

// ??????????????????

int ccw(const Point &p0, const Point &p1, const Point &p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > EPS)
    return COUNTER_CLOCKWISE;
  if (cross(a, b) < -EPS)
    return CLOCKWISE;
  if (dot(a, b) < -EPS)
    return ONLINE_BACK;
  if (norm(a) > norm(b))
    return ONLINE_FRONT;
  return ON_SEGMENT;
}

// ?????¢

double distanceLP(const Line &l, const Point &p) {
  return abs((l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}

double distanceSP(const Segment &s, const Point &p) {
  if (dot(s.p2 - s.p1, p - s.p1) < 0.0)
    return abs(p - s.p1);
  if (dot(s.p1 - s.p2, p - s.p2) < 0.0)
    return abs(p - s.p2);
  return distanceLP(s, p);
}

bool intersect(const Segment &s1, const Segment &s2);
double distance(const Segment &s1, const Segment &s2) {
  if (intersect(s1, s2))
    return 0.0;
  return min(min(distanceSP(s1, s2.p1), distance(s1, s2.p2)),
             min(distanceSP(s2, s1.p1), distance(s2, s1.p2)));
}

// ????????????

bool intersect(const Segment &s1, const Segment &s2) {
  return ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 &&
         ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0;
}

int intersect(const Circle &c, const Segment &s) {
  // ???????????° (0 ~ 2) ?????????
  double dist = distanceSP(s, c.p) - c.r;
  if (dist < -EPS)
    return 2;
  if (equal(dist, 0.0))
    return 1;
  return 0;
}

int intersect(const Circle &c1, const Circle &c2) {
  // ???????????° (0 ~ 2) ?????????
  double dist = abs(c1.p - c2.p) - (c1.r + c2.r);
  if (dist < -EPS)
    return 2;
  if (equal(dist, 0.0))
    return 1;
  return 0;
}

// ??????

Point crossPoint(const Segment &s1, const Segment &s2) {
  Vector base = s2.p2 - s2.p1;
  double d1 = abs(cross(base, s1.p1 - s2.p1));
  double d2 = abs(cross(base, s1.p2 - s2.p1));

  // !! ????????´?????¶????????????????????\\????????¨ 0 ?????? !!
  assert(!equal(d1 + d2, 0.0));

  double t = d1 / (d1 + d2);
  return s1.p1 + (s1.p2 - s1.p1) * t;
}

vector<Point> crossPoints(const Circle &c, const Line &l) {
  vector<Point> res;
  if (!intersect(c, l))
    return res;

  Vector pr = project(l, c.p);
  Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
  double base = sqrt(c.r * c.r - norm(pr - c.p));
  res.push_back(pr + e * base);
  res.push_back(pr - e * base);
  if (equalVector(res[0], res[1]))
    res.pop_back();
  return res;
}

vector<Point> crossPoints(const Circle &c1, const Circle &c2) {
  vector<Point> res;
  if (!intersect(c1, c2))
    return res;

  double d = abs(c1.p - c2.p);
  double a = acos(square(c1.r) + square(d) - square(c2.r) / (2 * c2.r * d));
  double t = arg(c2.p - c1.p);
  res.push_back(c1.p + polar(c1.r, t + a));
  res.push_back(c1.p + polar(c1.r, t - a));
  if (equalVector(res[0], res[1]))
    res.pop_back();
}

// ??????

int contains(const Polygon &g, const Point &p) {
  int n = g.size();
  bool res = false;
  for (int i = 0; i < n; i++) {
    Point a = g[i] - p;
    Point b = g[(i + 1) % n] - p;
    if (abs(cross(a, b)) < EPS && dot(a, b) < EPS)
      return ON;
    if (a.imag() > b.imag())
      swap(a, b);
    if (a.imag() < EPS && EPS < b.imag() && cross(a, b) > EPS)
      res = !res;
  }
  return res ? IN : OUT;
}

// ??????

Polygon convexHull(vector<Point> s) {
  Polygon u, l;
  if (s.size() < 3)
    return s;
  sort(s.begin(), s.end());
  u.push_back(s[0]);
  u.push_back(s[1]);
  l.push_back(s[s.size() - 1]);
  l.push_back(s[s.size() - 2]);

  for (int i = 2; i < s.size(); i++) {
    int n = u.size();
    while (n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE) {
      u.pop_back();
      n--;
    }
    u.push_back(s[i]);
  }

  for (int i = s.size() - 3; i >= 0; i--) {
    int n = l.size();
    while (n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE) {
      l.pop_back();
      n--;
    }
    l.push_back(s[i]);
  }

  reverse(l.begin(), l.end());
  for (int i = u.size() - 2; i >= 1; i--) {
    l.push_back(u[i]);
  }

  return l;
}
} // namespace geometry
namespace std {
bool operator<(const geometry::Point &a, const geometry::Point &b) {
  return geometry::cmpx(a, b);
}
} // namespace std

using namespace geometry;
int n;
Point p[40];
vector<int> np[40];
vector<int> res[41];

bool cmp(int a, int b) { return cmparg(p[a], p[b]); }

double area(Point &p, Point &a, Point &b) { return cross(a - p, b - p) / 2; }

void solve() {
  // [?§????][n ?§???¢][2??????][1??????]
  static double dp[40][41][40][40];
  fill_n(***dp, 40 * 41 * 40 * 40, INF);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < np[i].size(); j++) {
      for (int k = j + 1; k < np[i].size(); k++) {
        Point &p1 = p[np[i][j]];
        Point &p2 = p[np[i][k]];
        if (area(p[i], p1, p2) < 0.0)
          continue;
        if (p1.imag() < p[i].imag())
          continue;
        if (p2.imag() <= p[i].imag())
          continue;
        dp[i][3][j][k] = area(p[i], p1, p2);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 3; j < n; j++) {
      for (int k = 0; k < np[i].size(); k++) {
        for (int l = k + 1; l < np[i].size(); l++) {
          for (int m = l + 1; m < np[i].size(); m++) {
            Point &p0 = p[np[i][k]];
            Point &p1 = p[np[i][l]];
            Point &p2 = p[np[i][m]];
            if (p2.imag() <= p[i].imag())
              continue;
            if (area(p0, p1, p2) < 0.0)
              continue;

            dp[i][j + 1][l][m] =
                min(dp[i][j + 1][l][m], dp[i][j][k][l] + area(p[i], p1, p2));
          }
        }
      }
    }
  }

  for (int v = 3; v <= n; v++) {
    int st = 0, t = 0, s = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (dp[st][v][s][t] > dp[i][v][j][k]) {
            st = i;
            s = j;
            t = k;
          }
        }
      }
    }
    res[v].push_back(np[st][t] + 1);
    res[v].push_back(np[st][s] + 1);
    for (int i = v; i > 3; i--) {
      bool f = false;
      for (int j = 0; j < n; j++) {
        double cura = dp[st][i][s][t];
        double prea = dp[st][i - 1][j][s];
        double da = area(p[st], p[np[st][s]], p[np[st][t]]);
        if (cura == INF || prea == INF)
          continue;
        if (area(p[np[st][j]], p[np[st][s]], p[np[st][t]]) < 0.0)
          continue;
        if (equal(cura - prea, da)) {
          t = s;
          s = j;
          f = true;
          break;
        }
      }
      if (!f)
        break;
      res[v].push_back(np[st][s] + 1);
    }
    res[v].push_back(st + 1);
    reverse(res[v].begin(), res[v].end());
  }
}

void output(int v) {
  if (res[v].size() != v)
    cout << "NA" << endl;
  else {
    for (int i = 0; i < v; i++) {
      if (i)
        cout << " ";
      cout << res[v][i];
    }
    cout << endl;
  }
}

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j)
        np[i].push_back(j);
    }
    rp = p[i];
    sort(np[i].begin(), np[i].end(), cmp);
  }

  solve();

  int Q;
  cin >> Q;
  while (Q--) {
    int v;
    cin >> v;
    output(v);
  }
}

Error: 0

Fixed submission:

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

namespace geometry {
// ???????????§??????

typedef complex<double> Point;

struct Segment {
  Point p1, p2;
  Segment(const Point &p1 = Point(), const Point &p2 = Point())
      : p1(p1), p2(p2) {}
};

struct Circle {
  Point p;
  double r;
  Circle(const Point &p = Point(), double r = 0.0) : p(p), r(r) {}
};

typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;

// ??\\??????

inline istream &operator>>(istream &is, Point &p) {
  double x, y;
  is >> x >> y;
  p.real(x), p.imag(y);
  return is;
}

inline istream &operator>>(istream &is, Segment &s) {
  return is >> s.p1 >> s.p2;
}

inline istream &operator>>(istream &is, Circle &c) { return is >> c.p >> c.r; }

inline ostream &operator<<(ostream &os, Segment &s) {
  return os << "{" << s.p1 << ", " << s.p2 << "}";
}

inline ostream &operator<<(ostream &os, Circle &c) {
  return os << "{" << c.p << ", " << c.r << "}";
}

inline ostream &operator<<(ostream &os, Polygon &g) {
  os << "{";
  bool f = false;
  for (auto &p : g) {
    if (f)
      os << ", ";
    os << p;
    f = true;
  }
  return os << "}";
}

// ?????°, ??????

const double PI = acos(-1);
const double EPS = 1e-8;
const double INF = 1e16;
const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;
const int OUT = 0;
const int ON = 1;
const int IN = 2;

inline double square(double a) { return a * a; }
inline bool equal(double a, double b) { return abs(a - b) < EPS; }
inline bool equalVector(const Vector &a, const Vector &b) {
  return equal(a.real(), b.real()) && equal(a.imag(), b.imag());
}
inline double norm(const Point &a) {
  return square(a.real()) + square(a.imag());
}
inline double dot(const Point &a, const Point &b) {
  return (conj(a) * b).real();
}
inline double cross(const Point &a, const Point &b) {
  return (conj(a) * b).imag();
}

// ????????¢??° (x ??§?¨?, y ??§?¨?, ????§?)
// ????§?????????????????????? Point rp ?????£??\\????????????

bool cmpx(const Point &a, const Point &b) {
  if (!equal(a.real(), b.real()))
    return a.real() < b.real();
  return b.imag() < b.imag();
}

bool cmpy(const Point &a, const Point &b) {
  if (!equal(a.imag(), b.imag()))
    return a.imag() < b.imag();
  return a.real() < b.real();
}

Point rp;
bool cmparg(const Point &a, const Point &b) {
  double rada = arg(a - rp);
  if (rada < 0.0)
    rada += 2 * PI;
  double radb = arg(b - rp);
  if (radb < 0.0)
    radb += 2 * PI;
  if (!equal(rada, radb))
    return rada < radb;
  return norm(a) < norm(b);
}

// ??´???, ??????

bool orthgonal(const Vector &a, const Vector &b) {
  return equal(dot(a, b), 0.0);
}

bool parallel(const Vector &a, const Vector &b) {
  return equal(cross(a, b), 0.0);
}

// ????°?, ?°???±

Point project(const Segment &s, const Point &p) {
  Vector base = s.p2 - s.p1;
  double r = dot(p - s.p1, base) / norm(base);
  return s.p1 + base * r;
}

Point reflect(const Segment &s, const Point &p) {
  return p + (project(s, p) - p) * 2.0;
}

// ??????????????????

int ccw(const Point &p0, const Point &p1, const Point &p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > EPS)
    return COUNTER_CLOCKWISE;
  if (cross(a, b) < -EPS)
    return CLOCKWISE;
  if (dot(a, b) < -EPS)
    return ONLINE_BACK;
  if (norm(a) > norm(b))
    return ONLINE_FRONT;
  return ON_SEGMENT;
}

// ?????¢

double distanceLP(const Line &l, const Point &p) {
  return abs((l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}

double distanceSP(const Segment &s, const Point &p) {
  if (dot(s.p2 - s.p1, p - s.p1) < 0.0)
    return abs(p - s.p1);
  if (dot(s.p1 - s.p2, p - s.p2) < 0.0)
    return abs(p - s.p2);
  return distanceLP(s, p);
}

bool intersect(const Segment &s1, const Segment &s2);
double distance(const Segment &s1, const Segment &s2) {
  if (intersect(s1, s2))
    return 0.0;
  return min(min(distanceSP(s1, s2.p1), distance(s1, s2.p2)),
             min(distanceSP(s2, s1.p1), distance(s2, s1.p2)));
}

// ????????????

bool intersect(const Segment &s1, const Segment &s2) {
  return ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 &&
         ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0;
}

int intersect(const Circle &c, const Segment &s) {
  // ???????????° (0 ~ 2) ?????????
  double dist = distanceSP(s, c.p) - c.r;
  if (dist < -EPS)
    return 2;
  if (equal(dist, 0.0))
    return 1;
  return 0;
}

int intersect(const Circle &c1, const Circle &c2) {
  // ???????????° (0 ~ 2) ?????????
  double dist = abs(c1.p - c2.p) - (c1.r + c2.r);
  if (dist < -EPS)
    return 2;
  if (equal(dist, 0.0))
    return 1;
  return 0;
}

// ??????

Point crossPoint(const Segment &s1, const Segment &s2) {
  Vector base = s2.p2 - s2.p1;
  double d1 = abs(cross(base, s1.p1 - s2.p1));
  double d2 = abs(cross(base, s1.p2 - s2.p1));

  // !! ????????´?????¶????????????????????\\????????¨ 0 ?????? !!
  assert(!equal(d1 + d2, 0.0));

  double t = d1 / (d1 + d2);
  return s1.p1 + (s1.p2 - s1.p1) * t;
}

vector<Point> crossPoints(const Circle &c, const Line &l) {
  vector<Point> res;
  if (!intersect(c, l))
    return res;

  Vector pr = project(l, c.p);
  Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
  double base = sqrt(c.r * c.r - norm(pr - c.p));
  res.push_back(pr + e * base);
  res.push_back(pr - e * base);
  if (equalVector(res[0], res[1]))
    res.pop_back();
  return res;
}

vector<Point> crossPoints(const Circle &c1, const Circle &c2) {
  vector<Point> res;
  if (!intersect(c1, c2))
    return res;

  double d = abs(c1.p - c2.p);
  double a = acos(square(c1.r) + square(d) - square(c2.r) / (2 * c2.r * d));
  double t = arg(c2.p - c1.p);
  res.push_back(c1.p + polar(c1.r, t + a));
  res.push_back(c1.p + polar(c1.r, t - a));
  if (equalVector(res[0], res[1]))
    res.pop_back();
}

// ??????

int contains(const Polygon &g, const Point &p) {
  int n = g.size();
  bool res = false;
  for (int i = 0; i < n; i++) {
    Point a = g[i] - p;
    Point b = g[(i + 1) % n] - p;
    if (abs(cross(a, b)) < EPS && dot(a, b) < EPS)
      return ON;
    if (a.imag() > b.imag())
      swap(a, b);
    if (a.imag() < EPS && EPS < b.imag() && cross(a, b) > EPS)
      res = !res;
  }
  return res ? IN : OUT;
}

// ??????

Polygon convexHull(vector<Point> s) {
  Polygon u, l;
  if (s.size() < 3)
    return s;
  sort(s.begin(), s.end());
  u.push_back(s[0]);
  u.push_back(s[1]);
  l.push_back(s[s.size() - 1]);
  l.push_back(s[s.size() - 2]);

  for (int i = 2; i < s.size(); i++) {
    int n = u.size();
    while (n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE) {
      u.pop_back();
      n--;
    }
    u.push_back(s[i]);
  }

  for (int i = s.size() - 3; i >= 0; i--) {
    int n = l.size();
    while (n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE) {
      l.pop_back();
      n--;
    }
    l.push_back(s[i]);
  }

  reverse(l.begin(), l.end());
  for (int i = u.size() - 2; i >= 1; i--) {
    l.push_back(u[i]);
  }

  return l;
}
} // namespace geometry
namespace std {
bool operator<(const geometry::Point &a, const geometry::Point &b) {
  return geometry::cmpx(a, b);
}
} // namespace std

using namespace geometry;
int n;
Point p[40];
vector<int> np[40];
vector<int> res[41];

bool cmp(int a, int b) { return cmparg(p[a], p[b]); }

double area(Point &p, Point &a, Point &b) { return cross(a - p, b - p) / 2; }

void solve() {
  // [?§????][n ?§???¢][2??????][1??????]
  static double dp[40][40][40][40];
  fill_n(***dp, 40 * 40 * 40 * 40, INF);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < np[i].size(); j++) {
      for (int k = j + 1; k < np[i].size(); k++) {
        Point &p1 = p[np[i][j]];
        Point &p2 = p[np[i][k]];
        if (area(p[i], p1, p2) < 0.0)
          continue;
        if (p1.imag() < p[i].imag())
          continue;
        if (p2.imag() <= p[i].imag())
          continue;
        dp[i][3][j][k] = area(p[i], p1, p2);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 3; j < n; j++) {
      for (int k = 0; k < np[i].size(); k++) {
        for (int l = k + 1; l < np[i].size(); l++) {
          for (int m = l + 1; m < np[i].size(); m++) {
            Point &p0 = p[np[i][k]];
            Point &p1 = p[np[i][l]];
            Point &p2 = p[np[i][m]];
            if (p2.imag() <= p[i].imag())
              continue;
            if (area(p0, p1, p2) < 0.0)
              continue;

            dp[i][j + 1][l][m] =
                min(dp[i][j + 1][l][m], dp[i][j][k][l] + area(p[i], p1, p2));
          }
        }
      }
    }
  }

  for (int v = 3; v <= n; v++) {
    int st = 0, t = 0, s = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (dp[st][v][s][t] > dp[i][v][j][k]) {
            st = i;
            s = j;
            t = k;
          }
        }
      }
    }
    res[v].push_back(np[st][t] + 1);
    res[v].push_back(np[st][s] + 1);
    for (int i = v; i > 3; i--) {
      bool f = false;
      for (int j = 0; j < n; j++) {
        double cura = dp[st][i][s][t];
        double prea = dp[st][i - 1][j][s];
        double da = area(p[st], p[np[st][s]], p[np[st][t]]);
        if (cura == INF || prea == INF)
          continue;
        if (area(p[np[st][j]], p[np[st][s]], p[np[st][t]]) < 0.0)
          continue;
        if (equal(cura - prea, da)) {
          t = s;
          s = j;
          f = true;
          break;
        }
      }
      if (!f)
        break;
      res[v].push_back(np[st][s] + 1);
    }
    res[v].push_back(st + 1);
    reverse(res[v].begin(), res[v].end());
  }
}

void output(int v) {
  if (res[v].size() != v)
    cout << "NA" << endl;
  else {
    for (int i = 0; i < v; i++) {
      if (i)
        cout << " ";
      cout << res[v][i];
    }
    cout << endl;
  }
}

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j)
        np[i].push_back(j);
    }
    rp = p[i];
    sort(np[i].begin(), np[i].end(), cmp);
  }

  solve();

  int Q;
  cin >> Q;
  while (Q--) {
    int v;
    cin >> v;
    output(v);
  }
}