class Topology {

    static int[] sort(int[] vs, int[] rank) {
        int[] ws = Arrays.copyOf(vs, vs.length);

        // insertion sort using rank for the ordering
        for (int i = 0, j = i; i < vs.length - 1; j = ++i) {
            int v = ws[i + 1];
            while (rank[v] < rank[ws[j]]) {
                ws[j + 1] = ws[j];
                if (--j < 0)
                    break;
            }
            ws[j + 1] = v;
        }
        return ws;
    }

}