//我们先将输入序列进行升序排序,然后挨个检查是否满足插入条件,满足则输出,
//不满足则放进一个待插入的数组;每成功输出一个,就要回过头来检查待插入数组中的元素是否可以插入;
//挨个检查完后,若待插入数组中还有剩余元素,则需要继续将它们插入;
#include <iostream>
#include <algorithm>
using namespace std;
int index(int* a, int obj){//返回对应下标
for (int i = 0; ; i++){
if (a[i] == obj)
return i;
}
}
int judge(int* a, int n){//判断other数组中是否还有元素
for (int i = 0; i < n; i++){
if (a[i] != -1)
return 1;
}
return 0;
}
int main(){
int n;
cin >> n;
//输入
int* in = new int[n];
for (int i = 0; i < n; i++)
cin >> in[i];
//输入的升序排序,因为要求当前的插入有多种选择时,必须选择最小的数字,所以要优先判断小的数
int* up = new int[n];
for (int i = 0; i < n; i++)
up[i] = in[i];
sort(up, up + n);
//不满足插入条件的元素
int* other = new int[n];
for(int i=0;i<n;i++)
other[i]=0;
int count = 0;
//是否已插入的标志
int* flag = new int[n];
for (int i = 0; i < n; i++)
flag[i] = 0;
for (int i = 0; i < n; i++){
if (up[i] >= 0){//元素为负数说明没有元素
if (count > 0){//每次都要回头检查other中元素是否可以插入
for (int j = 0; j < count; j++){
if (other[j] != -1){
int tag = 0;
for (int k = other[j] % n; k != index(in, other[j]); k = (k + 1) % 11){
//other中插入条件:从下标为该数取余得数开始,到该数在输入序列中的位置之前,
//都已经有元素插入。
if (flag[k] == 0){
tag = 1;
break;
}
}
if (tag == 0){
cout << other[j] << " ";
flag[index(in, other[j])] = 1;
other[j] = -1;//插入后ohter[j]改为-1;
}
}
}
}
//判断是否满足插入条件
if (up[i] % n == index(in, up[i])){//该数取余得数正好对应其在输入序列中的顺序
cout << up[i] << " ";
flag[index(in, up[i])] = 1;
}
else
other[count++] = up[i];
}
}
while (judge(other, count)){//插入other中剩余元素
for (int i = 0; i < count; i++){
if (other[i] != -1){
int tag = 0;
for (int k = other[i] % n; k < index(in, other[i]); k++){
if (flag[k] == 0){
tag = 1;
break;
}
}
if (tag == 0){
cout << other[i] << " ";
flag[index(in, other[i])] = 1;
other[i] = -1;
break;
}
}
}
}
return 0;
}