#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector < int >;
using pii = pair < int, int >;
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(), (a).end()
#define qio() cin.tie(0) -> sync_with_stdio(0)
#define L(i, j, k) for (int i = j; i <= int(k); i++)
#define R(i, j, k) for (int i = j; i >= int(k); i--)
const int N = 3e5 + 5, V = 602;
int n, a[N], Rs, lb[N], rb[N], mat[V];
int fa[V], vs[V], es[V], rt[V];
vi pool;
bool spare[N], match[N];
int even[V][V];
vi odd[V][V];
vi T[V], G[V];
vector < array < int, 3 > > edge;
inline int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
if ((x = find(x)) == (y = find(y))) {
return void(es[x]++);
}
if (vs[x] < vs[y]) swap(x, y);
fa[y] = x, vs[x] += vs[y], es[x] += es[y] + 1;
}
namespace Match {
int dsu[V], pre[V], vis[V];
inline int find(int x) {
return x == dsu[x] ? x : dsu[x] = find(dsu[x]);
}
void bfs(int s) {
L(i, 0, V - 1) {
vis[i] = pre[i] = 0;
dsu[i] = i;
}
queue < int > Q;
auto lca = [&](int u, int v) {
static int dfn[N], dfc;
for (dfc++; ; swap(u, v)) {
if (dfn[u = find(u)] == dfc) return u;
if (u) dfn[u] = dfc;
u = pre[mat[u]];
}
return -1;
};
auto blossom = [&](int u, int v, int lc) {
for (; find(u) != lc; u = pre[v]) {
pre[u] = v, v = mat[u];
vis[v] = 1, Q.push(v);
dsu[u] = dsu[v] = lc; // dsu not fa
}
};
for (vis[s] = 1, Q.push(s); sz(Q); Q.pop()) {
int u = Q.front();
for (auto v : G[u]) if (find(u) != find(v) && vis[v] != 2) {
if (vis[v] == 1) {
int lc = lca(u, v);
blossom(u, v, lc);
blossom(v, u, lc);
continue;
}
vis[v] = 2, pre[v] = u;
if (!mat[v]) {
for (int o; u; u = pre[v = o]) {
pre[u] = o = mat[u];
mat[u] = v, mat[v] = u;
}
return;
}
vis[mat[v]] = 1, Q.push(mat[v]);
}
}
}
}
bool vis[V];
void dfs(int u) {
vis[u] = 1;
for (auto v : T[u]) {
if (!match[v] && sz(odd[u][v])) {
match[v] = 1;
int w = odd[u][v].back();
odd[u][v].pop_back(), odd[v][u].pop_back();
if (a[lb[w]] == v) a[lb[w] + 1] = v;
else a[rb[w] - 1] = v;
dfs(v);
} else if (!vis[v]) dfs(v);
}
}
int main() {
qio(), cin >> n;
memset(spare, 1, sizeof spare);
a[0] = a[n + 1] = N - 1, match[a[0]] = 1;
L(i, 1, n) cin >> a[i];
L(i, 1, n) {
if (a[i] == a[i - 1]) {
spare[a[i]] = 0;
match[a[i]] = 1;
}
if (!a[i - 1] || !a[i + 1]) spare[a[i]] = 0; // - 1 忘记打了呃呃
}
L(i, 1, n) if (spare[i]) {
pool.pb(i);
}
iota(fa, fa + V, 0);
fill(vs, vs + V, 1);
for (int las = 0, i = 1; i <= n + 1; i++) {
if (a[i]) {
int len = i - las - 1;
bool lhs = !match[a[las]], rhs = !match[a[i]];
if (len) lb[++Rs] = las, rb[Rs] = i;
if (len & 1) {
if (lhs && rhs && a[las] != a[i]) {
T[a[las]].pb(a[i]);
T[a[i]].pb(a[las]);
odd[a[i]][a[las]].pb(Rs);
odd[a[las]][a[i]].pb(Rs);
merge(a[i], a[las]);
// cerr << las << ' ' << i << " qwq\n";
} else if (lhs) {
match[a[las]] = 1;
a[las + 1] = a[las];
} else if (rhs) {
match[a[i]] = 1;
a[i - 1] = a[i];
}
} else if (len && lhs && rhs && a[i] != a[las]) {
// cerr << las << ' ' << i << " qaq\n";
edge.pb({a[i], a[las], Rs});
}
las = i;
}
}
for (auto [u, v, w] : edge) if (!match[u] && !match[v]) {
// cerr << "try " << u << ' ' << v << '\n';
if ((u = find(u)) != (v = find(v)) && vs[u] == es[u] + 1 && vs[v] == es[v] + 1) {
// cerr << " " << u << " - " << v << '\n';
even[u][v] = even[v][u] = w;
G[u].pb(v), G[v].pb(u);
}
}
L(i, 1, V - 2) if (!mat[i]) { // 没有匹配才可以跑
Match::bfs(i);
}
L(i, 1, V - 2) rt[find(i)] = find(i);
L(i, 1, V - 2) if (mat[i]) {
int w = even[i][mat[i]], x = a[lb[w]], y = a[rb[w]];
// cerr << "match " << x << ' ' << y << "; " << lb[w] << ' ' << rb[w] << '\n';
match[x] = match[y] = 1;
rt[i] = find(x) == i ? x : y;
a[lb[w] + 1] = x;
a[rb[w] - 1] = y;
}
L(i, 1, V - 2) if (fa[i] == i) {
dfs(rt[i]);
}
L(i, 1, n) {
if (!a[i]) {
if (!a[i + 1] && sz(pool)) {
a[i] = a[i + 1] = pool.back();
pool.pop_back();
} else a[i] = 1; // a[i + 1] should not be changed
}
cout << a[i] << " \n"[i == n];
}
return 0;
}