import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

alias pair = Tuple!(int, "s", int, "t");
alias segT = SegmentTree!(int, min);

void main()
{
  int n; readV(n);
  int[] p; readA(n, p);

  auto inv = new int[](n+1);
  foreach (i; 0..n) inv[p[i]] = i;

  auto st = new segT[](2);
  foreach (i; 0..2) st[i] = new segT(n, n+1);
  foreach (i; 0..n) st[i%2][i] = p[i];

  pair[] calc(int s, int t)
  {
    if (s == t) return [];
    auto p1 = st[s%2][s..t-1];
    auto p1i = inv[p1];
    auto p2 = st[(s+1)%2][s+1..t];
    auto p2i = inv[p2];

    auto r = calc(s, p1i) ~ calc(p1i+1, p2i) ~ calc(p2i+1, t);
    r.sort();
    return pair(p1, p2) ~ r;
  }

  auto ans = calc(0, n);
  foreach (i, ai; ans) {
    write(ai.s, " ", ai.t);
    if (i < n/2-1) write(" ");
  }
  writeln;
}

class SegmentTree(T, alias pred = "a + b")
{
  import core.bitop, std.functional;
  alias predFun = binaryFun!pred;

  const size_t n, an;
  T[] buf;
  T unit;

  this(size_t n, T unit = T.init)
  {
    this.n = n;
    this.unit = unit;
    an = n == 1 ? 1 : (1 << ((n-1).bsr + 1));
    buf = new T[](an*2);
    if (T.init != unit) buf[] = unit;
  }

  this(T[] init, T unit = T.init)
  {
    this(init.length, unit);
    buf[an..an+n][] = init[];
    foreach_reverse (i; 1..an)
      buf[i] = predFun(buf[i*2], buf[i*2+1]);
  }

  void opIndexAssign(T val, size_t i)
  {
    buf[i += an] = val;
    while (i /= 2)
      buf[i] = predFun(buf[i*2], buf[i*2+1]);
  }

  pure T opSlice(size_t l, size_t r)
  {
    l += an; r += an;
    T r1 = unit, r2 = unit;
    while (l != r) {
      if (l % 2) r1 = predFun(r1, buf[l++]);
      if (r % 2) r2 = predFun(buf[--r], r2);
      l /= 2; r /= 2;
    }
    return predFun(r1, r2);
  }

  pure T opIndex(size_t i) { return buf[i+an]; }
  pure size_t opDollar() { return n; }
}
