You are given undirected weighted graph. Find the length of the shortest cycle which starts from the vertex 1 and passes throught all the edges at least once. Graph may contain multiply edges between a pair of vertices and loops (edges from the vertex to itself).

The first line of the input contains two integers n and m (1 ≤ n ≤ 15, 0 ≤ m ≤ 2000), n is the amount of vertices, and m is the amount of edges. Following m lines contain edges as a triples x, y, w (1 ≤ x, y ≤ n, 1 ≤ w ≤ 10000), x, y are edge endpoints, and w is the edge length.

Output minimal cycle length or -1 if it doesn't exists.

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

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

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

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

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

int root(int[] uf, int u) {
	return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool conn(int[] uf, int u, int v) {
	u = uf.root(u);
	v = uf.root(v);
	if (u == v) return 0;
	if (uf[u] > uf[v]) swap(u, v);
	uf[u] += uf[v];
	uf[v] = u;
	return 1;
}

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

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

		int[] uf = new int[N];
		uf[] = -1;
		foreach (i; 0 .. M) {
			uf.conn(A[i], B[i]);
		}
		bool ok = true;
		foreach (i; 0 .. M) {
			if (uf.root(0) != uf.root(A[i])) {
				ok = false;
				break;
			}
		}
		if (!ok) {
			writeln(-1);
			continue;
		}

		int[] deg = new int[N];
		foreach (i; 0 .. M) {
			++deg[A[i]];
			++deg[B[i]];
		}

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int start;
		foreach (u; 0 .. N) {
			if (deg[u] % 2 == 0) {
				start |= 1 << u;
			}
		}

		int[] dp = new int[1 << N];
		dp[] = INF;
		dp[start] = 0;
		foreach (p; 0 .. 1 << N) {
			foreach (u; 0 .. N) foreach (v; 0 .. N) if (u != v && !(p & 1 << u) && !(p & 1 << v)) {
				chmin(dp[p | 1 << u | 1 << v], dp[p] + D[u][v]);
			}
		}

		const ans = reduce!"a + b"(0, C) + dp[(1 << N) - 1];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		A = new int[M];
		B = new int[M];
		C = new int[M];
		foreach (i; 0 .. M) {
			A[i] = readInt - 1;
			B[i] = readInt - 1;
			C[i] = readInt;
		}
if(N==1)for(;;){}

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

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

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
break;
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		A = new int[M];
		B = new int[M];
		C = new int[M];
		foreach (i; 0 .. M) {
			A[i] = readInt - 1;
			B[i] = readInt - 1;
			C[i] = readInt;
		}
if(!(1<=N&&N<=15))for(;;){}

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

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

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		A = new int[M];
		B = new int[M];
		C = new int[M];
		foreach (i; 0 .. M) {
			A[i] = readInt - 1;
			B[i] = readInt - 1;
			C[i] = readInt;
		}
if(N==2)for(;;){}

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

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

		int[] deg = new int[N];
		foreach (i; 0 .. M) {
			++deg[A[i]];
			++deg[B[i]];
		}

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int start;
		foreach (u; 0 .. N) {
			if (deg[u] % 2 == 0) {
				start |= 1 << u;
			}
		}

		int[] dp = new int[1 << N];
		dp[] = INF;
		dp[start] = 0;
		foreach (p; 0 .. 1 << N) {
			foreach (u; 0 .. N) foreach (v; 0 .. N) if (u != v && !(p & 1 << u) && !(p & 1 << v)) {
				chmin(dp[p | 1 << u | 1 << v], dp[p] + D[u][v]);
			}
		}

		const ans = reduce!"a + b"(0, C) + dp[(1 << N) - 1];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

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

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

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

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

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

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

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

immutable INF = 1_000_000_000;

int N, M;
int[] A, B, C;

int[][] D;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		A = new int[M];
		B = new int[M];
		C = new int[M];
		foreach (i; 0 .. M) {
			A[i] = readInt - 1;
			B[i] = readInt - 1;
			C[i] = readInt;
		}
if(N==3)for(;;){}

		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = INF;
			// D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}

		int[][] dp = new int[][](1 << N, N);
		foreach (p; 0 .. 1 << N) {
			dp[p][] = INF;
		}
		dp[0][0] = 0;
		foreach (p; 0 .. 1 << N) foreach (u; 0 .. N) {
			foreach (v; 0 .. N) if (!(p & 1 << v)) {
				chmin(dp[p | 1 << v][v], dp[p][u] + D[u][v]);
			}
		}

		const ans = dp[(1 << N) - 1][0];
		if (ans >= INF) {
			writeln(-1);
		} else {
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}


Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation. For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:  He looks through all the coins from left to right;  If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i + 1)-th.  Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence. The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task.

The first line contains single integer n (1 ≤ n ≤ 300 000) — number of coins that Sasha puts behind Dima. Second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.

Print n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on.

NoteLet's denote as O coin out of circulation, and as X — coin is circulation.At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won't make any exchanges.After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.XOOO  →  OOOXAfter replacement of the third coin, Dima's actions look this way:XOXO  →  OXOX  →  OOXXAfter replacement of the fourth coin, Dima's actions look this way:XOXX  →  OXXXFinally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges.

/+ dub.sdl:
    name "B"
    dependency "dcomp" version=">=0.7.3"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    int[] a;
    sc.read(a); a[]-=1;
    bool[] used = new bool[n];
    int r = n-1;
    writeln(1);
    foreach (int i, d; a) {
        used[d] = true;
        while (r >= 0 && used[r]) r--;
        int s = 1 + (i+1) - (n-1-r);
        writeln(s);
    }
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
version (X86) static if (__VERSION__ < 2071) {
    import core.bitop : bsf, bsr, popcnt;
    int bsf(ulong v) {
        foreach (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int bsr(ulong v) {
        foreach_reverse (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int popcnt(ulong v) {
        int c = 0;
        foreach (i; 0..64) {
            if (v & (1UL << i)) c++;
        }
        return c;
    }
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.array;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                FastAppender!(E[]) buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;

T[N] fixed(T, size_t N)(T[N] a) {return a;}

struct FastAppender(A, size_t MIN = 4) {
    import std.algorithm : max;
    import std.conv;
    import std.range.primitives : ElementEncodingType;
    import core.stdc.string : memcpy;

    private alias T = ElementEncodingType!A;
    private T* _data;
    private uint len, cap;

    @property size_t length() const {return len;}
    bool empty() const { return len == 0; }

    void reserve(size_t nlen) {
        import core.memory : GC;
        if (nlen <= cap) return;

        void* nx = GC.malloc(nlen * T.sizeof);

        cap = nlen.to!uint;
        if (len) memcpy(nx, _data, len * T.sizeof);
        _data = cast(T*)(nx);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void opOpAssign(string op : "~")(T item) {
        if (len == cap) {
            reserve(max(MIN, cap*2));
        }
        _data[len++] = item;
    }

    void insertBack(T item) {
        this ~= item;
    }

    void removeBack() {
        len--;
    }

    void clear() {
        len = 0;
    }
    ref inout(T) back() inout { assert(len); return _data[len-1]; }
    ref inout(T) opIndex(size_t i) inout { return _data[i]; }

    T[] data() {
        return (_data) ? _data[0..len] : null;
    }
}

/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/

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

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

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

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

void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}

// T, S: monoid
// opTT: T * T -> T
// opST: S * T * int -> T
//   (s t_a) ... (s t_{b-1}) = opST(s, t_a ... t_{b-1}, b - a)
// opSS: S * S -> S
// query(a, b, s): t_a <- s t_a, ..., t_{b-1} <- s t_{b-1};
//   returns t_a ... t_{b-1}
class SegmentTree(T, S, alias opTT, alias opST, alias opSS) {
  import std.functional : binaryFun;
  alias opTTFun = binaryFun!opTT;
  alias opSTFun = opST;
  alias opSSFun = binaryFun!opSS;
  const(T) idT;
  const(S) idS;

  int n;
  T[] ts;
  S[] ss;
  this(int n_, const(T) idT, const(S) idS) {
    this.idT = idT;
    this.idS = idS;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ss = new S[n << 1];
    ts[] = idT;
    ss[] = idS;
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opTTFun(ts[a << 1], ts[a << 1 | 1]);
  }
  T query(int a, int b, const(S) s) {
    return query(1, 0, n, a, b, s);
  }

 private:
  T query(int u, int l, int r, int a, int b, const(S) s) {
    if (a < l) a = l;
    if (b > r) b = r;
    if (a >= b) return idT;
    if (a == l && b == r) {
      ts[u] = opSTFun(s, ts[u], r - l);
      ss[u] = opSSFun(s, ss[u]);
      return ts[u];
    }
    const int uL = u << 1, uR = u << 1 | 1;
    const int mid = (l + r) >> 1;
    // speed-up: if (ss[u] != idS)
    {
      ts[uL] = opSTFun(ss[u], ts[uL], mid - l);
      ts[uR] = opSTFun(ss[u], ts[uR], r - mid);
      ss[uL] = opSSFun(ss[u], ss[uL]);
      ss[uR] = opSSFun(ss[u], ss[uR]);
      ss[u] = idS;
    }
    const T resL = query(uL, l, mid, a, b, s);
    const T resR = query(uR, mid, r, a, b, s);
    ts[u] = opTTFun(ts[uL], ts[uR]);
    return opTTFun(resL, resR);
  }
}

enum INF = 10^^9;

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

      auto bit = new int[N];
      auto seg = new SegmentTree!(int, int, max, (s, t, sz) => (s + t), "a + b")(N, -INF, 0);
      foreach (i; 0 .. N) {
        seg.at(i) = 0;
      }
      seg.build;

      auto ans = new int[N];
      foreach_reverse (i; 0 .. N) {
        const j = bit.bSum(P[i]);
        bit.bAdd(P[i], +1);
        const now = seg.query(P[i], P[i] + 1, 0);
        debug {
          writeln(i, " ", P[i], " ", j, " ", now);
        }
        seg.query(P[i], P[i] + 1, (P[i] - j) - now);
        seg.query(P[i] + 1, N, -1);
        ans[i] = max(seg.query(0, N, 0), 0);
      }

      foreach (i; 0 .. N) {
        write(ans[i] + 1, " ");
      }
      writeln(1);
    }
  } catch (EOFException e) {
  }
}


Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.What is the maximum number of apples he can collect?

The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land. The following n lines contains two integers each xi, ai ( - 105 ≤ xi ≤ 105, xi ≠ 0, 1 ≤ ai ≤ 105), representing the position of the i-th tree and number of apples on it. It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.

Output the maximum number of apples Amr can collect.

NoteIn the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.In the second sample test the optimal solution is to go left to x =  - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x =  - 2.In the third sample test the optimal solution is to go right to x = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.typecons;

void main() {
  alias pii = Tuple!(int, "k", int, "v");
  pii[] a1, a2;
  int n = readln().strip().to!int;
  foreach (i; 0..n) {
    auto input = readln().split().map!(to!int).array;
    int k = input[0], v = input[1];
    if (k < 0) {
      a1 ~= pii(-k, v);
    } else {
      a2 ~= pii(k, v);
    }
  }
  a1.sort();
  a2.sort();
  int m = cast(int)min(a1.length, a2.length);
  auto b1 = a1.map!(t => t.v).array;
  auto b2 = a2.map!(t => t.v).array;
  b1 ~= 0;
  b2 ~= 0;
  writeln(sum(b1[0..m+1]) + sum(b2[0..m+1]));
}

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

struct Tree {
    int pos, apples;
}

void main() {
    int n;
    readf(" %d", &n);
    auto trees = new Tree[n];
    foreach (ref tree; trees)
        readf(" %d %d", &tree.pos, &tree.apples);
    trees.sort!"a.pos < b.pos"();
    int mid = trees.length - trees.find!"a.pos > b"(0).length;
    int result = 0;
    int i, j;
    for (i = mid - 1, j = mid; i >= 0 && j < n; i--, j++)
        result += trees[i].apples + trees[j].apples;
    if (i >= 0)
        result += trees[i].apples;
    if (j < n)
        result += trees[j].apples;
    writeln(result);
}


You are given a string $$$s$$$ of lowercase Latin letters. The following operation can be used:   select one character (from 'a' to 'z') that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all 'c' with 'b' or replace all 'a' with 'z'. And you are given the integer $$$k$$$ — the maximum number of operations that can be performed. Find the minimum lexicographically possible string that can be obtained by performing no more than $$$k$$$ operations.The string $$$a=a_1a_2 \dots a_n$$$ is lexicographically smaller than the string $$$b = b_1b_2 \dots b_n$$$ if there exists an index $$$k$$$ ($$$1 \le k \le n$$$) such that $$$a_1=b_1$$$, $$$a_2=b_2$$$, ..., $$$a_{k-1}=b_{k-1}$$$, but $$$a_k < b_k$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases in the test. This is followed by descriptions of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^9$$$) — the size of the string $$$s$$$ and the maximum number of operations that can be performed on the string $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.  It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output the lexicographically minimal string that can be obtained from the string $$$s$$$ by performing no more than $$$k$$$ operations.

4

3 2

cba

4 5

fgde

7 5

gndcafb

4 19

ekyv

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] > k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);

    for(int i = 0; i < n; ++i){
        if(w[i] <= max2){
            write('a');
        }else if(w[i] <= cr || cr == -1){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

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

immutable int letters = 26;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto s = readln.strip.map !(q{a - 'a'}).array;
		int hi = 0;
		auto trans = letters.iota.array;
		foreach (int i, c; s)
		{
			while (k > 0 && trans[c] > 0)
			{
				int prev = trans[c];
				foreach (j; 0..letters)
				{
					if (trans[j] == prev)
					{
						trans[j] = trans[prev - 1];
					}
				}
				k -= 1;
			}
		}
		writeln (s.map !(c => cast (char) (trans[c] + 'a')));
	}
}

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] vis = new int[](26);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] >= k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);
    int minchar = 'a';

    for(int i = 0; i < n; ++i){
        if(w[i] <= max2){
            write(min(minchar, w[i] + 'a').to!dchar);
        }else if(w[i] <= cr){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] vis = new int[](26);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] >= k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);
    int minchar = 'a';
    if(imax == 0){ minchar = ('a' + low); }

    for(int i = 0; i < n; ++i){
        if(w[i] <= max2){
            write(min(minchar, w[i] + 'a').to!dchar);
        }else if(w[i] <= cr){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

// cheese-cracker [2022-05-05]

void solve(){
    int n = scan!int;
    int k = scan!int;
    auto word = scan!(dchar[]);
    int[] vis = new int[](26);
    int[] w;
    for(int i = 0; i < n; ++i){
        w ~= (word[i] - 'a').to!int;
    }
    int cr = -1;
    int max2 = 0;
    int imax = n;
    for(int i = 0; i < n; ++i){
        if(w[i] >= k){
            cr = w[i];
            imax = i;
            break;
        }
        max2 = max(max2, w[i]);
    }
    int kleft = k - max2;
    int low = max(cr - kleft, 0);
    int minchar = 'a';
    if(imax == 0){ minchar = ('a' + low); }

    for(int i = 0; i < n; ++i){
        if(w[i] <= k){
            write(min(minchar, w[i] + 'a').to!dchar);
        }else if(w[i] <= cr){
            write(min('a' + low, w[i] + 'a').to!dchar);
        }else{
            write((w[i] + 'a').to!dchar);
        }
    }
    writeln;
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }


Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $$$n \cdot m$$$ colored pieces that form a rectangle with $$$n$$$ rows and $$$m$$$ columns. The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different.   These subrectangles are flags.       These subrectangles are not flags.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1\,000$$$) — the number of rows and the number of columns on the blanket. Each of the next $$$n$$$ lines contains $$$m$$$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.

In the only line print the number of subrectangles which form valid flags.

Note  The selected subrectangles are flags in the first example.

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

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

    string[] arr;
    foreach (i; 0 .. n) {
        arr ~= readln.chomp;
    }

    auto byCol = new int[][] (n, m);
    foreach (rw; 0 .. n) {
        byCol[rw][m-1] = 1;
        foreach_reverse (c; 0 .. m-1) {
            byCol[rw][c] = arr[rw][c] == arr[rw][c+1] ?
                byCol[rw][c+1] + 1 : 1;
        }
    }

    auto byRow = new int[][] (n, m);
    foreach (c; 0 .. m) {
        byRow[n-1][c] = 1;
        foreach_reverse (rw; 0 .. n-1) {
            byRow[rw][c] = arr[rw][c] == arr[rw+1][c] ?
                byRow[rw+1][c] + 1 : 1;
        }
    }

    debug {
        byRow.each!writeln;
        byCol.each!writeln;
    }

    long ans = 0;
    foreach (rw; 0 .. n) {
        foreach (c; 0 .. m) {
            int reach1 = byRow[rw][c];

            if (rw + reach1 >= n) { continue; }

            int reach2 = byRow[rw + reach1][c];

            if (reach1 != reach2 || rw + reach1 + reach2 >= n) { continue; }

            int reach3 = min(reach1, byRow[rw + reach1 + reach2][c]);

            if (reach3 < reach1) { continue; }

            int rt = m;
            foreach (rwnxt; rw .. rw + reach1 + reach2 + reach3) {
                rt = min(rt, byCol[rwnxt][c]);
            }

            ans += rt;
        }
    }

    ans.writeln;
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math;
import std.bigint;

alias Tree = RBT!int;
alias ll = long;

void main() {
  GC.disable();

  int n, m;
  readf(" %s %s\n", n, m);
  string[] A = new string[n];
  foreach(i; 0 .. n) {
    A[i] = readln.strip;
  }

  int[] ccl = new int[n];
  ccl[] =0;
  long ans = 0;

  foreach(j;0 .. m) {
    int[] co; //= new int[];
    char[] ch;// = new char[];
    int[] ml;// = new int[];
    co ~= 0;
    ch ~= A[0][j];
    ml ~= (n+m+100);

    foreach(i; 0 .. n) {
      if (j >= 1 && A[i][j] == A[i][j-1]) ccl[i]++; else ccl[i] = 1;

      if (A[i][j] == ch[$-1]) {
        co[$-1]++;
        ml[$-1] = min(ml[$-1], ccl[i]);
      } else {
        co ~= 1;
        ch ~= A[i][j];
        ml ~= ccl[i];
      }

      if (co.length >= 3) {
        if (co[$-1] == co[$-2] && co[$-1] <= co[$-3]) {
          long t= min(ml[$-1], ml[$-2]);
          int sss = co[$-1];
          t = min(t, minElement( ccl[ i - sss* 3 + 1 .. i - sss*2 + 1 ] ) );
          ans += t;
        }
      }
    }
  }

  writeln(ans);

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math;
import std.bigint;

alias Tree = RBT!int;
alias ll = long;

void main() {
  GC.disable();

  int n, m;
  readf(" %s %s\n", n, m);
  string[] A = new string[n];
  foreach(i; 0 .. n) {
    A[i] = readln.strip;
  }

  int[] ccl = new int[n];
  ccl[] =0;
  long ans = 0;

  foreach(j;0 .. m) {
    int[] co; //= new int[];
    char[] ch;// = new char[];
    int[] ml;// = new int[];
    co ~= 0;
    ch ~= A[0][j];
    ml ~= (n+m+100);

    foreach(i; 0 .. n) {
      if (j >= 1 && A[i][j] == A[i][j-1]) { ccl[i]++;} else ccl[i] = 1;

      if (A[i][j] == ch[$-1]) {
        co[$-1]++;
        ml[$-1] = min(ml[$-1], ccl[i]);
      } else {
        co ~= 1;
        ch ~= A[i][j];
        ml ~= ccl[i];
      }

      if (co.length >= 3) {
        if (co[$-1] == co[$-2] && co[$-1] >= co[$-3]) {
          long t= min(ml[$-1], ml[$-2]);
          int sss = co[$-1];
          t = min(t, minElement( ccl[ i - sss* 3 + 1 .. i - sss*2 + 1 ] ) );
          ans += t;
        }
      }
    }
  }

  writeln(ans);

}


'Twas the night before Christmas, and Santa's frantically setting up his new Christmas tree! There are $$$n$$$ nodes in the tree, connected by $$$n-1$$$ edges. On each edge of the tree, there's a set of Christmas lights, which can be represented by an integer in binary representation.  He has $$$m$$$ elves come over and admire his tree. Each elf is assigned two nodes, $$$a$$$ and $$$b$$$, and that elf looks at all lights on the simple path between the two nodes. After this, the elf's favorite number becomes the bitwise XOR of the values of the lights on the edges in that path.However, the North Pole has been recovering from a nasty bout of flu. Because of this, Santa forgot some of the configurations of lights he had put on the tree, and he has already left the North Pole! Fortunately, the elves came to the rescue, and each one told Santa what pair of nodes he was assigned $$$(a_i, b_i)$$$, as well as the parity of the number of set bits in his favorite number. In other words, he remembers whether the number of $$$1$$$'s when his favorite number is written in binary is odd or even.Help Santa determine if it's possible that the memories are consistent, and if it is, remember what his tree looked like, and maybe you'll go down in history!

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq m \leq 2 \cdot 10^5$$$) — the size of tree and the number of elves respectively. The next $$$n-1$$$ lines of each test case each contains three integers, $$$x$$$, $$$y$$$, and $$$v$$$ ($$$1 \leq x, y \leq n$$$; $$$-1 \leq v < 2^{30}$$$) — meaning that there's an edge between nodes $$$x$$$ and $$$y$$$. If    $$$v = -1$$$: Santa doesn't remember what the set of lights were on for this edge.  $$$v \geq 0$$$: The set of lights on the edge is $$$v$$$.  The next $$$m$$$ lines of each test case each contains three integers, $$$a$$$, $$$b$$$, and $$$p$$$ ($$$1 \leq a, b \leq n$$$; $$$a \neq b$$$; $$$0 \leq p \leq 1$$$) — the nodes that the elf was assigned to, and the parity of the number of set bits in the elf's favorite number. It is guaranteed that the sum of all $$$n$$$ and the sum of all $$$m$$$ don't exceed $$$2 \cdot 10^5$$$ each. It is guaranteed that the given edges form a tree.

For each test case, first print either YES or NO (in any case), whether there's a tree consistent with Santa's memory or not.  If the answer is YES, print $$$n-1$$$ lines each containing three integers: $$$x$$$, $$$y$$$, and $$$v$$$ ($$$1 \le x, y \le n$$$; $$$0 \le v < 2^{30}$$$) — the edge and the integer on that edge. The set of edges must be the same as in the input, and if the value of some edge was specified earlier, it can not change. You can print the edges in any order. If there are multiple answers, print any.

NoteThe first test case is the image in the statement.One possible answer is assigning the value of the edge $$$(1, 2)$$$ to $$$5$$$, and the value of the edge $$$(2, 5)$$$ to $$$3$$$. This is correct because:   The first elf goes from node $$$2$$$ to node $$$3$$$. This elf's favorite number is $$$4$$$, so he remembers the value $$$1$$$ (as $$$4$$$ has an odd number of $$$1$$$ bits in its binary representation).  The second elf goes from node $$$2$$$ to node $$$5$$$. This elf's favorite number is $$$3$$$, so he remembers the value $$$0$$$ (as $$$3$$$ has an even number of $$$1$$$ bits in its binary representation).  The third elf goes from node $$$5$$$ to node $$$6$$$. This elf's favorite number is $$$7$$$, so he remembers the value $$$1$$$ (as $$$7$$$ has an odd number of $$$1$$$ bits in its binary representation).  The fourth elf goes from node $$$6$$$ to node $$$1$$$. This elf's favorite number is $$$1$$$, so he remembers the value $$$1$$$ (as $$$1$$$ has an odd number of $$$1$$$ bits in its binary representation).  The fifth elf goes from node $$$4$$$ to node $$$5$$$. This elf's favorite number is $$$4$$$, so he remembers the number $$$1$$$ (as $$$4$$$ has an odd number of $$$1$$$ bits in its binary representation). Note that there are other possible answers.

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto m = readInt!int;
  struct Adj
  {
    int value;
    int w;
    int index;
  }
  auto adj = new Adj[][](n);
  int getParity(int value)
  {
    int parity = 0;
    while (value)
      {
	parity ^= value&1;
	value >>= 1;
      }
    return parity;
  }
  auto edges = new int[3][](0);
  foreach(i; 0 .. n - 1)
    {
      auto x = readInt!int - 1;
      auto y = readInt!int - 1;
      auto v = readInt!int;
      adj[x] ~= Adj(v, y, i);
      adj[y] ~= Adj(v, x, i);
      edges ~= [x, y, v];
    }
  auto setAcc = new bool[](n);
  auto acc = new int[](n);
  auto rel = new Adj[][](n);
  foreach(i; 0 .. m)
    {
      auto a = readInt!int - 1;
      auto b = readInt!int - 1;
      auto p = readInt!int;
      rel[a] ~= Adj(p, b);
      rel[b] ~= Adj(p, a);
    }
  foreach(e; edges)
    {
      if (e[2] >= 0)
	{
	  auto p = getParity(e[2]);
	  rel[e[0]] ~= Adj(p, e[1]);
	  rel[e[1]] ~= Adj(p, e[0]);
	}
    }
  bool possible = true;
  void fillAcc(int v, int color)
  {
    if (setAcc[v])
      {
	if (acc[v] != color) possible = false;
	return;
      }
    else
      {
	setAcc[v] = true;
	acc[v] = color;
      }
    foreach(a; rel[v])
      {
	fillAcc(a.w, color ^ a.value);
      }
  }
  foreach(i; 0 .. n) if (!setAcc[i]) fillAcc(i, 0);
  if (!possible) return writeln("NO");
  writeln("YES");
  void fillEdges(int v, int parent)
  {
    foreach(a; adj[v]) if (a.w != parent)
      {
	int diff = acc[a.w] ^ acc[v];
	if (a.value < 0)
	  {
	    assert(a.value == edges[a.index][2]);
	    edges[a.index][2] = diff;
	  }
	fillEdges(a.w, v);
      }
  }
  fillEdges(0, -1);
  foreach(edge; edges)
    {
      writeln(edge[0] + 1, " ", edge[1] + 1, " ", edge[2]);
    }
  writeln;
}

// main {{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
// }}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

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

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

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

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

alias Value = int;
int root(int[] ps, Value[] qs, int u) {
  if (ps[u] < 0) {
    return u;
  } else {
    int r = root(ps, qs, ps[u]);
    // qs[u] += qs[ps[u]];
    qs[u] ^= qs[ps[u]];
    return (ps[u] = r);
  }
}
bool connect(int[] ps, Value[] qs, int u, int v, Value c) {
  int ru = root(ps, qs, u);
  int rv = root(ps, qs, v);
  // Value cc = c + qs[u] - qs[v];
  Value cc = c ^ qs[u] ^ qs[v];
  if (ru == rv) return (cc == 0);
  // if (ps[ru] > ps[rv]) { swap(ru, rv); cc *= -1; }
  if (ps[ru] > ps[rv]) { swap(ru, rv); }
  ps[ru] += ps[rv]; ps[rv] = ru; qs[rv] = cc;
  return true;
}

int N, M;
int[] A, B, C;
int[] U, V, P;

int[][] G;
int[] ds;

void dfs(int u, int p, int d) {
  ds[u] = d;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      const c = (~C[i]) ? (popcnt(C[i]) & 1) : 0;
      dfs(v, u, d ^ c);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        N = readInt;
        M = readInt;
        A = new int[N - 1];
        B = new int[N - 1];
        C = new int[N - 1];
        foreach (i; 0 .. N - 1) {
          A[i] = readInt - 1;
          B[i] = readInt - 1;
          C[i] = readInt;
        }
        U = new int[M];
        V = new int[M];
        P = new int[M];
        foreach (m; 0 .. M) {
          U[m] = readInt - 1;
          V[m] = readInt - 1;
          P[m] = readInt;
        }

        G = new int[][N];
        foreach (i; 0 .. N - 1) {
          G[A[i]] ~= i;
          G[B[i]] ~= i;
        }
        ds = new int[N];
        dfs(0, -1, 0);
        debug {
          writeln("ds = ", ds);
        }

        auto ps = new int[N];
        ps[] = -1;
        auto qs = new int[N];
        foreach (i; 0 .. N - 1) {
          if (~C[i]) {
            if (!connect(ps, qs, A[i], B[i], popcnt(C[i]) & 1)) {
              assert(false);
            }
          }
        }
        bool ok = true;
        foreach (m; 0 .. M) {
          if (!connect(ps, qs, U[m], V[m], P[m])) {
            ok = false;
          }
        }
        if (ok) {
          foreach (u; 0 .. N) {
            root(ps, qs, u);
          }
          writeln("YES");
          foreach (i; 0 .. N - 1) {
            int c = C[i];
            if (!~c) {
              c = qs[A[i]] ^ qs[B[i]];
            }
            writeln(A[i] + 1, " ", B[i] + 1, " ", c);
          }
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}


You are given two positive integers $$$n$$$ and $$$s$$$. Find the maximum possible median of an array of $$$n$$$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $$$s$$$.A median of an array of integers of length $$$m$$$ is the number standing on the $$$\lceil {\frac{m}{2}} \rceil$$$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $$$1$$$. For example, a median of the array $$$[20,40,20,50,50,30]$$$ is the $$$\lceil \frac{m}{2} \rceil$$$-th element of $$$[20,20,30,40,50,50]$$$, so it is $$$30$$$. There exist other definitions of the median, but in this problem we use the described definition.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with two integers $$$n$$$ and $$$s$$$ ($$$1 \le n, s \le 10^9$$$) — the length of the array and the required sum of the elements.

For each test case print a single integer — the maximum possible median.

NotePossible arrays for the first three test cases (in each array the median is underlined):  In the first test case $$$[\underline{5}]$$$  In the second test case $$$[\underline{2}, 3]$$$  In the third test case $$$[1, \underline{2}, 2]$$$

immutable multi = true;

void solve(int)
{
	auto n = readInt!long;
	auto s = readInt!long;
	long med = (n+1)/2;
	long frommed = n - med +1;
	return writeln(s / frommed);
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
	{
		popChar;
	}
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

import std.stdio, std.string, std.algorithm, std.array;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(i; 0..t){
	int n, s;
	scanf("%d %d", &n, &s);
	auto num_zero = (n - 1) / 2;
	writeln(s / (n - num_zero));
    }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n, s;
    readf!" %d %d "(n, s);
    long cnt = n / 2 + 1;
    long ans = s / cnt;
    writeln(ans);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, s;
		readf !(" %s %s") (n, s);
		n /= 2;
		n += 1;
		s /= n;
		writeln (s);
	}
}

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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto s = RD!int;

		auto cnt = n / 2 + 1;
		ans[ti] = s / cnt;
	}

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