# include <bits/stdc++.h>
using namespace std;
# define fi cin
# define fo cout
# define x first
# define y second
# define IOS ios_base :: sync_with_stdio(0)
int a[1 << 20];
int b[1 << 20];
pair < int , int > t[1 << 22];
void build(int p,int u,int node)
{
if (p == u) t[node] = {b[p],p};
else
{
int m = (p + u) / 2;
build(p,m,node << 1);
build(m+1,u,node << 1 | 1);
t[node] = max(t[node << 1],t[node << 1 | 1]);
}
}
// building the tree
void Del(int p,int u,int v,int node)
{
if (p == u) t[node] = {0,p};
else
{
int m = (p + u) / 2;
if (v <= m) Del(p,m,v,node << 1);
else Del(m+1,u,v,node << 1 | 1);
t[node] = max(t[node << 1],t[node << 1 | 1]);
}
}
// deleting a visited vertex
pair < int , int > query(int p,int u,int l,int r,int node)
{
if (l > r) return {0,0};
if (l <= p && u <= r) return t[node];
int m = (p + u) / 2;
pair < int , int > ans = {0,0};
if (l <= m) ans = max(ans,query(p,m,l,r,node << 1));
if (m+1<=r) ans = max(ans,query(m+1,u,l,r,node << 1 | 1));
return ans;
}
// find edge for a fixed interval
int was[1 << 20];
// visited/not
int n;
vector < int > Sort;
//sorted graph
void dfs(int node)
{
//node = i from tutorial
Del(1,n,node,1);
was[node] = 1;
if (b[node] != n + 1 && !was[b[node]]) dfs(b[node]);
//edges of first type
while (1)
{
auto it = query(1,n,1,a[node] - 1,1);
//it.y = j from tutorial,it.x is b[j]
if (it.x > node) dfs(it.y);//there is edge
else break;//there aren't remaining
}
//edges of second type
Sort.push_back(node);
}
int ans[1 << 20];
int main(void)
{
scanf("%d\n",&n);
for (int i = 1;i <= n;++i) b[i] = n + 1;
for (int i = 1;i <= n;++i) scanf("%d",&a[i]);
for (int i = 1;i <= n;++i)
if (a[i] != -1)
b[a[i]] = i;
//b from tutorial
for (int i = 1;i <= n;++i)
if (a[i] == -1)
a[i] = n + 1;
//just replacing
build(1,n,1);
//building the tree
for (int i = 1;i <= n;++i)
if (!was[i])
dfs(i);
//sorting the graph
int cnt = 0;
for (auto it : Sort)
ans[it] = ++cnt;
//assigning p[ans[i]] = i as stated in tutorial
for (int i = 1;i <= n;++i)
printf("%d%c",ans[i]," \n"[i == n]);
//printing one possible permutation
return 0;
}
IyBpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojIGRlZmluZSBmaSBjaW4KIyBkZWZpbmUgZm8gY291dAojIGRlZmluZSB4IGZpcnN0CiMgZGVmaW5lIHkgc2Vjb25kCiMgZGVmaW5lIElPUyBpb3NfYmFzZSA6OiBzeW5jX3dpdGhfc3RkaW8oMCkKaW50IGFbMSA8PCAyMF07CmludCBiWzEgPDwgMjBdOwpwYWlyIDwgaW50ICwgaW50ID4gdFsxIDw8IDIyXTsKdm9pZCBidWlsZChpbnQgcCxpbnQgdSxpbnQgbm9kZSkKewogICAgaWYgKHAgPT0gdSkgdFtub2RlXSA9IHtiW3BdLHB9OwogICAgZWxzZQogICAgewogICAgICAgIGludCBtID0gKHAgKyB1KSAvIDI7CiAgICAgICAgYnVpbGQocCxtLG5vZGUgPDwgMSk7CiAgICAgICAgYnVpbGQobSsxLHUsbm9kZSA8PCAxIHwgMSk7CiAgICAgICAgdFtub2RlXSA9IG1heCh0W25vZGUgPDwgMV0sdFtub2RlIDw8IDEgfCAxXSk7CiAgICB9Cn0KLy8gYnVpbGRpbmcgdGhlIHRyZWUgCnZvaWQgRGVsKGludCBwLGludCB1LGludCB2LGludCBub2RlKQp7CiAgICBpZiAocCA9PSB1KSB0W25vZGVdID0gezAscH07CiAgICBlbHNlCiAgICB7CiAgICAgICAgaW50IG0gPSAocCArIHUpIC8gMjsKICAgICAgICBpZiAodiA8PSBtKSBEZWwocCxtLHYsbm9kZSA8PCAxKTsKICAgICAgICBlbHNlIERlbChtKzEsdSx2LG5vZGUgPDwgMSB8IDEpOwogICAgICAgIHRbbm9kZV0gPSBtYXgodFtub2RlIDw8IDFdLHRbbm9kZSA8PCAxIHwgMV0pOwogICAgfQp9Ci8vIGRlbGV0aW5nIGEg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WFpbmluZwogICAgfQogICAgLy9lZGdlcyBvZiBzZWNvbmQgdHlwZQogICAgU29ydC5wdXNoX2JhY2sobm9kZSk7Cn0KaW50IGFuc1sxIDw8IDIwXTsKaW50IG1haW4odm9pZCkKewogICAgc2NhbmYoIiVkXG4iLCZuKTsKICAgIGZvciAoaW50IGkgPSAxO2kgPD0gbjsrK2kpIGJbaV0gPSBuICsgMTsKICAgIGZvciAoaW50IGkgPSAxO2kgPD0gbjsrK2kpIHNjYW5mKCIlZCIsJmFbaV0pOwogICAgZm9yIChpbnQgaSA9IDE7aSA8PSBuOysraSkKICAgICAgICBpZiAoYVtpXSAhPSAtMSkKICAgICAgICAgICAgYlthW2ldXSA9IGk7CiAgICAvL2IgZnJvbSB0dXRvcmlhbAogICAgZm9yIChpbnQgaSA9IDE7aSA8PSBuOysraSkKICAgICAgICBpZiAoYVtpXSA9PSAtMSkKICAgICAgICAgICAgYVtpXSA9IG4gKyAxOwogICAgLy9qdXN0IHJlcGxhY2luZwogICAgYnVpbGQoMSxuLDEpOwogICAgLy9idWlsZGluZyB0aGUgdHJlZQogICAgZm9yIChpbnQgaSA9IDE7aSA8PSBuOysraSkKICAgICAgICBpZiAoIXdhc1tpXSkKICAgICAgICAgICAgZGZzKGkpOwogICAgLy9zb3J0aW5nIHRoZSBncmFwaAogICAgaW50IGNudCA9IDA7CiAgICBmb3IgKGF1dG8gaXQgOiBTb3J0KQogICAgICAgIGFuc1tpdF0gPSArK2NudDsKICAgIC8vYXNzaWduaW5nIHBbYW5zW2ldXSA9IGkgYXMgc3RhdGVkIGluIHR1dG9yaWFsCiAgICBmb3IgKGludCBpID0gMTtpIDw9IG47KytpKQogICAgICAgIHByaW50ZigiJWQlYyIsYW5zW2ldLCIgXG4iW2kgPT0gbl0pOwogICAgLy9wcmludGluZyBvbmUgcG9zc2libGUgcGVybXV0YXRpb24KICAgIHJldHVybiAwOwp9