Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.    "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

NoteIn the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.In the second example, Vasya has to come before anyone else to be served.

import std.stdio;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto ts = read!long;
    auto tf = read!long;
    auto t  = read!long;
    auto n  = read!int;
    auto a  = new long[100_000];
    auto b  = new long[100_000];

    foreach (i; 0..n)
        a[i] = read!long;

    if (n == 0)
    {
        writeln(ts);
        return 0;
    }

    // find serving time for everybody
    long curr = ts;
    foreach (i; 0..n)
    {
        if (a[i] <= curr)
        {
            b[i] = curr;
            //b[i] = curr - a[i];
            curr += t;
        }
        else
        {
            b[i] = a[i];
            //b[i] = 0;
            curr = b[i] + t;
        }

        if (b[i] + t > tf)
            b[i] = long.max / 100;
    }

    // can make it without waiting?
    curr = ts;
    foreach (i; 0..n)
    {
        //writeln ("qq ", b[i], " ", curr);
        if (b[i] > curr && curr + t < tf)
        {
            writeln(curr);
            return 0;
        }
        curr = b[i] + t;
    }
    if (tf - curr >= t)
    {
        writeln(curr);
        return 0;
    }

    // find least waiting time
    long ans = a[0] - 1;
    long m = b[0] - a[0];
    foreach (i; 0..n)
    {
        b[i] -= a[i];
        if (b[i] < m)
        {
            m = b[i];
            ans = a[i] - 1;
        }
    }

    writeln(ans);
    //writeln(a[0..n]);
    //writeln(b[0..n]);

    return 0;
}

import std.stdio;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto ts = read!int;
    auto tf = read!int;
    auto t  = read!int;
    auto n  = read!int;
    auto a  = new int[100_000];
    auto b  = new int[100_000];

    foreach (i; 0..n)
        a[i] = read!int;

    if (n == 0)
        writeln(ts);

    // find serving time for everybody
    int curr = ts;
    foreach (i; 0..n)
    {
        if (a[i] <= curr)
        {
            b[i] = curr;
            //b[i] = curr - a[i];
            curr += t;
        }
        else
        {
            b[i] = a[i];
            //b[i] = 0;
            curr = b[i] + t;
        }
    }

    // can make it without waiting?
    curr = ts;
    foreach (i; 0..n)
    {
        //writeln ("qq ", b[i], " ", curr);
        if (b[i] > curr)
        {
            writeln(curr);
            return 0;
        }
        curr = b[i] + t;
    }
    if (tf - curr >= t)
    {
        writeln(curr);
        return 0;
    }

    // find least waiting time
    int ans = a[0] - 1;
    int m = b[0] - a[0];
    foreach (i; 0..n)
    {
        b[i] -= a[i];
        if (b[i] < m)
        {
            m = b[i];
            ans = a[i] - 1;
        }
    }

    writeln(ans);
    //writeln(a[0..n]);
    //writeln(b[0..n]);

    return 0;
}

import std.stdio;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto ts = read!long;
    auto tf = read!long;
    auto t  = read!long;
    auto n  = read!int;
    auto a  = new long[100_000];
    auto b  = new long[100_000];

    foreach (i; 0..n)
        a[i] = read!long;

    if (n == 0)
    {
        writeln(ts);
        return 0;
    }

    // find serving time for everybody
    long curr = ts;
    foreach (i; 0..n)
    {
        if (a[i] <= curr)
        {
            b[i] = curr;
            //b[i] = curr - a[i];
            curr += t;
        }
        else
        {
            b[i] = a[i];
            //b[i] = 0;
            curr = b[i] + t;
        }
    }

    // can make it without waiting?
    curr = ts;
    foreach (i; 0..n)
    {
        //writeln ("qq ", b[i], " ", curr);
        if (b[i] > curr)
        {
            writeln(curr);
            return 0;
        }
        curr = b[i] + t;
    }
    if (tf - curr >= t)
    {
        writeln(curr);
        return 0;
    }

    // find least waiting time
    long ans = a[0] - 1;
    long m = b[0] - a[0];
    foreach (i; 0..n)
    {
        b[i] -= a[i];
        if (b[i] < m)
        {
            m = b[i];
            ans = a[i] - 1;
        }
    }

    writeln(ans);
    //writeln(a[0..n]);
    //writeln(b[0..n]);

    return 0;
}


Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

NoteIn the first example you can simply make one move to obtain sequence [0, 2] with .In the second example the gcd of the sequence is already greater than 1.

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;

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

    if (A.reduce!gcd > 1) {
        writeln(0);
        return;
    }

    int ans = 0;
    foreach (i; 0..N) {
        if (A[i] % 2 == 0) continue;
        else if (i == N-1) ans += 2;
        else if (A[i+1] % 2 == 0) ans += 2;
        else {
            ans += 1;
            A[i+1] = 2;
        }
    }

    ans.writeln;
}

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.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;immutable int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	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))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos: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;
	}
}
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...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n;
loop:while(read(n))
	{
		auto a=arread!lint;
		lint xx=0;
		foreach(i;a)xx=gcd(xx,i);
		if(xx!=1)
		{
			writeln("YES\n0");
		}
		else
		{
			int ans=0;
			foreach(i;0..n-1)
			{
				while(a[i]&1)
				{
					long x=a[i]+a[i+1],y=a[i]-a[i+1];
					a[i]=x;
					a[i+1]=y;
					ans++;
				}
			}
			while((a[n-1]&1) || (a[n-2]&1))
			{
				long x=a[n-2]+a[n-1],y=a[n-2]-a[n-1];
				a[n-2]=x;
				a[n-1]=y;
				ans++;
			}
			writeln("YES\n",ans);
		}
	}
}


A palindrome is a string $$$t$$$ which reads the same backward as forward (formally, $$$t[i] = t[|t| + 1 - i]$$$ for all $$$i \in [1, |t|]$$$). Here $$$|t|$$$ denotes the length of a string $$$t$$$. For example, the strings 010, 1001 and 0 are palindromes.You have $$$n$$$ binary strings $$$s_1, s_2, \dots, s_n$$$ (each $$$s_i$$$ consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.Formally, in one move you:  choose four integer numbers $$$x, a, y, b$$$ such that $$$1 \le x, y \le n$$$ and $$$1 \le a \le |s_x|$$$ and $$$1 \le b \le |s_y|$$$ (where $$$x$$$ and $$$y$$$ are string indices and $$$a$$$ and $$$b$$$ are positions in strings $$$s_x$$$ and $$$s_y$$$ respectively),  swap (exchange) the characters $$$s_x[a]$$$ and $$$s_y[b]$$$. What is the maximum number of strings you can make palindromic simultaneously?

The first line contains single integer $$$Q$$$ ($$$1 \le Q \le 50$$$) — the number of test cases. The first line on each test case contains single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of binary strings you have. Next $$$n$$$ lines contains binary strings $$$s_1, s_2, \dots, s_n$$$ — one per line. It's guaranteed that $$$1 \le |s_i| \le 50$$$ and all strings constist of zeroes and/or ones.

Print $$$Q$$$ integers — one per test case. The $$$i$$$-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the $$$i$$$-th test case.

NoteIn the first test case, $$$s_1$$$ is palindrome, so the answer is $$$1$$$.In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make $$$s_1 = \text{0110}$$$, $$$s_2 = \text{111111}$$$ and $$$s_3 = \text{010000}$$$.In the third test case we can make both strings palindromic. For example, $$$s_1 = \text{11011}$$$ and $$$s_2 = \text{100001}$$$.In the last test case $$$s_2$$$ is palindrome and you can make $$$s_1$$$ palindrome, for example, by swapping $$$s_1[2]$$$ and $$$s_1[3]$$$.

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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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 = 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 main()
{
	auto q = RD!int;
	auto ans = new int[](q);
	foreach (i; 0..q)
	{
		auto n = RD!int;
		int odd;
		int cnt1, cnt2;
		foreach (j; 0..n)
		{
			auto s = RD!string;
			if (s.length % 2 == 1)
				++odd;
			foreach (c; s)
			{
				if (c == '1')
					++cnt1;
				else
					++cnt2;
			}
		}
		foreach (j; 0..odd)
		{
			if (cnt1 % 2 == 1)
				--cnt1;
			else if (cnt2 % 2 == 1)
				--cnt2;
			else if (cnt1 > cnt2)
				--cnt1;
			else
				--cnt2;
		}
		if (cnt1 % 2 == 0 && cnt2 % 2 == 0)
			ans[i] = n;
		else
			ans[i] = n-1;

	}

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


After you have read all the problems, probably, you think Alex is genius person. That's true! One day he came up with the following task.Given a sequence of integer numbers a1, a2, ..., an. You are to find a longest sequence b1, b2, ..., b4m, that satisfies the following conditions:  b4k + 1 = b4k + 3 for all valid integer k;  b4k + 2 = b4k + 4 for all valid integer k;  sequence b is subsequence of a (not necessarily contiguous subsequence). And finally... Alex had given this complicated task to George, and George gave it to you. Help George to cope with the task.

The first line contains a single integer n (1 ≤ n ≤ 5·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

In the first line print a single integer 4m — the maximal possible length of required sequence b. In the second line print 4m integers b1, b2, ..., b4m, that is required sequence. If there are multiple optimal answers you may print any of them.

import std.stdio;

immutable int N = 500001;

int[N<<2] tree;

void build(int x, int l, int r) {
  if (l == r) {
    tree[x] = -1;
  } else {
    int mid = (l + r) >> 1;
    build(x << 1, l, mid);
    build(x << 1 | 1, mid+1, r);
    tree[x] = -1;
  }
}

void update(int x, int l, int r, int ql, int qr, int qv) {
  if (l > qr || r < ql) {
    return;
  } else if (l >= ql && r <= qr) {
    tree[x] = qv;
  } else {
    int mid = (l + r) >> 1;
    if (tree[x] != -1) {
      tree[x<<1] = tree[x];
      tree[x<<1|1] = tree[x];
    }
    update(x << 1, l, mid, ql, qr, qv);
    update(x << 1 | 1, mid + 1, r, ql, qr, qv);
    tree[x] = -1;
  }
}

int query(int x, int l, int r, int qp) {
  if (l == r) {
    return tree[x];
  } else {
    int mid = (l + r) >> 1;
    if (tree[x] != -1) {
      tree[x<<1] = tree[x];
      tree[x<<1|1] = tree[x];
    }
    if (qp <= mid) {
      return query(x << 1, l, mid, qp);
    } else {
      return query(x << 1 | 1, mid+1, r, qp);
    }
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new int[n];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  build(1, 0, n-1);
  int[] ans;
  auto prev = new int[n];
  foreach (ref i; prev) {
    i = -1;
  }
  int it = 0;
  while (it < n) {
    int[int] pos;
    int jt = it;
    while (jt < n) {
      if (a[jt] in pos) {
        prev[jt] = pos[a[jt]];
      }
      int q = query(1, 0, n-1, prev[jt]);
      if (q != -1) {
        ans ~= a[prev[q]];
        ans ~= a[prev[jt]];
        ans ~= a[q];
        ans ~= a[jt];
        break;
      } else if (prev[jt] != -1 && prev[prev[jt]] != -1 && prev[prev[prev[jt]]] != -1) {
        ans ~= a[jt];
        ans ~= a[jt];
        ans ~= a[jt];
        ans ~= a[jt];
        break;
      } else {
        pos[a[jt]] = jt;
        if (prev[jt] != -1) {
          update(1, 0, n-1, prev[jt]+1, jt-1, jt);
        }
      }
      jt++;
    }
    it = jt + 1;
  }
  writeln(ans.length);
  writef("%(%s %)\n", ans);
}

import std.stdio;

immutable int N = 500001;

int[N<<2] tree;

void build(int x, int l, int r) {
  if (l == r) {
    tree[x] = -1;
  } else {
    int mid = (l + r) >> 1;
    build(x << 1, l, mid);
    build(x << 1 | 1, mid+1, r);
    tree[x] = -1;
  }
}

void update(int x, int l, int r, int ql, int qr, int qv) {
  if (l > qr || r < ql) {
    return;
  } else if (l >= ql && r <= qr) {
    tree[x] = qv;
  } else {
    int mid = (l + r) >> 1;
    if (tree[x] != -1) {
      tree[x<<1] = tree[x];
      tree[x<<1|1] = tree[x];
    }
    update(x << 1, l, mid, ql, qr, qv);
    update(x << 1 | 1, mid + 1, r, ql, qr, qv);
    tree[x] = -1;
  }
}

int query(int x, int l, int r, int qp) {
  if (l == r) {
    return tree[x];
  } else {
    int mid = (l + r) >> 1;
    if (tree[x] != -1) {
      tree[x<<1] = tree[x];
      tree[x<<1|1] = tree[x];
    }
    if (qp <= mid) {
      return query(x << 1, l, mid, qp);
    } else {
      return query(x << 1 | 1, mid+1, r, qp);
    }
  }
}

void main() {
  int n; readf(" %s", &n);
  auto a = new int[n];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  build(1, 0, n-1);
  int[] ans;
  auto prev = new int[n];
  foreach (ref i; prev) {
    i = -1;
  }
  int it = 0;
  while (it < n) {
    int[int] pos;
    int jt = it;
    while (jt < n) {
      if (a[jt] in pos) {
        prev[jt] = pos[a[jt]];
      }
      int q = query(1, 0, n-1, prev[jt]);
      if (q != -1) {
        ans ~= a[prev[q]];
        ans ~= a[prev[jt]];
        ans ~= a[q];
        ans ~= a[jt];
        break;
      } else {
        pos[a[jt]] = jt;
        if (prev[jt] != -1) {
          update(1, 0, n-1, prev[jt]+1, jt-1, jt);
        }
      }
      jt++;
    }
    it = jt + 1;
  }
  writeln(ans.length);
  writef("%(%s %)\n", ans);
}


Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with $$$N$$$ vertices and $$$M$$$ edges. In the graph, edge $$$i$$$ connects two different vertices $$$U_i$$$ and $$$V_i$$$ with a length of $$$W_i$$$. By using the $$$i$$$-th edge, something can move from $$$U_i$$$ to $$$V_i$$$, but not from $$$V_i$$$ to $$$U_i$$$.To play this game, initially Pak Chanek must place both of his hands onto two different vertices. In one move, he can move one of his hands to another vertex using an edge. To move a hand from vertex $$$U_i$$$ to vertex $$$V_i$$$, Pak Chanek needs a time of $$$W_i$$$ seconds. Note that Pak Chanek can only move one hand at a time. This game ends when both of Pak Chanek's hands are on the same vertex.Pak Chanek has several questions. For each $$$p$$$ satisfying $$$2 \leq p \leq N$$$, you need to find the minimum time in seconds needed for Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex $$$1$$$ and vertex $$$p$$$, or report if it is impossible.

The first line contains two integers $$$N$$$ and $$$M$$$ ($$$2 \leq N \leq 10^5$$$, $$$0 \leq M \leq 2 \cdot 10^5$$$) — the number of vertices and edges in the graph. The $$$i$$$-th of the next $$$M$$$ lines contains three integers $$$U_i$$$, $$$V_i$$$, and $$$W_i$$$ ($$$1 \le U_i, V_i \le N$$$, $$$U_i \neq V_i$$$, $$$1 \le W_i \le 10^9$$$) — a directed edge that connects two different vertices $$$U_i$$$ and $$$V_i$$$ with a length of $$$W_i$$$. There is no pair of different edges $$$i$$$ and $$$j$$$ such that $$$U_i = U_j$$$ and $$$V_i = V_j$$$.

Output a line containing $$$N-1$$$ integers. The $$$j$$$-th integer represents the minimum time in seconds needed by Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex $$$1$$$ and vertex $$$j+1$$$, or $$$-1$$$ if it is impossible.

NoteIf initially Pak Chanek's left hand is on vertex $$$1$$$ and his right hand is on vertex $$$5$$$, Pak Chanek can do the following moves:   Move his right hand to vertex $$$4$$$ in $$$1$$$ second.  Move his left hand to vertex $$$2$$$ in $$$2$$$ seconds.  Move his left hand to vertex $$$4$$$ in $$$1$$$ second. In total it needs $$$1+2+1=4$$$ seconds. It can be proven that there is no other way that is faster.

import std.stdio;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.container;

alias Tuple!(int, "to", long, "cost") Edge;
alias Tuple!(int, "v", long, "cost") State;

long[] dijkstra(in Edge[][] G, int N, int s) {
    auto PQ = heapify!"a.cost > b.cost"(new State[0], 0);
    auto D = new long[](N);
    D[] = long.max;
    PQ.insert(State(s, 0));
    D[s] = 0;
    while (! PQ.empty) {
        State cur = PQ.front; PQ.popFront;
        if (D[cur.v] != cur.cost) continue;
        foreach (e; G[cur.v]) {
            auto nextv = e.to;
            auto nexts = State(nextv, cur.cost + e.cost);
            if (D[nexts.v] > nexts.cost) {
                D[nexts.v] = nexts.cost;
                PQ.insert(nexts);
            }
        }
    }
    return D;
}

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto G = new Edge[][2*N];
    foreach (i; 0 .. N) {
        G[i] ~= Edge(N + i, 0);
    }
    foreach (i; 0 .. M) {
        int U, V, W; scanf("%d %d %d\n", &U, &V, &W);
        U--; V--;
        G[U] ~= Edge(V, W);
        G[N + V] ~= Edge(N + U, W);
    }
    auto D = dijkstra(G, 2*N, 0);
    long[] buf;
    for (int p = 1; p < N; p++) {
        long ans = long.max;
        for (int b = 0; b < 2; b++) {
            ans = min(ans, D[p + N * b]);
        }
        buf ~= (ans == long.max ? -1 : ans);
    }
    writefln("%(%s %)", buf);
}

import std.stdio;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.container;

alias Tuple!(int, "to", long, "cost") Edge;
alias Tuple!(int, "v", long, "cost") State;

long[] dijkstra(in Edge[][] G, int N, int s) {
    auto PQ = heapify!"a.cost > b.cost"(new State[0], 0);
    auto D = new long[](N);
    D[] = long.max;
    PQ.insert(State(0, 0));
    D[0] = 0;
    while (! PQ.empty) {
        State cur = PQ.front; PQ.popFront;
        if (D[cur.v] != cur.cost) continue;
        foreach (e; G[cur.v]) {
            auto nextv = e.to;
            auto nexts = State(nextv, cur.cost + e.cost);
            if (D[nexts.v] > nexts.cost) {
                D[nexts.v] = nexts.cost;
                PQ.insert(nexts);
            }
        }
    }
    return D;
}

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto G = new Edge[][2*N];
    foreach (i; 0 .. N) {
        G[i] ~= Edge(N + i, 0);
    }
    foreach (i; 0 .. M) {
        int U, V, W; scanf("%d %d %d\n", &U, &V, &W);
        U--; V--;
        G[U] ~= Edge(V, W);
        G[N + V] ~= Edge(N + U, W);
    }
    auto D = dijkstra(G, 2*N, 0);
    long[] buf;
    for (int p = 1; p < N; p++) {
        long ans = long.max;
        for (int b = 0; b < 2; b++) {
            ans = min(ans, D[p + N * b]);
        }
        buf ~= (ans == long.max ? -1 : ans);
    }
    writefln("%(%s %)", buf);
}

import std.stdio;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.container;

alias Tuple!(int, "to", long, "cost") Edge;
alias Tuple!(int, "v", long, "cost") State;

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto G = new Edge[][2*N];
    foreach (i; 0 .. N) {
        G[i] ~= Edge(N + i, 0);
    }
    foreach (i; 0 .. M) {
        int U, V, W; scanf("%d %d %d\n", &U, &V, &W);
        U--; V--;
        G[U] ~= Edge(V, W);
        G[N + V] ~= Edge(N + U, W);
    }
    auto PQ = heapify!"a.cost > b.cost"(new State[0], 0);
    auto D = new long[](2*N);
    D[] = long.max;
    PQ.insert(State(0, 0));
    D[0] = 0;
    while (! PQ.empty) {
        State cur = PQ.front; PQ.popFront;
        if (D[cur.v] != cur.cost) continue;
        foreach (e; G[cur.v]) {
            auto nextv = e.to;
            auto nexts = State(nextv, cur.cost + e.cost);
            if (D[nexts.v] > nexts.cost) {
                D[nexts.v] = nexts.cost;
                PQ.insert(nexts);
            }
        }
    }
    long[] buf;
    for (int p = 1; p < N; p++) {
        long ans = long.max;
        for (int b = 0; b < 2; b++) {
            ans = min(ans, D[p + N * b]);
        }
        buf ~= (ans == long.max ? -1 : ans);
    }
    writefln("%(%s %)", buf);
}

import std.stdio;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.container;

alias Tuple!(int, "from", int, "to", long, "cost") Edge;
alias Tuple!(int, "v", int, "b") ExtV;
alias Tuple!(ExtV, "v", long, "cost") State;

void main() {
    int N, M; readf("%d %d\n", &N, &M);
    auto G = new Edge[][N],
        rG = new Edge[][N];
    foreach (i; 0 .. M) {
        int U, V, W; readf("%d %d %d\n", &U, &V, &W);
        U--; V--;
        G[U] ~= Edge(U, V, W);
        rG[V] ~= Edge(V, U, W);
    }
    auto PQ = heapify!"a.cost > b.cost"(new State[0]);
    long[ExtV] D;
    PQ.insert(State(ExtV(0, 0), 0));
    while (! PQ.empty) {
        State cur = PQ.front; PQ.popFront;
        if (cur.v.b  == 0) {
            // can use original and reversed
            foreach (e; G[cur.v.v]) {
                auto nextv = e.to;
                auto nexts = State(ExtV(nextv, 0), cur.cost + e.cost);
                if (nexts.v !in D || D[nexts.v] > nexts.cost) {
                    D[nexts.v] = nexts.cost;
                    PQ.insert(nexts);
                }
            }
        }
        // can use reversed
        foreach (e; rG[cur.v.v]) {
            auto nextv = e.to;
            auto nexts = State(ExtV(nextv, 1), cur.cost + e.cost);
            if (nexts.v !in D || D[nexts.v] > nexts.cost) {
                D[nexts.v] = nexts.cost;
                PQ.insert(nexts);
            }
        }
    }
    long[] buf;
    for (int p = 1; p < N; p++) {
        long ans = int.max;
        for (int b = 0; b < 2; b++) {
            auto k = ExtV(p, b);
            if (k !in D) continue;
            ans = min(ans, D[k]);
        }
        buf ~= (ans == int.max ? -1 : ans);
    }
    writefln("%(%s %)", buf);
}


You are given a tree (an undirected connected acyclic graph) consisting of $$$n$$$ vertices and $$$n - 1$$$ edges. A number is written on each edge, each number is either $$$0$$$ (let's call such edges $$$0$$$-edges) or $$$1$$$ (those are $$$1$$$-edges).Let's call an ordered pair of vertices $$$(x, y)$$$ ($$$x \ne y$$$) valid if, while traversing the simple path from $$$x$$$ to $$$y$$$, we never go through a $$$0$$$-edge after going through a $$$1$$$-edge. Your task is to calculate the number of valid pairs in the tree.

The first line contains one integer $$$n$$$ ($$$2 \le n \le 200000$$$) — the number of vertices in the tree. Then $$$n - 1$$$ lines follow, each denoting an edge of the tree. Each edge is represented by three integers $$$x_i$$$, $$$y_i$$$ and $$$c_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$0 \le c_i \le 1$$$, $$$x_i \ne y_i$$$) — the vertices connected by this edge and the number written on it, respectively. It is guaranteed that the given edges form a tree.

Print one integer — the number of valid pairs of vertices.

NoteThe picture corresponding to the first example:

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;

void main() {
    auto N = readln.chomp.to!int;
    auto G = new Tuple!(int, int)[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        G[s[0]-1] ~= tuple(s[1]-1, s[2]);
        G[s[1]-1] ~= tuple(s[0]-1, s[2]);
    }

    auto uf0 = new UnionFind(N);
    auto uf1 = new UnionFind(N);
    foreach (i; 0..N) foreach (to; G[i]) (to[1] == 0 ? uf0 : uf1).unite(i, to[0]);

    long ans = 0;
    foreach (i; 0..N) if (uf0.find(i) == i) ans += 1L * -uf0.table[i] * (-uf0.table[i]-1);
    foreach (i; 0..N) if (uf1.find(i) == i) ans += 1L * -uf1.table[i] * (-uf1.table[i]-1);
    foreach (i; 0..N) ans += 1L * (-uf0.table[uf0.find(i)] - 1) * (-uf1.table[uf1.find(i)] - 1);

    ans.writeln;
}

class UnionFind {
    int N;
    int[] table;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
    }
}


In the Isle of Guernsey there are n different types of coins. For each i (1 ≤ i ≤ n), coin of type i is worth ai cents. It is possible that ai = aj for some i and j (i ≠ j). Bessie has some set of these coins totaling t cents. She tells Jessie q pairs of integers. For each i (1 ≤ i ≤ q), the pair bi, ci tells Jessie that Bessie has a strictly greater number of coins of type bi than coins of type ci. It is known that all bi are distinct and all ci are distinct. Help Jessie find the number of possible combinations of coins Bessie could have. Two combinations are considered different if there is some i (1 ≤ i ≤ n), such that the number of coins Bessie has of type i is different in the two combinations. Since the answer can be very large, output it modulo 1000000007 (109 + 7). If there are no possible combinations of coins totaling t cents that satisfy Bessie's conditions, output 0.

The first line contains three space-separated integers, n, q and t (1 ≤ n ≤ 300; 0 ≤ q ≤ n; 1 ≤ t ≤ 105). The second line contains n space separated integers, a1, a2, ..., an (1 ≤ ai ≤ 105). The next q lines each contain two distinct space-separated integers, bi and ci (1 ≤ bi, ci ≤ n; bi ≠ ci). It's guaranteed that all bi are distinct and all ci are distinct.

A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109 + 7).

NoteFor the first sample, the following 3 combinations give a total of 17 cents and satisfy the given conditions: {0 of type 1, 1 of type 2, 3 of type 3, 2 of type 4}, {0, 0, 6, 1}, {2, 0, 3, 1}.No other combinations exist. Note that even though 4 occurs in both bi and ci,  the problem conditions are still satisfied because all bi are distinct and all ci are distinct.

import std.stdio, std.getopt, std.random, std.range, std.string, std.conv;
import std.algorithm;

const MD = 1_000_000_007;

int[] a, sum;
int n;

int[][] dp1;

int dfs(int x, int t) {
	//writeln("start", " ", gi, " ", i, " ", t, " ");
	if (t < 0) return 0;
	if (x == sum.length) {
		if (!t) return 1;
		else return 0;
	}
	if (dp1[x][t] != -1) return dp1[x][t];
	int res = dfs(x+1, t)+dfs(x, t-sum[x]);
	res %= MD;
/*	foreach (j; 0..(t+1)) {
		if (t-sum[x]*j < 0) break;
		res += dfs(gi, i+1, t-sum[x]*j);
		res %= MD;
	}*/
	//writeln(gi, " ", i, " ", t, " ", res);
	return dp1[x][t] = res;
}

int main(string[] args) {
	int n, q; long t;
	readf("%d %d %d\n", &n, &q, &t);
	sum = new int[n];
	a = readln().split().map!(to!int)().array;
	auto next = new int[n];
	auto back = new int[n];
	next[] = -1; back[] = -1;
	dp1 = new int[][](n, cast(int)(t+1));
	foreach (i; 0..n) {
		dp1[i][] = -1;
	}
	foreach (i; 0..q) {
		int b, c;
		readf("%d %d\n", &b, &c); b--; c--;
		next[b] = c;
		back[c] = b;
	}
	auto count = 0;
	foreach (i; 0..n) {
		if (next[i] > -1) continue;
		int j = i;
		int c = 0;
		count++;
		while (back[j] != -1) {
			j = back[j];
			c++;
			t -= c*a[j];
			count++;
		}
		sum[j] = a[j];
		while (next[j] != -1) {
			j = next[j];
			sum[j] = sum[back[j]] + a[j];
		}
	}
	if (count < n || t < 0) {
		writeln(0);
		return 0;
	}
//	writeln(g);
//	writeln(sum);
//	writeln(dp1);
//	writeln(t);
	writeln(dfs(0, cast(int)(t)));
	return 0;
}

import std.stdio, std.getopt, std.random, std.range, std.string, std.conv;
import std.algorithm;

const MD = 1_000_000_007;

int[][] g;
int[] a, sum;
int n;

int[][] dp1, dp2;

int dfs(int gi, int i, int t) {
	//writeln("start", " ", gi, " ", i, " ", t, " ");
	if (t < 0) return 0;
	if (i == g[gi].length) {
		if (!t) return 1;
		else return 0;
	}
	auto x = g[gi][i];
	if (dp1[x][t] != -1) return dp1[x][t];
	int res = dfs(gi, i+1, t)+dfs(gi, i, t-sum[x]);
/*	foreach (j; 0..(t+1)) {
		if (t-sum[x]*j < 0) break;
		res += dfs(gi, i+1, t-sum[x]*j);
		res %= MD;
	}*/
	//writeln(gi, " ", i, " ", t, " ", res);
	return dp1[x][t] = res;
}

long dfsg(int gi, int t) {
	if (gi == g.length) {
		if (!t) return 1;
		else return 0;
	}
	if (dp2[gi][t] != -1) return dp2[gi][t];
	int res = 0;
	foreach (i; 0..t+1) {
		res += cast(long)(dfs(gi, 0, i)) * dfsg(gi+1, t-i) % MD;
		res %= MD;
	}
	return dp2[gi][t] = res;
}

int main(string[] args) {
	int n, q, t;
	readf("%d %d %d\n", &n, &q, &t);
	sum = new int[n];
	a = readln().split().map!(to!int)().array;
	auto next = new int[n];
	auto back = new int[n];
	next[] = -1; back[] = -1;
	dp1 = new int[][](n, t+1);
	dp2 = new int[][](n, t+1);
	foreach (i; 0..n) {
		dp1[i][] = -1;
		dp2[i][] = -1;
	}
	foreach (i; 0..q) {
		int b, c;
		readf("%d %d\n", &b, &c); b--; c--;
		next[b] = c;
		back[c] = b;
	}
	auto count = 0;
	foreach (i; 0..n) {
		if (next[i] > -1) continue;
		int[] path = [i];
		int j = i;
		int c = 0;
		while (back[j] != -1) {
			j = back[j];
			c++;
			t -= c*a[j];
			path ~= j;
		}
		sum[j] = a[j];
		while (next[j] != -1) {
			j = next[j];
			sum[j] = sum[back[j]] + a[j];
		}
		g ~= path;
		count += path.length;
	}
	if (count < n) {
		writeln(0);
		return 0;
	}
	writeln(g);
	writeln(sum);
	writeln(dp1);
	writeln(t);
	writeln(dfsg(0, t));
	return 0;
}

import std.stdio, std.getopt, std.random, std.range, std.string, std.conv;
import std.algorithm;

const MD = 1_000_000_007;

int[][] g;
int[] a, sum;
int n;

int[][] dp1, dp2;

int dfs(int gi, int i, int t) {
	//writeln("start", " ", gi, " ", i, " ", t, " ");
	if (t < 0) return 0;
	if (i == g[gi].length) {
		if (!t) return 1;
		else return 0;
	}
	auto x = g[gi][i];
	if (dp1[x][t] != -1) return dp1[x][t];
	int res = dfs(gi, i+1, t)+dfs(gi, i, t-sum[x]);
/*	foreach (j; 0..(t+1)) {
		if (t-sum[x]*j < 0) break;
		res += dfs(gi, i+1, t-sum[x]*j);
		res %= MD;
	}*/
	//writeln(gi, " ", i, " ", t, " ", res);
	return dp1[x][t] = res;
}

long dfsg(int gi, int t) {
	if (gi == g.length) {
		if (!t) return 1;
		else return 0;
	}
	if (dp2[gi][t] != -1) return dp2[gi][t];
	int res = 0;
	foreach (i; 0..t+1) {
		res += cast(long)(dfs(gi, 0, i)) * dfsg(gi+1, t-i) % MD;
		res %= MD;
	}
	return dp2[gi][t] = res;
}

int main(string[] args) {
	int n, q, t;
	readf("%d %d %d\n", &n, &q, &t);
	sum = new int[n];
	a = readln().split().map!(to!int)().array;
	auto next = new int[n];
	auto back = new int[n];
	next[] = -1; back[] = -1;
	dp1 = new int[][](n, t+1);
	dp2 = new int[][](n, t+1);
	foreach (i; 0..n) {
		dp1[i][] = -1;
		dp2[i][] = -1;
	}
	foreach (i; 0..q) {
		int b, c;
		readf("%d %d\n", &b, &c); b--; c--;
		next[b] = c;
		back[c] = b;
	}
	auto count = 0;
	foreach (i; 0..n) {
		if (next[i] > -1) continue;
		int[] path = [i];
		int j = i;
		int c = 0;
		while (back[j] != -1) {
			j = back[j];
			c++;
			t -= c*a[j];
			path ~= j;
		}
		sum[j] = a[j];
		while (next[j] != -1) {
			j = next[j];
			sum[j] = sum[back[j]] + a[j];
		}
		g ~= path;
		count += path.length;
	}
	if (count < n) {
		writeln(0);
		return 0;
	}
//	writeln(g);
//	writeln(sum);
//	writeln(dp1);
//	writeln(t);
	writeln(dfsg(0, t));
	return 0;
}

import std.stdio, std.getopt, std.random, std.range, std.string, std.conv;
import std.algorithm;

const MD = 1_000_000_007;

int[] a, sum;
int n;

int[][] dp1, dp2;

int dfs(int x, int t) {
	//writeln("start", " ", gi, " ", i, " ", t, " ");
	if (t < 0) return 0;
	if (x == sum.length) {
		if (!t) return 1;
		else return 0;
	}
	if (dp1[x][t] != -1) return dp1[x][t];
	int res = dfs(x+1, t)+dfs(x, t-sum[x]);
	res %= MD;
/*	foreach (j; 0..(t+1)) {
		if (t-sum[x]*j < 0) break;
		res += dfs(gi, i+1, t-sum[x]*j);
		res %= MD;
	}*/
	//writeln(gi, " ", i, " ", t, " ", res);
	return dp1[x][t] = res;
}

int main(string[] args) {
	int n, q, t;
	readf("%d %d %d\n", &n, &q, &t);
	sum = new int[n];
	a = readln().split().map!(to!int)().array;
	auto next = new int[n];
	auto back = new int[n];
	next[] = -1; back[] = -1;
	dp1 = new int[][](n, t+1);
	dp2 = new int[][](n, t+1);
	foreach (i; 0..n) {
		dp1[i][] = -1;
		dp2[i][] = -1;
	}
	foreach (i; 0..q) {
		int b, c;
		readf("%d %d\n", &b, &c); b--; c--;
		next[b] = c;
		back[c] = b;
	}
	auto count = 0;
	foreach (i; 0..n) {
		if (next[i] > -1) continue;
		int j = i;
		int c = 0;
		count++;
		while (back[j] != -1) {
			j = back[j];
			c++;
			t -= c*a[j];
			count++;
		}
		sum[j] = a[j];
		while (next[j] != -1) {
			j = next[j];
			sum[j] = sum[back[j]] + a[j];
		}
	}
	writeln(count);
	if (count < n || t < 0) {
		writeln(0);
		return 0;
	}
//	writeln(g);
//	writeln(sum);
//	writeln(dp1);
//	writeln(t);
	writeln(dfs(0, t));
	return 0;
}


Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by $$$2$$$ but the number itself is not divisible by $$$2$$$. For example, $$$13$$$, $$$1227$$$, $$$185217$$$ are ebne numbers, while $$$12$$$, $$$2$$$, $$$177013$$$, $$$265918$$$ are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.You are given a non-negative integer $$$s$$$, consisting of $$$n$$$ digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between $$$0$$$ (do not delete any digits at all) and $$$n-1$$$.For example, if you are given $$$s=$$$222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 $$$\rightarrow$$$ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to $$$70$$$ and is divisible by $$$2$$$, but number itself is not divisible by $$$2$$$: it means that the resulting number is ebne.Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3000$$$)  — the number of digits in the original number. The second line of each test case contains a non-negative integer number $$$s$$$, consisting of $$$n$$$ digits. It is guaranteed that $$$s$$$ does not contain leading zeros and the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.

For each test case given in the input print the answer in the following format:  If it is impossible to create an ebne number, print "-1" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.

NoteIn the first test case of the example, $$$1227$$$ is already an ebne number (as $$$1 + 2 + 2 + 7 = 12$$$, $$$12$$$ is divisible by $$$2$$$, while in the same time, $$$1227$$$ is not divisible by $$$2$$$) so we don't need to delete any digits. Answers such as $$$127$$$ and $$$17$$$ will also be accepted.In the second test case of the example, it is clearly impossible to create an ebne number from the given number.In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, $$$1$$$ digit such as $$$17703$$$, $$$77013$$$ or $$$17013$$$. Answers such as $$$1701$$$ or $$$770$$$ will not be accepted as they are not ebne numbers. Answer $$$013$$$ will not be accepted as it contains leading zeroes.Explanation: $$$1 + 7 + 7 + 0 + 3 = 18$$$. As $$$18$$$ is divisible by $$$2$$$ while $$$17703$$$ is not divisible by $$$2$$$, we can see that $$$17703$$$ is an ebne number. Same with $$$77013$$$ and $$$17013$$$; $$$1 + 7 + 0 + 1 = 9$$$. Because $$$9$$$ is not divisible by $$$2$$$, $$$1701$$$ is not an ebne number; $$$7 + 7 + 0 = 14$$$. This time, $$$14$$$ is divisible by $$$2$$$ but $$$770$$$ is also divisible by $$$2$$$, therefore, $$$770$$$ is not an ebne number.In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 $$$\rightarrow$$$ 22237320442418521717191 (delete the last digit).

import std.algorithm : max, maxElement;
import std.container : Array;
import std.stdio : stdin, write, writeln;
import std.typecons : Tuple, tuple;

struct IO {
    string readToken() {
        import std.range : empty, front, popFront, split;
        import std.stdio : readln;

        while (tokens.empty) {
            tokens = readln.split;
        }
        auto token = tokens.front;
        tokens.popFront;
        return token;
    }

    int readInt() {
        import std.conv : to;

        return readToken.to!int;
    }

    long readLong() {
        import std.conv : to;

        return readToken.to!long;
    }

    string[] tokens;
}

void main() {
    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        string s = io.readToken;
        int i = 0;
        while (i < n && (s[i] - '0') % 2 == 0) {
            i++;
        }
        if (i == n) {
            writeln(-1);
        }
        else {
            int j = n - 1;
            while (j >= 0 && (s[j] - '0') % 2 == 0) {
                j--;
            }
            if (i == j) {
                writeln(-1);
            }
            else {
                writeln(s[i], s[j]);
            }
        }
    }
}

import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;
import std.math;
import std.typecons;

DList!string _words;

void read(T...)(ref T t)
{
  void singleRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  static foreach(i; 0 .. T.length)
    {
      static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))
        {
          foreach(ref element; t[i])
            read(element);
        }
      else
        {
          singleRead(t[i]);
        }
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value = E.init)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

void main()
{
  int t;
  read(t);
  foreach(tc; 0 .. t)
    {
      int n = next!int;
      string s = next!string;
      auto res = s.filter!(c => to!int(c) % 2 != 0).array.take(2);
      if (res.length == 2)
        {
          foreach(r; res)
            write(r);
          writeln;
        }
      else
        {
          writeln(-1);
        }
    }
}

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 = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto s = RD!string;
		long cnt;
		string tmp;
		foreach (c; s)
		{
			auto num = [c].to!int;
			if (num % 2 == 1)
			{
				++cnt;
				tmp ~= c;
				if (cnt == 2) break;
			}
		}
		if (cnt != 2)
			ans[ti] = -1;
		else
			ans[ti] = tmp.to!long;
	}

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


Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$$.Exactly $$$q$$$ queries were performed on the array. During the $$$i$$$-th query some segment $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ was selected and values of elements on positions from $$$l_i$$$ to $$$r_i$$$ inclusive got changed to $$$i$$$. The order of the queries couldn't be changed and all $$$q$$$ queries were applied. It is also known that every position from $$$1$$$ to $$$n$$$ got covered by at least one segment.We could have offered you the problem about checking if some given array (consisting of $$$n$$$ integers with values from $$$1$$$ to $$$q$$$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $$$0$$$.Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.If there are multiple possible arrays then print any of them.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the number of elements of the array and the number of queries perfomed on it. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le q$$$) — the resulting array. If element at some position $$$j$$$ is equal to $$$0$$$ then the value of element at this position can be any integer from $$$1$$$ to $$$q$$$.

Print "YES" if the array $$$a$$$ can be obtained by performing $$$q$$$ queries. Segments $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ are chosen separately for each query. Every position from $$$1$$$ to $$$n$$$ should be covered by at least one segment.  Otherwise print "NO". If some array can be obtained then print $$$n$$$ integers on the second line — the $$$i$$$-th number should be equal to the $$$i$$$-th element of the resulting array and should have value from $$$1$$$ to $$$q$$$. This array should be obtainable by performing exactly $$$q$$$ queries. If there are multiple possible arrays then print any of them.

NoteIn the first example you can also replace $$$0$$$ with $$$1$$$ but not with $$$3$$$.In the second example it doesn't really matter what segments to choose until query $$$10$$$ when the segment is $$$(1, 3)$$$.The third example showcases the fact that the order of queries can't be changed, you can't firstly set $$$(1, 3)$$$ to $$$6$$$ and after that change $$$(2, 2)$$$ to $$$5$$$. The segment of $$$5$$$ should be applied before segment of $$$6$$$.There is a lot of correct resulting arrays for the fourth example.

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, std.bitmanip;

immutable int INF = 1 << 29;

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

    if (A.map!(a => a == 0).all) {
        writeln("YES");
        fill(B, Q);
        B.map!(to!string).join(" ").writeln;
        return;
    }

    auto next = iota(1, N+1).array;
    auto prev = iota(-1, N-1).array;

    auto LR = new int[][](Q+1, 2);
    foreach (i; 0..Q+1) LR[i][0] = INF, LR[i][1] = -1;

    foreach (i; 0..N) {
        LR[A[i]][0] = min(LR[A[i]][0], i);
        LR[A[i]][1] = max(LR[A[i]][1], i);
    }

    foreach_reverse (i; 1..Q+1) {
        if (LR[i][0] == INF) continue;
        for (int j = LR[i][0]; j <= LR[i][1]; j = next[j]) {
            if (A[j] != 0 && A[j] < i) {
                writeln("NO");
                return;
            } else if (B[j] == 0) {
                B[j] = i;
            }
        }

        if (LR[i][0] > 0) next[LR[i][0]-1] = next[LR[i][1]];
        if (LR[i][1] < N-1) prev[LR[i][1]+1] = prev[LR[i][0]];
    }

    foreach (i; 0..N) if (B[i] == 0) B[i] = 1;

    if (LR[Q][0] == INF) {
        bool ok = false;
        foreach (i; 0..N) {
            if (A[i] == 0) {
                B[i] = Q;
                ok = true;
                break;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }

    writeln("YES");
    B.map!(to!string).join(" ").writeln;
}

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

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

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

    int fstzro = arr.canFind(0) ? n - arr.find(0).length.to!int : -1;
    foreach (i; 0 .. n) {
        if (arr[i] == 0 && i > 0 && arr[i-1] != 0) {
            arr[i] = arr[i-1];
        }
    }
    foreach_reverse (i; 0 .. n) {
        if (arr[i] == 0 && i < n-1 && arr[i+1] != 0) arr[i] = arr[i+1];
    }
    foreach (i; 0 .. n) {
        if (arr[i] == 0) arr[i] = q;
    }

    debug { arr.writeln; }

    if (!arr.canFind(q)) {
        if (fstzro != -1) {
            arr[fstzro] = q;
        } else {
            writeln("NO");
            return;
        }
    }

    immutable int MAX_IDX = q;
    int[] fw = new int[] (MAX_IDX+1);
    void add(int idx, int v) {
        while (idx <= MAX_IDX) {
            fw[idx] = v;
            idx += (idx & -idx);
        }
    }

    int get(int idx) {
        int mx = -1;
        while (idx > 0) {
            mx = max(mx, fw[idx]);
            idx -= (idx & -idx);
        }
        return mx;
    }

    int[] lst = new int[] (MAX_IDX+1);
    lst[] = -1;
    foreach (i, e; arr) {
        if (lst[e] != -1 && lst[e] < get(e-1)) {
            writeln("NO");
            return;
        }
        lst[e] = cast(int) i;
        add(e, cast(int) i);
    }

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

// same submission to check against added tests
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

immutable int logHalf = 18;
immutable int half = 1 << logHalf;
immutable int limit = half << 1;

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

		auto lo = new int [q + 1];
		lo[] = int.max;
		auto hi = new int [q + 1];
		hi[] = int.min;
		int pos0 = -1;
		foreach (i, c; a)
		{
			if (c != 0)
			{
				lo[c] = min (lo[c], i);
				hi[c] = max (hi[c], i);
			}
			else
			{
				pos0 = i;
			}
		}
		if (lo[q] == int.max)
		{
			if (pos0 == -1)
			{
				writeln ("NO");
				continue multitest_loop;
			}
			else
			{
				lo[q] = pos0;
				hi[q] = pos0;
			}
		}

		foreach (j; 1..q)
		{
			if (lo[j] == int.max)
			{
				lo[j] = lo[q];
				hi[j] = hi[q];
			}
		}
		lo[1] = 0;
		hi[1] = n - 1;

		auto t = new int [limit];

		void tFill (int u, int v, int w)
		{
			for (u += half, v += half; u < v; u >>= 1, v >>= 1)
			{
				if (u & 1)
				{
					t[u] = w;
					u += 1;
				}
				if (v & 1)
				{
					v -= 1;
					t[v] = w;
				}
			}
		}

		int tGet (int u)
		{
			int res = 0;
			for (u += half; u > 0; u >>= 1)
			{
				res = max (res, t[u]);
			}
			return res;
		}

		foreach (j; 1..q + 1)
		{
			tFill (lo[j], hi[j] + 1, j);
		}

		auto b = n.iota.map !(i => tGet (i)).array;
		debug {writeln (b);}
		foreach (i; 0..n)
		{
			if (a[i] != 0 && a[i] != b[i])
			{
				writeln ("NO");
				continue multitest_loop;
			}
		}

		writeln ("YES");
		writefln ("%(%s %)", b);
	}
}

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

struct seg
{
    int l = int.max;
    int r;
}

int[] t;
void build(int[] a, int v)
{
    if (a.length == 1) {
        t[v] = a[0];
        if (!t[v])
            t[v] = int.max;
    }
    else {
        int mid = cast(int)a.length >> 1;
        build(a[0 .. mid], v * 2 + 1);
        build(a[mid .. $], v * 2 + 2);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int ql, int qr, int l, int r)
{
    if (l == ql && r == qr)
        return t[v];
    int mid = (l + r) / 2;
    if (qr <= mid)
        return get(v * 2 + 1, ql, qr, l, mid);
    if (ql >= mid)
        return get(v * 2 + 2, ql, qr, mid, r);
    int t1 = get(v * 2 + 1, ql, mid, l, mid);
    int t2 = get(v * 2 + 2, mid, qr, mid, r);
    return min(t1, t2);
}

void main()
{
    int n, m;
    readf("%d %d\n", &n, &m);
    auto a = readln.splitter.map!(to!int).array;
    t = new int[n * 4];
    build(a, 0);
    auto b = new seg[m];
    foreach (int i, val; a) {
        if (!val)
            continue;
        --val;
        b[val].l = min(b[val].l, i);
        b[val].r = max(b[val].r, i);
    }
    if (!a.count(m)) {
        bool ok = false;
        foreach (ref val; a) {
            if (!val) {
                val = m;
                ok = true;
                break;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }
    foreach (i, ref sg; b) {
        if (sg.l != int.max
            && get(0, sg.l, sg.r + 1, 0, n) <= i) {
            writeln("NO");
            return;
        }
    }
    if (a.count(0) == a.length)
        a[] = m;
    else {
        int i;
        while (!a[i])
            ++i;
        a[0 .. i] = a[i];
        while (i < a.length) {
            if (a[i]) {
                ++i;
                continue;
            }
            int c = i;
            while (i < a.length && !a[i])
                ++i;
            a[c .. i] = a[c - 1];
        }
    }
    writefln("YES\n%(%d %)", a);
}

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.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)); }

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

      auto mn = new int[Q + 1];
      auto mx = new int[Q + 1];
      mn[] = N;
      mx[] = -1;
      foreach (i; 0 .. N) {
        chmin(mn[A[i]], i);
        chmax(mx[A[i]], i);
      }

      bool ans = false;
      auto as = A.dup;

      int inner;
      foreach (i; 0 .. N) {
        if (A[i] != 0) {
          inner = A[i];
          break;
        }
      }
      if (inner == 0) {
        inner = Q;
      }
      DList!int stack;
      foreach (i; 0 .. N) {
        if (A[i] == 0) {
          as[i] = inner;
        } else {
          if (mn[A[i]] == i) {
            if (!stack.empty && stack.back >= A[i]) {
              debug {
                writeln("incorrect order");
              }
              goto failed;
            }
            stack ~= A[i];
            inner = stack.back;
          }
          if (stack.empty || stack.back != A[i]) {
            debug {
              writeln("not top");
            }
            goto failed;
          }
          if (mx[A[i]] == i) {
            stack.removeBack;
            if (!stack.empty) {
              inner = stack.back;
            }
          }
        }
      }
      if (as.count(Q) == 0) {
        if (A.count(0) == 0) {
          debug {
            writeln("place for Q not found");
          }
          goto failed;
        } else {
          foreach (i; 0 .. N) {
            if (A[i] == 0) {
              as[i] = Q;
              break;
            }
          }
        }
      }
      ans = true;
     failed:

      if (ans) {
        writeln("YES");
        foreach (i; 0 .. N) {
          if (i > 0) write(" ");
          write(as[i]);
        }
        writeln();
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}

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, std.bitmanip;

immutable int INF = 1 << 29;

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

    if (A.map!(a => a == 0).all) {
        writeln("YES");
        fill(B, Q);
        B.map!(to!string).join(" ").writeln;
        return;
    }

    auto next = iota(1, N+1).array;
    auto prev = iota(-1, N-1).array;

    auto LR = new int[][](Q+1, 2);
    foreach (i; 0..Q+1) LR[i][0] = INF, LR[i][1] = -1;

    foreach (i; 0..N) {
        LR[A[i]][0] = min(LR[A[i]][0], i);
        LR[A[i]][1] = max(LR[A[i]][1], i);
    }

    foreach_reverse (i; 1..Q+1) {
        if (LR[i][0] == INF) continue;
        for (int j = LR[i][0]; j <= LR[i][1]; j = next[j]) {
            if (A[j] != 0 && A[j] < i) {
                writeln("NO");
                return;
            } else if (A[j] == 0) {
                B[j] = i;
            }
        }
        for (int j = prev[LR[i][0]]; j >= 0 && B[j] == 0; j = prev[j]) {
            B[j] = i;
            LR[i][0] = j;
        }
        for (int j = next[LR[i][1]]; j < N && B[j] == 0; j = next[j]) {
            B[j] = i;
            LR[i][1] = j;
        }

        if (LR[i][0] > 0) next[LR[i][0]-1] = next[LR[i][1]];
        if (LR[i][1] < N-1) prev[LR[i][1]+1] = prev[LR[i][0]];
    }

    if (LR[Q][0] == INF) {
        bool ok = false;
        foreach (i; 0..N) {
            if (A[i] == 0) {
                B[i] = Q;
                ok = true;
                break;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }

    writeln("YES");
    B.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, std.bitmanip;

immutable int INF = 1 << 29;

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

    if (A.map!(a => a == 0).all) {
        writeln("YES");
        fill(B, Q);
        B.map!(to!string).join(" ").writeln;
        return;
    }

    auto next = iota(1, N+1).array;
    auto prev = iota(-1, N-1).array;

    auto LR = new int[][](Q+1, 2);
    foreach (i; 0..Q+1) LR[i][0] = INF, LR[i][1] = -1;

    foreach (i; 0..N) {
        LR[A[i]][0] = min(LR[A[i]][0], i);
        LR[A[i]][1] = max(LR[A[i]][1], i);
    }

    foreach_reverse (i; 1..Q+1) {
        if (LR[i][0] == INF) continue;
        for (int j = LR[i][0]; j <= LR[i][1]; j = next[j]) {
            if (A[j] != 0 && A[j] < i) {
                writeln("NO");
                return;
            } else if (A[j] == 0) {
                B[j] = i;
            }
        }

        if (LR[i][0] > 0) next[LR[i][0]-1] = next[LR[i][1]];
        if (LR[i][1] < N-1) prev[LR[i][1]+1] = prev[LR[i][0]];
    }

    foreach (i; 0..N) if (B[i] == 0) B[i] = 1;

    if (LR[Q][0] == INF) {
        bool ok = false;
        foreach (i; 0..N) {
            if (A[i] == 0) {
                B[i] = Q;
                ok = true;
                break;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }

    writeln("YES");
    B.map!(to!string).join(" ").write;
}

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, std.bitmanip;

immutable int INF = 1 << 29;

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

    if (A.map!(a => a == 0).all) {
        writeln("YES");
        fill(B, Q);
        B.map!(to!string).join(" ").writeln;
        return;
    }

    auto next = iota(1, N+1).array;
    auto prev = iota(-1, N-1).array;

    auto LR = new int[][](Q+1, 2);
    foreach (i; 0..Q+1) LR[i][0] = INF, LR[i][1] = -1;

    foreach (i; 0..N) {
        LR[A[i]][0] = min(LR[A[i]][0], i);
        LR[A[i]][1] = max(LR[A[i]][1], i);
    }

    foreach_reverse (i; 1..Q+1) {
        if (LR[i][0] == INF) continue;
        for (int j = LR[i][0]; j <= LR[i][1]; j = next[j]) {
            if (A[j] != 0 && A[j] < i) {
                writeln("NO");
                return;
            } else if (A[j] == 0) {
                B[j] = i;
            }
        }

        if (LR[i][0] > 0) next[LR[i][0]-1] = next[LR[i][1]];
        if (LR[i][1] < N-1) prev[LR[i][1]+1] = prev[LR[i][0]];
    }

    foreach (i; 0..N) if (B[i] == 0) B[i] = 1;

    if (LR[Q][0] == INF) {
        bool ok = false;
        foreach (i; 0..N) {
            if (A[i] == 0) {
                B[i] = Q;
                ok = true;
                break;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }

    writeln("YES");
    B.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, std.bitmanip;

immutable int INF = 1 << 29;

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

    if (A.map!(a => a == 0).all) {
        writeln("YES");
        fill(B, Q);
        B.map!(to!string).join(" ").writeln;
        return;
    }

    auto next = iota(1, N+1).array;
    auto prev = iota(-1, N-1).array;

    auto LR = new int[][](Q+1, 2);
    foreach (i; 0..Q+1) LR[i][0] = INF, LR[i][1] = -1;

    foreach (i; 0..N) {
        LR[A[i]][0] = min(LR[A[i]][0], i);
        LR[A[i]][1] = max(LR[A[i]][1], i);
    }

    foreach_reverse (i; 1..Q+1) {
        if (LR[i][0] == INF) continue;
        for (int j = LR[i][0]; j <= LR[i][1]; j = next[j]) {
            if (A[j] != 0 && A[j] < i) {
                writeln("NO");
                return;
            } else if (A[j] == 0) {
                B[j] = i;
            }
        }
        for (int j = prev[LR[i][0]]; j >= 0 && B[j] == 0; j = prev[j]) {
            B[j] = i;
        }
        for (int j = next[LR[i][1]]; j < N && B[j] == 0; j = next[j]) {
            B[j] = i;
        }

        if (LR[i][0] > 0) next[LR[i][0]-1] = next[LR[i][1]];
        if (LR[i][1] < N-1) prev[LR[i][1]+1] = prev[LR[i][0]];
    }

    if (LR[Q][0] == INF) {
        bool ok = false;
        foreach (i; 0..N) {
            if (A[i] == 0) {
                B[i] = Q;
                ok = true;
                break;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }

    writeln("YES");
    B.map!(to!string).join(" ").writeln;
}

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

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

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

    foreach (i; 0 .. n) {
        if (arr[i] == 0 && i > 0 && arr[i-1] != 0) arr[i] = arr[i-1];
    }
    foreach_reverse (i; 0 .. n) {
        if (arr[i] == 0 && i < n-1 && arr[i+1] != 0) arr[i] = arr[i+1];
    }
    foreach (i; 0 .. n) {
        if (arr[i] == 0) arr[i] = q;
    }

    debug { arr.writeln; }

    if (!arr.canFind(q)) {
        writeln("NO");
        return;
    }

    immutable int MAX_IDX = q;
    int[] fw = new int[] (MAX_IDX+1);
    void add(int idx, int v) {
        while (idx <= MAX_IDX) {
            fw[idx] = v;
            idx += (idx & -idx);
        }
    }

    int get(int idx) {
        int mx = -1;
        while (idx > 0) {
            mx = max(mx, fw[idx]);
            idx -= (idx & -idx);
        }
        return mx;
    }

    int[] lst = new int[] (MAX_IDX+1);
    lst[] = -1;
    foreach (i, e; arr) {
        if (lst[e] != -1 && lst[e] < get(e-1)) {
            writeln(i, ' ', e, ' ', lst[e], ' ', get(e-1));
            writeln("NO");
            return;
        }
        lst[e] = cast(int) i;
        add(e, cast(int) i);
    }

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

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

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

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

    foreach (i; 0 .. n) {
        if (arr[i] == 0 && i > 0 && arr[i-1] != 0) arr[i] = arr[i-1];
    }
    foreach_reverse (i; 0 .. n) {
        if (arr[i] == 0 && i < n-1 && arr[i+1] != 0) arr[i] = arr[i+1];
    }
    foreach (i; 0 .. n) {
        if (arr[i] == 0) arr[i] = q;
    }

    debug { arr.writeln; }

    immutable int MAX_IDX = q;
    int[] fw = new int[] (MAX_IDX+1);
    void add(int idx, int v) {
        while (idx <= MAX_IDX) {
            fw[idx] = v;
            idx += (idx & -idx);
        }
    }

    int get(int idx) {
        int mx = -1;
        while (idx > 0) {
            mx = max(mx, fw[idx]);
            idx -= (idx & -idx);
        }
        return mx;
    }

    int[] lst = new int[] (MAX_IDX+1);
    lst[] = -1;
    foreach (i, e; arr) {
        if (lst[e] != -1 && lst[e] < get(e-1)) {
            writeln(i, ' ', e, ' ', lst[e], ' ', get(e-1));
            writeln("NO");
            return;
        }
        lst[e] = cast(int) i;
        add(e, cast(int) i);
    }

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

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

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

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

    foreach (i; 0 .. n) {
        if (arr[i] == 0 && i > 0 && arr[i-1] != 0) arr[i] = arr[i-1];
    }
    foreach_reverse (i; 0 .. n) {
        if (arr[i] == 0 && i < n-1 && arr[i+1] != 0) arr[i] = arr[i+1];
    }
    foreach (i; 0 .. n) {
        if (arr[i] == 0) arr[i] = q;
    }

    debug { arr.writeln; }

    if (!arr.canFind(q)) {
        writeln("NO");
        return;
    }

    immutable int MAX_IDX = q;
    int[] fw = new int[] (MAX_IDX+1);
    void add(int idx, int v) {
        while (idx <= MAX_IDX) {
            fw[idx] = v;
            idx += (idx & -idx);
        }
    }

    int get(int idx) {
        int mx = -1;
        while (idx > 0) {
            mx = max(mx, fw[idx]);
            idx -= (idx & -idx);
        }
        return mx;
    }

    int[] lst = new int[] (MAX_IDX+1);
    lst[] = -1;
    foreach (i, e; arr) {
        if (lst[e] != -1 && lst[e] < get(e-1)) {
            writeln("NO");
            return;
        }
        lst[e] = cast(int) i;
        add(e, cast(int) i);
    }

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

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

struct seg
{
    int l = int.max;
    int r;
}

int[] t;
void build(int[] a, int v)
{
    if (a.length == 1) {
        t[v] = a[0];
    }
    else {
        int mid = cast(int)a.length >> 1;
        build(a[0 .. mid], v * 2 + 1);
        build(a[mid .. $], v * 2 + 2);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int ql, int qr, int l, int r)
{
    if (l == ql && r == qr)
        return t[v];
    int mid = (l + r) / 2;
    if (qr <= mid)
        return get(v * 2 + 1, ql, qr, l, mid);
    if (ql >= mid)
        return get(v * 2 + 2, ql, qr, mid, r);
    int t1 = get(v * 2 + 1, ql, mid, l, mid);
    int t2 = get(v * 2 + 2, mid, qr, mid, r);
    return min(t1, t2);
}

void main()
{
    int n, m;
    readf("%d %d\n", &n, &m);
    auto a = readln.splitter.map!(to!int).array;
    t = new int[n * 4];
    build(a, 0);
    auto b = new seg[m];
    foreach (int i, val; a) {
        if (!val)
            continue;
        --val;
        b[val].l = min(b[val].l, i);
        b[val].r = max(b[val].r, i);
    }
    foreach (i, ref sg; b) {
        if (sg.l != int.max
            && get(0, sg.l, sg.r + 1, 0, n) <= i) {
            writeln("NO");
            return;
        }
    }
    if (a.count(0) == a.length)
        a[] = m;
    else {
        int i;
        while (!a[i])
            ++i;
        a[0 .. i] = a[i];
        while (i < a.length) {
            if (a[i]) {
                ++i;
                continue;
            }
            writeln(i);
            int c = i;
            while (i < a.length && !a[i])
                ++i;
            writeln(i);
            a[c .. i] = a[c - 1];
        }
    }
    writefln("YES\n%(%d %)", a);
}

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

struct seg
{
    int l = int.max;
    int r;
}

int[] t;
void build(int[] a, int v)
{
    if (a.length == 1) {
        t[v] = a[0];
        if (!t[v])
            t[v] = int.max;
    }
    else {
        int mid = cast(int)a.length >> 1;
        build(a[0 .. mid], v * 2 + 1);
        build(a[mid .. $], v * 2 + 2);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int ql, int qr, int l, int r)
{
    if (l == ql && r == qr)
        return t[v];
    int mid = (l + r) / 2;
    if (qr <= mid)
        return get(v * 2 + 1, ql, qr, l, mid);
    if (ql >= mid)
        return get(v * 2 + 2, ql, qr, mid, r);
    int t1 = get(v * 2 + 1, ql, mid, l, mid);
    int t2 = get(v * 2 + 2, mid, qr, mid, r);
    return min(t1, t2);
}

void main()
{
    int n, m;
    readf("%d %d\n", &n, &m);
    auto a = readln.splitter.map!(to!int).array;
    t = new int[n * 4];
    build(a, 0);
    auto b = new seg[m];
    foreach (int i, val; a) {
        if (!val)
            continue;
        --val;
        b[val].l = min(b[val].l, i);
        b[val].r = max(b[val].r, i);
    }
    if (!a.count(m)) {
        bool ok = false;
        foreach (ref val; a) {
            if (!val) {
                val = m;
                ok = true;
            }
        }
        if (!ok) {
            writeln("NO");
            return;
        }
    }
    foreach (i, ref sg; b) {
        if (sg.l != int.max
            && get(0, sg.l, sg.r + 1, 0, n) <= i) {
            writeln("NO");
            return;
        }
    }
    if (a.count(0) == a.length)
        a[] = m;
    else {
        int i;
        while (!a[i])
            ++i;
        a[0 .. i] = a[i];
        while (i < a.length) {
            if (a[i]) {
                ++i;
                continue;
            }
            int c = i;
            while (i < a.length && !a[i])
                ++i;
            a[c .. i] = a[c - 1];
        }
    }
    writefln("YES\n%(%d %)", a);
}

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

struct seg
{
    int l = int.max;
    int r;
}

int[] t;
void build(int[] a, int v)
{
    if (a.length == 1) {
        t[v] = a[0];
    }
    else {
        int mid = cast(int)a.length >> 1;
        build(a[0 .. mid], v * 2 + 1);
        build(a[mid .. $], v * 2 + 2);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int ql, int qr, int l, int r)
{
    if (l == ql && r == qr)
        return t[v];
    int mid = (l + r) / 2;
    if (qr <= mid)
        return get(v * 2 + 1, ql, qr, l, mid);
    if (ql >= mid)
        return get(v * 2 + 2, ql, qr, mid, r);
    int t1 = get(v * 2 + 1, ql, mid, l, mid);
    int t2 = get(v * 2 + 2, mid, qr, mid, r);
    return min(t1, t2);
}

void main()
{
    int n, m;
    readf("%d %d\n", &n, &m);
    auto a = readln.splitter.map!(to!int).array;
    t = new int[n * 4];
    build(a, 0);
    auto b = new seg[m];
    foreach (int i, val; a) {
        if (!val)
            continue;
        --val;
        b[val].l = min(b[val].l, i);
        b[val].r = max(b[val].r, i);
    }
    foreach (i, ref sg; b) {
        if (sg.l != int.max
            && get(0, sg.l, sg.r + 1, 0, n) <= i) {
            writeln("NO");
            return;
        }
    }
    if (a.count(0) == a.length)
        a[] = m;
    else {
        int i;
        while (!a[i])
            ++i;
        a[0 .. i] = a[i];
        while (i < a.length) {
            if (a[i]) {
                ++i;
                continue;
            }
            int c = i;
            while (i < a.length && !a[i])
                ++i;
            a[c .. i] = a[c - 1];
        }
    }
    writefln("YES\n%(%d %)", a);
}

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

struct seg
{
    int l = int.max;
    int r;
}

int[] t;
void build(int[] a, int v)
{
    if (a.length == 1) {
        t[v] = a[0];
        if (!t[v])
            t[v] = int.max;
    }
    else {
        int mid = cast(int)a.length >> 1;
        build(a[0 .. mid], v * 2 + 1);
        build(a[mid .. $], v * 2 + 2);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int ql, int qr, int l, int r)
{
    if (l == ql && r == qr)
        return t[v];
    int mid = (l + r) / 2;
    if (qr <= mid)
        return get(v * 2 + 1, ql, qr, l, mid);
    if (ql >= mid)
        return get(v * 2 + 2, ql, qr, mid, r);
    int t1 = get(v * 2 + 1, ql, mid, l, mid);
    int t2 = get(v * 2 + 2, mid, qr, mid, r);
    return min(t1, t2);
}

void main()
{
    int n, m;
    readf("%d %d\n", &n, &m);
    auto a = readln.splitter.map!(to!int).array;
    t = new int[n * 4];
    build(a, 0);
    auto b = new seg[m];
    foreach (int i, val; a) {
        if (!val)
            continue;
        --val;
        b[val].l = min(b[val].l, i);
        b[val].r = max(b[val].r, i);
    }
    if (!a.count(0) && !a.count(m)) {
        writeln("NO");
        return;
    }
    foreach (i, ref sg; b) {
        if (sg.l != int.max
            && get(0, sg.l, sg.r + 1, 0, n) <= i) {
            writeln("NO");
            return;
        }
    }
    if (a.count(0) == a.length)
        a[] = m;
    else {
        int i;
        while (!a[i])
            ++i;
        a[0 .. i] = a[i];
        while (i < a.length) {
            if (a[i]) {
                ++i;
                continue;
            }
            int c = i;
            while (i < a.length && !a[i])
                ++i;
            a[c .. i] = a[c - 1];
        }
    }
    writefln("YES\n%(%d %)", a);
}

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

struct seg
{
    int l = int.max;
    int r;
}

int[] t;
void build(int[] a, int v)
{
    if (a.length == 1) {
        t[v] = a[0];
    }
    else {
        int mid = cast(int)a.length >> 1;
        build(a[0 .. mid], v * 2 + 1);
        build(a[mid .. $], v * 2 + 2);
        t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int ql, int qr, int l, int r)
{
    if (l == ql && r == qr)
        return t[v];
    int mid = (l + r) / 2;
    if (qr <= mid)
        return get(v * 2 + 1, ql, qr, l, mid);
    if (ql >= mid)
        return get(v * 2 + 2, ql, qr, mid, r);
    int t1 = get(v * 2 + 1, ql, mid, l, mid);
    int t2 = get(v * 2 + 2, mid, qr, mid, r);
    return min(t1, t2);
}

void main()
{
    int n, m;
    readf("%d %d\n", &n, &m);
    auto a = readln.splitter.map!(to!int).array;
    t = new int[n * 4];
    build(a, 0);
    auto b = new seg[m];
    foreach (int i, val; a) {
        if (!val)
            continue;
        --val;
        b[val].l = min(b[val].l, i);
        b[val].r = max(b[val].r, i);
    }
    foreach (i, ref sg; b) {
        if (sg.l != int.max
            && get(0, sg.l, sg.r + 1, 0, n) <= i) {
            writeln("NO");
            return;
        }
    }
    if (a.count(0) == a.length)
        a[] = m;
    else {
        int i;
        while (!a[i])
            ++i;
        a[0 .. i] = a[i];
        while (i < a.length) {
            if (a[i]) {
                ++i;
                continue;
            }
            writeln(i);
            int c = i;
            while (i < a.length && !a[i])
                ++i;
            writeln(i);
            a[c .. i] = a[c - 1];
        }
    }
    writefln("%(%d %)", a);
}

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.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)); }

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

      auto mn = new int[Q + 1];
      auto mx = new int[Q + 1];
      mn[] = N;
      mx[] = -1;
      foreach (i; 0 .. N) {
        chmin(mn[A[i]], i);
        chmax(mx[A[i]], i);
      }

      auto diss = new int[][N];
      auto fins = new int[][N];
      foreach (j; 1 .. Q + 1) {
        if (mn[j] <= mx[j]) {
          diss[mn[j]] ~= j;
          fins[mx[j]] ~= j;
        }
      }
      foreach (i; 0 .. N) {
        diss[i].sort!((j0, j1) => (j0 < j1));
        fins[i].sort!((j0, j1) => (j0 > j1));
      }
      debug {
        writeln("diss = ", diss);
        writeln("fins = ", fins);
      }

      bool ans = false;
      auto as = A.dup;

      int outer;
      foreach (i; 0 .. N) {
        if (diss[i]) {
          outer = diss[i][0];
          break;
        }
      }
      if (outer == 0) {
        outer = Q;
      }
      DList!int stack;
      foreach (i; 0 .. N) {
        if (A[i] == 0) {
          as[i] = outer;
        } else {
          foreach (j; diss[i]) {
            if (!stack.empty && stack.back >= j) {
              goto failed;
            }
            if (stack.empty) {
              outer = j;
            }
            stack ~= j;
          }
          if (stack.empty || stack.back != A[i]) {
            goto failed;
          }
          foreach (j; fins[i]) {
            if (stack.empty || stack.back != j) {
              goto failed;
            }
            stack.removeBack;
          }
        }
      }
      if (as.count(Q) == 0) {
        if (A.count(0) == 0) {
          goto failed;
        } else {
          foreach (i; 0 .. N) {
            if (A[i] == 0) {
              as[i] = Q;
              break;
            }
          }
        }
      }
      ans = true;
     failed:
      if (ans) {
        writeln("YES");
        foreach (i; 0 .. N) {
          if (i > 0) write(" ");
          write(as[i]);
        }
        writeln();
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}

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.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)); }

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

      auto mn = new int[Q + 1];
      auto mx = new int[Q + 1];
      mn[] = N;
      mx[] = -1;
      foreach (i; 0 .. N) {
        chmin(mn[A[i]], i);
        chmax(mx[A[i]], i);
      }

      auto diss = new int[][N];
      auto fins = new int[][N];
      foreach (j; 1 .. Q + 1) {
        if (mn[j] <= mx[j]) {
          diss[mn[j]] ~= j;
          fins[mx[j]] ~= j;
        }
      }
      foreach (i; 0 .. N) {
        diss[i].sort!((j0, j1) => (j0 < j1));
        fins[i].sort!((j0, j1) => (j0 > j1));
      }
      debug {
        writeln("diss = ", diss);
        writeln("fins = ", fins);
      }

      bool ans = false;
      auto as = A.dup;

      int outer;
      foreach (i; 0 .. N) {
        if (diss[i]) {
          outer = diss[i][0];
          break;
        }
      }
      if (outer == 0) {
        outer = Q;
      }
      DList!int stack;
      foreach (i; 0 .. N) {
        if (A[i] == 0) {
          as[i] = outer;
        } else {
          foreach (j; diss[i]) {
            if (!stack.empty && stack.back >= j) {
              goto failed;
            }
            if (stack.empty) {
              outer = j;
            }
            stack ~= j;
          }
          foreach (j; fins[i]) {
            if (stack.empty || stack.back != j) {
              goto failed;
            }
            stack.removeBack;
          }
        }
      }
      if (as.count(Q) == 0) {
        if (A.count(0) == 0) {
          goto failed;
        } else {
          foreach (i; 0 .. N) {
            if (A[i] == 0) {
              as[i] = Q;
              break;
            }
          }
        }
      }
      ans = true;
     failed:
      if (ans) {
        writeln("YES");
        foreach (i; 0 .. N) {
          if (i > 0) write(" ");
          write(as[i]);
        }
        writeln();
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}

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.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)); }

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

      auto mn = new int[Q + 1];
      auto mx = new int[Q + 1];
      mn[] = N;
      mx[] = -1;
      foreach (i; 0 .. N) {
        chmin(mn[A[i]], i);
        chmax(mx[A[i]], i);
      }

      auto diss = new int[][N];
      auto fins = new int[][N];
      foreach (j; 1 .. Q + 1) {
        if (mn[j] <= mx[j]) {
          diss[mn[j]] ~= j;
          fins[mx[j]] ~= j;
        }
      }
      foreach (i; 0 .. N) {
        diss[i].sort!((j0, j1) => (j0 < j1));
        fins[i].sort!((j0, j1) => (j0 > j1));
      }
      debug {
        writeln("diss = ", diss);
        writeln("fins = ", fins);
      }

      int outer;
      foreach (i; 0 .. N) {
        if (diss[i]) {
          outer = diss[i][0];
          break;
        }
      }
      if (outer == 0) {
        outer = 1;
      }
      auto as = A.dup;
      DList!int stack;
      bool ans = false;
      foreach (i; 0 .. N) {
        if (A[i] == 0) {
          as[i] = outer;
        } else {
          foreach (j; diss[i]) {
            if (!stack.empty && stack.back >= j) {
              goto failed;
            }
            if (stack.empty) {
              outer = j;
            }
            stack ~= j;
          }
          foreach (j; fins[i]) {
            if (stack.empty || stack.back != j) {
              goto failed;
            }
            stack.removeBack;
          }
        }
      }
      ans = true;
     failed:
      if (ans) {
        writeln("YES");
        foreach (i; 0 .. N) {
          if (i > 0) write(" ");
          write(as[i]);
        }
        writeln();
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}

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.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)); }

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

      auto mn = new int[Q + 1];
      auto mx = new int[Q + 1];
      mn[] = N;
      mx[] = -1;
      foreach (i; 0 .. N) {
        chmin(mn[A[i]], i);
        chmax(mx[A[i]], i);
      }

      bool ans = false;
      auto as = A.dup;

      int outer;
      foreach (i; 0 .. N) {
        if (A[i] != 0) {
          outer = A[i];
          break;
        }
      }
      if (outer == 0) {
        outer = Q;
      }
      DList!int stack;
      foreach (i; 0 .. N) {
        if (A[i] == 0) {
          as[i] = outer;
        } else {
          if (mn[A[i]] == i) {
            if (!stack.empty && stack.back >= A[i]) {
              debug {
                writeln("incorrect order");
              }
              goto failed;
            }
            if (stack.empty) {
              outer = A[i];
            }
            stack ~= A[i];
          }
          if (stack.empty || stack.back != A[i]) {
            debug {
              writeln("not top");
            }
            goto failed;
          }
          if (mx[A[i]] == i) {
            stack.removeBack;
          }
        }
      }
      if (as.count(Q) == 0) {
        if (A.count(0) == 0) {
          debug {
            writeln("place for Q not found");
          }
          goto failed;
        } else {
          foreach (i; 0 .. N) {
            if (A[i] == 0) {
              as[i] = Q;
              break;
            }
          }
        }
      }
      ans = true;
     failed:

      if (ans) {
        writeln("YES");
        foreach (i; 0 .. N) {
          if (i > 0) write(" ");
          write(as[i]);
        }
        writeln();
      } else {
        writeln("NO");
      }
    }
  } catch (EOFException e) {
  }
}