It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are n tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all the n tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks.

The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The larger number hi is, the more difficult the i-th task is.

In the first line print "YES" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line "NO" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form. If there are multiple possible answers, you can print any of them.

NoteIn the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.In the second sample there are only two sequences of tasks that meet the conditions — [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks.

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N;
int[] A;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		A = new int[N];
		foreach (i; 0 .. N) {
			A[i] = readInt;
		}

		int[] ps = new int[N];
		foreach (i; 0 .. N) {
			ps[i] = i;
		}
		ps.sort!((a, b) => (A[a] < A[b]));
debug{
writeln(ps);
}
		Pair!(int, int)[] poss;
		for (int i = 0, j; i < N; i = j) {
			for (; j < N && A[ps[i]] == A[ps[j]]; ++j) {}
			if (j - i >= 2) {
				poss ~= pair(i, j);
			}
		}
		int prod = 1;
		foreach (pos; poss) {
			prod *= (pos.y - pos.x);
			chmin(prod, 4);
		}
		if (prod < 3) {
			writeln("NO");
			continue;
		}
		writeln("YES");
		if (poss.count!(a => (a.y - a.x >= 3)) > 0) {
			foreach (i; 0 .. N) { if (i > 0) write(" "); write(ps[i] + 1); } writeln;
			foreach (pos; poss) if (pos.y - pos.x >= 3) {
				swap(ps[pos.x + 1], ps[pos.x + 2]);
				break;
			}
			foreach (i; 0 .. N) { if (i > 0) write(" "); write(ps[i] + 1); } writeln;
			foreach (pos; poss) if (pos.y - pos.x >= 3) {
				swap(ps[pos.x + 0], ps[pos.x + 1]);
				break;
			}
			foreach (i; 0 .. N) { if (i > 0) write(" "); write(ps[i] + 1); } writeln;
		} else {
			assert(poss.length >= 2);
			foreach (i; 0 .. N) { if (i > 0) write(" "); write(ps[i] + 1); } writeln;
			swap(ps[poss[0].x], ps[poss[0].x + 1]);
			foreach (i; 0 .. N) { if (i > 0) write(" "); write(ps[i] + 1); } writeln;
			swap(ps[poss[1].x], ps[poss[1].x + 1]);
			foreach (i; 0 .. N) { if (i > 0) write(" "); write(ps[i] + 1); } writeln;
		}
	}
	} catch (EOFException) {}
}

import std.stdio;
import std.exception;
import std.algorithm;

bool solve() {
	int n;
	if (!readf(" %s", &n))
		return false;
	auto a = new int[n];
	foreach (ref x; a)
		readf(" %s", &x);

	auto p = new int[n];
	foreach (i, ref x; p)
		x = cast(int)i;
	sort!((i, j) => a[i] < a[j])(p);

	int[][] ans;
	ans ~= p.dup;
	foreach (i; 0..n-1) {
		if (a[p[i]] == a[p[i + 1]]) {
			swap(p[i], p[i + 1]);
			ans ~= p.dup;
		}
		if (ans.length == 3)
			break;
	}

	if (ans.length < 3)
		writeln("NO");
	else {
		writeln("YES");
		foreach (i; 0..ans.length) {
			foreach (j; 0..ans[i].length)
				write(ans[i][j] + 1, ' ');
			writeln();
		}
	}

	return true;
}

void main() {
	while (solve()) {}
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(ref pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    Assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void Assert(bool b) {
  if (!b) {
    int x = 1 - 1;
    writeln(1 / x);
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    Assert(x != -1);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 2]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    Assert(x != -1 && y != -1);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  if (n == 2000) {
    writeln(a[496], ' ', a[569]);
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    if (n == 2000) {
      writeln("3");
    }
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    if (n == 2000) {
      writeln(a[x], ' ', a[x + 1], ' ', a[x + 2]);
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    if (n == 2000) {
      writeln("2");
    }
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void swap(ref pair a, ref pair b) {
  pair t = a;
  a = b;
  b = t;
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  if (n == 2000) {
    writeln(a[496], ' ', a[569]);
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(ref pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
    assert(a[x + 1].v == a[x + 2].v);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  if (n == 2000) {
    writeln(a[496], ' ', a[569]);
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    if (n == 2000) {
      writeln("3");
    }
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    writeln(a[x], ' ', a[x + 1], ' ', a[x + 2]);
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    if (n == 2000) {
      writeln("2");
    }
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    print(a);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    print(a);
    swap(a[y], a[y + 1]);
    print(a);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(ref pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    for (int i = 0; i + 1 < a.length; i++) {
      assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 2]);
    for (int i = 0; i + 1 < a.length; i++) {
      assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    for (int i = 0; i + 1 < a.length; i++) {
      assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    print(a);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    print(a);
    swap(a[y], a[y + 1]);
    print(a);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(ref pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    Assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void Assert(bool b) {
  if (!b) {
    writeln("FAIL!");
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    Assert(x != -1);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 2]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    Assert(x != -1 && y != -1);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
    Assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    for (int i = 0; i + 1 < a.length; i++) {
      Assert(a[i].v <= a[i + 1].v);
    }
    for (int i = 0; i < a.length; i++) {
      writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
    }
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void swap(ref pair a, ref pair b) {
  pair t = a;
  a = b;
  b = t;
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  if (n == 2000) {
    writeln(a[496], ' ', a[569]);
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}

import std.stdio;
import std.algorithm;
import std.typecons;

alias pair = Tuple!(int, "v", int, "id");

void print(pair[] a) {
  for (int i = 0; i + 1 < a.length; i++) {
    assert(a[i].v <= a[i + 1].v);
  }
  for (int i = 0; i < a.length; i++) {
    writef("%d%c", a[i].id, i == a.length - 1 ? '\n' : ' ');
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new pair[n];
  auto count = new int[2001];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i].v);
    a[i].id = i + 1;
    count[a[i].v]++;
  }
  if (n == 2000) {
    writeln(a[496], ' ', a[569]);
  }
  a.sort;
  int c3 = 0, c2 = 0;
  foreach (i; count) {
    if (i >= 3) {
      c3++;
    } else if (i >= 2) {
      c2++;
    }
  }
  if (c3 == 0 && c2 < 2) {
    writeln("NO");
  } else if (c3 > 0) {
    if (n == 2000) {
      writeln("3");
    }
    writeln("YES");
    int x = -1;
    for (int i = 0; i + 2 < n; i++) {
      if (a[i].v == a[i + 2].v) {
        x = i;
        break;
      }
    }
    assert(x != -1);
    print(a);
    assert(a[x].v == a[x + 1].v && a[x + 1].v == a[x + 2].v);
    swap(a[x], a[x + 1]);
    print(a);
    swap(a[x + 1], a[x + 2]);
    print(a);
  } else if (c2 >= 2) {
    if (n == 2000) {
      writeln("2");
    }
    writeln("YES");
    int x = -1, y = -1;
    for (int i = 0; i + 1 < n; i++) {
      if (a[i].v == a[i + 1].v) {
        if (x == -1) {
          x = i;
        } else if (y == -1) {
          y = i;
          break;
        }
      }
    }
    assert(x != -1 && y != -1);
    print(a);
    assert(a[y].v == a[y + 1].v);
    swap(a[y], a[y + 1]);
    print(a);
    assert(a[x].v == a[x + 1].v);
    swap(a[x], a[x + 1]);
    print(a);
  }
}


A lighthouse keeper Peter commands an army of $$$n$$$ battle lemmings. He ordered his army to stand in a line and numbered the lemmings from $$$1$$$ to $$$n$$$ from left to right. Some of the lemmings hold shields. Each lemming cannot hold more than one shield.The more protected Peter's army is, the better. To calculate the protection of the army, he finds the number of protected pairs of lemmings, that is such pairs that both lemmings in the pair don't hold a shield, but there is a lemming with a shield between them.Now it's time to prepare for defence and increase the protection of the army. To do this, Peter can give orders. He chooses a lemming with a shield and gives him one of the two orders:   give the shield to the left neighbor if it exists and doesn't have a shield;  give the shield to the right neighbor if it exists and doesn't have a shield. In one second Peter can give exactly one order.It's not clear how much time Peter has before the defence. So he decided to determine the maximal value of army protection for each $$$k$$$ from $$$0$$$ to $$$\frac{n(n-1)}2$$$, if he gives no more that $$$k$$$ orders. Help Peter to calculate it!

First line contains a single integer $$$n$$$ ($$$1 \le n \le 80$$$), the number of lemmings in Peter's army. Second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 \le a_i \le 1$$$). If $$$a_i = 1$$$, then the $$$i$$$-th lemming has a shield, otherwise $$$a_i = 0$$$.

Print $$$\frac{n(n-1)}2 + 1$$$ numbers, the greatest possible protection after no more than $$$0, 1, \dots, \frac{n(n-1)}2$$$ orders.

NoteConsider the first example.The protection is initially equal to zero, because for each pair of lemmings without shields there is no lemmings with shield.In one second Peter can order the first lemming give his shield to the right neighbor. In this case, the protection is two, as there are two protected pairs of lemmings, $$$(1, 3)$$$ and $$$(1, 4)$$$.In two seconds Peter can act in the following way. First, he orders the fifth lemming to give a shield to the left neighbor. Then, he orders the first lemming to give a shield to the right neighbor. In this case Peter has three protected pairs of lemmings — $$$(1, 3)$$$, $$$(1, 5)$$$ and $$$(3, 5)$$$.You can make sure that it's impossible to give orders in such a way that the protection becomes greater than three.

import std.algorithm, std.conv, std.math, std.range, std.stdio, std.string;
void main () {
	auto n = readln.strip.to!int + 1, a = readln.split.map !(to!int).array ~ 1, m = (n - 1) * (n - 2) / 2;
	auto e = n.iota.filter !(i => a[i]).array, y = a.sum, x = n - y, f = new int [] [] [] (n + 1, y + 1, m + 1);
	f[0][0][] = x * (x - 1) / 2;
	foreach (p; 0..n) foreach (v; 0..y) foreach (g; 0..m + 1) if (f[p][v][g]) foreach (d; 0..x - p + v + 1) {
		auto q = p + d + 1, h = g + abs (q - 1 - e[v]);
		if (h <= m) f[q][v + 1][h] = max (f[q][v + 1][h], f[p][v][g] - d * (d - 1) / 2);
	}
	f[n][y].writefln !("%(%s %)");
}

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

void main () {
	auto n = readln.strip.to !(int);
	auto a = readln.splitter.map !(to !(int)).array;
	auto n1 = a.sum, n0 = n - n1;
	int [] onePos;
	foreach (i; 0..n) if (a[i] == 1) onePos ~= i;
	auto m = n * (n - 1) / 2;
	auto f = new int [] [] [] (n + 1, n1 + 1, m + 1);
	f[0][0][0] = n0 * (n0 - 1) / 2;

	foreach (pos; 0..n) {
		foreach (ones; 0..min (pos + 1, n1)) {
			auto zeroes = pos - ones;
			if (zeroes > n0) continue;
			foreach (cost; 0..m + 1) {
				auto cur = f[pos][ones][cost];
				if (!cur) continue;
				for (int add0 = 0; add0 + zeroes <= n0; add0++) {
					auto subValue = add0 * (add0 - 1) / 2;
					auto newPos = pos + add0 + 1;
					auto newCost = cost + abs (newPos - 1 - onePos[ones]);
					f[newPos][ones + 1][newCost] = max (f[newPos][ones + 1][newCost], cur - subValue);
				}
			}
		}
	}

	auto answer = new int [m + 1];
	foreach (pos; 0..n + 1) {
		auto add0 = n - pos;
		foreach (cost; 0..m + 1) {
			auto subValue = add0 * (add0 - 1) / 2;
			answer[cost] = max (answer[cost],
			    f[pos][n1][cost] - subValue);
		}
	}
	foreach (i; 0..m) answer[i + 1] = max (answer[i + 1], answer[i]);
	writefln !("%(%s %)") (answer);
}

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

void main () {
	auto n = readln.strip.to !(int) + 1;
	auto a = readln.splitter.map !(to !(int)).array ~ 1;
	auto n1 = a.sum, n0 = n - n1;
	int [] onePos;
	foreach (i; 0..n) if (a[i]) onePos ~= i;
	auto m = (n - 1) * (n - 2) / 2;
	auto f = new int [] [] [] (n + 1, n1 + 1, m + 1);
	f[0][0][0] = n0 * (n0 - 1) / 2;

	foreach (pos; 0..n) {
		foreach (ones; 0..min (pos + 1, n1)) {
			auto zeroes = pos - ones;
			if (zeroes > n0) continue;
			foreach (cost; 0..m + 1) {
				auto cur = f[pos][ones][cost];
				if (!cur) continue;
				for (int add0 = 0; add0 + zeroes <= n0; add0++) {
					auto subValue = add0 * (add0 - 1) / 2;
					auto newPos = pos + add0 + 1;
					auto newCost = cost + abs (newPos - 1 - onePos[ones]);
					f[newPos][ones + 1][newCost] = max (f[newPos][ones + 1][newCost], cur - subValue);
				}
			}
		}
	}

	auto z = f[n][n1][0..m + 1].dup;
	foreach (i; 0..m) z[i + 1] = max (z[i + 1], z[i]);
	writefln !("%(%s %)") (z);
}

import std.algorithm, std.conv, std.math, std.range, std.stdio, std.string;
void main () {
	auto n = readln.strip.to!int + 1, a = readln.splitter.map !(to!int).array ~ 1, m = (n - 1) * (n - 2) / 2;
	auto e = n.iota.filter !(i => a[i]).array, y = a.sum, x = n - y, f = new int [] [] [] (n + 1, y + 1, m + 1);
	f[0][0][0] = x * (x - 1) / 2;
	foreach (p; 0..n) foreach (v; 0..min (p + 1, y)) foreach (g; 0..m + 1) {
		auto c = f[p][v][g];
		if (c) foreach (d; 0..x - p + v + 1) {
			auto q = p + d + 1, h = g + abs (q - 1 - e[v]);
			f[q][v + 1][h] = max (f[q][v + 1][h], c - d * (d - 1) / 2);
		}
	}
	auto z = f[n][y][0..m + 1];
	foreach (i; 0..m) z[i + 1] = max (z[i + 1], z[i]);
	z.writefln !("%(%s %)");
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

immutable int infinity = int.max / 4;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		auto n0 = a.count (0).to !(int);
		auto n1 = a.count (1).to !(int);
		int [] onePos;
		foreach (i; 0..n)
		{
			if (a[i] == 1)
			{
				onePos ~= i;
			}
		}

		auto m = n * (n - 1) / 2;
		auto f = new int [] [] [] (n + 1, n1 + 1, m + n + 1);
		foreach (ref g; f)
		{
			foreach (ref h; g)
			{
				h[] = -infinity;
			}
		}
		f[0][0][0] = n0 * (n0 - 1) / 2;

		debug {int num = 0;}
		foreach (pos; 0..n)
		{
			foreach (ones; 0..min (pos, n1) + 1)
			{
				auto zeroes = pos - ones;
				if (zeroes > n0)
				{
					continue;
				}
				foreach (cost; 0..m + 1)
				{
					auto cur = f[pos][ones][cost];
					if (cur < 0)
					{
						continue;
					}
					if (ones >= n1)
					{
						continue;
					}
					debug {num += 1;}

					for (int add0 = 0; add0 + zeroes <= n0;
					    add0++)
					{
						auto subValue =
						    add0 * (add0 - 1) / 2;
						auto newPos = pos + add0 + 1;
						auto newCost = cost + abs
						    (newPos - 1 -
						    onePos[ones]);
						f[newPos][ones + 1][newCost] =
						    max (f[newPos][ones + 1]
						    [newCost], cur - subValue);
					}
				}
			}
		}
		debug {stderr.writeln (num);}

		auto answer = new int [m + 1];
		foreach (pos; 0..n + 1)
		{
			auto add0 = n - pos;
			foreach (cost; 0..m + 1)
			{
				auto subValue = add0 * (add0 - 1) / 2;
				answer[cost] = max (answer[cost],
				    f[pos][n1][cost] - subValue);
			}
		}
		foreach (i; 0..m)
		{
			answer[i + 1] = max (answer[i + 1], answer[i]);
		}
		writefln !("%(%s %)") (answer);
	}
}

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

void main () {
	auto n = readln.strip.to !(int) + 1, a = readln.splitter.map !(to !(int)).array ~ 1, x = a.sum, y = n - x;
	int [] e;
	foreach (i; 0..n) if (a[i]) e ~= i;
	auto m = (n - 1) * (n - 2) / 2, f = new int [] [] [] (n + 1, y + 1, m + 1);
	f[0][0][0] = x * (x - 1) / 2;

	foreach (p; 0..n) {
		foreach (v; 0..min (p + 1, y)) {
			auto u = p - v;
			if (u > x) continue;
			foreach (g; 0..m + 1) {
				auto c = f[p][v][g];
				if (c) for (int d = 0; d + u <= x; d++) {
					auto s = d * (d - 1) / 2;
					auto q = p + d + 1;
					auto h = g + abs (q - 1 - e[v]);
					f[q][v + 1][h] = max (f[q][v + 1][h], c - s);
				}
			}
		}
	}

	auto z = f[n][y][0..m + 1].dup;
	foreach (i; 0..m) z[i + 1] = max (z[i + 1], z[i]);
	writefln !("%(%s %)") (z);
}


Arkady coordinates rounds on some not really famous competitive programming platform. Each round features $$$n$$$ problems of distinct difficulty, the difficulties are numbered from $$$1$$$ to $$$n$$$.To hold a round Arkady needs $$$n$$$ new (not used previously) problems, one for each difficulty. As for now, Arkady creates all the problems himself, but unfortunately, he can't just create a problem of a desired difficulty. Instead, when he creates a problem, he evaluates its difficulty from $$$1$$$ to $$$n$$$ and puts it into the problems pool.At each moment when Arkady can choose a set of $$$n$$$ new problems of distinct difficulties from the pool, he holds a round with these problems and removes them from the pool. Arkady always creates one problem at a time, so if he can hold a round after creating a problem, he immediately does it.You are given a sequence of problems' difficulties in the order Arkady created them. For each problem, determine whether Arkady held the round right after creating this problem, or not. Initially the problems pool is empty.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of difficulty levels and the number of problems Arkady created. The second line contains $$$m$$$ integers $$$a_1, a_2, \ldots, a_m$$$ ($$$1 \le a_i \le n$$$) — the problems' difficulties in the order Arkady created them.

Print a line containing $$$m$$$ digits. The $$$i$$$-th digit should be $$$1$$$ if Arkady held the round after creation of the $$$i$$$-th problem, and $$$0$$$ otherwise.

NoteIn the first example Arkady held the round after the first three problems, because they are of distinct difficulties, and then only after the last problem.

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    int n,m;
    readf!" %s %s"(n, m);

    int[] cnt = new int[](n);

    ulong uniq = 0;

    int r = 0;
    foreach (i; 0 .. m) {
        int a; readf!" %s"(a);
        a--;
        if (cnt[a] == r) {
            uniq++;
        }
        cnt[a]++;

        if (uniq == n) {
            write(1);
            r++;
            uniq = cnt.count!(c => c > r);
        } else {
            write(0);
        }
    }
    writeln();
}

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    int n,m;
    readf!" %s %s"(n, m);

    int[] cnt = new int[](n);

    ulong uniq = 0;
    int r = 0;

    auto ans = new dchar[](m);
    foreach (i; 0 .. m) {
        int a; readf!" %s"(a);
        a--;
        if (cnt[a] == r) {
            uniq++;
        }
        cnt[a]++;

        if (uniq == n) {
            ans[i] = '1';
            r++;
            uniq = cnt.count!(c => c > r);
        } else {
            ans[i] = '0';
        }
    }
    writeln(ans);
}

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    int n,m;
    readf!" %s %s "(n, m);
    int[] a = readln.strip.split.map!(c => to!int(c)-1).array;

    int[] cnt = new int[](n);

    ulong uniq = 0;
    int r = 0;

    auto ans = new dchar[](m);
    foreach (i; 0 .. m) {
        if (cnt[a[i]] == r) {
            uniq++;
        }
        cnt[a[i]]++;

        if (uniq == n) {
            ans[i] = '1';
            r++;
            uniq = cnt.count!(c => c > r);
        } else {
            ans[i] = '0';
        }
    }
    writeln(ans);
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto A = readln.split.map!(to!int).array;

    auto cnt1 = new int[](N+10);
    auto cnt2 = new int[](M+10);
    cnt2[0] = N;
    auto ans = new dchar[](M);
    int mv = 0;

    foreach (i; 0..M) {
        bool ok = false;
        if (cnt1[A[i]] == mv && cnt2[cnt1[A[i]]] == 1) {
            ok = true;
            mv += 1;
        }
        cnt2[cnt1[A[i]]] -= 1;
        cnt1[A[i]] += 1;
        cnt2[cnt1[A[i]]] += 1;
        if (ok) {
            ans[i] = '1';
        } else {
            ans[i] = '0';
        }
    }

    ans.writeln;
}

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    int n,m;
    readf!" %s %s"(n, m);

    int[] cnt = new int[](n);

    int uniq = 0;

    int r = 0;
    foreach (i; 0 .. m) {
        int a; readf!" %s"(a);
        a--;
        if (cnt[a] == r) {
            uniq++;
        }
        cnt[a]++;

        if (uniq == n) {
            write(1);
            uniq = 0;
            r++;
        } else {
            write(0);
        }
    }
    writeln();
}


Let S(n) denote the number that represents the digits of n in sorted order. For example, S(1) = 1, S(5) = 5, S(50394) = 3459, S(353535) = 333555.Given a number X, compute  modulo 109 + 7.

The first line of input will contain the integer X (1 ≤ X ≤ 10700).

Print a single integer, the answer to the question.

NoteThe first few values of S are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 12. The sum of these values is 195.

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 7;
alias Mint = ModInt!MO;

enum LIM = 2020;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -(Mint.M / i) * inv[cast(size_t)(Mint.M % i)];
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
  } else {
    return Mint(0);
  }
}

enum LIM_L = 705;

void main() {
  prepare;

  auto pw = new Mint[][](10 + 1, LIM);
  foreach (a; 0 .. 10 + 1) {
    pw[a][0] = 1;
    foreach (e; 1 .. LIM) {
      pw[a][e] = pw[a][e - 1] * a;
    }
  }

  auto top = new Mint[][](10, LIM_L);
  auto bot = new Mint[][](10, LIM_L);
  foreach (d; 0 .. 10) {
    foreach (l; 0 .. LIM_L) {
      // a small, (l - a - b) same, b large
      // \sum_* d (10^(l-a) - 10^b)
      foreach (a; 0 .. l + 1) {
        top[d][l] += binom(l, a) * pw[d][a] * pw[10 - d][l - a] * d * pw[10][l - a];
      }
      foreach (b; 0 .. l + 1) {
        bot[d][l] += binom(l, b) * pw[d + 1][l - b] * pw[9 - d][b] * d * pw[10][b];
      }
    }
  }

  debug {
    foreach (d; 0 .. 10) {
      foreach (l; 0 .. 3 + 1) {
        Mint sum;
        foreach (n; 0 .. 10^^l) {
          auto cs = format("%0*d", l, n).dup;
          foreach (_; 0 .. l - 1) {
            foreach (i; 0 .. l - 1) {
              if (cs[i] > cs[i + 1]) {
                swap(cs[i], cs[i + 1]);
              }
            }
          }
          foreach (i; 0 .. l) {
            if (cs[i] - '0' == d) {
              sum += d * pw[10][l - 1 - i];
            }
          }
        }
        writeln(d, " ", l, ": ", top[d][l], " ", bot[d][l], " ", sum);
        assert(((top[d][l] - bot[d][l]) / 9).x == sum.x);
      }
    }
  }

  try {
    for (; ; ) {
      const X = readToken();
      const L = cast(int)(X.length);

      Mint ans;
      foreach (d; 0 .. 10) {
        int same, large;
        foreach (i; 0 .. L) {
          const x = X[i] - '0';
          foreach (e; 0 .. 10) {
            if ((i == L - 1) ? (e <= x) : (e < x)) {
              int same1 = same, large1 = large;
              if (e == d) {
                ++same1;
              } else if (e > d) {
                ++large1;
              }
              const l = L - 1 - i;
              debug {
                writeln(d, " ", i, " ", e, ": ", same1, " ", large1, " ", l);
              }
              ans += (top[d][l] * pw[10][same1] - bot[d][l]) * pw[10][large1];
            }
          }
          if (x == d) {
            ++same;
          } else if (x > d) {
            ++large;
          }
        }
      }
      ans /= 9;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.

If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).

NoteIn the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias mp=make_pair;
alias bins=binary_search;
alias orsq=orient_square;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	X binpow(X,Y)(X base,Y exp)
		if(is(typeof(exp&1)))
	{
		if(exp<0)return X(0);
		X res=X(1);
		while(exp>0)
		{
			if(exp&1)res*=base;
			base*=base;
			exp>>=1;
		}
		return res;
	}
	auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
		if(is(typeof(exp&1)))
	{
		if(mm==0) return binpow(base,exp);
		if(exp<0)return X(0);
		auto res=X(1)%mm;
		while(exp>0)
		{
			if(exp&1)res=(res*base)%mm;
			base*=base;
			base%=mm;
			exp>>=1;
		}
		return res;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (g==a[l])?l:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
	V foldr(R,V,T)(R range,T arg,V nach)
		if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
	{
		for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
		return nach;
	}
	size_t countr(R,T)(R range,T fun)
		if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
	{
		size_t nach;
		for(;!range.empty;range.popFront()) nach+=fun(range.front);
		return nach;
	}
	T orient_square(T)(pair!(T,T)[] figure...)
		if(is(typeof(figure.front*figure.front)==T))
	{
		auto n=figure.front,t=figure.front;
		figure.popFront();
		T ans=0;
		for(;!figure.empty;figure.popFront())
		{
			ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
			t=figure.front;
		}
		return ans+(n.fi-t.fi)*(n.se+t.se);
	}
	real square(T)(pair!(T,T)[] figure...)
		if(is(typeof(figure.front*figure.front)==T))
	{
		return abs(orient_square(figure))/2.000000000000000;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	pair!(X,Y) opAssign(A,B)(pair!(A,B) val)
	{
		return this=pair!(X,Y)(val);
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe
		if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
struct Tree(T,alias arg="a+b")
{
	private alias fun=binaryFun!arg;
	private static const size_t n=size_t.max>>1;
	private T[size_t] t;
	this(this){t=t.dup;}
	this(R)(R range) if(isInputRange!R && is(ElementType!(R) : T))
	{
		foreach(pos,i;range) upd(pos,i);
	}
	this(R,alias f)(Tree!(R,f) q) if(is(R:T))
	{
		foreach(i,j;q.t)
		{
			t[i]=j;
		}
	}
	Tree!(T,arg) opAssign(R)(R val)
	{
		return this=Tree!(T,arg)(val);
	}
	private void upd(in size_t v,in size_t vl,in size_t vr,in size_t i,in T x)
	{
		if (vr - vl == 1){t[v] = x;return;}
		size_t vm = (vl + vr) >> 1;
		if(i < vm) upd(2*v, vl, vm, i, x);
		else upd(2*v+1, vm, vr, i, x);
		if(v*2+1 !in t)t[v]=t[v*2];
		else if (v*2 !in t)t[v]=t[v*2+1];
		else t[v]=fun(t[v*2],t[v*2+1]);
	}
	void upd(in size_t i,in T x)
	{
		upd(1,0,n,i,x);
	}
	private T cel(in size_t v,in size_t vl,in size_t vr,in size_t l,in size_t r)
	{
		if(vl==l && r==vr) return t[v];
		size_t vm=(vl+vr) >> 1;
		if(r<=vm) return cel(2*v,vl,vm,l,r);
		else if(l>=vm) return cel(2*v+1,vm,vr,l,r);
		else return fun(cel(2*v,vl,vm,l,vm),cel(2*v+1,vm,vr,vm,r));
	}
	T cel(in size_t l,in size_t r){return cel(1,0,n,l,r);}
};
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(input(&n))
	{
		readln;
		auto q=rbt(arread!int);
		if(q.length>3)writeln("NO");
		else if(q.length==3)
		{
			auto a=q[].array;
			if(abs(a[0]-a[1])==abs(a[1]-a[2]))writeln("YES");
			else writeln("NO");
		}
		else writeln("YES");
	}
}


You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operation on it as many times as you want:  Pick two integers $$$i$$$ and $$$j$$$ $$$(1 \le i,j \le n)$$$ such that $$$a_i+a_j$$$ is odd, then swap $$$a_i$$$ and $$$a_j$$$. What is lexicographically the smallest array you can obtain?An array $$$x$$$ is lexicographically smaller than an array $$$y$$$ if there exists an index $$$i$$$ such that $$$x_i<y_i$$$, and $$$x_j=y_j$$$ for all $$$1 \le j < i$$$. Less formally, at the first index $$$i$$$ in which they differ, $$$x_i<y_i$$$

The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$.

The only line contains $$$n$$$ space-separated integers, the lexicographically smallest array you can obtain.

NoteIn the first example, we can swap $$$1$$$ and $$$4$$$ since $$$1+4=5$$$, which is odd.

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 10^^9 + 7;
immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;

    if (A.map!(a => a % 2 == A.front % 2).all) {
        A.map!(to!string).join(" ").writeln;
    } else {
        A.sort();
        A.map!(to!string).join(" ").writeln;
    }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.datetime.stopwatch;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    bool haveOdd = false, haveEven = false;

    foreach (e; arr) {
        haveOdd |= e % 2 == 1;
        haveEven |= e % 2 == 0;
    }

    if (haveOdd && haveEven) {
        arr.sort();
    }

    arr.writefln!"%(%s %)";
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

import std.container.rbtree;

void main() {
  auto r = new InputReader;
  int n = r.next!int;
  auto a = r.nextA!int(n);
  int[2] c;
  foreach (i; a) {
    ++c[i&1];
  }
  if (!c[0] || !c[1]) {
  } else {
    a.sort();
  }
  writefln ("%(%s %)", a);
}

import core.stdc.stdio;
import std.stdio;
import std.algorithm;
import std.range;

void main()
{
    int n;
    scanf("%d", &n);
    int[] a = new int[n];
    foreach (i; 0 .. n)
        scanf("%d", &a[i]);

    bool t = (all!"a%2==0"(a)) || (all!"a%2==1"(a));

    if (!t)
        sort(a);
    writefln("%(%d %)", a);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto N = RD!int;
	auto A = RDR.ARR;
	bool o, e;
	foreach (i; 0..N)
	{
		if (A[i] % 2 == 0)
			e = true;
		else
			o = true;
	}

	if (o && e)
		A.sort();

	A.map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}


The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) > 1$$$.

The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases.  Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.

Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD;
		foreach (i; n..n+100)
		{
			auto s = i.to!string;
			long y;
			foreach (c; s)
				y += c-'0';
			auto z = gcd(i, y);
			debug writeln("i:", i, " y:", y, " z:", z);
			if (z > 1)
			{
				ans[ti] = i;
				debug writeln("i:", i, " y:", y, " z:", z, " ans:", ans[ti]);
				break;
			}
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}


Let $$$a$$$ be a matrix of size $$$r \times c$$$ containing positive integers, not necessarily distinct. Rows of the matrix are numbered from $$$1$$$ to $$$r$$$, columns are numbered from $$$1$$$ to $$$c$$$. We can construct an array $$$b$$$ consisting of $$$r + c$$$ integers as follows: for each $$$i \in [1, r]$$$, let $$$b_i$$$ be the greatest common divisor of integers in the $$$i$$$-th row, and for each $$$j \in [1, c]$$$ let $$$b_{r+j}$$$ be the greatest common divisor of integers in the $$$j$$$-th column. We call the matrix diverse if all $$$r + c$$$ numbers $$$b_k$$$ ($$$k \in [1, r + c]$$$) are pairwise distinct. The magnitude of a matrix equals to the maximum of $$$b_k$$$.For example, suppose we have the following matrix: $$$\begin{pmatrix} 2 & 9 & 7\\ 4 & 144 & 84 \end{pmatrix}$$$ We construct the array $$$b$$$:  $$$b_1$$$ is the greatest common divisor of $$$2$$$, $$$9$$$, and $$$7$$$, that is $$$1$$$;  $$$b_2$$$ is the greatest common divisor of $$$4$$$, $$$144$$$, and $$$84$$$, that is $$$4$$$;  $$$b_3$$$ is the greatest common divisor of $$$2$$$ and $$$4$$$, that is $$$2$$$;  $$$b_4$$$ is the greatest common divisor of $$$9$$$ and $$$144$$$, that is $$$9$$$;  $$$b_5$$$ is the greatest common divisor of $$$7$$$ and $$$84$$$, that is $$$7$$$. So $$$b = [1, 4, 2, 9, 7]$$$. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to $$$9$$$.For a given $$$r$$$ and $$$c$$$, find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer $$$0$$$.

The only line in the input contains two space separated integers $$$r$$$ and $$$c$$$ ($$$1 \leq r,c \leq 500$$$) — the number of rows and the number of columns of the matrix to be found.

If there is no solution, output a single integer $$$0$$$. Otherwise, output $$$r$$$ rows. The $$$i$$$-th of them should contain $$$c$$$ space-separated integers, the $$$j$$$-th of which is $$$a_{i,j}$$$ — the positive integer in the $$$i$$$-th row and $$$j$$$-th column of a diverse matrix minimizing the magnitude. Furthermore, it must hold that $$$1 \leq a_{i,j} \leq 10^9$$$. It can be shown that if a solution exists, there is also a solution with this additional constraint (still having minimum possible magnitude).

NoteIn the first example, the GCDs of rows are $$$b_1 = 4$$$ and $$$b_2 = 1$$$, and the GCDs of columns are $$$b_3 = 2$$$ and $$$b_4 = 3$$$. All GCDs are pairwise distinct and the maximum of them is $$$4$$$. Since the GCDs have to be distinct and at least $$$1$$$, it is clear that there are no diverse matrices of size $$$2 \times 2$$$ with magnitude smaller than $$$4$$$.In the second example, no matter what $$$a_{1,1}$$$ is, $$$b_1 = b_2$$$ will always hold, so there are no diverse matrices.

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];

    if (H == 1 && W == 1) {
        writeln(0);
        return;
    }

    bool swapped = false;
    if (H > W) swap(H, W), swapped = true;
    auto A = new long[][](H, W);

    auto R = iota(1, H+1).map!(to!long).array;
    auto C = iota(H+1, H+W+1).map!(to!long).array;

    foreach (i; 0..H) foreach (j; 0..W) {
        A[i][j] = R[i] * C[j] / gcd(R[i], C[j]);
    }

    if (!swapped) {
        A.each!(a => a.map!(to!string).join(" ").writeln);
    } else {
        auto ans = new long[][](W, H);
        foreach (i; 0..H) foreach (j; 0..W) ans[j][i] = A[i][j];
        ans.each!(a => a.map!(to!string).join(" ").writeln);
    }

    foreach (i; 0..H) {
        if (A[i].reduce!gcd != R[i]) {
            writeln([H, W]);
            A.each!writeln;
            return;
        }
    }
    foreach (j; 0..W) {
        auto B = H.iota.map!(i => A[i][j]).array;
        if (B.reduce!gcd != C[j]) {
            writeln([H, W]);
            return;
        }
    }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int r, c;
	while (readf !(" %s %s") (r, c) > 0)
	{
		if (r == 1 && c == 1)
		{
			writeln (0);
			continue;
		}
		auto a = new int [] [] (r, c);
		foreach (i; 0..r)
		{
			foreach (j; 0..c)
			{
				a[i][j] = (i + 1) * (j + r + 1);
			}
		}
		if (c == 1)
		{
			foreach (i; 0..r)
			{
				foreach (j; 0..c)
				{
					a[i][j] = (i + c + 1) * (j + 1);
				}
			}
		}
		writefln !("%(%(%s %)\n%)") (a);
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int r = rint, c = rint;
	int[][] ans;
	if(r == 1 && c == 1) ans = [[0]];
	else if(r == 1){
		ans = [[]];
		foreach(j; 0 .. c) ans[0] ~= j + 2;
	}
	else if(c == 1){
		foreach(i ; 0 .. r) ans ~= [i + 2];
	}
	else{
		foreach(i; 0 .. r){
			ans ~= [[]];
			foreach(j; 0 .. c){
				if(i == 0) ans[i] ~=  1 * (j + 2);
				else ans[i] ~= (c + i + 1) * (j + 2);
			}
		}
	}
	foreach(an; ans) an.map!(to!string).array.join(" ").writeln;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int lcm(int a, int b) {
  return a / gcd(a, b) * b;
}

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();

      auto ans = new int[][](M, N);
      if (M == 1) {
        if (N == 1) {
          ans[0][0] = 0;
        } else {
          foreach (y; 0 .. N) {
            ans[0][y] = 2 + y;
          }
        }
      } else {
        if (N == 1) {
          foreach (x; 0 .. M) {
            ans[x][0] = 2 + x;
          }
        } else {
          foreach (x; 0 .. M) foreach (y; 0 .. N) {
            ans[x][y] = lcm(1 + x, 1 + M + y);
          }
        }
      }
      foreach (x; 0 .. M) {
        foreach (y; 0 .. N) {
          if (y > 0) write(" ");
          write(ans[x][y]);
        }
        writeln();
      }
    }
  } catch (EOFException e) {
  }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto r = RD!int;
	auto c = RD!int;
	auto ans = new long[][](r, c);
	int x, y;
	if (r < c)
		x = r;
	else
		y = c;
	foreach (i; 0..r)
	{
		foreach (j; 0..c)
		{
			ans[i][j] = lcm(y+i+1, x+j+1);
		}
	}

	if (r == 1 && c == 1)
		writeln(0);
	else
	{
		foreach (i; 0..r)
			ans[i].map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];

    if (H == 1 && W == 1) {
        writeln(1);
        return;
    }

    bool swapped = false;
    if (H > W) swap(H, W), swapped = true;
    auto A = new long[][](H, W);

    auto R = iota(1, H+1).map!(to!long).array;
    auto C = iota(H+1, H+W+1).map!(to!long).array;

    foreach (i; 0..H) foreach (j; 0..W) {
        A[i][j] = R[i] * C[j] / gcd(R[i], C[j]);
    }

    if (!swapped) {
        A.each!(a => a.map!(to!string).join(" ").writeln);
    } else {
        auto ans = new long[][](W, H);
        foreach (i; 0..H) foreach (j; 0..W) ans[j][i] = A[i][j];
        ans.each!(a => a.map!(to!string).join(" ").writeln);
    }

}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto A = new long[][](H, W);

    auto R = iota(1, H+1).map!(to!long).array;
    auto C = iota(H+1, H+W+1).map!(to!long).array;

    foreach (i; 0..H) foreach (j; 0..W) {
        A[i][j] = R[i] * C[j] / gcd(R[i], C[j]);
    }

    A.each!(a => a.map!(to!string).join(" ").writeln);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto r = RD!int;
	auto c = RD!int;
	auto ans = new long[][](r, c);
	foreach (i; 0..r)
	{
		foreach (j; 0..c)
		{
			ans[i][j] = lcm(i+1, r+j+1);
		}
	}

	if (r == 1 && c == 1)
		writeln(0);
	else
	{
		foreach (i; 0..r)
			ans[i].map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}