{"nl": {"description": "Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this array at all.Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula: The less value c(a) is, the more beautiful the array is.It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.Help Levko and calculate what minimum number c(a) he can reach.", "input_spec": "The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 ≤ ai ≤ 109).", "output_spec": "A single number — the minimum value of c(a) Levko can get.", "sample_inputs": ["5 2\n4 7 4 7 4", "3 1\n-100 0 100", "6 3\n1 2 3 7 8 9"], "sample_outputs": ["0", "100", "1"], "notes": "NoteIn the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.In the third sample he can get array: 1, 2, 3, 4, 5, 6."}, "positive_code": [{"source_code": "import std.stdio;\n\nstatic immutable int N = 2010;\nint n, k;\nlong[N] a, f;\n\nbool check(long t) {\n f[] = 1;\n foreach (i; 2 .. n + 1)\n foreach (j; 1 .. i)\n if (a[j] - t * (i - j) <= a[i] && a[i] <= a[j] + t * (i - j))\n if (f[i] < f[j] + 1) f[i] = f[j] + 1;\n long max = 0;\n foreach (i; 1 .. n + 1)\n if (max < f[i]) max = f[i];\n return n - max <= k;\n}\n\nint main() {\n readf(\" %d %d\", &n, &k);\n foreach (i; 1 .. n + 1) readf(\" %d\", &a[i]);\n long l = 0, r = 2000000000;\n while (l < r) {\n long mid = l + r >> 1;\n if (check(mid)) r = mid;\n else l = mid + 1;\n }\n printf(\"%d\\n\", r);\n return 0;\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\n\nstatic immutable int N = 2010;\nint n, k;\nlong[N] a, f;\n\nbool check(long t) {\n f[] = 0, f[1] = 1;\n foreach (i; 2 .. n + 1)\n foreach (j; 1 .. i)\n if (a[j] - t * (i - j) <= a[i] && a[i] <= a[j] + t * (i - j))\n if (f[i] < f[j] + 1) f[i] = f[j] + 1;\n long max = 0;\n foreach (i; 1 .. n + 1)\n if (max < f[i]) max = f[i];\n return n - max <= k;\n}\n\nint main() {\n readf(\" %d %d\", &n, &k);\n foreach (i; 1 .. n + 1) readf(\" %d\", &a[i]);\n long l = 0, r = 1000000000;\n while (l < r) {\n long mid = l + r >> 1;\n if (check(mid)) r = mid;\n else l = mid + 1;\n }\n printf(\"%d\\n\", l);\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nstatic immutable int N = 2010;\nint n, k;\nlong[N] a, f;\n\nbool check(long t) {\n f[] = 1;\n foreach (i; 2 .. n + 1)\n foreach (j; 1 .. i)\n if (a[j] - t * (i - j) <= a[i] && a[i] <= a[j] + t * (i - j))\n if (f[i] < f[j] + 1) f[i] = f[j] + 1;\n long max = 0;\n foreach (i; 1 .. n + 1)\n if (max < f[i]) max = f[i];\n return n - max <= k;\n}\n\nint main() {\n readf(\" %d %d\", &n, &k);\n foreach (i; 1 .. n + 1) readf(\" %d\", &a[i]);\n long l = 0, r = 1000000000;\n while (l < r) {\n long mid = l + r >> 1;\n if (check(mid)) r = mid;\n else l = mid + 1;\n }\n printf(\"%d\\n\", r);\n return 0;\n}\n"}], "src_uid": "544dd0c7274ac337744b8ba0996add1d"} {"nl": {"description": "The main server of Gomble company received a log of one top-secret process, the name of which can't be revealed. The log was written in the following format: «[date:time]: message», where for each «[date:time]» value existed not more than 10 lines. All the files were encoded in a very complicated manner, and only one programmer — Alex — managed to decode them. The code was so complicated that Alex needed four weeks to decode it. Right after the decoding process was finished, all the files were deleted. But after the files deletion, Alex noticed that he saved the recordings in format «[time]: message». So, information about the dates was lost. However, as the lines were added into the log in chronological order, it's not difficult to say if the recordings could appear during one day or not. It is possible also to find the minimum amount of days during which the log was written.So, to make up for his mistake Alex has to find the minimum amount of days covered by the log. Note that Alex doesn't have to find the minimum amount of days between the beginning and the end of the logging, he has to find the minimum amount of dates in which records could be done. (See Sample test 2 for further clarifications).We should remind you that the process made not more than 10 recordings in a minute. Consider that a midnight belongs to coming day.", "input_spec": "The first input line contains number n (1 ≤ n ≤ 100). The following n lines contain recordings in format «[time]: message», where time is given in format «hh:mm x.m.». For hh two-digit numbers from 01 to 12 are used, for mm two-digit numbers from 00 to 59 are used, and x is either character «a» or character «p». A message is a non-empty sequence of Latin letters and/or spaces, it doesn't start or end with a space. The length of each message doesn't exceed 20.", "output_spec": "Output one number — the minimum amount of days covered by the log.", "sample_inputs": ["5\n[05:00 a.m.]: Server is started\n[05:00 a.m.]: Rescan initialized\n[01:13 p.m.]: Request processed\n[01:10 p.m.]: Request processed\n[11:40 p.m.]: Rescan completed", "3\n[09:00 a.m.]: User logged in\n[08:00 a.m.]: User logged in\n[07:00 a.m.]: User logged in"], "sample_outputs": ["2", "3"], "notes": "NoteFormally the 12-hour time format is described at: http://en.wikipedia.org/wiki/12-hour_clock. The problem authors recommend you to look through these descriptions before you start with the problem."}, "positive_code": [{"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.variant,\n\tstd.stdio,std.bitmanip;\nimport core.stdc.stdio : freopen;\nalias RedBlackTree Rbt;\nalias redBlackTree rbt;\nalias BigInt big;\nalias pair!(int,int) pii;\nalias pair!(long,long) pll;\nalias boyerMooreFinder strfind;\nalias make_pair mp;\nalias binary_search bins;\nalias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});\npair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)\n{\n\tpair!(X,Y) pp;\n\tpp.fi=x_;\n\tpp.se=y_;\n\treturn pp;\n}\nimmutable int mod=10^^9+7;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b)\n\t{\n\t\treturn a/gcd(a,b)*b;\n\t}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) \n\t\tif(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tint[] a=new int[n];\n\t\tforeach(i;0..n)\n\t\t{\n\t\t\tint h,m;\n\t\t\tchar c;\n\t\t\treadf(\"%c\",&c);\n\t\t\tdebug writeln(c);\n\t\t\treadf(\"%d:%d %c\",&h,&m,&c);\n\t\t\treadln;\n\t\t\tif(c=='a')\n\t\t\t{\n\t\t\t\tif(h!=12)a[i]=60*h+m;\n\t\t\t\telse a[i]=m;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif(h!=12)a[i]=60*((h+12)%24)+m;\n\t\t\t\telse a[i]=60*h+m;\n\t\t\t}\n\t\t}\n\t\tdebug writeln(a);\n\t\tint ans=0,k=0;\n\t\tforeach(i;1..n)\n\t\t{\n\t\t\tif(a[i]==a[i-1])k++;\n\t\t\telse k=1;\n\t\t\tif(k>10)\n\t\t\t{\n\t\t\t\tif(ans==0)ans++;\n\t\t\t\tans++;\n\t\t\t\tk=1;\n\t\t\t}\n\t\t\tif(a[i]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tint[] a=new int[n];\n\t\tforeach(i;0..n)\n\t\t{\n\t\t\tint h,m;\n\t\t\tchar c;\n\t\t\treadf(\"%c\",&c);\n\t\t\tdebug writeln(c);\n\t\t\treadf(\"%d:%d %c\",&h,&m,&c);\n\t\t\treadln;\n\t\t\tif(c=='a')a[i]=60*h+m;\n\t\t\telse a[i]=60*((h+12)%24)+m;\n\t\t}\n\t\tdebug writeln(a);\n\t\tint ans=0,k=0;\n\t\tforeach(i;1..n)\n\t\t{\n\t\t\tif(a[i]==a[i-1])k++;\n\t\t\telse k=1;\n\t\t\tif(k>10)\n\t\t\t{\n\t\t\t\tans++;\n\t\t\t\tk=1;\n\t\t\t}\n\t\t\tif(a[i]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tint[] a=new int[n];\n\t\tforeach(i;0..n)\n\t\t{\n\t\t\tint h,m;\n\t\t\tchar c;\n\t\t\treadf(\"%c\",&c);\n\t\t\tdebug writeln(c);\n\t\t\treadf(\"%d:%d %c\",&h,&m,&c);\n\t\t\treadln;\n\t\t\tif(c=='a')\n\t\t\t{\n\t\t\t\tif(h!=12)a[i]=60*h+m;\n\t\t\t\telse a[i]=m;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif(h!=12)a[i]=60*((h+12)%24)+m;\n\t\t\t\telse a[i]=60*h+m;\n\t\t\t}\n\t\t}\n\t\tdebug writeln(a);\n\t\tint ans=0,k=0;\n\t\tforeach(i;1..n)\n\t\t{\n\t\t\tif(a[i]==a[i-1])k++;\n\t\t\telse k=1;\n\t\t\tif(k>10)\n\t\t\t{\n\t\t\t\tans++;\n\t\t\t\tk=1;\n\t\t\t}\n\t\t\tif(a[i]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tint[] a=new int[n];\n\t\tforeach(i;0..n)\n\t\t{\n\t\t\tint h,m;\n\t\t\tchar c;\n\t\t\treadf(\"%c\",&c);\n\t\t\tdebug writeln(c);\n\t\t\treadf(\"%d:%d %c\",&h,&m,&c);\n\t\t\treadln;\n\t\t\tif(c=='a')a[i]=60*h+m;\n\t\t\telse a[i]=60*((h+12)%24)+m;\n\t\t}\n\t\tdebug writeln(a);\n\t\tint ans=0,k=0;\n\t\tforeach(i;1..n)\n\t\t{\n\t\t\tif(a[i]==a[i-1])k++;\n\t\t\telse k=1;\n\t\t\tif(k>10)\n\t\t\t{\n\t\t\t\tans++;\n\t\t\t\tk=1;\n\t\t\t}\n\t\t\tif(a[i] t) c += e-1-t;\n }\n\n ans = min(ans, c);\n if (ans == c) {\n st = t;\n }\n }\n\n writeln(st, \" \", ans);\n\n}\n"}], "negative_code": [], "src_uid": "bce9ebad1dc8bd5aae516c4ca9e551c0"} {"nl": {"description": "Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a result. Then he calculates that is defined as the maximal number of non-overlapping substrings equal to p that can be found in s'. He wants to make this number as big as possible.More formally, let's define as maximum value of over all s' that can be obtained by removing exactly x characters from s. Dreamoon wants to know for all x from 0 to |s| where |s| denotes the length of string s.", "input_spec": "The first line of the input contains the string s (1 ≤ |s| ≤ 2 000). The second line of the input contains the string p (1 ≤ |p| ≤ 500). Both strings will only consist of lower case English letters.", "output_spec": "Print |s| + 1 space-separated integers in a single line representing the for all x from 0 to |s|.", "sample_inputs": ["aaaaa\naa", "axbaxxb\nab"], "sample_outputs": ["2 2 1 1 0 0", "0 1 1 2 1 1 0 0"], "notes": "NoteFor the first sample, the corresponding optimal values of s' after removal 0 through |s| = 5 characters from s are {\"aaaaa\", \"aaaa\", \"aaa\", \"aa\", \"a\", \"\"}. For the second sample, possible corresponding optimal values of s' are {\"axbaxxb\", \"abaxxb\", \"axbab\", \"abab\", \"aba\", \"ab\", \"a\", \"\"}."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.conv;\nimport std.algorithm;\n\nvoid main() {\n string s = readln.strip;\n string t = readln.strip;\n int n = to!(int)(s.length);\n int m = to!(int)(t.length);\n auto to = new int[n];\n to[] = -1;\n for (int i = 0; i < n; i++) {\n int j = i, count = 0;\n while (j < n && count < m) {\n count += s[j] == t[count];\n j++;\n }\n if (count == m) {\n to[i] = j;\n }\n }\n auto f = new int[][](n + 1, n + 1);\n foreach (ref g; f) {\n g[] = int.min;\n }\n for (int i = 0; i <= n; i++) {\n f[i][0] = 0;\n }\n for (int j = 0; j <= n; j++) {\n for (int i = 0; i < n; i++) {\n if (f[i][j] != int.min) {\n if (j + 1 <= n) { \n f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);\n }\n f[i + 1][j] = max(f[i + 1][j], f[i][j]);\n if (to[i] != -1) {\n f[to[i]][j + to[i] - i - m] = max(f[to[i]][j + to[i] - i - m], f[i][j] + 1);\n }\n }\n }\n }\n auto ans = new int[n + 1];\n for (int i = 0; i <= n; i++) {\n for (int j = 0; j <= n; j++) {\n ans[j] = max(ans[j], f[i][j]);\n }\n }\n writefln(\"%(%s %)\", ans);\n}\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint M, N;\nstring S, T;\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tS = readToken;\n\t\tT = readToken;\n\t\t\n\t\tM = S.length;\n\t\tN = T.length;\n\t\tint[][] dp = new int[][](M + 1, M + 1);\n\t\tforeach (i; 0 .. M + 1) {\n\t\t\tdp[i][] = -1;\n\t\t}\n\t\tdp[0][0] = 0;\n\t\tforeach (i; 0 .. M)\t{\n\t\t\t{\n\t\t\t\tbool ok = true;\n\t\t\t\tint ii = i;\n\t\t\t\tforeach (j; 0 .. N) {\n\t\t\t\t\tfor (; ii < M && S[ii] != T[j]; ++ii) {}\n\t\t\t\t\tif (ii < M) {\n\t\t\t\t\t\t++ii;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (ok) {\n\t\t\t\t\tconst dk = (ii - i) - N;\n\t\t\t\t\tforeach (k; 0 .. M + 1) if (k + dk <= M) {\n\t\t\t\t\t\tif (dp[i][k] >= 0) {\n\t\t\t\t\t\t\tchmax(dp[ii][k + dk], dp[i][k] + 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tforeach (k; 0 .. M + 1) {\n\t\t\t\tif (dp[i][k] >= 0) {\n\t\t\t\t\tchmax(dp[i + 1][k], dp[i][k]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tforeach (k; 0 .. M + 1) if (k + 1 <= M) {\n\t\t\t\tif (dp[i][k] >= 0) {\n\t\t\t\t\tchmax(dp[i + 1][k + 1], dp[i][k]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln(dp[M].to!string.removechars(\"[],\"));\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.conv;\nimport std.algorithm;\n\nvoid main() {\n string s = readln.strip;\n string t = readln.strip;\n int n = to!(int)(s.length);\n int m = to!(int)(t.length);\n auto to = new int[n];\n to[] = -1;\n for (int i = 0; i < n; i++) {\n int j = i, count = 0;\n while (j < n && count < m) {\n count += s[j] == t[count];\n j++;\n }\n if (count == m) {\n to[i] = j;\n }\n }\n auto f = new int[][](n + 1, n + 1);\n foreach (ref g; f) {\n g[] = int.min;\n }\n for (int i = 0; i <= n; i++) {\n f[i][0] = 0;\n }\n for (int j = 0; j <= n; j++) {\n for (int i = 0; i < n; i++) {\n if (f[i][j] != int.min) {\n if (j + 1 <= n) { \n f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);\n }\n if (to[i] != -1) {\n f[to[i]][j + to[i] - i - m] = max(f[to[i]][j + to[i] - i - m], f[i][j] + 1);\n }\n }\n }\n }\n auto ans = new int[n + 1];\n for (int i = 0; i <= n; i++) {\n for (int j = 0; j <= n; j++) {\n ans[j] = max(ans[j], f[i][j]);\n }\n }\n writefln(\"%(%s %)\", ans);\n}\n"}, {"source_code": "import std.stdio;\nimport std.string;\nimport std.conv;\nimport std.algorithm;\n\nvoid main() {\n string s = readln.strip;\n string t = readln.strip;\n int n = to!(int)(s.length);\n int m = to!(int)(t.length);\n auto to = new int[n];\n to[] = -1;\n for (int i = 0; i < n; i++) {\n int j = i, count = 0;\n while (j < n && count < m) {\n count += s[j] == t[count];\n j++;\n }\n if (count == m) {\n to[i] = j;\n }\n }\n auto f = new int[][](n + 1, n + 1);\n foreach (ref g; f) {\n g[] = int.min;\n }\n f[0][0] = 0;\n for (int j = 0; j <= n; j++) {\n for (int i = 0; i < n; i++) {\n if (f[i][j] != int.min) {\n if (i + 1 <= n && j + 1 <= n) { \n f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);\n }\n if (to[i] != -1) {\n assert(j + to[i] - i - m <= n);\n f[to[i]][j + to[i] - i - m] = max(f[to[i]][j + to[i] - i - m], f[i][j] + 1);\n }\n }\n }\n }\n auto ans = new int[n + 1];\n for (int i = 0; i <= n; i++) {\n for (int j = 0; j <= n; j++) {\n ans[j] = max(ans[j], f[i][j]);\n }\n }\n writefln(\"%(%s %)\", ans);\n}\n"}], "src_uid": "abdf06347e6db23932ef07020f49aa52"} {"nl": {"description": "You are given a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The height of the $$$i$$$-th vertex is $$$h_i$$$. You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency $$$e$$$ costs $$$e$$$ coins, where $$$e > 0$$$.It is considered that a vertex $$$x$$$ gets a signal if for some pair of towers at the vertices $$$u$$$ and $$$v$$$ ($$$u \\neq v$$$, but it is allowed that $$$x = u$$$ or $$$x = v$$$) with efficiencies $$$e_u$$$ and $$$e_v$$$, respectively, it is satisfied that $$$\\min(e_u, e_v) \\geq h_x$$$ and $$$x$$$ lies on the path between $$$u$$$ and $$$v$$$.Find the minimum number of coins required to set up towers so that you can get a signal at all vertices.", "input_spec": "The first line contains an integer $$$n$$$ ($$$2 \\le n \\le 200\\,000$$$) — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$h_i$$$ ($$$1 \\le h_i \\le 10^9$$$) — the heights of the vertices. Each of the next $$$n - 1$$$ lines contain a pair of numbers $$$v_i, u_i$$$ ($$$1 \\le v_i, u_i \\le n$$$) — an edge of the tree. It is guaranteed that the given edges form a tree.", "output_spec": "Print one integer — the minimum required number of coins.", "sample_inputs": ["3\n1 2 1\n1 2\n2 3", "5\n1 3 3 1 3\n1 3\n5 4\n4 3\n2 3", "2\n6 1\n1 2"], "sample_outputs": ["4", "7", "12"], "notes": "NoteIn the first test case it's optimal to install two towers with efficiencies $$$2$$$ at vertices $$$1$$$ and $$$3$$$.In the second test case it's optimal to install a tower with efficiency $$$1$$$ at vertex $$$1$$$ and two towers with efficiencies $$$3$$$ at vertices $$$2$$$ and $$$5$$$.In the third test case it's optimal to install two towers with efficiencies $$$6$$$ at vertices $$$1$$$ and $$$2$$$."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nlong[] H;\nint[] A, B;\n\nint[][] G;\nint[] par;\nalias Result = Tuple!(long, \"m\", long, \"s\");\nResult[] dp, DP;\n\nvoid dfs(int u, int p) {\n par[u] = p;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n dfs(v, u);\n }\n }\n // init\n Result f;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n // add dp[v]\n chmax(f.m, dp[v].m);\n f.s += dp[v].s;\n }\n }\n // calc dp[u]\n if (f.m < H[u]) {\n f.s += (H[u] - f.m);\n f.m = H[u];\n }\n dp[u] = f;\n}\n\nvoid DFS(int u, int p) {\n // init\n Result[] gs;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n // add dp[v]\n gs ~= dp[v];\n }\n }\n if (p != -1) {\n // add DP[u]\n gs ~= DP[u];\n }\n \n const len = cast(int)(gs.length);\n auto ls = new long[len + 1];\n auto rs = new long[len + 1];\n ls[0] = 0;\n foreach (j; 0 .. len) ls[j + 1] = max(ls[j], gs[j].m);\n rs[len] = 0;\n foreach_reverse (j; 0 .. len) rs[j] = max(gs[j].m, rs[j + 1]);\n long sum;\n foreach (j; 0 .. len) sum += gs[j].s;\n \n int j;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n // calc DP[v], removing dp[v]\n Result f;\n f.m = max(ls[j], rs[j + 1]);\n f.s = sum - gs[j].s;\n if (f.m < H[u]) {\n f.s += (H[u] - f.m);\n f.m = H[u];\n }\n DP[v] = f;\n ++j;\n }\n }\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n DFS(v, u);\n }\n }\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt;\n H = new long[N];\n foreach (u; 0 .. N) {\n H[u] = readLong;\n }\n A = new int[N - 1];\n B = new int[N - 1];\n foreach (i; 0 .. N - 1) {\n A[i] = readInt - 1;\n B[i] = readInt - 1;\n }\n G = new int[][N];\n foreach (i; 0 .. N - 1) {\n G[A[i]] ~= i;\n G[B[i]] ~= i;\n }\n \n \n par.length = N;\n dp.length = N;\n DP.length = N;\n const rt = 0;\n dfs(rt, -1);\n DFS(rt, -1);\n debug {\n writeln(\"rt = \", rt);\n writeln(\"dp = \", dp);\n writeln(\"DP = \", DP);\n }\n long ans = long.max;\n foreach (u; 0 .. N) if (G[u].length == 1) {\n // init\n Result f;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != par[u]) {\n // add dp[v]\n chmax(f.m, dp[v].m);\n f.s += dp[v].s;\n }\n }\n if (u != rt) {\n // add DP[u]\n chmax(f.m, DP[u].m);\n f.s += DP[u].s;\n }\n // calc ans, rooted at u\n if (f.m < H[u]) {\n f.s += (H[u] - f.m);\n f.m = H[u];\n }\n const cost = f.s + f.m;\n debug {\n writeln(u, \": \", f, \" \", cost);\n }\n chmin(ans, cost);\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nlong[] H;\nint[] A, B;\n\nint[][] G;\nint[] par;\nalias Result = Tuple!(long, \"m\", long, \"s\");\nResult[] dp, DP;\n\nvoid dfs(int u, int p) {\n par[u] = p;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n dfs(v, u);\n }\n }\n // init\n Result f;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n // add dp[v]\n chmax(f.m, dp[v].m);\n f.s += dp[v].s;\n }\n }\n // calc dp[u]\n if (f.m < H[u]) {\n f.s += (H[u] - f.m);\n f.m = H[u];\n }\n dp[u] = f;\n}\n\nvoid DFS(int u, int p) {\n // init\n Result[] gs;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n // add dp[v]\n gs ~= dp[v];\n }\n }\n if (p != -1) {\n // add DP[u]\n gs ~= DP[u];\n }\n \n const len = cast(int)(gs.length);\n auto ls = new long[len + 1];\n auto rs = new long[len + 1];\n ls[0] = 0;\n foreach (j; 0 .. len) ls[j + 1] = max(ls[j], gs[j].m);\n rs[len] = 0;\n foreach_reverse (j; 0 .. len) rs[j] = max(gs[j].m, ls[j + 1]);\n long sum;\n foreach (j; 0 .. len) sum += gs[j].s;\n \n int j;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n // calc DP[v], removing dp[v]\n Result f;\n f.m = max(ls[j], rs[j + 1]);\n f.s = sum - gs[j].s;\n if (f.m < H[u]) {\n f.s += (H[u] - f.m);\n f.m = H[u];\n }\n DP[v] = f;\n ++j;\n }\n }\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n DFS(v, u);\n }\n }\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt;\n H = new long[N];\n foreach (u; 0 .. N) {\n H[u] = readLong;\n }\n A = new int[N - 1];\n B = new int[N - 1];\n foreach (i; 0 .. N - 1) {\n A[i] = readInt - 1;\n B[i] = readInt - 1;\n }\n G = new int[][N];\n foreach (i; 0 .. N - 1) {\n G[A[i]] ~= i;\n G[B[i]] ~= i;\n }\n \n \n par.length = N;\n dp.length = N;\n DP.length = N;\n const rt = 0;\n dfs(rt, -1);\n DFS(rt, -1);\n debug {\n writeln(\"rt = \", rt);\n writeln(\"dp = \", dp);\n writeln(\"DP = \", DP);\n }\n long ans = long.max;\n foreach (u; 0 .. N) if (G[u].length == 1) {\n // init\n Result f;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != par[u]) {\n // add dp[v]\n chmax(f.m, dp[v].m);\n f.s += dp[v].s;\n }\n }\n if (u != rt) {\n // add DP[u]\n chmax(f.m, DP[u].m);\n f.s += DP[u].s;\n }\n // calc ans, rooted at u\n if (f.m < H[u]) {\n f.s += (H[u] - f.m);\n f.m = H[u];\n }\n const cost = f.s + f.m;\n debug {\n writeln(u, \": \", f, \" \", cost);\n }\n chmin(ans, cost);\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "a569cd5f8bf8aaf011858f839e91848a"} {"nl": {"description": "You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $$$1$$$, and in each cell you can either put a platform or leave it empty.In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $$$p$$$, then bounces off it, then bounces off a platform in the cell $$$(p + k)$$$, then a platform in the cell $$$(p + 2k)$$$, and so on every $$$k$$$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $$$p$$$ and $$$k$$$.You already have some level pattern $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, ..., $$$a_n$$$, where $$$a_i = 0$$$ means there is no platform in the cell $$$i$$$, and $$$a_i = 1$$$ means there is one. You want to modify it so that the level can be passed with given $$$p$$$ and $$$k$$$. In $$$x$$$ seconds you can add a platform in some empty cell. In $$$y$$$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $$$p$$$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $$$p$$$ and $$$k$$$?", "input_spec": "The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$p$$$, and $$$k$$$ ($$$1 \\le p \\le n \\le 10^5$$$, $$$1 \\le k \\le n$$$) — the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $$$a_1 a_2 a_3 \\ldots a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$) — the initial pattern written without spaces. The last line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \\le x, y \\le 10^4$$$) — the time required to add a platform and to remove the first cell correspondingly. The sum of $$$n$$$ over test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case output a single integer — the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable.", "sample_inputs": ["3\n10 3 2\n0101010101\n2 2\n5 4 1\n00000\n2 10\n11 2 3\n10110011000\n4 3"], "sample_outputs": ["2\n4\n10"], "notes": "NoteIn the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $$$y = 2$$$.In the second test case it's best to add a platform to both cells $$$4$$$ and $$$5$$$: 00000 $$$\\to$$$ 00011. The time required is $$$x \\cdot 2 = 4$$$.In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $$$10$$$-th: 10110011000 $$$\\to$$$ 10110011010. The time required is $$$y \\cdot 2 + x = 10$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tlong[int] memo;\n\t\tforeach (i; 0..n-p)\n\t\t{\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\t\t\t\tauto m = memo.get(pp, -1);\n\t\t\t\tif (m != -1) return m;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tif (pp+k < n)\n\t\t\t\t{\n\t\t\t\t\tauto res2 = (pp+k-p)*y + res;\n\t\t\t\t\tans[ti].chmin(res2);\n\t\t\t\t}\n\t\t\t\tmemo[pp] = c + res;\n\t\t\t\treturn c + res;\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(p+i) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint pos = p+i;\n\t\t\t\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tauto res2 = max(pp+k-pos, n-p)*y + res;\n\t\t\t\tans[ti].chmin(res2);\n\t\t\t\treturn c + res;\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(pos) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint pos = p+i;\n\t\t\t\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\t\t\t\tlong c;\n\t\t\t\tif (a[pp] == '0')\n\t\t\t\t\tc = x;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\treturn min((pp+k-pos)*y + res , c + res);\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(pos) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint pos = p+i;\n\t\t\t\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tauto res2 = (pp+k-pos)*y + res;\n\t\t\t\tans[ti].chmin(res2);\n\t\t\t\treturn min(res2, c + res);\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(pos) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tif (pp+k < n)\n\t\t\t\t{\n\t\t\t\t\tauto res2 = (pp+k-p)*y + res;\n\t\t\t\t\tans[ti].chmin(res2);\n\t\t\t\t}\n\t\t\t\treturn c + res;\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(p+i) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint pos = p+i;\n\t\t\t\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tif (pp+k <= n-p)\n\t\t\t\t{\n\t\t\t\t\tauto res2 = (pp+k)*y + res;\n\t\t\t\t\tans[ti].chmin(res2);\n\t\t\t\t}\n\t\t\t\treturn c + res;\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(pos) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint pos = p+i;\n\t\t\t\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tif (pp+k-p < n-p)\n\t\t\t\t{\n\t\t\t\t\tauto res2 = (pp+k-p)*y + res;\n\t\t\t\t\tans[ti].chmin(res2);\n\t\t\t\t}\n\t\t\t\treturn c + res;\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(pos) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RD!int-1;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\n\t\tans[ti] = long.max;\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint pos = p+i;\n\t\t\t\n\t\t\tlong dfs(int pp)\n\t\t\t{\n\t\t\t\tif (pp >= n) return 0;\n\n\t\t\t\tlong c = a[pp] == '0' ? x : 0;\n\t\t\t\tauto res = dfs(pp+k);\n\t\t\t\tif (pp+k < n-p)\n\t\t\t\t{\n\t\t\t\t\tauto res2 = (pp+k)*y + res;\n\t\t\t\t\tans[ti].chmin(res2);\n\t\t\t\t}\n\t\t\t\treturn c + res;\n\t\t\t}\n\t\t\tans[ti].chmin(dfs(pos) + i*y);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "src_uid": "112a0cac4e1365c854bb651d3ee38df9"} {"nl": {"description": "You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.", "input_spec": "The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.", "output_spec": "For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.", "sample_inputs": ["1\n12", "2\n6\n8", "3\n1\n2\n3"], "sample_outputs": ["3", "1\n2", "-1\n-1\n-1"], "notes": "Note12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.8 = 4 + 4, 6 can't be split into several composite summands.1, 2, 3 are less than any composite number, so they do not have valid splittings."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n auto Q = readln.chomp.to!int;\n long ans;\n while (Q--) {\n auto n = readln.chomp.to!long;\n if (n % 4 == 0) {\n writeln(n / 4);\n } else if (n % 4 == 1) {\n if (n <= 5) writeln(-1);\n else writeln((n-9)/4 + 1);\n } else if (n % 4 == 2) {\n if (n == 2) writeln(-1);\n else writeln((n-6)/4 + 1);\n } else {\n if (n <= 11) writeln(-1);\n else writeln((n-15)/4 + 2);\n }\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n enum LIM = 1000;\n auto isnp = new bool[LIM];\n foreach (p; 2 .. LIM) {\n if (!isnp[p]) {\n for (int n = 2 * p; n < LIM; n += p) {\n isnp[n] = true;\n }\n }\n }\n auto dp = new int[LIM];\n dp[] = -1;\n dp[0] = 0;\n foreach (x; 0 .. LIM) {\n if (dp[x] >= 0) {\n foreach (n; 2 .. LIM - x) {\n if (isnp[n]) {\n chmax(dp[x + n], dp[x] + 1);\n }\n }\n }\n }\n debug {\n writeln(dp[0 .. 100]);\n }\n \n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n int ans;\n if (N < LIM) {\n ans = dp[N];\n } else {\n ans = N / 4;\n if (N % 2 != 0) {\n --ans;\n }\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n const long MAX = 10^^9 + 1;\n long[] primes;\n auto table = new bool[](10^^5);\n table.fill(true);\n table[0] = table[1] = false;\n foreach (i; 2..10^^5) {\n if (table[i]) {\n for (int j = i+i; j < 10^^5; j += i) {\n table[j] = false;\n }\n }\n }\n\n for (long i = 2; i * i < MAX; ++i) {\n if (table[i.to!int]) primes ~= i;\n }\n\n auto Q = readln.chomp.to!int;\n while (Q--) {\n auto n = readln.chomp.to!long;\n auto nn = n;\n long[] hoge;\n foreach (p; primes) {\n while (n % p == 0) {\n n /= p;\n hoge ~= p;\n }\n }\n if (n > 1) hoge ~= n;\n hoge.sort();\n if (hoge.length <= 1) writeln(-1);\n else writeln(nn / (hoge[0] * hoge[1]));\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n const long MAX = 10^^9 + 1;\n long[] primes;\n auto table = new bool[](10^^5);\n table.fill(true);\n table[0] = table[1] = false;\n foreach (i; 2..10^^5) {\n if (table[i]) {\n for (int j = i+i; j < 10^^5; j += i) {\n table[j] = false;\n }\n }\n }\n\n for (long i = 2; i * i < MAX; ++i) {\n if (table[i.to!int]) primes ~= i;\n }\n\n auto Q = readln.chomp.to!int;\n while (Q--) {\n auto n = readln.chomp.to!long;\n auto nn = n;\n long[] hoge;\n foreach (p; primes) {\n while (n % p == 0) {\n n /= p;\n hoge ~= p;\n }\n }\n hoge.sort();\n if (hoge.length <= 1) writeln(-1);\n else writeln(nn / (hoge[0] * hoge[1]));\n }\n}\n"}], "src_uid": "0c2550b2df0849a62969edf5b73e0ac5"} {"nl": {"description": "Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 100). The second line contains 2·n integers a1, a2, ... a2n (1 ≤ ai ≤ 1000).", "output_spec": "If it's possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print \"YES\". Otherwise print \"NO\".", "sample_inputs": ["2\n1 3 2 4", "1\n3 3"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "// Try Codeforces\n// author: Leonardone @ NEETSDKASU\nimport std.stdio : readln, writeln;\nimport std.string : chomp;\nimport std.array : split;\nimport std.conv : to;\nimport std.algorithm : sort;\n\nauto gets() { return readln.chomp; }\nauto getNum(T)() { return gets.to!T; }\nauto getVals(T)() { return gets.split.to!(T[]); }\n\nvoid main() {\n auto n = getNum!int;\n auto xs = getVals!int;\n xs.sort;\n writeln(xs[n-1] < xs[n] ? \"YES\" : \"NO\");\n}"}], "negative_code": [], "src_uid": "7b806b9a402a83a5b8943e7f3436cc9a"} {"nl": {"description": "Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \\ldots, a_{2n}$$$ ($$$1 \\leq a_i \\leq 1000$$$) — the elements of the array $$$a$$$.", "output_spec": "For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any.", "sample_inputs": ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"], "sample_outputs": ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"], "notes": "NoteIn the first test case, $$$b = \\{3+6, 4+5\\} = \\{9, 9\\}$$$ and $$$\\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \\{9+10\\} = \\{19\\}$$$ and $$$\\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \\{1+2, 3+3, 4+5, 90+3\\} = \\{3, 6, 9, 93\\}$$$ and $$$\\mathrm{gcd}(3, 6, 9, 93) = 3$$$."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto N = readln.chomp.to!int;\n auto aa = readln.split.to!(int[]);\n int[] os, es;\n foreach (i, a; aa) {\n if (a%2 == 0) {\n es ~= i.to!int+1;\n } else {\n os ~= i.to!int+1;\n }\n }\n int cnt;\n while (cnt < N-1 && os.length > 1) {\n writeln(os[0], \" \", os[1]);\n ++cnt;\n os = os[2..$];\n }\n while (cnt < N-1) {\n writeln(es[0], \" \", es[1]);\n ++cnt;\n es = es[2..$];\n }\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tint [2] cur;\n\t\tint [2] [] ans;\n\t\tforeach (int i, c; a[0..$ - 1])\n\t\t{\n\t\t\tif (cur[c & 1])\n\t\t\t{\n\t\t\t\tans ~= [cur[c & 1], i + 1];\n\t\t\t\tcur[c & 1] = 0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tcur[c & 1] = i + 1;\n\t\t\t}\n\t\t}\n\t\twritefln !(\"%(%(%s %)\\n%)\") (ans);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tint[][] ans;\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA!int;\n\t\t\n\t\tdebug writeln(\"n:\", n);\n\t\tint cnt;\n\t\tint x = -1;\n\t\tforeach (i; 0..n*2)\n\t\t{\n\t\t\tif (cnt == n-1) break;\n\t\t\tif (a[i] % 2)\n\t\t\t{\n\t\t\t\tif (x == -1)\n\t\t\t\t{\n\t\t\t\t\tx = i;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans ~= [x, i];\n\t\t\t\t\tx = -1;\n\t\t\t\t\t++cnt;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug writeln(\"ti:\", ti, \" cnt:\", cnt);\n\t\tx = -1;\n\t\tforeach (i; 0..n*2)\n\t\t{\n\t\t\tif (cnt == n-1) break;\n\t\t\tdebug writeln(\"i:\", i);\n\t\t\tif ((a[i] % 2) == 0)\n\t\t\t{\n\t\t\t\tif (x == -1)\n\t\t\t\t{\n\t\t\t\t\tx = i;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans ~= [x, i];\n\t\t\t\t\tx = -1;\n\t\t\t\t\t++cnt;\n\t\t\t\t} \n\t\t\t}\n\t\t}\n\t\tdebug writeln(\"ti:\", ti, \" cnt:\", cnt);\n\t}\n\t\n\tdebug writeln(ans);\n\tforeach (e; ans)\n\t{\n\t\twriteln(e[0]+1, \" \", e[1]+1);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tint[][] ans;\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA!int;\n\t\t\n\t\tdebug writeln(\"n:\", n);\n\t\tint cnt;\n\t\tint x = -1;\n\t\tforeach (i; 0..n*2)\n\t\t{\n\t\t\tif (cnt == n-1) break;\n\t\t\tif (a[i] % 2)\n\t\t\t{\n\t\t\t\tif (x == -1)\n\t\t\t\t{\n\t\t\t\t\tx = a[i];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans ~= [x, a[i]];\n\t\t\t\t\tx = -1;\n\t\t\t\t\t++cnt;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug writeln(\"ti:\", ti, \" cnt:\", cnt);\n\t\tx = -1;\n\t\tforeach (i; 0..n*2)\n\t\t{\n\t\t\tif (cnt == n-1) break;\n\t\t\tdebug writeln(\"i:\", i);\n\t\t\tif ((a[i] % 2) == 0)\n\t\t\t{\n\t\t\t\tif (x == -1)\n\t\t\t\t{\n\t\t\t\t\tx = a[i];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans ~= [x, a[i]];\n\t\t\t\t\tx = -1;\n\t\t\t\t\t++cnt;\n\t\t\t\t} \n\t\t\t}\n\t\t}\n\t\tdebug writeln(\"ti:\", ti, \" cnt:\", cnt);\n\t}\n\t\n\tdebug writeln(ans);\n\tforeach (e; ans)\n\t{\n\t\twriteln(e[0], \" \", e[1]);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "src_uid": "96fac9f9377bf03e144067bf93716d3d"} {"nl": {"description": "Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell. ", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.", "output_spec": "Print a single number — the maximum number of trees that you can cut down by the given rules.", "sample_inputs": ["5\n1 2\n2 1\n5 10\n10 9\n19 1", "5\n1 2\n2 1\n5 10\n10 9\n20 1"], "sample_outputs": ["3", "4"], "notes": "NoteIn the first sample you can fell the trees like that: fell the 1-st tree to the left — now it occupies segment [ - 1;1] fell the 2-nd tree to the right — now it occupies segment [2;3] leave the 3-rd tree — it occupies point 5 leave the 4-th tree — it occupies point 10 fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19]."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n int N;\n scanf(\"%d\", &N);\n\n auto X = new int[](N);\n auto H = new int[](N);\n foreach (i; 0..N) {\n scanf(\"%d %d\", &X[i], &H[i]);\n }\n\n if (N == 1) {\n writeln(1);\n return;\n }\n\n auto dp = new int[][](N, 3); // no cut, left, right\n dp[0][0] = 0;\n dp[0][1] = 1;\n dp[0][2] = (X[0] + H[0] < X[1]) ? 1 : 0;\n\n foreach (i; 1..N) {\n dp[i][0] = dp[i-1].reduce!(max);\n \n if (X[i-1] + H[i-1] < X[i] - H[i])\n dp[i][1] = dp[i-1][2] + 1;\n if (X[i-1] < X[i] - H[i])\n dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][1]) + 1);\n\n if (i == N-1)\n dp[i][2] = dp[i][0] + 1;\n else if (X[i] + H[i] < X[i+1])\n dp[i][2] = dp[i][0] + 1;\n }\n\n dp[N-1].reduce!(max).writeln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n int N;\n scanf(\"%d\", &N);\n\n auto X = new int[](N);\n auto H = new int[](N);\n foreach (i; 0..N) {\n scanf(\"%d %d\", &X[i], &H[i]);\n }\n\n if (N == 1) {\n writeln(H[0] + 1);\n return;\n }\n\n auto dp = new int[][](N, 3); // no cut, left, right\n dp[0][0] = 0;\n dp[0][1] = 1;\n dp[0][2] = (X[0] + H[0] < X[1]) ? 1 : 0;\n\n foreach (i; 1..N) {\n dp[i][0] = dp[i-1].reduce!(max);\n \n if (X[i-1] + H[i-1] < X[i] - H[i])\n dp[i][1] = dp[i-1][2] + 1;\n if (X[i-1] < X[i] - H[i])\n dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][1]) + 1);\n\n if (i == N-1)\n dp[i][2] = dp[i][0] + 1;\n else if (X[i] + H[i] < X[i+1])\n dp[i][2] = dp[i][0] + 1;\n }\n\n dp[N-1].reduce!(max).writeln;\n}\n"}], "src_uid": "a850dd88a67a6295823e70e2c5c857c9"} {"nl": {"description": "You are given an array of integers $$$a_1, a_2, \\ldots, a_n$$$ and an integer $$$x$$$.You need to select the maximum number of elements in the array, such that for every subsegment $$$a_l, a_{l + 1}, \\ldots, a_r$$$ containing strictly more than one element $$$(l < r)$$$, either: At least one element on this subsegment is not selected, or $$$a_l + a_{l+1} + \\ldots + a_r \\geq x \\cdot (r - l + 1)$$$. ", "input_spec": "The first line of input contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10$$$): the number of test cases. The descriptions of $$$t$$$ test cases follow, three lines per test case. In the first line you are given one integer $$$n$$$ ($$$1 \\leq n \\leq 50\\,000$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-100\\,000 \\leq a_i \\leq 100\\,000$$$). The third line contains one integer $$$x$$$ ($$$-100\\,000 \\leq x \\leq 100\\,000$$$).", "output_spec": "For each test case, print one integer: the maximum number of elements that you can select.", "sample_inputs": ["4\n5\n1 2 3 4 5\n2\n10\n2 4 2 4 2 4 2 4 2 4\n3\n3\n-10 -5 -10\n-8\n3\n9 9 -3\n5"], "sample_outputs": ["4\n8\n2\n2"], "notes": "NoteIn the first example, one valid way to select the elements is $$$[\\underline{1}, 2, \\underline{3}, \\underline{4}, \\underline{5}]$$$. All subsegments satisfy at least one of the criteria. For example, for the subsegment $$$l = 1$$$, $$$r = 2$$$ we have that the element $$$2$$$ is not selected, satisfying the first criterion. For the subsegment $$$l = 3$$$, $$$r = 5$$$ we have $$$3 + 4 + 5 = 12 \\ge 2 \\cdot 3$$$, satisfying the second criterion.We can't select all elements, because in this case for $$$l = 1$$$, $$$r = 2$$$ all elements are selected and we have $$$a_1 + a_2 = 3 < 2 \\cdot 2$$$. Thus, the maximum number of selected elements is $$$4$$$.In the second example, one valid solution is $$$[\\underline{2}, \\underline{4}, 2, \\underline{4}, \\underline{2}, \\underline{4}, 2, \\underline{4}, \\underline{2}, \\underline{4}]$$$.In the third example, one valid solution is $$$[\\underline{-10}, -5, \\underline{-10}]$$$.In the fourth example, one valid solution is $$$[\\underline{9}, \\underline{9}, -3]$$$."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong;\n }\n const X = readLong;\n \n auto B = new long[N + 1];\n foreach (i; 0 .. N) {\n B[i + 1] = B[i] + (A[i] - X);\n }\n debug {\n writeln(\"B = \", B);\n }\n \n auto dp = new int[][](N + 2, 2);\n foreach (i; 0 .. N + 2) {\n dp[i][] = -1;\n }\n dp[0][0] = 0;\n foreach (i; 0 .. N + 2) foreach (s; 0 .. 2) if (dp[i][s] >= 0) {\n // cut\n if (i + 1 <= N + 1) {\n chmax(dp[i + 1][0], dp[i][s]);\n }\n if (i + 2 <= N + 1 && B[i] > B[i + 1]) {\n chmax(dp[i + 2][1], dp[i][s] + 1);\n }\n // 1\n if (i + 1 <= N + 1) {\n if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i]) {\n chmax(dp[i + 1][0], dp[i][s] + 1);\n }\n }\n // 2\n if (i + 2 <= N + 1 && B[i] > B[i + 1]) {\n if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i + 1]) {\n chmax(dp[i + 2][1], dp[i][s] + 2);\n }\n }\n }\n \n const ans = dp[$ - 1].maxElement;\n writeln(ans - 1);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto x = readln.strip.to !(int);\r\n\r\n\t\tauto f = new int [4] [n + 1];\r\n\t\tf[0][0] = 0;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tf[i + 1][0] = f[i][].maxElement;\r\n\t\t\tf[i + 1][1] = f[i][0] + 1;\r\n\t\t\tif (i >= 1 &&\r\n\t\t\t a[i] + a[i - 1] >= x * 2)\r\n\t\t\t{\r\n\t\t\t\tf[i + 1][2] = f[i][1] + 1;\r\n\t\t\t\tif (i >= 2 &&\r\n\t\t\t\t a[i] + a[i - 1] + a[i - 2] >= x * 3)\r\n\t\t\t\t{\r\n\t\t\t\t\tf[i + 1][3] =\r\n\t\t\t\t\t f[i][2..$].maxElement + 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tf[n][].maxElement.writeln;\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong;\n }\n const X = readLong;\n \n auto B = new long[N + 1];\n foreach (i; 0 .. N) {\n B[i + 1] = B[i] + (A[i] - X);\n }\n debug {\n writeln(\"B = \", B);\n }\n \n auto dp = new int[][](N + 2, 2);\n foreach (i; 0 .. N + 2) {\n dp[i][] = -1;\n }\n dp[0][0] = 0;\n foreach (i; 0 .. N + 2) foreach (s; 0 .. 2) if (dp[i][s] >= 0) {\n // cut\n if (i + 1 <= N + 1) {\n chmax(dp[i + 1][0], dp[i][s]);\n }\n if (i + 2 <= N + 1 && B[i] > B[i + 1]) {\n chmax(dp[i + 2][0], dp[i][s] + 1);\n }\n // 1\n if (i + 1 <= N + 1) {\n if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i]) {\n chmax(dp[i + 1][0], dp[i][s] + 1);\n }\n }\n // 2\n if (i + 2 <= N + 1 && B[i] > B[i + 1]) {\n if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i + 1]) {\n chmax(dp[i + 2][1], dp[i][s] + 2);\n }\n }\n }\n \n const ans = dp[$ - 1].maxElement;\n writeln(ans - 1);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong;\n }\n const X = readLong;\n \n auto B = new long[N + 1];\n foreach (i; 0 .. N) {\n B[i + 1] = B[i] + (A[i] - X);\n }\n debug {\n writeln(\"B = \", B);\n }\n \n auto dp = new int[][](N + 2, 2);\n foreach (i; 0 .. N + 2) {\n dp[i][] = -1;\n }\n dp[0][0] = 0;\n foreach (i; 0 .. N + 2) foreach (s; 0 .. 2) if (dp[i][s] >= 0) {\n // cut\n if (i + 1 <= N + 1) {\n chmax(dp[i + 1][0], dp[i][s]);\n }\n // 1\n if (i + 1 <= N + 1) {\n if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i]) {\n chmax(dp[i + 1][0], dp[i][s] + 1);\n }\n }\n // 2\n if (i + 2 <= N + 1 && B[i] > B[i + 1]) {\n if (i == 0 || B[s ? (i - 2) : (i - 1)] <= B[i + 1]) {\n chmax(dp[i + 2][1], dp[i][s] + 2);\n }\n }\n }\n \n const ans = dp[$ - 1].maxElement;\n writeln(ans - 1);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto x = readln.strip.to !(int);\r\n\r\n\t\tauto f = new int [4] [n + 1];\r\n\t\tf[0][0] = 0;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tf[i + 1][0] = f[i][].maxElement;\r\n\t\t\tf[i + 1][1] = f[i][0] + 1;\r\n\t\t\tif (i >= 1 &&\r\n\t\t\t a[i] + a[i - 1] >= x * 2)\r\n\t\t\t{\r\n\t\t\t\tf[i + 1][2] = f[i][1] + 1;\r\n\t\t\t\tif (i >= 2 &&\r\n\t\t\t\t a[i] + a[i - 1] + a[i - 2] >= x * 3)\r\n\t\t\t\t{\r\n\t\t\t\t\tf[i + 1][3] = f[i][2] + 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tf[n][].maxElement.writeln;\r\n\t}\r\n}\r\n"}], "src_uid": "79ecf771f4a54c2c9f988e069f7bfceb"} {"nl": {"description": "We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 6000$$$) — the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \\le n, k \\le 10^6$$$) — the initial position of point $$$A$$$ and desirable absolute difference.", "output_spec": "For each test case, print the minimum number of steps to make point $$$B$$$ exist.", "sample_inputs": ["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"], "sample_outputs": ["0\n3\n1000000\n0\n1\n0"], "notes": "NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$. "}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string[] tk;\n string readString() {\n while (tk.empty) \n tk = readln.split;\n auto tkt = tk.front;\n tk.popFront;\n return tkt;\n }\n int readInt() { \n return readString.to!int; \n }\n double readDouble() { \n return readString.to!double; \n }\n}\n\nvoid main() {\n IO cin;\n int t = 1;\n t = cin.readInt;\n while (t--) {\n int n = cin.readInt;\n int k = cin.readInt;\n if (k == 0) {\n if (n % 2 == 0) writeln(0);\n else writeln(1);\n } else {\n if (k > n) writeln(k - n);\n else {\n if (n % 2 == 0 && k % 2 == 1) writeln(1);\n else if (n % 2 == 1 && k % 2 == 0) writeln(1);\n else writeln(0);\n }\n } \n } \n}"}, {"source_code": "// unihernandez22\n// https://codeforces.com/contest/1401/problem/A\n// implementation\n\nimport std.stdio;\n\nvoid main() {\n int n, k, t;\n scanf(\"%d\", &t);\n foreach (_; 0 .. t) {\n scanf(\"%d %d\", &n, &k);\n if (k <= n) {\n if (n%2 == k%2) {\n writeln(\"0\");\n } else {\n writeln(\"1\");\n }\n } else\n writeln(k - n);\n }\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\twriteln (k > n ? k - n : ((n ^ k) & 1));\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tif (k >= n)\n\t\t{\n\t\t\tans[ti] = k - n;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tn -= k;\n\t\t\tans[ti] = n % 2;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string[] tk;\n string readString() {\n while (tk.empty) \n tk = readln.split;\n auto tkt = tk.front;\n tk.popFront;\n return tkt;\n }\n int readInt() { \n return readString.to!int; \n }\n double readDouble() { \n return readString.to!double; \n }\n}\n\nvoid main() {\n IO cin;\n int t = 1;\n t = cin.readInt;\n while (t--) {\n int n = cin.readInt;\n int k = cin.readInt;\n if (n > k) {\n if (n % 2 == 0) writeln(0);\n else writeln(1);\n } else {\n writeln(k - n);\n }\n } \n}"}], "src_uid": "f98d1d426f68241bad437eb221f617f4"} {"nl": {"description": "Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it \"Mishka's Adjacent Replacements Algorithm\". This algorithm can be represented as a sequence of steps: Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; $$$\\dots$$$ Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \\in\\{1, 2, \\ldots, 5 \\cdot 10^8\\}$$$ as described above.For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ $$$\\dots$$$ $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array.Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.", "input_spec": "The first line of the input contains one integer number $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the elements of the array.", "output_spec": "Print $$$n$$$ integers — $$$b_1, b_2, \\dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying \"Mishka's Adjacent Replacements Algorithm\" to the array $$$a$$$. Note that you cannot change the order of elements in the array.", "sample_inputs": ["5\n1 2 4 5 10", "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000"], "sample_outputs": ["1 1 3 5 9", "9999 9 50605065 1 5 89 5 999999999 60506055 999999999"], "notes": "NoteThe first example is described in the problem statement."}, "positive_code": [{"source_code": "module acmd;\n\nimport std.stdio;\nimport std.conv;\n\nint main () {\n version (offline_judje) {\n stdin.reopen (\"input.txt\", \"rt\");\n }\n int n;\n readf!\" %d\" (n);\n foreach (i; 0 .. n) {\n int num;\n readf!\" %d\" (num);\n if (num % 2 == 0)\n num--;\n writef!\"%d \" (num);\n }\n\n return 0;\n}\n\n"}, {"source_code": "import std.algorithm, std.conv, std.stdio;\n\nvoid main()\n{\n int n;\n readf !\"%s\\n\" (n);\n auto arr = readln.splitter.map!(to!int).map!(x => x-(1 - x%2));\n writefln(\"%-(%d %)\", arr);\n}"}, {"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.algorithm;\n\nvoid main()\n{\n auto n = readln.strip.to!ulong;\n auto a = readln.strip.split();\n foreach (i, v; a)\n {\n auto va = v.to!ulong;\n if (va % 2 == 1)\n write(va, \" \");\n else\n write(va - 1, \" \");\n }\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n arr = arr.map!(x => x % 2 == 0 ? x - 1 : x).array;\n \n arr.writefln!(\"%(%s %)\");\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\nvoid main(){\n auto n = readln.strip.to!ulong;\n auto s = readln.strip.split();\n foreach (i, k; s){\n auto ans = k.to!ulong;\n if (ans % 2) write(ans, \" \");\n else write(ans - 1, \" \");\n }\n}"}, {"source_code": "import std.stdio;\nint main()\n{\n\tint a=0;\n\treadf(\" %d\", &a);\n\tint[] m = new int[a];\n\tfor (int i=0; i 0)\n\t{\n\t\tauto wall = new bool [] [] (2, n + 2);\n\t\tint block = 0;\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf !(\" %s %s\") (u, v);\n\t\t\tu -= 1;\n\t\t\tint mult = wall[u][v] ? -1 : +1;\n\t\t\tforeach (k; -1..+2)\n\t\t\t{\n\t\t\t\tblock += wall[u ^ 1][v + k] * mult;\n\t\t\t}\n\t\t\twall[u][v] ^= true;\n\t\t\twriteln (block == 0 ? \"Yes\" : \"No\");\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint, q = rint;\n\n\tbool[][] xf = new bool[][](2, n);\n\tint cnt;\n\n\tforeach(_; 0 .. q){\n\t\tint r = rint - 1, c = rint - 1;\n\t\tif(xf[r][c]){\n\t\t\txf[r][c] = 0;\n\t\t\tif(c > 0 && xf[1 - r][c - 1]) cnt -= 1;\n\t\t\tif(xf[1 - r][c]) cnt -= 1;\n\t\t\tif(c < n - 1 && xf[1 - r][c + 1]) cnt -= 1;\n\t\t}\n\t\telse{\n\t\t\txf[r][c] = 1;\n\t\t\tif(c > 0 && xf[1 - r][c - 1]) cnt += 1;\n\t\t\tif(xf[1 - r][c]) cnt += 1;\n\t\t\tif(c < n - 1 && xf[1 - r][c + 1]) cnt += 1;\n\t\t}\n\t\tif(cnt > 0) \"No\".writeln;\n\t\telse \"Yes\".writeln;\n\t}\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const Q = readInt();\n auto R = new int[Q];\n auto C = new int[Q];\n foreach (q; 0 .. Q) {\n R[q] = readInt() - 1;\n C[q] = readInt() - 1;\n }\n \n auto can = new bool[][](2, N);\n foreach (x; 0 .. 2) {\n can[x][] = true;\n }\n \n auto vals = new int[N - 1];\n vals[] = 1;\n int now = N - 1;\n \n void check(int y) {\n if (0 <= y && y < N - 1) {\n now -= vals[y];\n vals[y] = (can[0][y] && can[0][y + 1] || can[1][y] && can[1][y + 1]) ? 1 : 0;\n now += vals[y];\n }\n }\n \n foreach (q; 0 .. Q) {\n can[R[q]][C[q]] = !can[R[q]][C[q]];\n check(C[q] - 1);\n check(C[q]);\n writeln((now == N - 1) ? \"Yes\" : \"No\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, q;\n readf(\"%s %s\", &n, &q);\n readln;\n \n auto board = new bool[][] (2, n);\n \n alias t = Tuple!(int, int);\n auto vert = make!(RedBlackTree!int);\n auto cross = make!(RedBlackTree!t);\n \n foreach (_; 0 .. q) {\n int r, c;\n readf(\"%s %s\", &r, &c);\n readln;\n \n --r, --c;\n board[r][c] = !board[r][c];\n \n if (board[r][c]) {\n if (board[1 - r][c]) { vert.insert(c); }\n \n if (c > 0 && board[1-r][c-1]) { cross.insert(tuple(1-r, c-1)); }\n if (c < n-1 && board[1-r][c+1]) { cross.insert(tuple(r, c)); }\n } else {\n if (c in vert) { vert.removeKey(c); }\n \n if (c > 0 && tuple(1-r, c-1) in cross) { cross.removeKey(tuple(1-r, c-1)); }\n if (c < n-1 && tuple(r, c) in cross) { cross.removeKey(tuple(r, c)); }\n }\n \n writeln(vert.empty() && cross.empty() ? \"Yes\" : \"No\");\n }\n}"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\n\nDList!string _words;\n\nvoid read(T...)(ref T t)\n{\n void singleRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n static foreach(i; 0 .. T.length)\n {\n static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))\n {\n foreach(ref element; t[i])\n read(element);\n }\n else\n {\n singleRead(t[i]);\n }\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value = E.init)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\nvoid main()\n{\n auto n = next!long, q = next!long;\n auto state = new bool[][2];\n foreach(ref row; state)\n row = new bool[n.ind];\n auto conns = long(0);\n foreach(i; 0 .. q)\n {\n auto r = next!long - 1, c = next!long - 1;\n state[r.ind][c.ind] = !state[r.ind][c.ind];\n if (state[r.ind][c.ind])\n {\n foreach(oc; c - 1 .. c + 2)\n if (oc >= 0 && oc < n)\n if (state[(r^1).ind][oc.ind])\n conns++;\n }\n else\n {\n foreach(oc; c - 1 .. c + 2)\n if (oc >= 0 && oc < n)\n if (state[(r^1).ind][oc.ind])\n conns--;\n }\n if (conns > 0 || state[0][0] || state[1][$ - 1])\n writeln(\"No\");\n else\n writeln(\"Yes\");\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto q = RD!int;\n\tauto ans = new bool[](q);\n\tauto cell = new bool[][](2, n);\n\tbool[int] set;\n\tforeach (i; 0..q)\n\t{\n\t\tauto r = RD!int-1;\n\t\tauto c = RD!int-1;\n\t\tcell[r][c] = !cell[r][c];\n\t\tdebug writeln(cell[0]);\n\t\tdebug writeln(cell[1]);\n\t\tif (cell[r][c])\n\t\t{\n\t\t\tbool ok = i == 0 ? true : ans[i-1];\n\t\t\tforeach (j; max(0, c-1)..min(n, c+2))\n\t\t\t{\n\t\t\t\tif (cell[r^1][j])\n\t\t\t\t{\n\t\t\t\t\tok = false;\n\t\t\t\t\tset[r*n+c] = true; \n\t\t\t\t}\n\t\t\t}\n\t\t\tans[i] = ok;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (set.get(r*n+c, false))\n\t\t\t\tset.remove(r*n+c);\n\n\t\t\tforeach (j; max(0, c-1)..min(n, c+2))\n\t\t\t{\n\t\t\t\tif (!cell[r^1][j]) continue;\n\t\t\t\tif (set.get((r^1)*n+j, false) == false) continue;\n\t\t\t\tbool ok = true;\n\t\t\t\tforeach (k; max(0, j-1)..min(n, j+2))\n\t\t\t\t{\n\t\t\t\t\tif (cell[r][k])\n\t\t\t\t\t\tok = false;\n\t\t\t\t}\n\t\t\t\tif (ok)\n\t\t\t\t\tset.remove((r^1)*n+j);\n\t\t\t}\n\t\t\tans[i] = set.length == 0;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e ? \"Yes\" : \"No\");\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "af036416721694d1843368988ca78e8e"} {"nl": {"description": "This is an interactive problem.Khanh has $$$n$$$ points on the Cartesian plane, denoted by $$$a_1, a_2, \\ldots, a_n$$$. All points' coordinates are integers between $$$-10^9$$$ and $$$10^9$$$, inclusive. No three points are collinear. He says that these points are vertices of a convex polygon; in other words, there exists a permutation $$$p_1, p_2, \\ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$ such that the polygon $$$a_{p_1} a_{p_2} \\ldots a_{p_n}$$$ is convex and vertices are listed in counter-clockwise order.Khanh gives you the number $$$n$$$, but hides the coordinates of his points. Your task is to guess the above permutation by asking multiple queries. In each query, you give Khanh $$$4$$$ integers $$$t$$$, $$$i$$$, $$$j$$$, $$$k$$$; where either $$$t = 1$$$ or $$$t = 2$$$; and $$$i$$$, $$$j$$$, $$$k$$$ are three distinct indices from $$$1$$$ to $$$n$$$, inclusive. In response, Khanh tells you: if $$$t = 1$$$, the area of the triangle $$$a_ia_ja_k$$$ multiplied by $$$2$$$. if $$$t = 2$$$, the sign of the cross product of two vectors $$$\\overrightarrow{a_ia_j}$$$ and $$$\\overrightarrow{a_ia_k}$$$. Recall that the cross product of vector $$$\\overrightarrow{a} = (x_a, y_a)$$$ and vector $$$\\overrightarrow{b} = (x_b, y_b)$$$ is the integer $$$x_a \\cdot y_b - x_b \\cdot y_a$$$. The sign of a number is $$$1$$$ it it is positive, and $$$-1$$$ otherwise. It can be proven that the cross product obtained in the above queries can not be $$$0$$$.You can ask at most $$$3 \\cdot n$$$ queries.Please note that Khanh fixes the coordinates of his points and does not change it while answering your queries. You do not need to guess the coordinates. In your permutation $$$a_{p_1}a_{p_2}\\ldots a_{p_n}$$$, $$$p_1$$$ should be equal to $$$1$$$ and the indices of vertices should be listed in counter-clockwise order.", "input_spec": null, "output_spec": null, "sample_inputs": ["6\n\n15\n\n-1\n\n1"], "sample_outputs": ["1 1 4 6\n\n2 1 5 6\n\n2 2 1 4\n\n0 1 3 4 2 6 5"], "notes": "NoteThe image below shows the hidden polygon in the example: The interaction in the example goes as below: Contestant reads $$$n = 6$$$. Contestant asks a query with $$$t = 1$$$, $$$i = 1$$$, $$$j = 4$$$, $$$k = 6$$$. Jury answers $$$15$$$. The area of the triangle $$$A_1A_4A_6$$$ is $$$7.5$$$. Note that the answer is two times the area of the triangle. Contestant asks a query with $$$t = 2$$$, $$$i = 1$$$, $$$j = 5$$$, $$$k = 6$$$. Jury answers $$$-1$$$. The cross product of $$$\\overrightarrow{A_1A_5} = (2, 2)$$$ and $$$\\overrightarrow{A_1A_6} = (4, 1)$$$ is $$$-2$$$. The sign of $$$-2$$$ is $$$-1$$$. Contestant asks a query with $$$t = 2$$$, $$$i = 2$$$, $$$j = 1$$$, $$$k = 4$$$. Jury answers $$$1$$$. The cross product of $$$\\overrightarrow{A_2A_1} = (-5, 2)$$$ and $$$\\overrightarrow{A_2A_4} = (-2, -1)$$$ is $$$1$$$. The sign of $$$1$$$ is $$$1$$$. Contestant says that the permutation is $$$(1, 3, 4, 2, 6, 5)$$$. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nlong askArea (int i, int j, int k)\n{\n\twriteln (1, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n\tstdout.flush ();\n\tauto res = readln.strip.to !(long);\n\treturn res;\n}\n\nint askSign (int i, int j, int k)\n{\n\twriteln (2, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n\tstdout.flush ();\n\tauto res = readln.strip.to !(int);\n\treturn res;\n}\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\n\t\tint i = 0;\n\t\tint j = 1;\n\t\tforeach (k; 2..n)\n\t\t{\n\t\t\tif (askSign (i, j, k) < 0)\n\t\t\t{\n\t\t\t\tj = k;\n\t\t\t}\n\t\t}\n\n\t\talias Pair = Tuple !(long, q{area}, int, q{num});\n\t\tPair [] p;\n\t\tforeach (k; 1..n)\n\t\t{\n\t\t\tif (k != j)\n\t\t\t{\n\t\t\t\tauto cur = askArea (i, j, k);\n\t\t\t\tp ~= Pair (cur, k);\n\t\t\t}\n\t\t}\n\t\tsort (p);\n\n\t\tint [] left = [];\n\t\tint [] right = [i, j];\n\t\tforeach (const ref c; p)\n\t\t{\n\t\t\tauto num = c.num;\n\t\t\tif (askSign (right[$ - 2], right[$ - 1], num) < 0)\n\t\t\t{\n\t\t\t\tleft ~= right[$ - 1];\n\t\t\t\tright.length -= 1;\n\t\t\t\tright.assumeSafeAppend ();\n\t\t\t}\n\t\t\tright ~= num;\n\t\t}\n\n\t\tauto ans = right ~ left.retro.array;\n\t\tans[] += 1;\n\t\twritefln (\"0 %(%s %)\", ans);\n\t\tstdout.flush ();\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\t\n\tX[] as, bs;\n\tforeach(i; 2 .. n){\n\t\tif(ask(2, 0, 1, i) < 0) as ~= X(i, ask(1, 0, 1, i));\n\t\telse bs ~= X(i, ask(1, 1, 0, i));\n\t}\n\t\n\tX az = X(-1, -1), bz = X(-1, -1);\n\tforeach(a; as) if(a.v > az.v) az = a;\n\tforeach(b; bs) if(b.v > bz.v) bz = b;\n\t\n\tX[] a0s, a1s, b0s, b1s;\n\tforeach(a; as){\n\t\tif(a.u == az.u) continue;\n\t\tif(ask(2, 0, az.u, a.u) < 0) a0s ~= a;\n\t\telse a1s ~= a;\n\t}\n\tforeach(b; bs){\n\t\tif(b.u == bz.u) continue;\n\t\tif(ask(2, 1, bz.u, b.u) < 0) b0s ~= b;\n\t\telse b1s ~= b;\n\t}\n\ta0s.sort!\"a.vb.v\"();\n\tb0s.sort!\"a.vb.v\"();\n\t\n\tX[] ans = [X(0, 0)] ~ a0s;\n\tif(az.u >= 0) ans ~= az;\n\tans ~= a1s ~ [X(1, 0)] ~ b0s;\n\tif(bz.u >= 0) ans ~= bz;\n\tans ~= b1s;\n\twriteln(\"0 \", ans.map!(x => x.u + 1).map!(to!string).array.join(\" \"));\n}\n\nstruct X{\n\tint u;\n\tlong v;\n}\n\nlong ask(int t, int i, int j, int k){\n\twriteln(t, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n\tstdout.flush;\n\treturn rlong;\n}\n\n \n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\n\ndebug {\n long[] X, Y;\n}\n\nlong Ask(int t, int i, int j, int k) {\n long ret;\n debug {\n ret = (X[j] - X[i]) * (Y[k] - Y[i]) - (Y[j] - Y[i]) * (X[k] - X[i]);\n if (t == 1) {\n ret = abs(ret);\n } else {\n ret = sgn(ret);\n }\n } else {\n writeln(t, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n stdout.flush;\n ret = readLong();\n }\n return ret;\n}\n\nvoid main() {\n N = readInt();\n debug {\n X = new long[N];\n Y = new long[N];\n foreach (i; 0 .. N) {\n X[i] = readLong();\n Y[i] = readLong();\n }\n }\n \n auto as = new long[N];\n foreach (i; 2 .. N) {\n as[i] = Ask(1, 0, 1, i);\n as[i] *= Ask(2, 0, 1, i);\n }\n debug {\n writeln(\"as = \", as);\n }\n \n int[] ans;\n ans ~= 0;\n {\n int im;\n foreach (i; 2 .. N) {\n if (as[im] > as[i]) {\n im = i;\n }\n }\n if (im != 0) {\n int[] ims, ips;\n foreach (i; 2 .. N) {\n if (as[i] < 0 && i != im) {\n const res = Ask(2, 0, im, i);\n (res < 0) ? (ims ~= i) : (ips ~= i);\n }\n }\n ims.sort!((i, j) => (as[i] > as[j]));\n ips.sort!((i, j) => (as[i] < as[j]));\n foreach (i; ims) ans ~= i;\n ans ~= im;\n foreach (i; ips) ans ~= i;\n }\n }\n ans ~= 1;\n {\n int im;\n foreach (i; 2 .. N) {\n if (as[im] < as[i]) {\n im = i;\n }\n }\n if (im != 0) {\n int[] ims, ips;\n foreach (i; 2 .. N) {\n if (as[i] > 0 && i != im) {\n const res = Ask(2, 0, im, i);\n (res < 0) ? (ims ~= i) : (ips ~= i);\n }\n }\n ims.sort!((i, j) => (as[i] < as[j]));\n ips.sort!((i, j) => (as[i] > as[j]));\n foreach (i; ims) ans ~= i;\n ans ~= im;\n foreach (i; ips) ans ~= i;\n }\n }\n \n write(0);\n foreach (i; ans) {\n write(\" \", i + 1);\n }\n writeln();\n stdout.flush;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\t\n\tX[] as, bs;\n\tforeach(i; 2 .. n){\n\t\tif(ask(2, 0, 1, i) < 0) as ~= X(i, ask(1, 0, 1, i));\n\t\telse bs ~= X(i, ask(1, 1, 0, i));\n\t}\n\t\n\tX az = X(-1, -1), bz = X(-1, -1);\n\tforeach(a; as) if(a.v > az.v) az = a;\n\tforeach(b; bs) if(b.v > bz.v) bz = b;\n\t\n\tX[] a0s, a1s, b0s, b1s;\n\tforeach(a; as){\n\t\tif(a.u == az.u || ask(2, 0, az.u, a.u) < 0) a0s ~= a;\n\t\telse a1s ~= a;\n\t}\n\tforeach(b; bs){\n\t\tif(b.u == bz.u || ask(2, 1, bz.u, b.u) < 0) b0s ~= b;\n\t\telse b1s ~= b;\n\t}\n\ta0s.sort!\"a.vb.v\"();\n\tb0s.sort!\"a.vb.v\"();\n\t\n\tX[] ans = [X(0, 0)] ~ a0s ~ a1s ~ [X(1, 0)] ~ b0s ~ b1s;\n\twriteln(\"0 \", ans.map!(x => x.u + 1).map!(to!string).array.join(\" \"));\n}\n\nstruct X{\n\tint u;\n\tlong v;\n}\n\nlong ask(int t, int i, int j, int k){\n\twriteln(t, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n\tstdout.flush;\n\treturn rlong;\n}\n\n \n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\tint p = uniform(0, n);\n\tint q = uniform(0, n);\n\tif(p == q) q += 1, q %= n;\n\t\n\tint[] as, bs;\n\tforeach(i; 0 .. n){\n\t\tif(i == p || i == q) continue;\n\t\tlong x = ask(2, p, q, i);\n\t\tif(x < 0) as ~= i;\n\t\telse bs ~= i;\n\t}\n\tint[] ans = [p] ~ calc(as, p, q) ~ [q] ~ calc(bs, q, p);\n\t\n\tint i0;\n\twhile(ans[i0] != 0) i0 += 1;\n\t\n\tans = ans[i0 .. $] ~ ans[0 .. i0];\n\tans.map!(x => x + 1).map!(to!string).array.join(\" \").writeln;\n}\n\nint[] calc(int[] us, int p, int q){\n\tif(us.length <= 1) return us;\n\tint[] as, bs;\n\tint r = 0;\n\tlong highest = 0;\n\tforeach(u; us){\n\t\tlong w = ask(1, p, q, u);\n\t\tif(w > highest) r = u, highest = w;\n\t}\n\tforeach(u; us){\n\t\tif(r == u) continue;\n\t\tlong x = ask(2, p, r, u);\n\t\tif(x < 0) as ~= u;\n\t\telse bs ~= u;\n\t}\n\treturn calc(as, p, r) ~ [r] ~ calc(bs, r, q);\n}\n\nlong ask(int t, int i, int j, int k){\n\twriteln(t, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n\tstdout.flush;\n\treturn rlong;\n}\n\n\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\t\n\tX[] as, bs;\n\tX az = X(-1, -1), bz = X(-1, -1);\n\tforeach(i; 2 .. n){\n\t\tif(ask(2, 0, 1, i) < 0){\n\t\t\tX a = X(i, ask(1, 0, 1, i));\n\t\t\tas ~= a;\n\t\t\tif(az.v < a.v) az = a;\n\t\t}\n\t\telse{\n\t\t\tX b = X(i, ask(1, 1, 0, i));\n\t\t\tbs ~= b;\n\t\t\tif(bz.v < b.v) bz = b;\n\t\t}\n\t}\n\tX[] a0s, a1s, b0s, b1s;\n\tforeach(a; as){\n\t\tif(a.u == az.u) continue;\n\t\tif(ask(2, 0, az.u, a.u) < 0) a0s ~= a;\n\t\telse a1s ~= a;\n\t}\n\tforeach(b; bs){\n\t\tif(b.u == bz.u) continue;\n\t\tif(ask(2, 1, bz.u, b.u) < 0) b0s ~= b;\n\t\telse b1s ~= b;\n\t}\n\ta0s.sort!\"a.vb.v\"();\n\tb0s.sort!\"a.vb.v\"();\n\t\n\tX[] ans = [X(0, 0)] ~ a0s ~ az ~ a1s ~ [X(1, 0)] ~ b0s ~ bz ~ b1s;\n\twriteln(\"0 \", ans.map!(x => x.u + 1).map!(to!string).array.join(\" \"));\n}\n\nstruct X{\n\tint u;\n\tlong v;\n}\n\nlong ask(int t, int i, int j, int k){\n\twriteln(t, \" \", i + 1, \" \", j + 1, \" \", k + 1);\n\tstdout.flush;\n\treturn rlong;\n}\n\n \n"}], "src_uid": "6c0ed9fe0a104fc9380c199003a22e90"} {"nl": {"description": "You are given a tree with n nodes (numbered from 1 to n) rooted at node 1. Also, each node has two values associated with it. The values for i-th node are ai and bi.You can jump from a node to any node in its subtree. The cost of one jump from node x to node y is the product of ax and by. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 1.Note that you cannot jump from a node to itself.", "input_spec": "The first line of input contains an integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. The second line contains n space-separated integers a1,  a2,  ...,  an( - 105  ≤  ai  ≤  105). The third line contains n space-separated integers b1,  b2,  ...,  bn( - 105  ≤  bi  ≤  105). Next n  -  1 lines contains two space-separated integers ui and vi (1 ≤ ui,  vi ≤  n) describing edge between nodes ui and vi in the tree.", "output_spec": "Output n space-separated integers, i-th of which denotes the minimum cost of a path from node i to reach any leaf.", "sample_inputs": ["3\n2 10 -1\n7 -7 5\n2 3\n2 1", "4\n5 -10 5 7\n-8 -80 -3 -10\n2 1\n2 4\n1 3"], "sample_outputs": ["10 50 0", "-300 100 0 0"], "notes": "NoteIn the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost a2 × b3 = 50. For node 1, jump directly to node 3 with cost a1 × b3 = 10.In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost a2 × b4 = 100. For node 1, jump to node 2 with cost a1 × b2 =  - 400 followed by a jump from 2 to 4 with cost a2 × b4 = 100."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n// class Func = TX -> TY\n// for any f, g: Func, x -> sgn(g(x) - f(x)) must be monotone\nclass LiChaoTree(Func) {\n import std.algorithm : swap;\n alias TX = Func.TX;\n alias TY = Func.TY;\n const(TX) L, R;\n class Tree {\n Tree l, r;\n Func f;\n this(Func f) {\n this.f = f;\n }\n }\n Tree root;\n\n // [L, R)\n this(TX L, TX R) {\n assert(L <= R, \"LiChaoTree: L <= R must hold\");\n this.L = L;\n this.R = R;\n }\n // Add g to the whole [L, R)\n void add(Func g) {\n root = add(root, L, R, g);\n }\n // Add g to [a, b)\n void add(TX a, TX b, Func g) {\n root = add(root, L, R, a, b, g);\n }\n // Get max at a\n TY opCall(TX a) {\n TY mx = TY.min;\n TX l = L, r = R;\n for (Tree u = root; u; ) {\n if (u.f) {\n const fX = u.f(a);\n if (mx < fX) mx = fX;\n }\n const mid = l + (r - l) / 2;\n if (a < mid) {\n u = u.l; r = mid;\n } else {\n u = u.r; l = mid;\n }\n }\n return mx;\n }\n\n private:\n Tree add(Tree u0, TX l, TX r, Func g) {\n if (!u0) return new Tree(g);\n TY gL = g(l), gR = g(r);\n for (Tree u = u0; ; ) {\n TY fL = u.f ? u.f(l) : TY.min, fR = u.f ? u.f(r) : TY.min;\n if (fL >= gL && fR >= gR) return u0;\n if (fL <= gL && fR <= gR) { u.f = g; return u0; }\n const mid = l + (r - l) / 2;\n TY fMid = u.f(mid), gMid = g(mid);\n if (fMid < gMid) {\n swap(u.f, g);\n if (!g) return u0;\n if (gL < fL) {\n if (!u.l) { u.l = new Tree(g); return u0; }\n u = u.l; r = mid; gL = fL; gR = fMid;\n } else {\n if (!u.r) { u.r = new Tree(g); return u0; }\n u = u.r; l = mid; gL = fMid; gR = fR;\n }\n } else {\n if (fL < gL) {\n if (!u.l) { u.l = new Tree(g); return u0; }\n u = u.l; r = mid; gR = gMid;\n } else {\n if (!u.r) { u.r = new Tree(g); return u0; }\n u = u.r; l = mid; gL = gMid;\n }\n }\n }\n }\n Tree add(Tree u, TX l, TX r, TX a, TX b, Func g) {\n if (b <= l || r <= a) return u;\n if (a <= l && r <= b) return add(u, l, r, g);\n if (u && u.f && u.f(l) >= g(l) && u.f(r) >= g(r)) return u;\n if (!u) u = new Tree(null);\n const mid = l + (r - l) / 2;\n u.l = add(u.l, l, mid, a, b, g);\n u.r = add(u.r, mid, r, a, b, g);\n return u;\n }\n}\n\n// y = p x + q\nclass Line {\n alias TX = long;\n alias TY = long;\n long p, q;\n this(long p, long q) {\n this.p = p;\n this.q = q;\n }\n TY opCall(TX x) {\n return p * x + q;\n }\n}\n\n\nenum INF = 10L^^18;\nenum LIM = 10^^5 + 10;\n\nint N;\nlong[] A, B;\nint[] U, V;\n\nint[][] G;\nlong[] dp;\nLine[][] liness;\nLiChaoTree!Line[] lcts;\n\nvoid dfs(int u, int p) {\n liness[u] = [];\n lcts[u] = new LiChaoTree!Line(-LIM, LIM);\n foreach (i; G[u]) {\n const v = U[i] ^ V[i] ^ u;\n if (v != p) {\n dfs(v, u);\n if (liness[u].length < liness[v].length) {\n swap(liness[u], liness[v]);\n swap(lcts[u], lcts[v]);\n }\n liness[u] ~= liness[v];\n foreach (l; liness[v]) {\n lcts[u].add(l);\n }\n }\n }\n if (liness[u]) {\n dp[u] = -lcts[u](A[u]);\n } else {\n // leaf\n dp[u] = 0;\n }\n auto l = new Line(-B[u], -dp[u]);\n liness[u] ~= l;\n lcts[u].add(l);\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n A = new long[N];\n foreach (u; 0 .. N) {\n A[u] = readLong();\n }\n B = new long[N];\n foreach (u; 0 .. N) {\n B[u] = readLong();\n }\n U = new int[N - 1];\n V = new int[N - 1];\n foreach (i; 0 .. N - 1) {\n U[i] = readInt() - 1;\n V[i] = readInt() - 1;\n }\n \n G = new int[][N];\n foreach (i; 0 .. N - 1) {\n G[U[i]] ~= i;\n G[V[i]] ~= i;\n }\n dp.length = N;\n liness.length = N;\n lcts.length = N;\n dfs(0, -1);\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(dp[u]);\n }\n writeln();\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "8f511d06d2c28817b80f56d1153da805"} {"nl": {"description": "You are given two circles. Find the area of their intersection.", "input_spec": "The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle. The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.", "output_spec": "Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.", "sample_inputs": ["0 0 4\n6 0 4", "0 0 5\n11 0 5"], "sample_outputs": ["7.25298806364175601379", "0.00000000000000000000"], "notes": null}, "positive_code": [{"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nimmutable real eps = 1e-9;\n\n\nint main()\n{\n real x1, y1, r1;\n real x2, y2, r2;\n\n readf(\" %f %f %f\", &x1, &y1, &r1);\n readf(\" %f %f %f\", &x2, &y2, &r2);\n readln();\n\n real d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);\n real result;\n\n if (d - r1 - r2 > 0)\n result = 0.0;\n else if (d - r1 + r2 < eps)\n result = PI * r2 ^^ 2;\n else if (d - r2 + r1 < eps)\n result = PI * r1 ^^ 2;\n else {\n real angle = 2 * acos((r1 ^^ 2 + d ^^ 2 - r2 ^^ 2) /\n 2 / r1 / d);\n real sectorSquare = r1 ^^ 2 * angle / 2;\n real segmentSquare = sectorSquare -\n 1.0 / 2 * r1 ^^ 2 * sin(angle);\n\n real angle2 = 2 * acos((r2 ^^ 2 + d ^^ 2 - r1 ^^ 2) /\n 2 / r2 / d);\n real sectorSquare2 = r2 ^^ 2 * angle2 / 2;\n real segmentSquare2 = sectorSquare2 -\n 1.0 / 2 * r2 ^^ 2 * sin(angle2);\n\n result = segmentSquare + segmentSquare2;\n }\n\n writefln(\"%.10f\", result);\n\n\treturn 0;\n}\n"}], "negative_code": [{"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nimmutable double eps = 1e-7;\n\n\nint main()\n{\n double x1, y1, r1;\n double x2, y2, r2;\n\n readf(\" %f %f %f\", &x1, &y1, &r1);\n readf(\" %f %f %f\", &x2, &y2, &r2);\n readln();\n\n double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);\n double result;\n\n if (d - r1 - r2 > 0)\n result = 0.0;\n else if (d - r1 + r2 < eps)\n result = PI * r2 ^^ 2;\n else if (d - r2 + r1 < eps)\n result = PI * r1 ^^ 2;\n else {\n double h = (r1 + r2 - d) / 2.0;\n\n double angle = 2 * acos((r1 - h) / r1);\n\n result = 1.0 * r1 ^^ 2 * (angle - sin(angle));\n\n debug {writeln(d, ' ', h, ' ', angle);};\n }\n\n writefln(\"%.10f\", result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nimmutable double eps = 1e-7;\n\n\nint main()\n{\n double x1, y1, r1;\n double x2, y2, r2;\n\n readf(\" %f %f %f\", &x1, &y1, &r1);\n readf(\" %f %f %f\", &x2, &y2, &r2);\n readln();\n\n double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);\n double result;\n\n if (d - r1 - r2 > 0)\n result = 0.0;\n else if (d - r1 + r2 < eps)\n result = PI * r2 ^^ 2;\n else if (d - r2 + r1 < eps)\n result = PI * r1 ^^ 2;\n else {\n double angle = 2 * acos((r1 ^^ 2 + d ^^ 2 - r2 ^^ 2) /\n 2 / r1 / d);\n double sectorSquare = r1 ^^ 2 * angle / 2;\n double h = r1 / cos(angle / 2);\n double segmentSquare = sectorSquare - 1.0 / 2 * r1 * r1 * sin(angle);\n\n debug {writeln(angle, ' ',\n sectorSquare, ' ',\n h, ' ', segmentSquare);};\n\n result = segmentSquare * 2;\n }\n\n writefln(\"%.10f\", result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nimmutable double eps = 1e-7;\n\n\nint main()\n{\n double x1, y1, r1;\n double x2, y2, r2;\n\n readf(\" %f %f %f\", &x1, &y1, &r1);\n readf(\" %f %f %f\", &x2, &y2, &r2);\n readln();\n\n double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);\n double result;\n\n if (d - r1 - r2 > 0)\n result = 0.0;\n else if (d - r1 + r2 < eps)\n result = PI * r2 ^^ 2;\n else if (d - r2 + r1 < eps)\n result = PI * r1 ^^ 2;\n else {\n double angle = 2 * acos((r1 ^^ 2 + d ^^ 2 - r2 ^^ 2) /\n 2 / r1 / d);\n double sectorSquare = r1 ^^ 2 * angle / 2;\n double h = r1 / cos(angle / 2);\n double segmentSquare = sectorSquare -\n 1.0 / 2 * r1 * r1 * sin(angle);\n\n double angle2 = 2 * acos((r2 ^^ 2 + d ^^ 2 - r1 ^^ 2) /\n 2 / r2 / d);\n double sectorSquare2 = r2 ^^ 2 * angle2 / 2;\n double h2 = r2 / cos(angle2 / 2);\n double segmentSquare2 = sectorSquare2 -\n 1.0 / 2 * r2 ^^ 2 * sin(angle2);\n\n debug {writeln(angle, ' ',\n sectorSquare, ' ',\n h, ' ', segmentSquare);};\n\n result = segmentSquare + segmentSquare2;\n }\n\n writefln(\"%.10f\", result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nimmutable double eps = 1e-7;\n\n\nint main()\n{\n double x1, y1, r1;\n double x2, y2, r2;\n\n readf(\" %f %f %f\", &x1, &y1, &r1);\n readf(\" %f %f %f\", &x2, &y2, &r2);\n readln();\n\n double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);\n double result;\n\n if (d - r1 - r2 > 0)\n result = 0.0;\n else if (d - r1 + r2 < eps)\n result = PI * r1 ^^ 2;\n else if (d - r2 + r1 < eps)\n result = PI * r2 ^^ 2;\n else {\n double h = (r1 + r2 - d) / 2.0;\n\n double angle = 2 * acos((r1 - h) / r1);\n\n result = 1.0 * r1 ^^ 2 * (angle - sin(angle));\n\n debug {writeln(d, ' ', h, ' ', angle);};\n }\n\n writefln(\"%.10f\", result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nimmutable double eps = 1e-7;\n\n\nint main()\n{\n double x1, y1, r1;\n double x2, y2, r2;\n\n readf(\" %f %f %f\", &x1, &y1, &r1);\n readf(\" %f %f %f\", &x2, &y2, &r2);\n readln();\n\n double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);\n double result;\n\n if (d - r1 - r2 > 0)\n result = 0.0;\n else if (d - r1 + r2 < eps)\n result = 0.0;\n else if (d - r2 + r1 < eps)\n result = 0.0;\n else {\n double h = (r1 + r2 - d) / 2.0;\n\n double angle = 2 * acos((r1 - h) / r1);\n\n result = 1.0 * r1 ^^ 2 * (angle - sin(angle));\n\n debug {writeln(d, ' ', h, ' ', angle);};\n }\n\n writefln(\"%.10f\", result);\n\n\treturn 0;\n}\n"}], "src_uid": "94d98a05741019f648693085d2f08872"} {"nl": {"description": "HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≤ fi ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments. The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector.", "output_spec": "Print the only integer — the number of time units needed to read the file.", "sample_inputs": ["3\n3 1 2", "5\n1 3 5 4 2"], "sample_outputs": ["3", "10"], "notes": "NoteIn the second example the head moves in the following way: 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.conv;\nimport std.regex;\nimport std.file;\nimport std.math;\nvoid main()\n{\n \n string[] inp = split(strip(readln()));\n int n = to!int(inp[0]);\n string[] s = split(strip(readln()));\n int[] a=new int[s.length];\n for(int i = 0 ; i < s.length ; i++)\n {\n a[to!int(s[i])-1]=i;\n }\n long res;\n for(int i = 1 ; i < a.length ; i++)\n {\n res+=abs(a[i]-a[i-1]);\n }\n writeln(res);\n\n //writeln(\"-1\");\n\n}\n\n \n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto p = readln.split.map !(to !(int)).array;\n\t\tauto q = new int [n];\n\t\tforeach (i, c; p)\n\t\t{\n\t\t\tq[c - 1] = i;\n\t\t}\n\t\t(n - 1).iota.map !(i => abs (q[i + 1] - q[i]))\n\t\t .sum (0L).writeln;\n\t}\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.typecons;\nimport std.conv;\nimport std.algorithm;\nimport std.math;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto n = readln().strip().to! (int);\n auto f = [0] ~ readln().strip().split().map! (to! (int)).array();\n\n auto dict = new int [n + 2];\n foreach (int i; 1..f.length)\n dict[f[i]] = i;\n\n long result;\n for (int i = 2; i <= n; i++)\n result += abs(dict[i] - dict[i - 1]);\n\n writeln(result);\n\n\treturn 0;\n}\n"}], "negative_code": [{"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.typecons;\nimport std.conv;\nimport std.algorithm;\nimport std.math;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto n = readln().strip().to! (int);\n auto f = readln().strip().split().map! (to! (int)).array();\n\n int[int] dict;\n foreach (int i; 0..f.length)\n dict[f[i] - 1] = i;\n\n int result;\n for (int i = 0; i < n - 1; i++)\n result += abs(dict[i + 1] - dict[i]);\n\n writeln(result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.typecons;\nimport std.conv;\nimport std.algorithm;\nimport std.math;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto n = readln().strip().to! (int);\n auto f = [0] ~ readln().strip().split().map! (to! (int)).array();\n\n auto dict = new int [n + 2];\n foreach (int i; 1..f.length)\n dict[f[i]] = i;\n\n int result;\n for (int i = 2; i <= n; i++)\n result += abs(dict[i] - dict[i - 1]);\n\n writeln(result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.typecons;\nimport std.conv;\nimport std.algorithm;\nimport std.math;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto n = readln().strip().to! (int);\n auto f = [0] ~ readln().strip().split().map! (to! (int)).array();\n\n int[int] dict;\n foreach (int i; 1..f.length)\n dict[f[i]] = i;\n\n int result;\n for (int i = 1; i <= n - 1; i++)\n result += abs(dict[i + 1] - dict[i]);\n\n writeln(result);\n\n\treturn 0;\n}\n"}], "src_uid": "54e2b6bea0dc6ee68366405945af50c6"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and an array $$$b$$$, with length $$$n$$$. Initially $$$b_i=0$$$ for each $$$1 \\leq i \\leq n$$$.In one move you can choose an integer $$$i$$$ ($$$1 \\leq i \\leq n$$$), and add $$$a_i$$$ to $$$b_i$$$ or subtract $$$a_i$$$ from $$$b_i$$$. What is the minimum number of moves needed to make $$$b$$$ increasing (that is, every element is strictly greater than every element before it)?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 5000$$$). The second line contains $$$n$$$ integers, $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array $$$a$$$.", "output_spec": "Print a single integer, the minimum number of moves to make $$$b$$$ increasing.", "sample_inputs": ["5\n1 2 3 4 5", "7\n1 2 1 2 1 2 1", "8\n1 8 2 7 3 6 4 5"], "sample_outputs": ["4", "10", "16"], "notes": "NoteExample $$$1$$$: you can subtract $$$a_1$$$ from $$$b_1$$$, and add $$$a_3$$$, $$$a_4$$$, and $$$a_5$$$ to $$$b_3$$$, $$$b_4$$$, and $$$b_5$$$ respectively. The final array will be [$$$-1$$$, $$$0$$$, $$$3$$$, $$$4$$$, $$$5$$$] after $$$4$$$ moves.Example $$$2$$$: you can reach [$$$-3$$$, $$$-2$$$, $$$-1$$$, $$$0$$$, $$$1$$$, $$$2$$$, $$$3$$$] in $$$10$$$ moves."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto n = RD!int;\r\n\tauto a = RDA;\r\n\r\n\tlong ans = long.max;\r\n\tforeach (i; 0..n)\r\n\t{\r\n\t\tlong cnt;\r\n\t\t{\r\n\t\t\tlong last;\r\n\t\t\tforeach_reverse (j; 0..i)\r\n\t\t\t{\r\n\t\t\t\tauto c = (last+a[j]) / a[j];\r\n\t\t\t\tlast = a[j] * c;\r\n\t\t\t\tcnt += c;\r\n\t\t\t}\r\n\t\t}\r\n\t\t{\r\n\t\t\tlong last;\r\n\t\t\tforeach (j; i+1..n)\r\n\t\t\t{\r\n\t\t\t\tauto c = (last+a[j]) / a[j];\r\n\t\t\t\tlast = a[j] * c;\r\n\t\t\t\tcnt += c;\r\n\t\t\t}\r\n\t\t}\r\n\t\tans.chmin(cnt);\r\n\t}\r\n\r\n\twriteln(ans);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tlong res = long.max;\r\n\t\tforeach (k; 0..n)\r\n\t\t{\r\n\t\t\tlong cur = 0;\r\n\t\t\tlong lo = 0;\r\n\t\t\tforeach_reverse (i; 0..k)\r\n\t\t\t{\r\n\t\t\t\tlong steps = lo / a[i] - 1;\r\n\t\t\t\tcur -= steps;\r\n\t\t\t\tlo = steps * a[i];\r\n\t\t\t}\r\n\t\t\tlong hi = 0;\r\n\t\t\tforeach (j; k + 1..n)\r\n\t\t\t{\r\n\t\t\t\tlong steps = hi / a[j] + 1;\r\n\t\t\t\tcur += steps;\r\n\t\t\t\thi = steps * a[j];\r\n\t\t\t}\r\n\t\t\tres = min (res, cur);\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum INF = 10L^^18;\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt;\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong;\n }\n \n long ans = INF;\n foreach (i; 0 .. N) {\n long cost;\n {\n long b = 0;\n foreach_reverse (j; 0 .. i) {\n const k = b / A[j] + 1;\n cost += k;\n b = A[j] * k;\n }\n }\n {\n long b = 0;\n foreach (j; i + 1 .. N) {\n const k = b / A[j] + 1;\n cost += k;\n b = A[j] * k;\n }\n }\n debug {\n writeln(i, \": \", cost);\n }\n chmin(ans, cost);\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "da2fb0ea61808905a133021223f6148d"} {"nl": {"description": "This is the easy version of the problem. The only difference between easy and hard versions is the constraint of $$$m$$$. You can make hacks only if both versions are solved.Chiori loves dolls and now she is going to decorate her bedroom! As a doll collector, Chiori has got $$$n$$$ dolls. The $$$i$$$-th doll has a non-negative integer value $$$a_i$$$ ($$$a_i < 2^m$$$, $$$m$$$ is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are $$$2^n$$$ different picking ways.Let $$$x$$$ be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls $$$x = 0$$$). The value of this picking way is equal to the number of $$$1$$$-bits in the binary representation of $$$x$$$. More formally, it is also equal to the number of indices $$$0 \\leq i < m$$$, such that $$$\\left\\lfloor \\frac{x}{2^i} \\right\\rfloor$$$ is odd.Tell her the number of picking ways with value $$$i$$$ for each integer $$$i$$$ from $$$0$$$ to $$$m$$$. Due to the answers can be very huge, print them by modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le m \\le 35$$$)  — the number of dolls and the maximum value of the picking way. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i < 2^m$$$)  — the values of dolls.", "output_spec": "Print $$$m+1$$$ integers $$$p_0, p_1, \\ldots, p_m$$$  — $$$p_i$$$ is equal to the number of picking ways with value $$$i$$$ by modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["4 4\n3 5 8 14", "6 7\n11 45 14 9 19 81"], "sample_outputs": ["2 2 6 6 0", "1 2 11 20 15 10 5 0"], "notes": null}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n import std.conv : to;\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n if (a < 0) return (this = inv()^^(-a));\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e > 0; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op: \"-\")() const { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const {\n return mixin(\"ModInt(this) \" ~ op ~ \"= a\");\n }\n ModInt opBinaryRight(string op)(long a) const {\n return mixin(\"ModInt(a) \" ~ op ~ \"= this\");\n }\n bool opCast(T: bool)() const { return (x != 0); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 998244353;\nalias Mint = ModInt!MO;\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const M = readInt();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n \n long[] bases;\n foreach (i; 0 .. N) {\n long a = A[i];\n foreach (base; bases) {\n chmin(a, a ^ base);\n }\n if (a) {\n bases ~= a;\n }\n }\n bases.sort!\"a > b\";\n const r = cast(int)(bases.length);\n foreach (j; 0 .. r) {\n foreach (k; j + 1 .. r) {\n chmin(bases[k], bases[k] ^ bases[j]);\n }\n }\n debug {\n foreach (base; bases) {\n writefln(\"%0*b\", M, base);\n }\n }\n \n long[] ps, qs;\n foreach (j; 0 .. r) {\n if (bases[j] & ~((1L << (M / 2)) - 1)) {\n ps ~= bases[j];\n } else {\n qs ~= bases[j];\n }\n }\n debug {\n writeln(\"ps = \", ps);\n writeln(\"qs = \", qs);\n }\n const psLen = cast(int)(ps.length);\n const qsLen = cast(int)(qs.length);\n auto psSum = new long[1 << psLen];\n auto qsSum = new long[1 << qsLen];\n foreach (j; 0 .. psLen) {\n foreach (h; 0 .. 1 << j) {\n psSum[h | 1 << j] = psSum[h] ^ ps[j];\n }\n }\n foreach (j; 0 .. qsLen) {\n foreach (h; 0 .. 1 << j) {\n qsSum[h | 1 << j] = qsSum[h] ^ qs[j];\n }\n }\n auto xss = new Mint[][](M - M / 2 + 1, 1 << (M / 2));\n auto ys = new Mint[1 << (M / 2)];\n foreach (h; 0 .. 1 << psLen) {\n xss[popcnt(psSum[h] >> (M / 2))][cast(int)(psSum[h] & ((1L << (M / 2)) - 1))] += 1;\n }\n foreach (h; 0 .. 1 << qsLen) {\n ys[cast(int)(qsSum[h])] += 1;\n }\n \n foreach (e; 0 .. M / 2) {\n foreach (f; 0 .. 1 << (M / 2)) {\n if (!(f & 1 << e)) {\n const tmp = ys[f] - ys[f | 1 << e];\n ys[f] += ys[f | 1 << e];\n ys[f | 1 << e] = tmp;\n }\n }\n }\n const invTwo = Mint(2)^^(-(M / 2));\n auto ans = new Mint[M + 1];\n foreach (s; 0 .. M - M / 2 + 1) {\n foreach (e; 0 .. M / 2) {\n foreach (f; 0 .. 1 << (M / 2)) {\n if (!(f & 1 << e)) {\n const tmp = xss[s][f] - xss[s][f | 1 << e];\n xss[s][f] += xss[s][f | 1 << e];\n xss[s][f | 1 << e] = tmp;\n }\n }\n }\n xss[s][] *= ys[];\n xss[s][] *= invTwo;\n foreach (e; 0 .. M / 2) {\n foreach (f; 0 .. 1 << (M / 2)) {\n if (!(f & 1 << e)) {\n const tmp = xss[s][f] - xss[s][f | 1 << e];\n xss[s][f] += xss[s][f | 1 << e];\n xss[s][f | 1 << e] = tmp;\n }\n }\n }\n foreach (f; 0 .. 1 << (M / 2)) {\n ans[s + popcnt(f)] += xss[s][f];\n }\n }\n \n ans[] *= Mint(2)^^(N - r);\n foreach (s; 0 .. M + 1) {\n if (s > 0) write(\" \");\n write(ans[s]);\n }\n writeln();\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "40a2d69987359fc83a9c3e5eff52ce34"} {"nl": {"description": "The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are $$$n$$$ snacks flavors, numbered with integers $$$1, 2, \\ldots, n$$$. Bessie has $$$n$$$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows: First, Bessie will line up the guests in some way. Then in this order, guests will approach the snacks one by one. Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad. Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.", "input_spec": "The first line contains integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 10^5$$$, $$$1 \\le k \\le 10^5$$$), the number of snacks and the number of guests. The $$$i$$$-th of the following $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$, $$$x_i \\ne y_i$$$), favorite snack flavors of the $$$i$$$-th guest.", "output_spec": "Output one integer, the smallest possible number of sad guests.", "sample_inputs": ["5 4\n1 2\n4 3\n1 4\n3 4", "6 5\n2 3\n2 1\n3 4\n6 5\n4 5"], "sample_outputs": ["1", "0"], "notes": "NoteIn the first example, Bessie can order the guests like this: $$$3, 1, 2, 4$$$. Guest $$$3$$$ goes first and eats snacks $$$1$$$ and $$$4$$$. Then the guest $$$1$$$ goes and eats the snack $$$2$$$ only, because the snack $$$1$$$ has already been eaten. Similarly, the guest $$$2$$$ goes up and eats the snack $$$3$$$ only. All the snacks are gone, so the guest $$$4$$$ will be sad. In the second example, one optimal ordering is $$$2, 1, 3, 5, 4$$$. All the guests will be satisfied."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto p = new int[] (k+1);\n auto g = new int[][] (n+1);\n foreach (i; 1 .. k+1) {\n int x, y;\n readf(\"%s %s\", &x, &y);\n readln;\n \n p[i] = x;\n g[x] ~= y;\n g[y] ~= x;\n }\n \n int ok = 0;\n auto vis = new bool[] (n+1);\n vis[] = false;\n foreach (i; 1 .. k+1) {\n if (vis[p[i]]) { continue; }\n \n int dfs(int v) {\n vis[v] = true;\n int subn = 0;\n foreach (u; g[v]) {\n if (vis[u]) { continue; }\n subn += dfs(u);\n }\n \n return 1 + subn;\n }\n \n ok += dfs(p[i]) - 1;\n }\n \n int ans = k - ok;\n \n ans.writeln;\n}"}, {"source_code": "import std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nimport std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0;\nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return read.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtype!real;\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\nvoid solve(){\n\tint n = rint, k = rint;\n\t\n\tNode[] nodes;\n\tforeach(i; 0 .. n) nodes ~= new Node(i, 0);\n\t\n\tEdge[] edges;\n\tforeach(j; 0 .. k){\n\t\tint x = rint - 1, y = rint - 1;\n\t\tedges ~= new Edge(nodes[x], nodes[y], 0);\n\t\tnodes[x].value += 1, nodes[y].value += 1;\n\t}\n\t\n\tforeach(ed; edges){\n\t\tif(ed.node1.group.id != ed.node2.group.id){\n\t\t\ted.node1.group.eat(ed.node2.group);\n\t\t}\n\t}\n\t\n\tlong ans;\n\tforeach(nd; nodes){\n\t\tif(nd.group.id != nd.id) continue;\n\t\tlong edcnt;\n\t\tforeach(nx; nd.group.nodes) edcnt += nx.value;\n\t\tans += edcnt / 2 - (nd.group.nodes.length - 1);\n\t}\n\t\n\tans.writeln;\n\t\n}\n\n\n// Union-find ※多分使いやすいやつ\nclass Edge{\n\tNode node1;\n\tNode node2;\n\tlong value;\n\tthis(Node node1, Node node2, long value){\n\t\tthis.node1 = node1, this.node2 = node2;\n\t\tthis.value = value;\n\t}\n\toverride string toString(){\n\t\treturn [node1.id, node2.id, value].to!string;\n\t}\n}\n\nclass Node{\n\tlong id;\n\tlong value;\n\tGroup group;\n\tthis(long id, long value){\n\t\tthis.id = id;\n\t\tthis.value = value;\n\t\tthis.group = new Group(this);\n\t}\n\toverride string toString(){\n\t\treturn [id, group.id, value].to!string;\n\t}\n}\n\nclass Group{\n\tlong value;\n\tlong pendingEdgeCount;\n\tlong id;\n\tlong edgecount;\n\tNode[] nodes;\n\tthis(Node node){\n\t\tthis.id = node.id;\n\t\tthis.nodes = [node];\n\t\tthis.value = node.value;\n\t}\n\tvoid eat(Group gp){\n\t\tif(gp.nodes.length > this.nodes.length) gp.eat(this);\n\t\telse{\n\t\t\tforeach(nd; gp.nodes){\n\t\t\t\tthis.nodes ~= nd;\n\t\t\t\tnd.group = this;\n\t\t\t\tthis.value += nd.value;\n\t\t\t}\n\t\t\tthis.edgecount += gp.edgecount;\n\t\t\tthis.pendingEdgeCount += gp.pendingEdgeCount;\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nstruct UnionFind\n{\n\tvoid init(int n) { par = new int[](n); foreach (i; 0..n) par[i] = i; cnt = new int[](n); fill(cnt, 1); }\n\tint root(int i) { return par[i] == i ? i : (par[i] = root(par[i])); }\n\tbool same(int i, int j) { return root(i) == root(j); }\n\tvoid unite(int i, int j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }\n\tint size(int i) { return cnt[root(i)]; }\n\tint[] par, cnt;\n}\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto k = RD!int;\n\t//auto edges = new int[][](n);\n\tUnionFind uf;\n\tuf.init(n);\n\tforeach (i; 0..k)\n\t{\n\t\tauto a = RD!int-1;\n\t\tauto b = RD!int-1;\n\t\t//edges[a] ~= b;\n\t\t//edges[b] ~= a;\n\t\tuf.unite(a, b);\n\t}\n\n\tauto cnt = new int[](n);\n\tforeach (i; 0..n)\n\t{\n\t\tcnt[i] = uf.size(i);\n\t}\n\n\tlong ans;\n\tauto index = cnt.MAKE_IDX!(\"a > b\")();\n\tauto used = new bool[](n);\n\tforeach (i; index)\n\t{\n\t\tauto r = uf.root(cast(int)i);\n\t\tif (used[r]) continue;\n\t\tans += cnt[i]-1;\n\t\tused[r] = true;\n\t}\n\n\twriteln(max(k-ans, 0));\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "a7d68ecc1a9ee23cf98f51fe6651ae13"} {"nl": {"description": "Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?", "input_spec": "The first line contains a single integer n — the length of the sequence of games (1 ≤ n ≤ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record.", "output_spec": "In the first line print a single number k — the number of options for numbers s and t. In each of the following k lines print two integers si and ti — the option for numbers s and t. Print the options in the order of increasing si, and for equal si — in the order of increasing ti.", "sample_inputs": ["5\n1 2 1 2 1", "4\n1 1 1 1", "4\n1 2 1 2", "8\n2 1 2 1 1 1 1 1"], "sample_outputs": ["2\n1 3\n3 1", "3\n1 4\n2 2\n4 1", "0", "3\n1 6\n2 3\n6 1"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.typecons;\n\nconst int MAX = 100005;\nint n;\nint[MAX] a;\nint[MAX] f1, f2, pos1, pos2;\nTuple!(int,int)[MAX] ans;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n int c1, c2;\n foreach(i; 0..n) {\n f1[i] = c1-1;\n f2[i] = c2-1;\n if (a[i] == 1) {\n pos1[c1++] = i;\n } else {\n pos2[c2++] = i;\n }\n }\n\n int numOfAns = 0;\n\nmatch: \n foreach_reverse(t; 1..n+1) {\n int i = 0;\n int win1, win2;\n while (i < n) {\n int end1 = f1[i] + t < c1 ? pos1[f1[i] + t] : n;\n int end2 = f2[i] + t < c2 ? pos2[f2[i] + t] : n;\n int nextEnd = min(end1, end2);\n if (nextEnd >= n) {\n continue match;\n }\n i = nextEnd + 1;\n if (end1 < end2) {\n win1++;\n } else {\n win2++;\n }\n }\n if ((win1 < win2 && a[n-1] == 2) || (win1 > win2 && a[n-1] == 1))\n ans[numOfAns++] = tuple(max(win1, win2), t);\n }\n\n ans[0..numOfAns].sort!((x, y) => (x[0] == y[0]) ? x[1] < y[1] : x[0] < y[0]);\n\n writeln(numOfAns);\n foreach(x; ans[0..numOfAns]) writeln(x[0], \" \", x[1]);\n}\n"}, {"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport std.range;\nimport std.numeric;\nimport std.traits;\nimport std.variant;\nimport std.concurrency;\n\nvoid main(string[] args)\n{\n auto n = next!int;\n auto a = next!int(n);\n auto pos1 = new int[](n + 1); pos1[] = -1;\n auto pos2 = new int[](n + 1); pos2[] = -1;\n auto cnt1 = new int[](n);\n auto cnt2 = new int[](n);\n foreach(i, ai; a)\n {\n if (i != 0)\n\t{\n\t cnt1[i] = cnt1[i - 1];\n\t cnt2[i] = cnt2[i - 1];\n\t}\n if (ai == 1)\n\t{\n\t cnt1[i]++;\n\t pos1[cnt1[i]] = cast(int) i;\n\t}\n else\n\t{\n\t cnt2[i]++;\n\t pos2[cnt2[i]] = cast(int) i;\n\t}\n }\n foreach(ref c; pos1) if (c == -1) c = n;\n foreach(ref c; pos2) if (c == -1) c = n;\n auto options = new Tuple!(int, int)[](0);\n debug writeln(\"pos1 = \", pos1);\n debug writeln(\"pos2 = \", pos2);\n sTest: foreach(s; 1 .. n + 1)\n {\n debug writeln(\"trying for s = \", s);\n int i = void;\n int ni = void;\n int sets1 = 0;\n int sets2 = 0;\n int next1 = pos1[s];\n int next2 = pos2[s];\n if (next1 < next2)\n\t{\n\t i = next1;\n\t sets1++;\n\t}\n else\n\t{\n\t i = next2;\n\t sets2++;\n\t}\n while (i < n)\n\t{\n\t debug writeln(\"in i = \", i, \" sets1 = \", sets1, \" sets2 = \", sets2);\n\t if (i == n - 1)\n\t {\n\t if (sets1 == sets2)\n\t\tcontinue sTest;\n\t if (sets1 > sets2)\n\t\t{\n\t\t if (a[i] == 1)\n\t\t options ~= tuple(sets1, s);\n\t\t}\n\t else\n\t\t{\n\t\t if (a[i] == 2)\n\t\t options ~= tuple(sets2, s);\n\t\t}\n\t continue sTest;\n\t }\n\t \n\t next1 = (cnt1[i] + s <= n)? pos1[cnt1[i] + s] : n;\n\t next2 = (cnt2[i] + s <= n)? pos2[cnt2[i] + s] : n;\n\t if (next1 < next2)\n\t {\n\t ni = next1;\n\t sets1++;\n\t }\n\t else\n\t {\n\t ni = next2;\n\t sets2++;\n\t }\n\t \n\t i = ni;\n\t}\n }\n auto soptions = sort(options);\n writeln(options.length);\n foreach(option; options)\n {\n writeln(option[0], \" \", option[1]);\n }\n}\n\nstatic this()\n{\n popChar;\n}\n\nstruct Any(T)\n{\n static auto read()\n {\n static if (is(T == char))\n {\n\tauto res = frontChar;\n\tpopChar;\n\treturn res;\n }\n }\n}\n\nstruct Unsigned(T)\n{\n auto read()\n {\n auto res = T(0);\n while(!frontChar.isDigit) popChar;\n do\n {\n\tres = res * T(10) + T(frontChar - '0');\n\tpopChar;\n }\n while(frontChar.isDigit);\n return res;\n }\n}\n\nstruct Matrix(T, alias rows, alias cols) \n{\n T[][] _cell;\n alias _cell this;\n // Ring constructor\n this(int n)\n {\n final switch(n)\n {\n case 0:\n\t_cell = new T[][](rows, cols);\n\tbreak;\n case 1:\n\tdebug assert(rows == cols);\n\t_cell = new T[][](rows, cols);\n\tforeach(i; 0 .. rows)\n\t _cell[i][i] = T(1);\n\tbreak;\n }\n }\n // Copy constructor\n this(Matrix!(T, rows, cols) other)\n {\n _cell = new T[][](rows, cols);\n foreach(i; 0 .. rows)\n _cell[i][] = other[i][];\n }\n auto opBinary(string op)(Matrix!(T, rows, cols) other)\n if (op == \"+\" || op == \"-\")\n\t {\n\t auto res = Matrix!(T, rows, cols)(0);\n\t foreach(i; 0 .. rows)\n\t foreach(j; 0 .. cols)\n\t res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});\n\t return res;\n\t }\n auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)\n if (op == \"*\")\n\t {\n\t debug assert(cols == otherRows);\n\t auto res = Matrix!(T, rows, otherCols)(0);\n\t foreach(i; 0 .. rows)\n\t foreach(k; 0 .. cols)\n\t foreach(j; 0 .. otherCols)\n\t\t res[i][j] += this[i][k] * other[k][j];\n\t return res;\n\t }\n ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)\n {\n static if (op == \"*\")\n {\n\tdebug assert(rows == cols);\n\talias n = rows;\n\tauto res = Matrix!(T, n, n)(0);\n\tT factor;\n\tforeach(i; 0 .. n)\n\t foreach(k; 0 .. n)\n\t foreach(j; 0 .. n)\n\t res[i][j] += this[i][k] * other[k][j];\n\tforeach(i; 0 .. n)\n\t this[i][] = res[i][];\n }\n else\n {\n\tforeach(i; 0 .. rows)\n\t foreach(j; 0 .. cols)\n\t mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});\n }\n return this;\n }\n mixin powMethod;\n}\n\nstruct Mod(T, alias mod)\n{\n alias This = Mod!(T, mod);\n T _rep;\n this(T t)\n {\n _rep = t % mod;\n if (_rep < 0) _rep += mod;\n }\n static auto plain(T t)\n {\n auto res = Mod!(T, mod)();\n res._rep = t;\n return res;\n }\n static auto fromPositive(T t)\n {\n pragma(inline, true);\n return Mod!(T, mod).plain(t % mod);\n }\n static auto fromNegative(T t)\n {\n pragma(inline, true);\n return Mod!(T, mod).plain(t % mod + mod);\n }\n string toString()\n {\n return to!string(_rep);\n }\n debug invariant\n {\n assert(_rep >= 0 && _rep < mod);\n }\n auto opBinary(string op)(This b)\n {\n static if (op == \"+\")\n {\n\tT resrep = _rep + b._rep;\n\tif (resrep >= mod) resrep -= mod;\n\treturn This.plain(resrep);\n }\n else static if (op == \"-\")\n {\n\tT resrep = _rep - b._rep;\n\tif (resrep < 0) resrep += mod;\n\treturn This.plain(resrep);\n }\n else static if (op == \"*\")\n {\n\treturn This.fromPositive(_rep * b._rep);\n }\n }\n auto opOpAssign(string op)(This b)\n {\n mixin(q{_rep }, text(op, \"=\"), q{b._rep;});\n static if (op == \"+\")\n {\n\tif (_rep >= mod) _rep -= mod;\n }\n else static if (op == \"-\")\n {\n\tif (_rep < 0) _rep += mod;\n }\n else static if (op == \"*\")\n {\n\t_rep %= mod;\n }\n return this;\n }\n \n auto opBinary(string op)(long exp)\n if (op == \"^^\")\n\t {\n\t return pow(exp);\n\t }\n mixin powMethod;\n}\n\nmixin template powMethod()\n{\n auto pow(this ThisType)(long exp)\n {\n auto res = ThisType(1);\n auto pow = ThisType(this);\n while(exp)\n\t{\n\t if (exp & 1) res *= pow;\n\t pow *= pow;\n\t exp >>= 1;\n\t}\n return res;\n }\n}\n// INPUT\nchar frontChar;\nvoid popChar()\n{\n import core.stdc.stdio;\n frontChar = cast(char) getchar;\n if (feof(stdin)) frontChar = ' ';\n}\nauto makeArray(T, S...)(S s)\n{\n enum brackets = () {\n string res;\n foreach(i; 0 .. s.length)\n res ~= \"[]\";\n return res;\n } ();\n mixin(q{alias Type = T}, brackets, q{;});\n return new Type(s);\n}\ntemplate isNumberLike(T)\n{\n enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));\n}\nvoid popWhite()\n{\n import std.ascii;\n while (frontChar.isWhite) popChar;\n}\nvoid print(T...)(T t)\n{\n static foreach(ti; t)\n {\n static if (is(typeof(ti) == string))\n\t{\n\t write(ti, \" \");\n\t}\n else static if (isArray!(typeof(ti)))\n\t{\n\t foreach(e; ti) print(e);\n\t}\n else static if (isTuple!(typeof(ti)))\n\t{\n\t static foreach(i; ti.length) writesp(ti[i]);\n\t}\n else\n\t{\n\t write(ti, \" \");\n\t}\n }\n}\nvoid println(T...)(T t)\n{\n static if (t.length == 0)\n writeln;\n static foreach(ti; t)\n {\n print(ti);\n writeln;\n }\n}\nauto next(alias T, S...)(S s)\n{\n pragma(inline, true);\n static if (s.length > 0)\n {\n auto res = makeArray!(typeof(next!T()))(s);\n S t;\n enum loops = () {\n\tstring res;\n\tforeach(i; 0 .. s.length)\n\t {\n\t auto ti = text(q{t[}, i, q{]});\n\t res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});\n\t }\n\treturn res;\n } ();\n enum indexing = () {\n\tstring res = \"res\";\n\tforeach(i; 0 .. s.length)\n\t res ~= text(q{[t[}, i, q{]]});\n\treturn res;\n } ();\n mixin(loops, indexing, q{ = next!T;});\n return res;\n }\n else\n {\n static if (isNumberLike!T)\n\t{\n\t import std.ascii: isDigit;\n\t T res;\n\t while(frontChar.isWhite) popChar;\n\t T multiplier = T(1);\n\t if (frontChar == '-')\n\t {\n\t multiplier = T(-1);\n\t popChar;\n\t }\n\t else if (frontChar == '+')\n\t {\n\t popChar;\n\t }\n\t debug assert(frontChar.isDigit);\n\t do\n\t {\n\t res = res * T(10) + T(frontChar - '0');\n\t popChar;\n\t }\n\t while(frontChar.isDigit);\n\t return multiplier * res;\n\t}\n else static if (is(T == string))\n\t{\n\t import std.ascii: isWhite;\n\t string res;\n\t while(frontChar.isWhite)\n\t popChar;\n\t while(!frontChar.isWhite)\n\t {\n\t res ~= frontChar;\n\t popChar;\n\t }\n\t return res;\n\t}\n else static if (is(T == char))\n\t{\n\t while(frontChar.isWhite) popChar;\n\t auto res = frontChar;\n\t popChar;\n\t return res;\n\t}\n else\n\t{\n\t return T.read;\n\t}\n }\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tauto a = [0] ~ readln.split.map !(to !(int)).array;\n\t\tint [] sa = [0];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tsa ~= sa[i] + (a[i + 1] == 1);\n\t\t}\n\t\tsa ~= n * 10;\n\t\tint [] sb = [0];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tsb ~= sb[i] + (a[i + 1] == 2);\n\t\t}\n\t\tsb ~= n * 10;\n\t\tint [2] [] res;\n\t\tforeach (t; 1..n + 1)\n\t\t{\n\t\t\tint pos = 0;\n\t\t\tint ca = 0;\n\t\t\tint cb = 0;\n\t\t\tint last = 0;\n\t\t\tbool ok = true;\n\t\t\twhile (pos < n)\n\t\t\t{\n\t\t\t\tint lo = pos - 1;\n\t\t\t\tint hi = n;\n\t\t\t\twhile (lo < hi)\n\t\t\t\t{\n\t\t\t\t\tint me = ((lo + hi + 3) >> 1) - 1;\n\t\t\t\t\tif (sa[me + 1] - sa[pos] >= t ||\n\t\t\t\t\t sb[me + 1] - sb[pos] >= t)\n\t\t\t\t\t{\n\t\t\t\t\t\thi = me - 1;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tlo = me;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tint next = lo + 1;\n\t\t\t\tif (sa[next + 1] - sa[pos] == t)\n\t\t\t\t{\n\t\t\t\t\tlast = 1;\n\t\t\t\t\tca++;\n\t\t\t\t}\n\t\t\t\telse if (sb[next + 1] - sb[pos] == t)\n\t\t\t\t{\n\t\t\t\t\tlast = 2;\n\t\t\t\t\tcb++;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tassert (next >= n);\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tpos = next + 1;\n\t\t\t}\n\n\t\t\tok &= (last == 1 && ca > cb) ||\n\t\t\t (last == 2 && ca < cb);\n\t\t\tif (ok)\n\t\t\t{\n\t\t\t\tres ~= [max (ca, cb), t];\n\t\t\t}\n\t\t}\n\n\t\tsort (res);\n\t\twriteln (res.length);\n\t\tforeach (cur; res)\n\t\t{\n\t\t\twriteln (cur[0], ' ', cur[1]);\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.typecons;\n\nconst int MAX = 100005;\nint n;\nint[MAX] a;\nint[MAX] f1, f2, pos1, pos2;\nTuple!(int,int)[MAX] ans;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n int c1, c2;\n foreach(i; 0..n) {\n f1[i] = c1-1;\n f2[i] = c2-1;\n if (a[i] == 1) {\n pos1[c1++] = i;\n } else {\n pos2[c2++] = i;\n }\n }\n\n int numOfAns = 0;\n\nmatch: \n foreach_reverse(t; 1..n+1) {\n int i = 0;\n int win1, win2;\n while (i < n) {\n int end1 = f1[i] + t < c1 ? pos1[f1[i] + t] : n;\n int end2 = f2[i] + t < c2 ? pos2[f2[i] + t] : n;\n int nextEnd = min(end1, end2);\n if (nextEnd >= n) {\n continue match;\n }\n i = nextEnd + 1;\n if (end1 < end2) {\n win1++;\n } else {\n win2++;\n }\n }\n if (win1 != win2) \n ans[numOfAns++] = tuple(max(win1, win2), t);\n }\n\n writeln(numOfAns);\n foreach(x; ans[0..numOfAns]) writeln(x[0], \" \", x[1]);\n}\n"}, {"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.typecons;\n\nconst int MAX = 100005;\nint n;\nint[MAX] a;\nint[MAX] f1, f2, pos1, pos2;\nTuple!(int,int)[MAX] ans;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n int c1, c2;\n foreach(i; 0..n) {\n f1[i] = c1-1;\n f2[i] = c2-1;\n if (a[i] == 1) {\n pos1[c1++] = i;\n } else {\n pos2[c2++] = i;\n }\n }\n\n int numOfAns = 0;\n\nmatch: \n foreach_reverse(t; 1..n+1) {\n int i = 0;\n int win1, win2;\n while (i < n) {\n int end1 = f1[i] + t < c1 ? pos1[f1[i] + t] : n;\n int end2 = f2[i] + t < c2 ? pos2[f2[i] + t] : n;\n int nextEnd = min(end1, end2);\n if (nextEnd >= n) {\n continue match;\n }\n i = nextEnd + 1;\n if (end1 < end2) {\n win1++;\n } else {\n win2++;\n }\n }\n if ((win1 < win2 && a[n-1] == 2) || (win1 > win2 && a[n-1] == 1))\n ans[numOfAns++] = tuple(max(win1, win2), t);\n }\n\n writeln(numOfAns);\n foreach(x; ans[0..numOfAns]) writeln(x[0], \" \", x[1]);\n}\n"}, {"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport std.range;\nimport std.numeric;\nimport std.traits;\nimport std.variant;\nimport std.concurrency;\n\nvoid main(string[] args)\n{\n auto n = next!int;\n auto a = next!int(n);\n auto pos1 = new int[](n + 1); pos1[] = -1;\n auto pos2 = new int[](n + 1); pos2[] = -1;\n auto cnt1 = new int[](n);\n auto cnt2 = new int[](n);\n foreach(i, ai; a)\n {\n if (i != 0)\n\t{\n\t cnt1[i] = cnt1[i - 1];\n\t cnt2[i] = cnt2[i - 1];\n\t}\n if (ai == 1)\n\t{\n\t cnt1[i]++;\n\t pos1[cnt1[i]] = cast(int) i;\n\t}\n else\n\t{\n\t cnt2[i]++;\n\t pos2[cnt2[i]] = cast(int) i;\n\t}\n }\n foreach(ref c; pos1) if (c == -1) c = n;\n foreach(ref c; pos2) if (c == -1) c = n;\n auto options = new Tuple!(int, int)[](0);\n debug writeln(\"pos1 = \", pos1);\n debug writeln(\"pos2 = \", pos2);\n sTest: foreach(s; 1 .. n + 1)\n {\n debug writeln(\"trying for s = \", s);\n int i = void;\n int ni = void;\n int sets1 = 0;\n int sets2 = 0;\n int next1 = pos1[s];\n int next2 = pos2[s];\n if (next1 < next2)\n\t{\n\t i = next1;\n\t sets1++;\n\t}\n else\n\t{\n\t i = next2;\n\t sets2++;\n\t}\n while (i < n)\n\t{\n\t debug writeln(\"in i = \", i, \" sets1 = \", sets1, \" sets2 = \", sets2);\n\t if (i == n - 1)\n\t {\n\t if (sets1 == sets2)\n\t\tcontinue sTest;\n\t options ~= tuple(max(sets1, sets2), s);\n\t continue sTest;\n\t }\n\t \n\t next1 = (cnt1[i] + s <= n)? pos1[cnt1[i] + s] : n;\n\t next2 = (cnt2[i] + s <= n)? pos2[cnt2[i] + s] : n;\n\t if (next1 < next2)\n\t {\n\t ni = next1;\n\t sets1++;\n\t }\n\t else\n\t {\n\t ni = next2;\n\t sets2++;\n\t }\n\t \n\t i = ni;\n\t}\n }\n auto soptions = sort(options);\n writeln(options.length);\n foreach(option; options)\n {\n writeln(option[0], \" \", option[1]);\n }\n}\n\nstatic this()\n{\n popChar;\n}\n\nstruct Any(T)\n{\n static auto read()\n {\n static if (is(T == char))\n {\n\tauto res = frontChar;\n\tpopChar;\n\treturn res;\n }\n }\n}\n\nstruct Unsigned(T)\n{\n auto read()\n {\n auto res = T(0);\n while(!frontChar.isDigit) popChar;\n do\n {\n\tres = res * T(10) + T(frontChar - '0');\n\tpopChar;\n }\n while(frontChar.isDigit);\n return res;\n }\n}\n\nstruct Matrix(T, alias rows, alias cols) \n{\n T[][] _cell;\n alias _cell this;\n // Ring constructor\n this(int n)\n {\n final switch(n)\n {\n case 0:\n\t_cell = new T[][](rows, cols);\n\tbreak;\n case 1:\n\tdebug assert(rows == cols);\n\t_cell = new T[][](rows, cols);\n\tforeach(i; 0 .. rows)\n\t _cell[i][i] = T(1);\n\tbreak;\n }\n }\n // Copy constructor\n this(Matrix!(T, rows, cols) other)\n {\n _cell = new T[][](rows, cols);\n foreach(i; 0 .. rows)\n _cell[i][] = other[i][];\n }\n auto opBinary(string op)(Matrix!(T, rows, cols) other)\n if (op == \"+\" || op == \"-\")\n\t {\n\t auto res = Matrix!(T, rows, cols)(0);\n\t foreach(i; 0 .. rows)\n\t foreach(j; 0 .. cols)\n\t res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});\n\t return res;\n\t }\n auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)\n if (op == \"*\")\n\t {\n\t debug assert(cols == otherRows);\n\t auto res = Matrix!(T, rows, otherCols)(0);\n\t foreach(i; 0 .. rows)\n\t foreach(k; 0 .. cols)\n\t foreach(j; 0 .. otherCols)\n\t\t res[i][j] += this[i][k] * other[k][j];\n\t return res;\n\t }\n ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)\n {\n static if (op == \"*\")\n {\n\tdebug assert(rows == cols);\n\talias n = rows;\n\tauto res = Matrix!(T, n, n)(0);\n\tT factor;\n\tforeach(i; 0 .. n)\n\t foreach(k; 0 .. n)\n\t foreach(j; 0 .. n)\n\t res[i][j] += this[i][k] * other[k][j];\n\tforeach(i; 0 .. n)\n\t this[i][] = res[i][];\n }\n else\n {\n\tforeach(i; 0 .. rows)\n\t foreach(j; 0 .. cols)\n\t mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});\n }\n return this;\n }\n mixin powMethod;\n}\n\nstruct Mod(T, alias mod)\n{\n alias This = Mod!(T, mod);\n T _rep;\n this(T t)\n {\n _rep = t % mod;\n if (_rep < 0) _rep += mod;\n }\n static auto plain(T t)\n {\n auto res = Mod!(T, mod)();\n res._rep = t;\n return res;\n }\n static auto fromPositive(T t)\n {\n pragma(inline, true);\n return Mod!(T, mod).plain(t % mod);\n }\n static auto fromNegative(T t)\n {\n pragma(inline, true);\n return Mod!(T, mod).plain(t % mod + mod);\n }\n string toString()\n {\n return to!string(_rep);\n }\n debug invariant\n {\n assert(_rep >= 0 && _rep < mod);\n }\n auto opBinary(string op)(This b)\n {\n static if (op == \"+\")\n {\n\tT resrep = _rep + b._rep;\n\tif (resrep >= mod) resrep -= mod;\n\treturn This.plain(resrep);\n }\n else static if (op == \"-\")\n {\n\tT resrep = _rep - b._rep;\n\tif (resrep < 0) resrep += mod;\n\treturn This.plain(resrep);\n }\n else static if (op == \"*\")\n {\n\treturn This.fromPositive(_rep * b._rep);\n }\n }\n auto opOpAssign(string op)(This b)\n {\n mixin(q{_rep }, text(op, \"=\"), q{b._rep;});\n static if (op == \"+\")\n {\n\tif (_rep >= mod) _rep -= mod;\n }\n else static if (op == \"-\")\n {\n\tif (_rep < 0) _rep += mod;\n }\n else static if (op == \"*\")\n {\n\t_rep %= mod;\n }\n return this;\n }\n \n auto opBinary(string op)(long exp)\n if (op == \"^^\")\n\t {\n\t return pow(exp);\n\t }\n mixin powMethod;\n}\n\nmixin template powMethod()\n{\n auto pow(this ThisType)(long exp)\n {\n auto res = ThisType(1);\n auto pow = ThisType(this);\n while(exp)\n\t{\n\t if (exp & 1) res *= pow;\n\t pow *= pow;\n\t exp >>= 1;\n\t}\n return res;\n }\n}\n// INPUT\nchar frontChar;\nvoid popChar()\n{\n import core.stdc.stdio;\n frontChar = cast(char) getchar;\n if (feof(stdin)) frontChar = ' ';\n}\nauto makeArray(T, S...)(S s)\n{\n enum brackets = () {\n string res;\n foreach(i; 0 .. s.length)\n res ~= \"[]\";\n return res;\n } ();\n mixin(q{alias Type = T}, brackets, q{;});\n return new Type(s);\n}\ntemplate isNumberLike(T)\n{\n enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));\n}\nvoid popWhite()\n{\n import std.ascii;\n while (frontChar.isWhite) popChar;\n}\nvoid print(T...)(T t)\n{\n static foreach(ti; t)\n {\n static if (is(typeof(ti) == string))\n\t{\n\t write(ti, \" \");\n\t}\n else static if (isArray!(typeof(ti)))\n\t{\n\t foreach(e; ti) print(e);\n\t}\n else static if (isTuple!(typeof(ti)))\n\t{\n\t static foreach(i; ti.length) writesp(ti[i]);\n\t}\n else\n\t{\n\t write(ti, \" \");\n\t}\n }\n}\nvoid println(T...)(T t)\n{\n static if (t.length == 0)\n writeln;\n static foreach(ti; t)\n {\n print(ti);\n writeln;\n }\n}\nauto next(alias T, S...)(S s)\n{\n pragma(inline, true);\n static if (s.length > 0)\n {\n auto res = makeArray!(typeof(next!T()))(s);\n S t;\n enum loops = () {\n\tstring res;\n\tforeach(i; 0 .. s.length)\n\t {\n\t auto ti = text(q{t[}, i, q{]});\n\t res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});\n\t }\n\treturn res;\n } ();\n enum indexing = () {\n\tstring res = \"res\";\n\tforeach(i; 0 .. s.length)\n\t res ~= text(q{[t[}, i, q{]]});\n\treturn res;\n } ();\n mixin(loops, indexing, q{ = next!T;});\n return res;\n }\n else\n {\n static if (isNumberLike!T)\n\t{\n\t import std.ascii: isDigit;\n\t T res;\n\t while(frontChar.isWhite) popChar;\n\t T multiplier = T(1);\n\t if (frontChar == '-')\n\t {\n\t multiplier = T(-1);\n\t popChar;\n\t }\n\t else if (frontChar == '+')\n\t {\n\t popChar;\n\t }\n\t debug assert(frontChar.isDigit);\n\t do\n\t {\n\t res = res * T(10) + T(frontChar - '0');\n\t popChar;\n\t }\n\t while(frontChar.isDigit);\n\t return multiplier * res;\n\t}\n else static if (is(T == string))\n\t{\n\t import std.ascii: isWhite;\n\t string res;\n\t while(frontChar.isWhite)\n\t popChar;\n\t while(!frontChar.isWhite)\n\t {\n\t res ~= frontChar;\n\t popChar;\n\t }\n\t return res;\n\t}\n else static if (is(T == char))\n\t{\n\t while(frontChar.isWhite) popChar;\n\t auto res = frontChar;\n\t popChar;\n\t return res;\n\t}\n else\n\t{\n\t return T.read;\n\t}\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport std.range;\nimport std.numeric;\nimport std.traits;\nimport std.variant;\nimport std.concurrency;\n\nvoid main(string[] args)\n{\n auto n = next!int;\n auto a = next!int(n);\n auto pos1 = new int[](n + 1); pos1[] = -1;\n auto pos2 = new int[](n + 1); pos2[] = -1;\n auto cnt1 = new int[](n);\n auto cnt2 = new int[](n);\n foreach(i, ai; a)\n {\n if (i != 0)\n\t{\n\t cnt1[i] = cnt1[i - 1];\n\t cnt2[i] = cnt2[i - 1];\n\t}\n if (ai == 1)\n\t{\n\t cnt1[i]++;\n\t pos1[cnt1[i]] = cast(int) i;\n\t}\n else\n\t{\n\t cnt2[i]++;\n\t pos2[cnt2[i]] = cast(int) i;\n\t}\n }\n foreach(ref c; pos1) if (c == -1) c = n;\n foreach(ref c; pos2) if (c == -1) c = n;\n auto options = new Tuple!(int, int)[](0);\n debug writeln(\"pos1 = \", pos1);\n debug writeln(\"pos2 = \", pos2);\n sTest: foreach(s; 1 .. n + 1)\n {\n debug writeln(\"trying for s = \", s);\n int i = void;\n int ni = void;\n int sets1 = 0;\n int sets2 = 0;\n int next1 = pos1[s];\n int next2 = pos2[s];\n if (next1 < next2)\n\t{\n\t i = next1;\n\t sets1++;\n\t}\n else\n\t{\n\t i = next2;\n\t sets2++;\n\t}\n while (i < n)\n\t{\n\t debug writeln(\"in i = \", i, \" sets1 = \", sets1, \" sets2 = \", sets2);\n\t if (i == n - 1)\n\t {\n\t if (sets1 == sets2)\n\t\tcontinue sTest;\n\t options ~= tuple(s, max(sets1, sets2));\n\t continue sTest;\n\t }\n\t \n\t next1 = (cnt1[i] + s <= n)? pos1[cnt1[i] + s] : n;\n\t next2 = (cnt2[i] + s <= n)? pos2[cnt2[i] + s] : n;\n\t if (next1 < next2)\n\t {\n\t ni = next1;\n\t sets1++;\n\t }\n\t else\n\t {\n\t ni = next2;\n\t sets2++;\n\t }\n\t \n\t i = ni;\n\t}\n }\n writeln(options.length);\n foreach(option; options)\n {\n writeln(option[0], \" \", option[1]);\n }\n}\n\nstatic this()\n{\n popChar;\n}\n\nstruct Any(T)\n{\n static auto read()\n {\n static if (is(T == char))\n {\n\tauto res = frontChar;\n\tpopChar;\n\treturn res;\n }\n }\n}\n\nstruct Unsigned(T)\n{\n auto read()\n {\n auto res = T(0);\n while(!frontChar.isDigit) popChar;\n do\n {\n\tres = res * T(10) + T(frontChar - '0');\n\tpopChar;\n }\n while(frontChar.isDigit);\n return res;\n }\n}\n\nstruct Matrix(T, alias rows, alias cols) \n{\n T[][] _cell;\n alias _cell this;\n // Ring constructor\n this(int n)\n {\n final switch(n)\n {\n case 0:\n\t_cell = new T[][](rows, cols);\n\tbreak;\n case 1:\n\tdebug assert(rows == cols);\n\t_cell = new T[][](rows, cols);\n\tforeach(i; 0 .. rows)\n\t _cell[i][i] = T(1);\n\tbreak;\n }\n }\n // Copy constructor\n this(Matrix!(T, rows, cols) other)\n {\n _cell = new T[][](rows, cols);\n foreach(i; 0 .. rows)\n _cell[i][] = other[i][];\n }\n auto opBinary(string op)(Matrix!(T, rows, cols) other)\n if (op == \"+\" || op == \"-\")\n\t {\n\t auto res = Matrix!(T, rows, cols)(0);\n\t foreach(i; 0 .. rows)\n\t foreach(j; 0 .. cols)\n\t res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});\n\t return res;\n\t }\n auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)\n if (op == \"*\")\n\t {\n\t debug assert(cols == otherRows);\n\t auto res = Matrix!(T, rows, otherCols)(0);\n\t foreach(i; 0 .. rows)\n\t foreach(k; 0 .. cols)\n\t foreach(j; 0 .. otherCols)\n\t\t res[i][j] += this[i][k] * other[k][j];\n\t return res;\n\t }\n ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)\n {\n static if (op == \"*\")\n {\n\tdebug assert(rows == cols);\n\talias n = rows;\n\tauto res = Matrix!(T, n, n)(0);\n\tT factor;\n\tforeach(i; 0 .. n)\n\t foreach(k; 0 .. n)\n\t foreach(j; 0 .. n)\n\t res[i][j] += this[i][k] * other[k][j];\n\tforeach(i; 0 .. n)\n\t this[i][] = res[i][];\n }\n else\n {\n\tforeach(i; 0 .. rows)\n\t foreach(j; 0 .. cols)\n\t mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});\n }\n return this;\n }\n mixin powMethod;\n}\n\nstruct Mod(T, alias mod)\n{\n alias This = Mod!(T, mod);\n T _rep;\n this(T t)\n {\n _rep = t % mod;\n if (_rep < 0) _rep += mod;\n }\n static auto plain(T t)\n {\n auto res = Mod!(T, mod)();\n res._rep = t;\n return res;\n }\n static auto fromPositive(T t)\n {\n pragma(inline, true);\n return Mod!(T, mod).plain(t % mod);\n }\n static auto fromNegative(T t)\n {\n pragma(inline, true);\n return Mod!(T, mod).plain(t % mod + mod);\n }\n string toString()\n {\n return to!string(_rep);\n }\n debug invariant\n {\n assert(_rep >= 0 && _rep < mod);\n }\n auto opBinary(string op)(This b)\n {\n static if (op == \"+\")\n {\n\tT resrep = _rep + b._rep;\n\tif (resrep >= mod) resrep -= mod;\n\treturn This.plain(resrep);\n }\n else static if (op == \"-\")\n {\n\tT resrep = _rep - b._rep;\n\tif (resrep < 0) resrep += mod;\n\treturn This.plain(resrep);\n }\n else static if (op == \"*\")\n {\n\treturn This.fromPositive(_rep * b._rep);\n }\n }\n auto opOpAssign(string op)(This b)\n {\n mixin(q{_rep }, text(op, \"=\"), q{b._rep;});\n static if (op == \"+\")\n {\n\tif (_rep >= mod) _rep -= mod;\n }\n else static if (op == \"-\")\n {\n\tif (_rep < 0) _rep += mod;\n }\n else static if (op == \"*\")\n {\n\t_rep %= mod;\n }\n return this;\n }\n \n auto opBinary(string op)(long exp)\n if (op == \"^^\")\n\t {\n\t return pow(exp);\n\t }\n mixin powMethod;\n}\n\nmixin template powMethod()\n{\n auto pow(this ThisType)(long exp)\n {\n auto res = ThisType(1);\n auto pow = ThisType(this);\n while(exp)\n\t{\n\t if (exp & 1) res *= pow;\n\t pow *= pow;\n\t exp >>= 1;\n\t}\n return res;\n }\n}\n// INPUT\nchar frontChar;\nvoid popChar()\n{\n import core.stdc.stdio;\n frontChar = cast(char) getchar;\n if (feof(stdin)) frontChar = ' ';\n}\nauto makeArray(T, S...)(S s)\n{\n enum brackets = () {\n string res;\n foreach(i; 0 .. s.length)\n res ~= \"[]\";\n return res;\n } ();\n mixin(q{alias Type = T}, brackets, q{;});\n return new Type(s);\n}\ntemplate isNumberLike(T)\n{\n enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));\n}\nvoid popWhite()\n{\n import std.ascii;\n while (frontChar.isWhite) popChar;\n}\nvoid print(T...)(T t)\n{\n static foreach(ti; t)\n {\n static if (is(typeof(ti) == string))\n\t{\n\t write(ti, \" \");\n\t}\n else static if (isArray!(typeof(ti)))\n\t{\n\t foreach(e; ti) print(e);\n\t}\n else static if (isTuple!(typeof(ti)))\n\t{\n\t static foreach(i; ti.length) writesp(ti[i]);\n\t}\n else\n\t{\n\t write(ti, \" \");\n\t}\n }\n}\nvoid println(T...)(T t)\n{\n static if (t.length == 0)\n writeln;\n static foreach(ti; t)\n {\n print(ti);\n writeln;\n }\n}\nauto next(alias T, S...)(S s)\n{\n pragma(inline, true);\n static if (s.length > 0)\n {\n auto res = makeArray!(typeof(next!T()))(s);\n S t;\n enum loops = () {\n\tstring res;\n\tforeach(i; 0 .. s.length)\n\t {\n\t auto ti = text(q{t[}, i, q{]});\n\t res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});\n\t }\n\treturn res;\n } ();\n enum indexing = () {\n\tstring res = \"res\";\n\tforeach(i; 0 .. s.length)\n\t res ~= text(q{[t[}, i, q{]]});\n\treturn res;\n } ();\n mixin(loops, indexing, q{ = next!T;});\n return res;\n }\n else\n {\n static if (isNumberLike!T)\n\t{\n\t import std.ascii: isDigit;\n\t T res;\n\t while(frontChar.isWhite) popChar;\n\t T multiplier = T(1);\n\t if (frontChar == '-')\n\t {\n\t multiplier = T(-1);\n\t popChar;\n\t }\n\t else if (frontChar == '+')\n\t {\n\t popChar;\n\t }\n\t debug assert(frontChar.isDigit);\n\t do\n\t {\n\t res = res * T(10) + T(frontChar - '0');\n\t popChar;\n\t }\n\t while(frontChar.isDigit);\n\t return multiplier * res;\n\t}\n else static if (is(T == string))\n\t{\n\t import std.ascii: isWhite;\n\t string res;\n\t while(frontChar.isWhite)\n\t popChar;\n\t while(!frontChar.isWhite)\n\t {\n\t res ~= frontChar;\n\t popChar;\n\t }\n\t return res;\n\t}\n else static if (is(T == char))\n\t{\n\t while(frontChar.isWhite) popChar;\n\t auto res = frontChar;\n\t popChar;\n\t return res;\n\t}\n else\n\t{\n\t return T.read;\n\t}\n }\n}\n"}], "src_uid": "eefea51c77b411640a3b92b9f2dd2cf1"} {"nl": {"description": "You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly ti milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer ai to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than ai problems overall (including problem i).Formally, suppose you solve problems p1, p2, ..., pk during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≤ apj.You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do.", "input_spec": "The first line contains two integers n and T (1 ≤ n ≤ 2·105; 1 ≤ T ≤ 109) — the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next n lines contains two integers ai and ti (1 ≤ ai ≤ n; 1 ≤ ti ≤ 104). The problems are numbered from 1 to n.", "output_spec": "In the first line, output a single integer s — your maximum possible final score. In the second line, output a single integer k (0 ≤ k ≤ n) — the number of problems you should solve. In the third line, output k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the indexes of problems you should solve, in any order. If there are several optimal sets of problems, you may output any of them.", "sample_inputs": ["5 300\n3 100\n4 150\n4 80\n2 90\n2 300", "2 100\n1 787\n2 788", "2 100\n2 42\n2 58"], "sample_outputs": ["2\n3\n3 1 4", "0\n0", "2\n2\n1 2"], "notes": "NoteIn the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points.In the second example, the length of the exam is catastrophically not enough to solve even a single problem.In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nalias Problem = Tuple!(int, \"num\", long, \"time\", int, \"index\");\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto T = s[1].to!long;\n auto P = new Problem[](N);\n foreach (i; 0..N) {\n s = readln.split.map!(to!int);\n P[i] = tuple(s[0], s[1].to!long, i+1);\n }\n P.sort!\"a[1] < b[1]\";\n\n int hi = N+1;\n int lo = 0;\n while (hi - lo > 1) {\n int mid = (hi + lo) / 2;\n long tmp = 0;\n int cnt = 0;\n foreach (p; P) {\n if (p.num < mid) continue;\n tmp += p.time;\n cnt += 1;\n if (cnt >= mid) break;\n }\n if (cnt >= mid && tmp <= T) {\n lo = mid;\n } else {\n hi = mid;\n }\n }\n\n int[] ans;\n int cnt = 0;\n foreach (p; P) {\n if (p.num >= lo) {\n ans ~= p.index;\n cnt += 1;\n }\n if (cnt >= lo) break;\n }\n\n lo.writeln;\n lo.writeln;\n if (lo == 0) writeln;\n else ans.map!(to!string).join(\" \").writeln;\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\n//import core.stdc.stdio;\nimport core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\n__gshared:\n\nalias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];\n\nauto getAddrTuple() {\n return tuple();\n}\n\nauto getAddrTuple(T, U...)(ref T head, ref U tail) {\n return tuple(&head, getAddrTuple(tail).expand);\n}\n\nbool read(T...)(ref T vars) if (vars.length > 0) {\n enum spec = ' ' ~ replicate(\"%s \", vars.length);\n return readf(spec, getAddrTuple(vars).expand) == vars.length;\n}\n\nvoid memSet(T, size_t n)(ref T[n] a, ubyte value) {\n memset(a.ptr, value, a.sizeof);\n}\n\nvoid memSet(T)(T[ ] a, ubyte value) {\n memset(a.ptr, value, a.length * T.sizeof);\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Problem {\n int id, limit, time;\n\n int opCmp(Problem rhs) const {\n return tuple(time, limit).opCmp(tuple(rhs.time, rhs.limit));\n }\n}\n\nint n, total;\nProblem[200_000] _problems;\nProblem[ ] problems;\nRedBlackTree!(Problem, q{a.id < b.id})[200_000] _solvedDist;\nRedBlackTree!(Problem, q{a.id < b.id})[ ] solvedDist;\nint curCount, bestCount;\nRedBlackTree!(Problem, q{a < b}, true) solved;\n\nvoid run(alias callback)() {\n generate!(redBlackTree!(q{a.id < b.id}, Problem)).take(n).copy(solvedDist);\n int curTime = 0;\n curCount = 0;\n auto solvable = redBlackTree!true(problems);\n solved = redBlackTree!(true, Problem);\n foreach (c; 1 .. n + 1) {\n while (!solved.empty && !solvable.empty && solvable.front.time < solved.front.time) {\n const a = solvable.front;\n solvable.removeFront();\n if (c <= a.limit) {\n const b = solved.front;\n solved.removeFront();\n solvedDist[b.limit - 1].removeKey(b);\n curTime -= b.time - a.time;\n solved.insert(a);\n solvedDist[a.limit - 1].insert(a);\n if (c <= b.limit)\n solvable.insert(b);\n }\n }\n while (solved.length < c && !solvable.empty && curTime + solvable.front.time <= total) {\n const a = solvable.front;\n solvable.removeFront();\n if (c <= a.limit) {\n curTime += a.time;\n curCount++;\n solved.insert(a);\n solvedDist[a.limit - 1].insert(a);\n if (callback())\n return;\n }\n }\n foreach (p; solvedDist[c - 1]) {\n assert(p.limit == c);\n solved.removeKey(p);\n curTime -= p.time;\n curCount--;\n }\n solvedDist[c - 1] = null;\n }\n}\n\nvoid main() {\n while (read(n, total)) {\n problems = _problems[0 .. n];\n solvedDist = _solvedDist[0 .. n];\n foreach (int i, ref p; problems) {\n p.id = i + 1;\n read(p.limit, p.time);\n }\n\n problems.multiSort!(q{a.limit < b.limit}, q{a.time < b.time});\n bestCount = 0;\n run!({\n if (curCount > bestCount)\n bestCount = curCount;\n return false;\n });\n\n writeln(bestCount, '\\n', bestCount);\n run!({\n if (curCount != bestCount)\n return false;\n writefln(\"%(%s %)\", solved[ ].map!q{a.id});\n return true;\n });\n debug writeln();\n }\n\n stdout.flush();\n _Exit(0);\n}\n"}, {"source_code": "import core.bitop, std.algorithm, std.bigint, std.bitmanip, std.complex,\n\tstd.container, std.conv, std.datetime, std.functional, std.math, std.meta;\nimport std.numeric : Fft, fft, gcd, inverseFft;\nimport std.parallelism, std.random, std.range, std.regex, std.stdio, std.string,\n\tstd.traits, std.typecons, std.uni, std.variant;\n\nalias pair(X, Y) = Tuple!(X, \"fi\", Y, \"se\");\nalias mp = make_pair;\nalias pii = pair!(int, int);\nalias pll = pair!(long, long);\n// alias gcd = std.numeric.gcd;\nalias lint = long;\nalias rbt = RedBlackTree;\n/// struct vector as c++ vector\nstruct Vec(T)\n{\n\tprivate Array!T buf;\n\talias buf this;\n\tthis(this)\n\t{\n\t\tbuf = buf.dup;\n\t}\n\t/// construct from length\n\tthis(U)(U length)\n\t\t\tif (is(U == int) || is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint))\n\t{\n\t\tbuf.length = length;\n\t}\n\t/// construct from length and default value\n\tthis(U, X)(U length, X val)\n\t\t\tif (isImplicitlyConvertible!(X, T) || (is(U == int)\n\t\t\t\t|| is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint)))\n\t{\n\t\tbuf.length = length;\n\t\tforeach (ref x; buf[])\n\t\t{\n\t\t\tx = val;\n\t\t}\n\t}\n\t/// construct from other array\n\tthis(U)(U[] values...) if (isImplicitlyConvertible!(U, T))\n\t{\n\t\tbuf = Array!(T)(values);\n\t}\n\t/// construct from other range\n\tthis(Range)(Range r)\n\t\t\tif (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range,\n\t\t\t\tT) && !is(Range == T[]))\n\t{\n\t\tbuf = Array!(T)(r);\n\t}\n\t/// integer length\n\tint size() @property const\n\t{\n\t\treturn cast(int) buf.length;\n\t}\n}\n///modulo\nenum mm = 10 ^^ 9 + 7, mm2 = mm + 2;\npublic\n{\n\tpure nothrow\n\t{\n\t\t///lcm\n\t\tT lcm(T)(auto ref T a, auto ref T b)\n\t\t{\n\t\t\treturn a / gcd(a, b) * b;\n\t\t}\n\t\t///make_pair\n\t\tpair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)\n\t\t{\n\t\t\treturn tuple!(X, \"fi\", Y, \"se\")(x_, y_);\n\t\t}\n\t\t///BigInt greatest common divizor\n\t\tBigInt biggcd(BigInt a, BigInt b)\n\t\t{\n\t\t\tBigInt res = 1;\n\t\t\twhile (b)\n\t\t\t{\n\t\t\t\tif ((a & 1) && (b & 1))\n\t\t\t\t{\n\t\t\t\t\ta -= b;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (a & 1)\n\t\t\t\t{\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (b & 1)\n\t\t\t\t{\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres <<= 1;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\tif (a < b)\n\t\t\t\t\tswap(a, b);\n\t\t\t}\n\t\t\treturn res * a;\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).lowerBound(g).length;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn a.length - assumeSorted(a).upperBound(g).length;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).contains(g);\n\t\t}\n\t}\n\t///write an array\n\tvoid putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)\n\t{\n\t\twritefln(\"%(%s\" ~ ch ~ \"%)\", a);\n\t}\n\t///write a matrix\n\tvoid putarr(X)(in X a)\n\t\t\tif (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))\n\t{\n\t\twritefln(\"%(%(%s %)\\n%)\", a);\n\t}\n\t///get an array\n\tbool getarr(X)(X a, in size_t n)\n\t{\n\t\tbool b = 1;\n\t\tforeach (ref i; a[0 .. n])\n\t\t\tb = input(&i);\n\t\treturn b;\n\t}\n\t///read without format\n\tbool input(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\treturn readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t}\n\t///readln without format\n\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\tconst bool fl = readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\treadln;\n\t\treturn fl;\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tint n, T;\n\tread(n, T);\n\tauto a = new Tuple!(int, int, int)[n];\n\tforeach (i; 0 .. n)\n\t{\n\t\tread(a[i][0], a[i][1]);\n\t\ta[i][2] = i + 1;\n\t}\n\tsort!\"a[1]>= 1;\n\t\t\t\t}\n\t\t\t\telse if (a & 1)\n\t\t\t\t{\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (b & 1)\n\t\t\t\t{\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres <<= 1;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\tif (a < b)\n\t\t\t\t\tswap(a, b);\n\t\t\t}\n\t\t\treturn res * a;\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).lowerBound(g).length;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn a.length - assumeSorted(a).upperBound(g).length;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).contains(g);\n\t\t}\n\t}\n\t///write an array\n\tvoid putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)\n\t{\n\t\twritefln(\"%(%s\" ~ ch ~ \"%)\", a);\n\t}\n\t///write a matrix\n\tvoid putarr(X)(in X a)\n\t\t\tif (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))\n\t{\n\t\twritefln(\"%(%(%s %)\\n%)\", a);\n\t}\n\t///get an array\n\tbool getarr(X)(X a, in size_t n)\n\t{\n\t\tbool b = 1;\n\t\tforeach (ref i; a[0 .. n])\n\t\t\tb = input(&i);\n\t\treturn b;\n\t}\n\t///read without format\n\tbool input(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\treturn readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t}\n\t///readln without format\n\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\tconst bool fl = readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\treadln;\n\t\treturn fl;\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tint n, T;\n\tread(n, T);\n\tauto a = new Tuple!(int, int, int)[n];\n\tforeach (i; 0 .. n)\n\t{\n\t\tread(a[i][0], a[i][1]);\n\t\ta[i][2] = i + 1;\n\t}\n\tsort!\"a[1] 1) {\n int mid = (hi + lo) / 2;\n long tmp = 0;\n int cnt = 0;\n foreach (p; P) {\n if (p.num < mid) continue;\n tmp += p.time;\n cnt += 1;\n if (cnt >= mid) break;\n }\n if (cnt >= mid && tmp <= T) {\n lo = mid;\n } else {\n hi = mid;\n }\n }\n\n int[] ans;\n foreach (i; 0..lo) if (P[i].num >= lo) ans ~= P[i].index;\n lo.writeln;\n lo.writeln;\n ans.map!(to!string).join(\" \").writeln;\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\n//import core.stdc.stdio;\nimport core.stdc.stdlib: alloca, malloc, calloc, realloc, _Exit;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\n__gshared:\n\nalias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];\n\nauto getAddrTuple() {\n return tuple();\n}\n\nauto getAddrTuple(T, U...)(ref T head, ref U tail) {\n return tuple(&head, getAddrTuple(tail).expand);\n}\n\nbool read(T...)(ref T vars) if (vars.length > 0) {\n enum spec = ' ' ~ replicate(\"%s \", vars.length);\n return readf(spec, getAddrTuple(vars).expand) == vars.length;\n}\n\nvoid memSet(T, size_t n)(ref T[n] a, ubyte value) {\n memset(a.ptr, value, a.sizeof);\n}\n\nvoid memSet(T)(T[ ] a, ubyte value) {\n memset(a.ptr, value, a.length * T.sizeof);\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Problem {\n int id, limit, time;\n\n int opCmp(Problem rhs) const {\n return tuple(time, limit).opCmp(tuple(rhs.time, rhs.limit));\n }\n}\n\nProblem[200_000] _problems;\nRedBlackTree!(Problem, q{a.id < b.id})[200_000] _solvedDist;\n\nvoid main() {\n int n, total;\n while (read(n, total)) {\n auto problems = _problems[0 .. n];\n auto solvedDist = _solvedDist[0 .. n];\n foreach (int i, ref p; problems) {\n p.id = i + 1;\n read(p.limit, p.time);\n }\n\n problems.multiSort!(q{a.limit < b.limit}, q{a.time < b.time});\n generate!(redBlackTree!(q{a.id < b.id}, Problem)).take(n).copy(solvedDist);\n int bestTime = 10^^9 + 1, bestCount = 0;\n int curTime = 0, curCount = 0;\n auto solvable = redBlackTree!(true, Problem);\n auto solved = redBlackTree!(true, Problem);\n int pLastIndex = 0;\n foreach (c; 1 .. n + 1) {\n for (; pLastIndex < n && problems[pLastIndex].limit <= c; pLastIndex++)\n solvable.insert(problems[pLastIndex]);\n while (!solved.empty && !solvable.empty && solvable.front.time < solved.front.time) {\n const a = solvable.front;\n solvable.removeFront();\n if (c <= a.limit) {\n const b = solved.front;\n solved.removeFront();\n solvedDist[b.limit - 1].removeKey(b);\n curTime -= b.time - a.time;\n solved.insert(a);\n solvedDist[a.limit - 1].insert(a);\n if (c <= b.limit)\n solvable.insert(b);\n }\n }\n while (solved.length < c && !solvable.empty && curTime + solvable.front.time <= total) {\n const a = solvable.front;\n solvable.removeFront();\n if (c <= a.limit) {\n curTime += a.time;\n curCount++;\n solved.insert(a);\n solvedDist[a.limit - 1].insert(a);\n if (curCount > bestCount) {\n bestTime = curTime;\n bestCount = curCount;\n }\n }\n }\n foreach (p; solvedDist[c - 1]) {\n assert(p.limit == c);\n solved.removeKey(p);\n curTime -= p.time;\n curCount--;\n }\n solvedDist[c - 1] = null;\n }\n\n writeln(bestCount, '\\n', bestCount);\n generate!(redBlackTree!(q{a.id < b.id}, Problem)).take(n).copy(solvedDist);\n curTime = 0;\n curCount = 0;\n solvable.clear();\n solved.clear();\n pLastIndex = 0;\n restoreResult:\n foreach (c; 1 .. n + 1) {\n for (; pLastIndex < n && problems[pLastIndex].limit <= c; pLastIndex++)\n solvable.insert(problems[pLastIndex]);\n while (!solved.empty && !solvable.empty && solvable.front.time < solved.front.time) {\n const a = solvable.front;\n solvable.removeFront();\n if (c <= a.limit) {\n const b = solved.front;\n solved.removeFront();\n solvedDist[b.limit - 1].removeKey(b);\n curTime -= b.time - a.time;\n solved.insert(a);\n solvedDist[a.limit - 1].insert(a);\n if (c <= b.limit)\n solvable.insert(b);\n }\n }\n while (solved.length < c && !solvable.empty && curTime + solvable.front.time <= total) {\n const a = solvable.front;\n solvable.removeFront();\n if (c <= a.limit) {\n curTime += a.time;\n curCount++;\n solved.insert(a);\n solvedDist[a.limit - 1].insert(a);\n if (curCount == bestCount) {\n writefln(\"%(%s %)\", solved[ ].map!q{a.id});\n break restoreResult;\n }\n }\n }\n foreach (p; solvedDist[c - 1]) {\n assert(p.limit == c);\n solved.removeKey(p);\n curTime -= p.time;\n curCount--;\n }\n solvedDist[c - 1] = null;\n }\n debug writeln();\n }\n\n stdout.flush();\n _Exit(0);\n}\n"}], "src_uid": "5294cad0d35a6c8ed40a8322ba5fd7b1"} {"nl": {"description": " Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters \"a\", \"b\" and \"c\". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string \"abc\" as a substring. A valid replacement of a character is replacing it with \"a\", \"b\" or \"c\".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \\le n, q \\le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters \"a\", \"b\" and \"c\". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \\le i \\le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is \"a\", \"b\" or \"c\".", "output_spec": "For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain \"abc\" as a substring.", "sample_inputs": ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"], "sample_outputs": ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"], "notes": "NoteLet's consider the state of the string after each query: $$$s =$$$ \"abcabcabc\". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bbcaccabb\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bbcabcabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bbcbbcbbc\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bccabcabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bccbbcbbc\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcaabcabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcbbc\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabbcabc\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcabb\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabccabc\". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcabb\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabccaac\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcaac\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"bcabccaab\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"bcabbcaab\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"ccabccaab\". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ \"ccabbcaab\". This string does not contain \"abc\" as a substring. $$$s =$$$ \"ccaaccaab\". In this case the string does not contain \"abc\" as a substring and no replacements are needed."}, "positive_code": [{"source_code": "immutable multi = false;\n\nvoid solve(int tc)\n{\n auto n = readInt!int;\n auto q = readInt!int;\n auto s = cast(char[])readString;\n auto cnt = new int[](n);\n int sm;\n int isAbc(int i)\n {\n if (i + 3 > n) return 0;\n return int(s[i .. i + 3] == \"abc\");\n }\n foreach(i, ref ci; cnt)\n {\n ci = isAbc(cast(int)i);\n }\n sm = cnt.sum;\n void reCalc(int i)\n {\n if (i < 0 || i >= n) return;\n auto ld = cnt[i];\n auto nw = isAbc(i);\n auto dlt = nw - ld;\n cnt[i] = nw;\n sm += dlt;\n }\n foreach(qi; 0 .. q)\n {\n auto pos = readInt!int - 1;\n auto cString = readString;\n auto c = cString[0];\n s[pos] = c;\n foreach(p; pos - 3 .. pos + 1)\n\t{\n\t reCalc(p);\n\t}\n sm.writeln;\n }\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nbool check_abc(char[] s, int i)\n{\n if (s[i] == 'a') {\n if (i + 2 < s.length && s[i + 1] == 'b' && s[i + 2] == 'c')\n return true;\n } else if (s[i] == 'b') {\n if (i - 1 >= 0 && i + 1 < s.length && s[i - 1] == 'a' && s[i + 1] == 'c')\n return true;\n } else if (s[i] == 'c'){\n if (i - 2 >= 0 && s[i - 1] == 'b' && s[i - 2] == 'a')\n return true;\n }\n return false;\n}\n\nvoid main()\n{\n long n, q;\n readf!\" %d %d \"(n, q);\n char[] s = readln.strip.dup;\n long ans = 0;\n char last = 'c';\n foreach (ch ; s) {\n if (ch == 'a')\n last = 'a';\n else if (ch == 'b' && last == 'a')\n last = 'b';\n else if (ch == 'c' && last == 'b') {\n ans++;\n last = 'c';\n } else\n last = 'c';\n }\n while (q-- > 0) {\n int pos;\n char ch;\n readf!\" %d %c \"(pos, ch);\n pos--;\n if (check_abc(s, pos))\n ans--;\n s[pos] = ch;\n if (check_abc(s, pos))\n ans++;\n writeln(ans);\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tint n, q;\r\n\twhile (readf !(\" %s %s\") (n, q) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto s = ['x', 'x'] ~ readln.strip.dup ~ ['x', 'x'];\r\n\t\tauto cur = s.count (\"abc\");\r\n\t\tforeach (j; 0..q)\r\n\t\t{\r\n\t\t\tint i;\r\n\t\t\tchar ch;\r\n\t\t\treadf !(\" %s %s\") (i, ch);\r\n\t\t\ti += 1;\r\n\t\t\tcur -= (s[i - 2..i + 1] == \"abc\");\r\n\t\t\tcur -= (s[i - 1..i + 2] == \"abc\");\r\n\t\t\tcur -= (s[i - 0..i + 3] == \"abc\");\r\n\t\t\ts[i] = ch;\r\n\t\t\tcur += (s[i - 2..i + 1] == \"abc\");\r\n\t\t\tcur += (s[i - 1..i + 2] == \"abc\");\r\n\t\t\tcur += (s[i - 0..i + 3] == \"abc\");\r\n\t\t\twriteln (cur);\r\n\t\t}\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "db473ad780a93983667d12b1357c6e2f"} {"nl": {"description": "Let us define two functions f and g on positive integer numbers. You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers x between l and r inclusive, such that g(x) = k. ", "input_spec": "The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries. Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).", "output_spec": "For each query, print a single line containing the answer for that query.", "sample_inputs": ["4\n22 73 9\n45 64 6\n47 55 7\n2 62 4", "4\n82 94 6\n56 67 4\n28 59 9\n39 74 4"], "sample_outputs": ["1\n4\n0\n8", "3\n1\n1\n5"], "notes": "NoteIn the first example: g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9 g(47) = g(48) = g(60) = g(61) = 6 There are no such integers between 47 and 55. g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4 "}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 10 ^^ 6 + 6;\n\nauto gm = new int [limit];\n\nint g (int n)\n{\n\tif (gm[n] == 0)\n\t{\n\t\tif (n < 10)\n\t\t{\n\t\t\tgm[n] = n;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tgm[n] = n.text\n\t\t\t .map !(q{a - '0'})\n\t\t\t .filter !(q{a > 0})\n\t\t\t .fold !(q{a * b}) (1)\n\t\t\t .g;\n\t\t}\n\t}\n\treturn gm[n];\n}\n\nvoid main ()\n{\n\tauto gm = limit.iota.map !(g).array;\n\tauto c = new int [limit] [10];\n\tforeach (d; 1..10)\n\t{\n\t\tforeach (i; 1..limit)\n\t\t{\n\t\t\tc[d][i] = c[d][i - 1] + (gm[i] == d);\n\t\t}\n\t}\n\n\tint q;\n\twhile (readf (\" %s\", &q) > 0)\n\t{\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint lo, hi, k;\n\t\t\treadf (\" %s %s %s\", &lo, &hi, &k);\n\t\t\twriteln (c[k][hi] - c[k][lo - 1]);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum LIM = 10^^6 + 10;\n\nvoid main() {\n auto fs = new int[LIM];\n fs[0] = 1;\n foreach (n; 1 .. LIM) {\n fs[n] = fs[n / 10] * ((n % 10 == 0) ? 1 : (n % 10));\n }\n auto gs = new int[LIM];\n foreach (n; 1 .. LIM) {\n gs[n] = (n < 10) ? n : gs[fs[n]];\n }\n \n auto nums = new int[][](10, LIM);\n foreach (k; 0 .. 10) {\n foreach (n; 1 .. LIM) {\n nums[k][n] = nums[k][n - 1] + ((gs[n] == k) ? 1 : 0);\n }\n }\n \n try {\n for (; ; ) {\n const Q = readInt();\n foreach (q; 0 .. Q) {\n const l = readInt();\n const r = readInt();\n const k = readInt();\n const ans = nums[k][r] - nums[k][l - 1];\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "7a647a5f10cdcd2b54a1927107edea4f"} {"nl": {"description": "A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.", "input_spec": "The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).", "output_spec": "Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.", "sample_inputs": ["1033", "10", "11"], "sample_outputs": ["33", "0", "-1"], "notes": "NoteIn the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nstring solve (int [] a, int rem, int num)\n{\n\tauto n = cast (int) (a.length);\n\tforeach_reverse (i; 0..n)\n\t{\n\t\tif (a[i] % 3 == rem)\n\t\t{\n\t\t\ta[i] = -1;\n\t\t\tnum -= 1;\n\t\t\tif (num == 0)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif (num > 0)\n\t{\n\t\treturn \"\";\n\t}\n\ta = a.filter !(x => x != -1).array;\n\twhile (a.length > 1 && a[0] == 0)\n\t{\n\t\ta = a[1..$];\n\t}\n\tif (a.length == 0)\n\t{\n\t\treturn \"\";\n\t}\n\treturn a.map !(x => cast (char) (x + '0')).text;\n}\n\nstring best (string a, string b)\n{\n\tif (a.length > b.length)\n\t{\n\t\treturn a;\n\t}\n\treturn b;\n}\n\nvoid main ()\n{\n\tstring s;\n\twhile ((s = readln.strip) != \"\")\n\t{\n\t\tauto a = s.map !(x => cast (int) (x - '0')).array;\n\t\tauto total = cast (int) (sum (a) % 3);\n\t\tauto res = \"\";\n\t\tif (total == 0)\n\t\t{\n\t\t\tres = s;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tres = best (res, solve (a.dup, total, 1));\n\t\t\tres = best (res, solve (a.dup, 3 - total, 2));\n\t\t}\n\t\tif (res == \"\")\n\t\t{\n\t\t\tres = \"-1\";\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "df9942d1eb66b1f3b5c6b665b446cd3e"} {"nl": {"description": "The city of Fishtopia can be imagined as a grid of $$$4$$$ rows and an odd number of columns. It has two main villages; the first is located at the top-left cell $$$(1,1)$$$, people who stay there love fishing at the Tuna pond at the bottom-right cell $$$(4, n)$$$. The second village is located at $$$(4, 1)$$$ and its people love the Salmon pond at $$$(1, n)$$$.The mayor of Fishtopia wants to place $$$k$$$ hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.A person can move from one cell to another if those cells are not occupied by hotels and share a side.Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?", "input_spec": "The first line of input contain two integers, $$$n$$$ and $$$k$$$ ($$$3 \\leq n \\leq 99$$$, $$$0 \\leq k \\leq 2\\times(n-2)$$$), $$$n$$$ is odd, the width of the city, and the number of hotels to be placed, respectively.", "output_spec": "Print \"YES\", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print \"NO\". If it is possible, print an extra $$$4$$$ lines that describe the city, each line should have $$$n$$$ characters, each of which is \"#\" if that cell has a hotel on it, or \".\" if not.", "sample_inputs": ["7 2", "5 3"], "sample_outputs": ["YES\n.......\n.#.....\n.#.....\n.......", "YES\n.....\n.###.\n.....\n....."], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto W = s[0];\n auto K = s[1];\n auto A = new bool[][](4, W);\n auto C = W/2;\n\n \"YES\".writeln;\n\n outer: foreach (i; 1..3) {\n foreach (j; 1..C) {\n if (K <= 1) break outer;\n A[i][j] = true;\n A[i][W-j-1] = true;\n K -= 2;\n }\n }\n\n foreach (i; 1..3) {\n if (K == 0) break;\n A[i][C] = true;\n K -= 1;\n }\n\n foreach (i; 0..4) A[i].map!(a => a ? '#' : '.').writeln;\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\nimport std.numeric;\n\nvoid main()\n{\n\tint n, k;\n\treadln.chomp.split.tie(n, k);\n\tif (2 * (n - 2) < k) {\n\t\t\"NO\".writeln;\n\t\treturn;\n\t}\n\t\"YES\".writeln;\n\tchar[][] ans = new char[][](4, n);\n\tforeach (i; 0..4) {\n\t\tforeach (j; 0..n) {\n\t\t\tans[i][j] = '.';\n\t\t}\n\t}\n\t// vertical\n\tfor (int i = 1; i <= 2; ++i) {\n\t\tif (k < n - 2) {\n\t\t\tbreak;\n\t\t}\n\t\tfor (int j = 1; j < n - 1; ++j) {\n\t\t\tans[i][j] = '#';\n\t\t}\n\t\tk -= n - 2;\n\t}\n\timmutable MID = n / 2;\n\tif (k % 2 == 1) {\n\t\tans[2][MID] = '#';\n\t\t--k;\n\t}\n\tfor (int i = 1; k > 0; ++i) {\n\t\tans[2][MID - i] = ans[2][MID + i] = '#';\n\t\tk -= 2;\n\t}\n\tforeach (s; ans) {\n\t\tforeach (c; s) {\n\t\t\tc.write;\n\t\t}\n\t\twriteln;\n\t}\n}\n\nvoid tie(R, Args...)(R arr, ref Args args)\n\t\tif (isRandomAccessRange!R || isArray!R)\nin\n{\n\tassert (arr.length == args.length);\n}\nbody\n{\n\tforeach (i, ref v; args) {\n\t\talias T = typeof(v);\n\t\tv = arr[i].to!T;\n\t}\n}\n\nvoid verbose(Args...)(in Args args)\n{\n\tstderr.write(\"[\");\n\tforeach (i, ref v; args) {\n\t\tif (i) stderr.write(\", \");\n\t\tstderr.write(v);\n\t}\n\tstderr.writeln(\"]\");\n}\n\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nvoid main()\n{\n int n, k; readV(n, k);\n\n auto b = new bool[][](4, n);\n\n if (k%2 == 0) {\n foreach (i; 0..k/2)\n b[1][i+1] = b[2][i+1] = true;\n } else if (k <= n-2) {\n foreach (i; 0..k)\n b[1][i+(n-k)/2] = true;\n } else {\n foreach (i; 0..n-2)\n b[1][i+1] = true;\n auto k2 = k-(n-2)+1;\n foreach (i; 0..k2)\n b[2][i+(n-k2)/2] = true;\n b[2][n/2] = false;\n }\n\n writeln(\"YES\");\n foreach (i; 0..4) {\n foreach (j; 0..n)\n write(b[i][j] ? \"#\" : \".\");\n writeln;\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\nimport std.numeric;\n\nvoid main()\n{\n\tint n, k;\n\treadln.chomp.split.tie(n, k);\n\tif (2 * (n - 2) < k) {\n\t\t\"NO\".writeln;\n\t\treturn;\n\t}\n\tchar[][] ans = new char[][](4, n);\n\tforeach (i; 0..4) {\n\t\tforeach (j; 0..n) {\n\t\t\tans[i][j] = '.';\n\t\t}\n\t}\n\tloop:\n\tfor (int i = 1; i <= 2; ++i) {\n\t\tfor (int j = 1; j < n - 1; ++j) {\n\t\t\tif (k == 0) {\n\t\t\t\tbreak loop;\n\t\t\t}\n\t\t\tans[i][j] = '#';\n\t\t\t--k;\n\t\t}\n\t}\n\t\"YES\".writeln;\n\tforeach (s; ans) {\n\t\tforeach (c; s) {\n\t\t\twritef(\"%c\", c);\n\t\t}\n\t\twriteln;\n\t}\n}\n\nvoid tie(R, Args...)(R arr, ref Args args)\n\t\tif (isRandomAccessRange!R || isArray!R)\nin\n{\n\tassert (arr.length == args.length);\n}\nbody\n{\n\tforeach (i, ref v; args) {\n\t\talias T = typeof(v);\n\t\tv = arr[i].to!T;\n\t}\n}\n\nvoid verbose(Args...)(in Args args)\n{\n\tstderr.write(\"[\");\n\tforeach (i, ref v; args) {\n\t\tif (i) stderr.write(\", \");\n\t\tstderr.write(v);\n\t}\n\tstderr.writeln(\"]\");\n}\n\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\nimport std.numeric;\n\nvoid main()\n{\n\tint n, k;\n\treadln.chomp.split.tie(n, k);\n\tif (2 * (n - 2) < k) {\n\t\t\"NO\".writeln;\n\t\treturn;\n\t}\n\tchar[][] ans = new char[][](4, n);\n\tforeach (i; 0..4) {\n\t\tforeach (j; 0..n) {\n\t\t\tans[i][j] = '.';\n\t\t}\n\t}\n\t// vertical\n\tfor (int i = 1; i <= 2; ++i) {\n\t\tif (k < n - 2) {\n\t\t\tbreak;\n\t\t}\n\t\tfor (int j = 1; j < n - 1; ++j) {\n\t\t\tans[i][j] = '#';\n\t\t}\n\t\tk -= n - 2;\n\t}\n\timmutable MID = n / 2;\n\tif (k % 2 == 1) {\n\t\tans[2][MID] = '#';\n\t\t--k;\n\t}\n\tfor (int i = 1; k > 0; ++i) {\n\t\tans[2][MID - i] = ans[2][MID + i] = '#';\n\t\tk -= 2;\n\t}\n\tforeach (s; ans) {\n\t\tforeach (c; s) {\n\t\t\tc.write;\n\t\t}\n\t\twriteln;\n\t}\n}\n\nvoid tie(R, Args...)(R arr, ref Args args)\n\t\tif (isRandomAccessRange!R || isArray!R)\nin\n{\n\tassert (arr.length == args.length);\n}\nbody\n{\n\tforeach (i, ref v; args) {\n\t\talias T = typeof(v);\n\t\tv = arr[i].to!T;\n\t}\n}\n\nvoid verbose(Args...)(in Args args)\n{\n\tstderr.write(\"[\");\n\tforeach (i, ref v; args) {\n\t\tif (i) stderr.write(\", \");\n\t\tstderr.write(v);\n\t}\n\tstderr.writeln(\"]\");\n}\n\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nvoid main()\n{\n int n, k; readV(n, k);\n\n auto isHotel(int i, int j)\n {\n if (i == 0 || i == 3 || j == 0 || j == n-1) return false;\n auto a = j%2 == 1 ? (j-1)*2+(i-1) : (j-2)*2+(i-1)+(n-1);\n return a < k;\n }\n\n writeln(\"YES\");\n foreach (i; 0..4) {\n foreach (j; 0..n) {\n write(isHotel(i, j) ? \"#\" : \".\");\n }\n writeln;\n }\n}\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nvoid main()\n{\n int n, k; readV(n, k);\n\n auto isHotel(int i, int j)\n {\n if (i == 0 || i == 3 || j == 0 || j == n-1) return false;\n auto a = j%2 == 1 ? (j-1)+(i-1) : (j-2)+(i-1)+(n-1);\n return a < k;\n }\n\n writeln(\"YES\");\n foreach (i; 0..4) {\n foreach (j; 0..n) {\n write(isHotel(i, j) ? \"#\" : \".\");\n }\n writeln;\n }\n}\n"}], "src_uid": "2669feb8200769869c4b2c29012059ed"} {"nl": {"description": "Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done! To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible. Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1. ", "input_spec": "The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n. The second line contains 2k distinct integers u1, u2, ..., u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located. The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads. ", "output_spec": "Print the maximum possible sum of distances in the division of universities into k pairs.", "sample_inputs": ["7 2\n1 5 6 2\n1 3\n3 2\n4 5\n3 7\n4 3\n4 6", "9 3\n3 2 1 6 5 9\n8 9\n3 2\n2 7\n3 4\n7 6\n4 5\n2 1\n2 8"], "sample_outputs": ["6", "9"], "notes": "NoteThe figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\n\nvoid main ()\n{\n\tint n, k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\tauto a = new int [] [n];\n\t\tauto b = new bool [n];\n\t\tb[] = false;\n\t\tforeach (i; 0..k * 2)\n\t\t{\n\t\t\tint v;\n\t\t\treadf (\" %s\", &v);\n\t\t\tv--;\n\t\t\tb[v] = true;\n\t\t}\n\t\tforeach (i; 0..n - 1)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf (\" %s %s\", &u, &v);\n\t\t\tu--;\n\t\t\tv--;\n\t\t\ta[u] ~= v;\n\t\t\ta[v] ~= u;\n\t\t}\n\n\t\tlong res = 0;\n\n\t\tint recur (int v, int p)\n\t\t{\n\t\t\tint num = 0;\n\t\t\tnum += b[v];\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\tif (u != p)\n\t\t\t\t{\n\t\t\t\t\tint t = recur (u, v);\n\t\t\t\t\tres += min (t, 2 * k - t);\n\t\t\t\t\tnum += t;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn num;\n\t\t}\n\n\t\trecur (0, -1);\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.stdio;\nimport std.algorithm;\n\nint N, K;\nint[][] G;\nint[] d;\nlong ans = 0;\n\nvoid dfs(int v, int par) {\n\tforeach (u; G[v]) {\n\t\tif (u == par) continue;\n\t\tdfs(u, v); d[v] += d[u];\n\t}\n\tans += min(d[v], 2 * K - d[v]);\n}\n\nvoid main() {\n\treadf(\"%d %d\", &N, &K);\n\tG = new int[][N + 1];\n\td = new int[N + 1];\n\tforeach (i; 0..2*K) {\n\t\tint In; readf(\" %d\", &In); d[In] = 1;\n\t}\n\tforeach (i; 1..N) {\n\t\tint u, v; readf(\" %d %d\", &u, &v);\n\t\tG[u] ~= v; G[v] ~= u;\n\t}\n\tdfs(1, 0);\n\twriteln(ans);\n}"}], "negative_code": [], "src_uid": "ca22cf92727a38fbb3c085b9362602db"} {"nl": {"description": "Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill: the graph contains exactly 2n + p edges; the graph doesn't contain self-loops and multiple edges; for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges. A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. Your task is to find a p-interesting graph consisting of n vertices.", "input_spec": "The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; ) — the number of vertices in the graph and the interest value for the appropriate test. It is guaranteed that the required graph exists.", "output_spec": "For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n. Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.", "sample_inputs": ["1\n6 0"], "sample_outputs": ["1 2\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6"], "notes": null}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid solve(int n, int p) {\n Tuple!(int, int)[] ans;\n foreach (i; 0 .. n) {\n ans ~= tuple(i, (i+1) % n);\n }\n foreach (i; 0 .. n) {\n ans ~= tuple(i, (i+2) % n);\n }\n int v = 0, d = 3;\n foreach (_; 0 .. p) {\n ans ~= tuple(v, (v+d) % n);\n v = (v+1) % n;\n if (v == 0) ++d;\n }\n \n ans.each!(t => writeln(t[0]+1, ' ', t[1]+1));\n}\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n \n while (t--) {\n int n, p;\n readf(\"%s %s\", &n, &p);\n readln;\n \n solve(n, p);\n }\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nTuple!(int, int)[] solve(int n, int p) {\n Tuple!(int, int)[] ans;\n foreach (i; 0 .. n) {\n ans ~= tuple(i, (i+1) % n);\n }\n foreach (i; 0 .. n) {\n ans ~= tuple(i, (i+2) % n);\n }\n int v = 0, d = 3;\n foreach (_; 0 .. p) {\n ans ~= tuple(v, (v+d) % n);\n v = (v+1) % n;\n if (v == 0) ++d;\n }\n \n foreach (ref rw; ans) rw[0] += 1, rw[1] += 1;\n return ans;\n}\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n \n while (t--) {\n int n, p;\n readf(\"%s %s\", &n, &p);\n readln;\n \n auto ans = solve(n, p);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint tests;\n\treadf (\" %s\", &tests);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, p;\n\t\treadf (\" %s %s\", &n, &p);\n\t\tauto a = new bool [] [] (n, n);\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint j = (i + 1) % n;\n\t\t\ta[i][j] = a[j][i] = true;\n\t\t\tint k = (j + 1) % n;\n\t\t\ta[i][k] = a[k][i] = true;\n\t\t}\n\t\tint m = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tforeach (j; i + 1..n)\n\t\t\t{\n\t\t\t\tif (m < p && !a[i][j])\n\t\t\t\t{\n\t\t\t\t\tm++;\n\t\t\t\t\ta[i][j] = a[j][i] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tforeach (j; i + 1..n)\n\t\t\t{\n\t\t\t\tif (a[i][j])\n\t\t\t\t{\n\t\t\t\t\twritefln (\"%s %s\", i + 1, j + 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid solve(int n, int p) {\n Tuple!(int, int)[] ans;\n foreach (i; 0 .. n) {\n ans ~= tuple(i, (i+1) % n);\n }\n foreach (i; 0 .. n) {\n ans ~= tuple(i, (i+2) % n);\n }\n int v = 1, d = 3;\n foreach (_; 0 .. p) {\n ans ~= tuple(v, (v+d) % n);\n v = (v+1) % n;\n if (v == 0) ++d;\n }\n \n ans.each!(t => writeln(t[0]+1, ' ', t[1]+1));\n}\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n \n while (t--) {\n int n, p;\n readf(\"%s %s\", &n, &p);\n readln;\n \n solve(n, p);\n }\n}"}], "src_uid": "ddbac4053bd07eada84bc44275367ae2"} {"nl": {"description": "Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas). Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa. Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him оf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.", "input_spec": "The first line contains four integers k, x, n, m (3 ≤ k ≤ 50; 0 ≤ x ≤ 109; 1 ≤ n, m ≤ 100).", "output_spec": "In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them. If the required pair of strings doesn't exist, print \"Happy new year!\" without the quotes.", "sample_inputs": ["3 2 2 2", "3 3 2 2", "3 0 2 2", "4 3 2 1", "4 2 2 1"], "sample_outputs": ["AC\nAC", "Happy new year!", "AA\nAA", "Happy new year!", "Happy new year!"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n long c;\n}\n\nP[] gen(int n) {\n if (n == 1) {\n return [P('A', 'A', 0),\n P('C', 'C', 0),\n P('X', 'X', 0)];\n }\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n if (n == 2 && s[x] == 'A' && s[y] == 'C') continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nlong stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n if (n == 1) {\n return p.a.to!string;\n }\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n foreach (i; 0 .. p.c) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.a != 'X') ret = p.a ~ ret;\n if (p.b != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nstring solve (int k, int x, int n, int m)\n{\n\tstring [2] res;\n\tbool found = false;\n\n\talias Tuple !(bool, \"b\", bool, \"e\", long, \"num\") part;\n\tauto cur = new part [k];\n\tint [2] d = [n, m];\n\n\tstring build (int p)\n\t{\n\t\tauto c = cur[p];\n\t\tstring res = \"\";\n\t\tif (c.b)\n\t\t{\n\t\t\tres ~= 'C';\n\t\t}\n\t\tforeach (i; 0..c.num)\n\t\t{\n\t\t\tres ~= \"AC\";\n\t\t}\n\t\tforeach (i; c.b + c.e + c.num * 2..d[p])\n\t\t{\n\t\t\tres ~= 'B';\n\t\t}\n\t\tif (c.e)\n\t\t{\n\t\t\tres ~= 'A';\n\t\t}\n\t\treturn res;\n\t}\n\n\tvoid gen (int p)\n\t{\n\t\tif (p == 2)\n\t\t{\n\t\t\tforeach (i; 2..k)\n\t\t\t{\n\t\t\t\tcur[i] = part (cur[i - 2].b,\n\t\t\t\t cur[i - 1].e,\n\t\t\t\t cur[i - 2].num +\n\t\t\t\t cur[i - 1].num +\n\t\t\t\t (cur[i - 2].e &&\n\t\t\t\t cur[i - 1].b));\n\t\t\t}\n\t\t\tif (cur[k - 1].num == x && !found)\n\t\t\t{\n\t\t\t\tforeach (i; 0..2)\n\t\t\t\t{\n\t\t\t\t\tres[i] = build (i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tforeach (b; 0..2)\n\t\t\t{\n\t\t\t\tforeach (e; 0..2)\n\t\t\t\t{\n\t\t\t\t\tint num = 0;\n\t\t\t\t\twhile (b + e + num * 2 <= d[p])\n\t\t\t\t\t{\n\t\t\t\t\t\tcur[p] = part (cast (bool) b,\n\t\t\t\t\t\t cast (bool) e,\n\t\t\t\t\t\t num);\n\t\t\t\t\t\tgen (p + 1);\n\t\t\t\t\t\tnum++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tgen (0);\n\n\tif (res == [\"\", \"\"])\n\t{\n\t\treturn \"Happy new year!\";\n\t}\n\treturn res[0] ~ '\\n' ~ res[1];\n}\n\nvoid main ()\n{\n\tint k, x, n, m;\n\twhile (readf (\" %s %s %s %s\", &k, &x, &n, &m) > 0)\n\t{\n\t\twriteln (solve (k, x, n, m));\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n int c;\n}\n\nP[] gen(int n) {\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nint stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n if (p.a != 'X') ret ~= p.a;\n foreach (i; 0 .. p.c) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.b != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n int c;\n}\n\nP[] gen(int n) {\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nint stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n foreach (i; 0 .. p.c) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.a != 'X') ret = p.a ~ ret;\n if (p.a != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n int c;\n}\n\nP[] gen(int n) {\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nint stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n foreach (i; 0 .. p.c) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.a != 'X') ret = p.a ~ ret;\n if (p.b != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n long c;\n}\n\nP[] gen(int n) {\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nlong stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n foreach (i; 0 .. p.c) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.a != 'X') ret = p.a ~ ret;\n if (p.b != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n long c;\n}\n\nP[] gen(int n) {\n if (n == 1) {\n return [P('A', 'A', 0),\n P('C', 'C', 0),\n P('X', 'X', 0)];\n }\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nlong stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n if (n == 1) {\n return p.a.to!string;\n }\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n foreach (i; 0 .. p.c) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.a != 'X') ret = p.a ~ ret;\n if (p.b != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nstruct P {\n char a, b;\n int c;\n}\n\nP[] gen(int n) {\n P[] ret;\n static const s = \"ACX\";\n foreach (i; 0 .. n / 2 + 1) {\n foreach (x; 0 .. 3) {\n foreach (y; 0 .. 3) {\n int r = n - i * 2;\n if (s[x] != 'X') r--;\n if (s[y] != 'X') r--;\n if (r < 0) continue;\n ret ~= P(s[x], s[y], i);\n }\n }\n }\n return ret;\n}\n\nP step(P a, P b) {\n P ret;\n ret.c = a.c + b.c;\n ret.a = a.a;\n ret.b = b.b;\n if (a.b == 'A' && b.a == 'C') {\n ret.c++;\n }\n return ret;\n}\n\nint stepK(P a, P b, int k) {\n P t;\n foreach (i; 0 .. k) {\n t = step(a, b);\n a = b;\n b = t;\n }\n return t.c;\n}\n\nstring format(P p, int n) {\n string ret;\n if (p.a != 'X') n--;\n if (p.b != 'X') n--;\n if (p.a != 'X') ret ~= p.a;\n foreach (i; 0 .. n / 2) {\n ret ~= \"AC\";\n }\n foreach (i; ret.length .. n) {\n ret ~= 'X';\n }\n if (p.b != 'X') ret ~= p.b;\n return ret;\n}\n\nvoid main() {\n int k, x, n, m;\n readf(\"%d %d %d %d\\n\", &k, &x, &n, &m);\n P[] a, b;\n a = gen(n);\n b = gen(m);\n foreach (i; a) {\n foreach (j; b) {\n if (stepK(i, j, k - 2) == x) {\n writeln(format(i, n));\n writeln(format(j, m));\n return;\n }\n }\n }\n writeln(\"Happy new year!\");\n}\n"}], "src_uid": "1d55d31320368ddb1439ee086d40b57c"} {"nl": {"description": "You are given an array a consisting of positive integers and q queries to this array. There are two types of queries: 1 l r x — for each index i such that l ≤ i ≤ r set ai = x. 2 l r — find the minimum among such ai that l ≤ i ≤ r. We decided that this problem is too easy. So the array a is given in a compressed form: there is an array b consisting of n elements and a number k in the input, and before all queries a is equal to the concatenation of k arrays b (so the size of a is n·k).", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 104). The second line contains n integers — elements of the array b (1 ≤ bi ≤ 109). The third line contains one integer q (1 ≤ q ≤ 105). Then q lines follow, each representing a query. Each query is given either as 1 l r x — set all elements in the segment from l till r (including borders) to x (1 ≤ l ≤ r ≤ n·k, 1 ≤ x ≤ 109) or as 2 l r — find the minimum among all elements in the segment from l till r (1 ≤ l ≤ r ≤ n·k).", "output_spec": "For each query of type 2 print the answer to this query — the minimum on the corresponding segment.", "sample_inputs": ["3 1\n1 2 3\n3\n2 1 3\n1 1 2 4\n2 1 3", "3 2\n1 2 3\n5\n2 4 4\n1 4 4 5\n2 4 4\n1 1 6 1\n2 6 6"], "sample_outputs": ["1\n3", "1\n5\n1"], "notes": null}, "positive_code": [{"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new node;\n\t\tv.l.val=t[pos*2];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new node;\n\t\tv.r.val=t[pos*2+1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\t/*if(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}*/\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new vertex;\n\t\tv.l.val=t[1];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new vertex;\n\t\tv.r.val=t[1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.val=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.val=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}], "negative_code": [{"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new node;\n\t\tv.l.val=t[pos*2];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new node;\n\t\tv.r.val=t[pos*2+1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\tif(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new vertex;\n\t\tv.l.val=t[1];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new vertex;\n\t\tv.r.val=t[1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new node;\n\t\tv.l.val=t[pos*2];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new node;\n\t\tv.r.val=t[pos*2+1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\tif(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)v.l=new vertex;\n\tif(!v.r)v.r=new vertex;\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)v.v=new node;\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(v.x)\n\t\t{\n\t\t\tif(!v.v)v.v=new node;\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new node;\n\t\tv.l.val=t[pos*2];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new node;\n\t\tv.r.val=t[pos*2+1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\t/*if(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}*/\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new vertex;\n\t\tv.l.val=t[1];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new vertex;\n\t\tv.r.val=t[1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new node;\n\t\tv.l.val=t[pos*2];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new node;\n\t\tv.r.val=t[pos*2+1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\tif(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new vertex;\n\t\tv.l.val=t[1];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new vertex;\n\t\tv.r.val=t[1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)v.v=new node;\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(v.x)\n\t\t{\n\t\t\tif(!v.v)v.v=new node;\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nvoid push1(node t)\n{\n\tif(!t.l)\n\t{\n\t\tt.l=new node;\n\t\tt.l.val=int.max;\n\t}\n\tif(!t.r)\n\t{\n\t\tt.r=new node;\n\t\tt.r.val=int.max;\n\t}\n\tif(t.x!=0)t.l.x=t.r.x=t.val=t.x;\n\tt.x=0;\n}\nint cel1(node t,int tl,int tr,int l,int r)\n{\n\t//writeln(l,' ',r);\n\tif(!t)\n\t{\n\t\t//writeln(\"ok\");\n\t\t//writeln(l,' ',r);*/\n\t\treturn cel0(1,0,n-1,l,r);\n\t}\n\tif(t.x)\n\t{\n\t\t//writeln(t.x);\n\t\treturn t.x;\n\t}\n\telse if(tl==l && tr==r)\n\t{\n\t\t//writeln(t.x);\n\t\treturn t.x?t.x:t.val;\n\t}\n\telse\n\t{\n\t\tif(t.x)push1(t);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel1(t.l,tl,tm,l,r);\n\t\telse if(l>tm)return cel1(t.r,tm+1,tr,l,r);\n\t\telse return min(cel1(t.l,tl,tm,l,tm),cel1(t.r,tm+1,tr,tm+1,r));\n\t}\n}\nvoid upd1(node t,int tl,int tr,int l,int r,int x)\n{\n\t//writeln(tl,' ',tr,' ',l,' ',r);\n\tif(tl==l && tr==r)\n\t{\n\t\tt.x=x;\n\t\t//writeln(x,' ',l,' ',r);\n\t}\n\telse\n\t{\n\t\tif(t.x)push1(t);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm){if(!t.l){t.l=new node;}upd1(t.l,tl,tm,l,r,x);}\n\t\telse if(l>tm){if(!t.r){t.r=new node;}upd1(t.r,tm+1,tr,l,r,x);}\n\t\telse {\n\t\t\tif(!t.l){t.l=new node;}\n\t\t\tif(!t.r){t.r=new node;}\n\t\t\tupd1(t.l,tl,tm,l,tm,x);\n\t\t\tupd1(t.r,tm+1,tr,tm+1,r,x);\n\t\t\t\n\t\t\t}\n\t\tt.val=min(t.r?(t.r.x?t.r.x:t.r.val):cel0(1,0,n-1,tm+1,tr),t.l?(t.l.x?t.l.x:t.l.val):cel0(1,0,n-1,tl,tm));\n\t\t//writeln(t.val);\n\t}\n}\nvoid push2(vertex t)\n{\n\tif(!t.l)\n\t{\n\t\tt.l=new vertex;\n\t\tt.l.val=cel0(1,0,n-1,0,n-1);\n\t}\n\tif(!t.r)\n\t{\n\t\tt.r=new vertex;\n\t\tt.r.val=cel0(1,0,n-1,0,n-1);\n\t}\n\tif(t.x!=0)t.r.x=t.l.x=t.val=t.x;\n\tt.x=0;\n}\nint cel2(vertex t,int tl,int tr,int l,int r,int l0=-1,int r0=-1)\n{\n\t//writeln(tl,' ',tr,' ',l,' ',r);\n\t//writeln(t.x);\n\tif(t.x)return t.x;\n\tif(tl==tr && l0!=-1)\n\t{\n\t\t//writeln(\"ok\");\n\t\treturn cel1(t.v,0,n-1,l0,r0);\n\t}\n\telse if(tl==l && tr==r)\n\t{\n\t\treturn t.x?t.x:t.val;\n\t}\n\telse\n\t{\n\t\tpush2(t);\n\t\t//writeln(!t.l);\n\t\tint tm=(tl+tr)/2;\n\t\tif(r<=tm)return cel2(t.l,tl,tm,l,r,l0,r0);\n\t\telse if(l>tm)return cel2(t.r,tm+1,tr,l,r,l0,r0);\n\t\telse return min(cel2(t.l,tl,tm,l,tm,l0,r0),cel2(t.r,tm+1,tr,tm+1,r,l0,r0));\n\t}\n}\nvoid upd2(vertex t,int tl,int tr,int l,int r,int x,int l0=-1,int r0=-1)\n{\n\t//writeln(\"warn\");\n\tif(tl==tr && l0!=-1)\n\t{\n\t\tif(!t.v)t.v=new node;\n\t\t//writeln(l,' ',r);\n\t\tupd1(t.v,0,n-1,l0,r0,x);\n\t\tt.val=t.v.x?t.v.x:t.v.val;\n\t\t//writeln(t.val);\n\t\treturn;\n\t}\n\telse if(tl==l && tr==r)\n\t{\n\t\tt.x=x;return;\n\t}\n\telse\n\t{\n\t\tpush2(t);\n\t\tint tm=(tl+tr)/2;\n\t\tif(r<=tm){upd2(t.l,tl,tm,l,r,x,l0,r0);}\n\t\telse if(l>tm)upd2(t.r,tm+1,tr,l,r,x,l0,r0);\n\t\telse {upd2(t.l,tl,tm,l,tm,x);upd2(t.r,tm+1,tr,tm+1,r,x,l0,r0);}\n\t\t//t.val=cel0(1,0,n-1,0,n-1);\n\t\tt.val=min(t.r.x?t.r.x:t.r.val,t.l.x?t.l.x:t.l.val);\n\t\t\n\t\treturn;\n\t}\n}\nint[] b,t;\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\n\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tvertex root=new vertex;\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[n*4];\n\tbuild(1,0,n-1);\n\tint q;\n\tread(q);\n\t//root.val=t[1];\n\tforeach(i;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\t//writeln(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tl--;r--;\n\t\t\tint f=(l+n-1)/n,g=(r+1)/n;\n\t\t\t//writeln(f,' ',g);\n\t\t\tif(l/n==r/n)\n\t\t\t{\n\t\t\t\t//writeln(\"ko\");\n\t\t\t\tupd2(root,0,k-1,r/n,r/n,x,l%n,r%n);\n\t\t\t}\n\t\t\telse \n\t\t\t{\n\t\t\t\tif(f1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)v.l=new node;\n\tif(!v.r)v.r=new node;\n\tv.l.val=t[pos*2];\n\tv.r.val=t[pos*2+1];\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\tif(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)v.l=new vertex;\n\tif(!v.r)v.r=new vertex;\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tv.v=new node;\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(v.x)\n\t\t{\n\t\t\tif(!v.v)v.v=new node;\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nint[] b,t;\nvoid push(node v,int pos)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new node;\n\t\tv.l.val=t[pos*2];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new node;\n\t\tv.r.val=t[pos*2+1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(node v,int pos,int tl,int tr,int l,int r,int x)\n{\n\tif(tl==l && tr==r)v.x=x;\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)upd(v.l,pos*2,tl,tm,l,r,x);\n\t\telse if(l>tm)upd(v.r,pos*2+1,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,pos*2,tl,tm,l,tm,x);\n\t\t\tupd(v.r,pos*2+1,tm+1,tr,tm+1,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(node v,int pos,int tl,int tr,int l,int r)\n{\n\tif(!v)\n\t{\n\t\treturn cel0(pos,tl,tr,l,r);\n\t}\n\tif(tl==l && tr==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse\n\t{\n\t\tpush(v,pos);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel(v.l,pos*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel(v.r,pos*2+1,tm+1,tr,l,r);\n\t\telse return min(cel(v.l,pos*2,tl,tm,l,tm),cel(v.r,pos*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid push(vertex v)\n{\n\tif(!v.l)\n\t{\n\t\tv.l=new vertex;\n\t\tv.l.val=t[1];\n\t}\n\tif(!v.r)\n\t{\n\t\tv.r=new vertex;\n\t\tv.r.val=t[1];\n\t}\n\tif(v.x)v.val=v.l.x=v.r.x=v.x;\n\tv.x=0;\n}\nvoid upd(vertex v,int tl,int tr,int l,int r,int x)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\tv.x=x;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(!v.v)\n\t\t{\n\t\t\tv.v=new node;\n\t\t\tv.v.val=t[1];\n\t\t}\n\t\tif(v.x)\n\t\t{\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\tupd(v.v,1,0,n-1,l-tl*n,r-tl*n,x);\n\t\tv.val=v.v.x?v.v.x:v.v.val;\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)upd(v.l,tl,tm,l,r,x);\n\t\telse if(l/n>tm)upd(v.r,tm+1,tr,l,r,x);\n\t\telse\n\t\t{\n\t\t\tupd(v.l,tl,tm,l,tm*n+n-1,x);\n\t\t\tupd(v.r,tm+1,tr,(tm+1)*n,r,x);\n\t\t}\n\t\tv.val=min(v.r.x?v.r.x:v.r.val,v.l.x?v.l.x:v.l.val);\n\t}\n}\nint cel(vertex v,int tl,int tr,int l,int r)\n{\n\tif(tl*n==l && tr*n+n-1==r)\n\t{\n\t\treturn v.x?v.x:v.val;\n\t}\n\telse if(tl==tr)\n\t{\n\t\tif(v.x)\n\t\t{\n\t\t\tif(!v.v)v.v=new node;\n\t\t\tv.v.x=v.x;\n\t\t\tv.x=0;\n\t\t}\n\t\treturn cel(v.v,1,0,n-1,l-tl*n,r-tl*n);\n\t}\n\telse\n\t{\n\t\tpush(v);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r/n<=tm)return cel(v.l,tl,tm,l,r);\n\t\telse if(l/n>tm)return cel(v.r,tm+1,tr,l,r);\n\t\telse\n\t\t{\n\t\t\treturn min(cel(v.l,tl,tm,l,tm*n+n-1),cel(v.r,tm+1,tr,(tm+1)*n,r));\n\t\t}\n\t}\n}\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[4*n];\n\tint q;\n\tread(q);\n\tvertex root=new vertex;\n\tbuild(1,0,n-1);\n\troot.val=t[1];\n\tforeach(ii;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tupd(root,0,k-1,l-1,r-1,x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l,r;\n\t\t\tread(l,r);\n\t\t\twrite(cel(root,0,k-1,l-1,r-1),'\\n');\n\t\t}\n\t}\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nclass node\n{\n\tint val,x;\n\tnode l,r;\n};\nclass vertex\n{\n\tnode v;\n\tint val,x;\n\tvertex l,r;\n};\nint n,k;\nvoid push1(node t)\n{\n\tif(!t.l)\n\t{\n\t\tt.l=new node;\n\t\tt.l.val=int.max;\n\t}\n\tif(!t.r)\n\t{\n\t\tt.r=new node;\n\t\tt.r.val=int.max;\n\t}\n\tif(t.x!=0)t.l.x=t.r.x=t.val=t.x;\n\tt.x=0;\n}\nint cel1(node t,int tl,int tr,int l,int r)\n{\n\t//writeln(l,' ',r);\n\tif(!t)\n\t{\n\t\t//writeln(\"ok\");\n\t\t//writeln(l,' ',r);*/\n\t\treturn cel0(1,0,n-1,l,r);\n\t}\n\tif(t.x)\n\t{\n\t\t//writeln(t.x);\n\t\treturn t.x;\n\t}\n\telse if(tl==l && tr==r)\n\t{\n\t\t//writeln(t.x);\n\t\treturn t.x?t.x:t.val;\n\t}\n\telse\n\t{\n\t\tif(t.x)push1(t);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel1(t.l,tl,tm,l,r);\n\t\telse if(l>tm)return cel1(t.r,tm+1,tr,l,r);\n\t\telse return min(cel1(t.l,tl,tm,l,tm),cel1(t.r,tm+1,tr,tm+1,r));\n\t}\n}\nvoid upd1(node t,int tl,int tr,int l,int r,int x)\n{\n\t//writeln(tl,' ',tr,' ',l,' ',r);\n\tif(tl==l && tr==r)\n\t{\n\t\tt.x=x;\n\t\t//writeln(x,' ',l,' ',r);\n\t}\n\telse\n\t{\n\t\tif(t.x)push1(t);\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm){if(!t.l){t.l=new node;}upd1(t.l,tl,tm,l,r,x);}\n\t\telse if(l>tm){if(!t.r){t.r=new node;}upd1(t.r,tm+1,tr,l,r,x);}\n\t\telse {\n\t\t\tif(!t.l){t.l=new node;}\n\t\t\tif(!t.r){t.r=new node;}\n\t\t\tupd1(t.l,tl,tm,l,tm,x);\n\t\t\tupd1(t.r,tm+1,tr,tm+1,r,x);\n\t\t\t\n\t\t\t}\n\t\tt.val=min(t.r?(t.r.x?t.r.x:t.r.val):cel0(1,0,n-1,tm+1,tr),t.l?(t.l.x?t.l.x:t.l.val):cel0(1,0,n-1,tl,tm));\n\t\t//writeln(t.val);\n\t}\n}\nvoid push2(vertex t)\n{\n\tif(!t.l)\n\t{\n\t\tt.l=new vertex;\n\t\tt.l.val=cel0(1,0,n-1,0,n-1);\n\t}\n\tif(!t.r)\n\t{\n\t\tt.r=new vertex;\n\t\tt.r.val=cel0(1,0,n-1,0,n-1);\n\t}\n\tif(t.x!=0)t.r.x=t.l.x=t.val=t.x;\n\tt.x=0;\n}\nint cel2(vertex t,int tl,int tr,int l,int r,int l0=-1,int r0=-1)\n{\n\t//writeln(tl,' ',tr,' ',l,' ',r);\n\t//writeln(t.x);\n\tif(t.x)return t.x;\n\tif(tl==tr && l0!=-1)\n\t{\n\t\t//writeln(\"ok\");\n\t\treturn cel1(t.v,0,n-1,l0,r0);\n\t}\n\telse if(tl==l && tr==r)\n\t{\n\t\treturn t.x?t.x:t.val;\n\t}\n\telse\n\t{\n\t\tpush2(t);\n\t\t//writeln(!t.l);\n\t\tint tm=(tl+tr)/2;\n\t\tif(r<=tm)return cel2(t.l,tl,tm,l,r,l0,r0);\n\t\telse if(l>tm)return cel2(t.r,tm+1,tr,l,r,l0,r0);\n\t\telse return min(cel2(t.l,tl,tm,l,tm,l0,r0),cel2(t.r,tm+1,tr,tm+1,r,l0,r0));\n\t}\n}\nvoid upd2(vertex t,int tl,int tr,int l,int r,int x,int l0=-1,int r0=-1)\n{\n\t//writeln(\"warn\");\n\tif(tl==tr && l0!=-1)\n\t{\n\t\tif(!t.v)t.v=new node;\n\t\t//writeln(l,' ',r);\n\t\tupd1(t.v,0,n-1,l0,r0,x);\n\t\tt.val=t.v.x?t.v.x:t.v.val;\n\t\t//writeln(t.val);\n\t\treturn;\n\t}\n\telse if(tl==l && tr==r)\n\t{\n\t\tt.x=x;return;\n\t}\n\telse\n\t{\n\t\tpush2(t);\n\t\tint tm=(tl+tr)/2;\n\t\tif(r<=tm){upd2(t.l,tl,tm,l,r,x,l0,r0);}\n\t\telse if(l>tm)upd2(t.r,tm+1,tr,l,r,x,l0,r0);\n\t\telse {upd2(t.l,tl,tm,l,tm,x);upd2(t.r,tm+1,tr,tm+1,r,x,l0,r0);}\n\t\t//t.val=cel0(1,0,n-1,0,n-1);\n\t\tt.val=min(t.r.x?t.r.x:t.r.val,t.l.x?t.l.x:t.l.val);\n\t\t\n\t\treturn;\n\t}\n}\nint[] b,t;\nvoid build(int v,int tl,int tr)\n{\n\tif(tl==tr)t[v]=b[tl];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tbuild(v*2,tl,tm);\n\t\tbuild(v*2+1,tm+1,tr);\n\t\tt[v]=min(t[v*2+1],t[v*2]);\n\t}\n}\nint cel0(int v,int tl,int tr,int l,int r)\n{\n\t//writeln(\"ok\");\n\tif(tl==l && tr==r)return t[v];\n\telse\n\t{\n\t\tint tm=(tl+tr)>>1;\n\t\tif(r<=tm)return cel0(v*2,tl,tm,l,r);\n\t\telse if(l>tm)return cel0(v*2+1,tm+1,tr,l,r);\n\t\telse return min(cel0(v*2,tl,tm,l,tm),cel0(v*2+1,tm+1,tr,tm+1,r));\n\t}\n}\n\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tvertex root=new vertex;\n\t//writeln(!root);\n\t/*int n;\nloop:while(read(n))\n\t{\n\t\t\n\t}*/\n\tread(n,k);\n\tb=arread!int;\n\tt=new int[n*4];\n\tbuild(1,0,n-1);\n\tint q;\n\tread(q);\n\troot.val=t[1];\n\tforeach(i;0..q)\n\t{\n\t\tint h;\n\t\tinput(h);\n\t\t//writeln(h);\n\t\tif(h==1)\n\t\t{\n\t\t\tint l,r,x;\n\t\t\tread(l,r,x);\n\t\t\tl--;r--;\n\t\t\tint f=(l+n-1)/n,g=(r+1)/n;\n\t\t\t//writeln(f,' ',g);\n\t\t\tif(l/n==r/n)\n\t\t\t{\n\t\t\t\t//writeln(\"ko\");\n\t\t\t\tupd2(root,0,k-1,r/n,r/n,x,l%n,r%n);\n\t\t\t}\n\t\t\telse \n\t\t\t{\n\t\t\t\tif(f= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new bool[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD;\r\n\t\tauto m = RD;\r\n\t\tauto k = RD-1;\r\n\r\n\t\tif (m < n-1) continue;\r\n\t\tauto cnt = n * (n-1) / 2;\r\n\t\tif (m > cnt) continue;\r\n\t\tlong kk;\r\n\t\tif (n == 1)\r\n\t\t\tkk = 0;\r\n\t\telse if (m == cnt)\r\n\t\t\tkk = 1;\r\n\t\telse\r\n\t\t\tkk = 2;\r\n\t\tdebug writeln(\"kk:\", kk);\r\n\t\tif (kk < k)\r\n\t\t\tans[ti] = true;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e ? \"YES\" : \"NO\");\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "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;\n\nvoid main()\n{\n long t;\n readf(\" %d \", &t);\n while (t--) {\n long n, m, k;\n readf(\" %d %d %d \", &n, &m, &k);\n if (m < n - 1) {\n writeln(\"NO\");\n continue;\n }\n if (m > n * (n - 1) / 2) {\n writeln(\"NO\");\n continue;\n }\n long min_d = 2;\n if (m == n * (n - 1) / 2) {\n min_d = 1;\n }\n if (n == 1) {\n min_d = 0;\n }\n writeln(min_d < k - 1 ? \"YES\" : \"NO\");\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new bool[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto m = RD!int;\r\n\t\tauto k = RD!int-1;\r\n\r\n\t\tif (m < n-1) continue;\r\n\t\tauto cnt = n * (n-1) / 2;\r\n\t\tif (m > cnt) continue;\r\n\t\tlong kk;\r\n\t\tif (n == 1)\r\n\t\t\tkk = 0;\r\n\t\telse if (m == cnt)\r\n\t\t\tkk = 1;\r\n\t\telse\r\n\t\t\tkk = 2;\r\n\t\tdebug writeln(\"kk:\", kk);\r\n\t\tif (kk < k)\r\n\t\t\tans[ti] = true;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e ? \"YES\" : \"NO\");\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "src_uid": "f853a61741518cb884c00c8b760692aa"} {"nl": {"description": "You are given three positive integers $$$n$$$, $$$a$$$ and $$$b$$$. You have to construct a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters such that each substring of length $$$a$$$ has exactly $$$b$$$ distinct letters. It is guaranteed that the answer exists.You have to answer $$$t$$$ independent test cases.Recall that the substring $$$s[l \\dots r]$$$ is the string $$$s_l, s_{l+1}, \\dots, s_{r}$$$ and its length is $$$r - l + 1$$$. In this problem you are only interested in substrings of length $$$a$$$.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of a test case contains three space-separated integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \\le a \\le n \\le 2000, 1 \\le b \\le \\min(26, a)$$$), where $$$n$$$ is the length of the required string, $$$a$$$ is the length of a substring and $$$b$$$ is the required number of distinct letters in each substring of length $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$\\sum n \\le 2000$$$).", "output_spec": "For each test case, print the answer — such a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters that each substring of length $$$a$$$ has exactly $$$b$$$ distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists.", "sample_inputs": ["4\n7 5 3\n6 1 1\n6 6 1\n5 2 2"], "sample_outputs": ["tleelte\nqwerty\nvvvvvv\nabcde"], "notes": "NoteIn the first test case of the example, consider all the substrings of length $$$5$$$: \"tleel\": it contains $$$3$$$ distinct (unique) letters, \"leelt\": it contains $$$3$$$ distinct (unique) letters, \"eelte\": it contains $$$3$$$ distinct (unique) letters. "}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n auto nab = readln.split.to!(int[]);\n auto N = nab[0];\n auto A = nab[1];\n auto B = nab[2];\n\n char[] rs, ss;\n foreach (i; 0..A) {\n rs ~= (min(i, B-1) + 'a').to!char;\n }\n foreach (i; 0..N) {\n ss ~= rs[i%A];\n }\n writeln(ss);\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint tests;\n\treadf !(\" %s\") (tests);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, a, b;\n\t\treadf !(\" %s %s %s\") (n, a, b);\n\t\tstring s;\n\t\ts.reserve (n);\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\ts ~= cast (char) (i % b + 'a');\n\t\t}\n\t\twriteln (s);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new string[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RD!int;\n\t\tauto b = RD!int;\n\n\t\tauto loop = a - b;\n\t\tint num;\n\t\tint cnt;\n\t\twhile (ans[ti].length < n)\n\t\t{\n\t\t\tif (cnt % a < loop)\n\t\t\t{\n\t\t\t\tans[ti] ~= 'a';\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tans[ti] ~= cast(char)('a'+(num%b));\n\t\t\t\t++num;\n\t\t\t}\n\t\t\t++cnt;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "a82f15c1b0ddcacc1de966be20cfc5a2"} {"nl": {"description": "Nikita likes tasks on order statistics, for example, he can easily find the $$$k$$$-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number $$$x$$$ is the $$$k$$$-th number in increasing order on this segment. In other words, you should find the number of segments of a given array such that there are exactly $$$k$$$ numbers of this segment which are less than $$$x$$$.Nikita wants to get answer for this question for each $$$k$$$ from $$$0$$$ to $$$n$$$, where $$$n$$$ is the size of the array.", "input_spec": "The first line contains two integers $$$n$$$ and $$$x$$$ $$$(1 \\le n \\le 2 \\cdot 10^5, -10^9 \\le x \\le 10^9)$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ $$$(-10^9 \\le a_i \\le 10^9)$$$ — the given array.", "output_spec": "Print $$$n+1$$$ integers, where the $$$i$$$-th number is the answer for Nikita's question for $$$k=i-1$$$.", "sample_inputs": ["5 3\n1 2 3 4 5", "2 6\n-5 9", "6 99\n-1 -1 -1 -1 -1 -1"], "sample_outputs": ["6 5 4 0 0 0", "1 2 0", "0 6 5 4 3 2 1"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.complex;\nimport std.conv;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int limit = 1 << 19;\nimmutable int half = 512;\n\nvoid main ()\n{\n\tint n, x;\n\twhile (readf (\" %s %s\", &n, &x) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto v = new real [limit];\n\t\tv[] = 0;\n\t\tint pos = 0;\n\t\tint cur = 1;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tif (c < x)\n\t\t\t{\n\t\t\t\tv[pos] = cur;\n\t\t\t\tpos += 1;\n\t\t\t\tcur = 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tcur += 1;\n\t\t\t}\n\t\t}\n\t\tv[pos] = cur;\n\t\tpos += 1;\n\t\tauto u = v.map !(c => Complex !(real) (cast (long) (c) % half, cast (long) (c) / half)).array;\n\t\treverse (u[0..pos]);\n//\t\tdebug {writeln (u.take (n).map !(c => c.round.to !(long)));}\n\t\tdebug {writeln (v.take (n).map !(c => c.round.to !(long)));}\n\n\t\tauto f = new Fft (limit);\n\t\tauto uf = f.fft !(real) (u);\n\t\tauto vf = f.fft !(real) (v);\n\t\tauto wf = uf.dup;\n\t\twf[] *= vf[];\n\t\tauto w = f.inverseFft !(real) (wf);\n\t\tdebug {writeln (w.take (n + 1).map !(c => c.re.round.to !(long)));}\n\t\tauto wr = w.take (n + 1).map !(c => c.re.round.to !(long) + half * c.im.round.to !(long)).array;\n\t\twr[pos - 1] = 0;\n\t\tforeach (i; 0..pos)\n\t\t{\n\t\t\twr[pos - 1] += cast (long) (v[i] * (v[i] - 1) / 2);\n\t\t}\n\t\treverse (wr[0..pos]);\n\t\twr[pos..$][] = 0;\n\t\twritefln (\"%(%s %)\", wr);\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.complex, std.conv, std.math, std.numeric, std.range, std.stdio;\nimmutable int L = 1 << 19, H = 512;\nvoid main () {\n\tint n, x;\n\treadf (\" %s %s \", &n, &x);\n\tauto a = readln.splitter.map !(to!int).array;\n\tauto v = a.splitter !(t => t < x).map !(t => t.length + 1L).array;\n\tauto k = v.length;\n\tv.length = L;\n\tauto u = v.map !(c => Complex!real (c % H, c / H)).array;\n\treverse (u[0..k]);\n\tauto g = u.fft!real, h = v.fft!real;\n\tg[] *= h[];\n\tauto w = g.inverseFft!real[0..k].retro.map !(c => to!long (c.re.round + H * c.im.round)).array;\n\tw[0] = k.iota.map !(i => (v[i] * (v[i] - 1) / 2)).sum;\n\tw.length = n + 1;\n\twritefln (\"%(%s %)\", w);\n}\n"}, {"source_code": "import std.algorithm, std.complex, std.conv, std.math, std.numeric, std.range, std.stdio;\n\nimmutable int limit = 1 << 19, half = 512;\n\nvoid main ()\n{\n\tint n, x;\n\treadf (\" %s %s \", &n, &x);\n\tauto a = readln.splitter.map !(to!int).array ~ int.min;\n\tauto v = new long [limit];\n\tint pos = 0, cur = 0;\n\tforeach (c; a)\n\t{\n\t\t++cur;\n\t\tif (c < x) v[pos++] = cur, cur = 0;\n\t}\n\tauto u = v.map !(c => Complex!real (c % half, c / half)).array;\n\treverse (u[0..pos]);\n\tauto f = new Fft (limit);\n\tauto g = f.fft!real (u), h = f.fft!real (v);\n\tg[] *= h[];\n\tauto w = f.inverseFft!real (g).take (n + 1).map !(c => cast (long) (c.re.round + half * c.im.round)).array;\n\tw[pos - 1] = 0;\n\tforeach (i; 0..pos) w[pos - 1] += (v[i] * (v[i] - 1) / 2);\n\treverse (w[0..pos]);\n\tw[pos..$] = 0;\n\twritefln (\"%(%s %)\", w);\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nclass Fft(int K) {\n // 1, 1/4, 1/8, 3/8, 1/16, 5/16, 3/16, 7/16, ...\n Complex!real[] g;\n this() {\n static assert(K >= 2, \"Fft: K >= 2 must hold\");\n g.length = 1 << (K - 1);\n g[0] = 1;\n g[1 << (K - 2)] = fromPolar(1.0L, (2.0L * PI) / (1 << K));\n for (int l = 1 << (K - 2); l >= 2; l >>= 1) {\n g[l >> 1] = g[l] * g[l];\n }\n for (int l = 2; l <= 1 << (K - 2); l <<= 1) {\n for (int i = 1; i < l; ++i) {\n g[l + i] = g[l] * g[i];\n }\n }\n }\n void fft(Complex!real[] x) const {\n const n = cast(int)(x.length);\n assert(!(n & (n - 1)) && n <= 1 << K);\n for (int l = n; l >>= 1; ) {\n for (int i = 0; i < (n >> 1) / l; ++i) {\n for (int j = (i << 1) * l; j < (i << 1 | 1) * l; ++j) {\n const t = g[i] * x[j + l];\n x[j + l] = x[j] - t;\n x[j] += t;\n }\n }\n }\n for (int i = 0, j = 0; i < n; ++i) {\n if (i < j) swap(x[i], x[j]);\n for (int l = n; (l >>= 1) && !((j ^= l) & l); ) {}\n }\n }\n}\nenum FFT_K = 19;\nFft!FFT_K FFT;\n\n\n\nvoid main() {\n FFT = new Fft!FFT_K();\n \n try {\n for (; ; ) {\n const N = readInt();\n const X = readInt();\n auto A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n auto ss = new int[N + 1];\n foreach (i; 0 .. N) {\n ss[i + 1] = ss[i] + ((A[i] < X) ? 1 : 0);\n }\n debug {\n writeln(\"ss = \", ss);\n }\n \n auto fs = new Complex!real[1 << FFT_K];\n auto gs = new Complex!real[1 << FFT_K];\n fs[] = complex(0.0L, 0.0L);\n gs[] = complex(0.0L, 0.0L);\n foreach (i; 0 .. N + 1) {\n fs[ss[i]] += 1.0L;\n gs[N - ss[i]] += 1.0L;\n }\n FFT.fft(fs);\n FFT.fft(gs);\n foreach (j; 0 .. 1 << FFT_K) {\n fs[j] *= gs[j] / (1 << FFT_K);\n }\n fs[1 .. $].reverse;\n FFT.fft(fs);\n auto ans = new long[N + 1];\n foreach (k; 1 .. N + 1) {\n ans[k] = cast(long)(fs[N + k].re + 0.5L);\n }\n ans[0] = 1L * N * (N + 1) / 2 - ans[1 .. N + 1].sum;\n \n foreach (k; 0 .. N + 1) {\n if (k > 0) write(\" \");\n write(ans[k]);\n }\n writeln();\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.complex : g = abs;\nimport std.conv;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int limit = 1 << 20;\n\nvoid main ()\n{\n\tint n, x;\n\twhile (readf (\" %s %s\", &n, &x) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto v = new real [limit];\n\t\tv[] = 0;\n\t\tint pos = 0;\n\t\tint cur = 1;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tif (c < x)\n\t\t\t{\n\t\t\t\tv[pos] = cur;\n\t\t\t\tpos += 1;\n\t\t\t\tcur = 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tcur += 1;\n\t\t\t}\n\t\t}\n\t\tv[pos] = cur;\n\t\tpos += 1;\n\t\tauto u = v.dup;\n\t\treverse (u[0..pos]);\n\t\tdebug {writeln (u.take (n).map !(c => c.round.to !(long)));}\n\t\tdebug {writeln (v.take (n).map !(c => c.round.to !(long)));}\n\n\t\tauto f = new Fft (limit);\n\t\tauto uf = f.fft !(real) (u);\n\t\tauto vf = f.fft !(real) (v);\n\t\tauto wf = uf.dup;\n\t\twf[] *= vf[];\n\t\tauto w = f.inverseFft !(real) (wf);\n\t\tdebug {writeln (w.take (n + 1).map !(c => g (c).round.to !(long)));}\n\t\tauto wr = w.take (n + 1).map !(c => g (c).round.to !(long)).array;\n\t\twr[pos - 1] = 0;\n\t\tforeach (i; 0..pos)\n\t\t{\n\t\t\twr[pos - 1] += cast (long) (v[i] * (v[i] - 1) / 2);\n\t\t}\n\t\treverse (wr[0..pos]);\n\t\twr[pos..$][] = 0;\n\t\twritefln (\"%(%s %)\", wr);\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.complex, std.conv, std.math, std.numeric, std.range, std.stdio;\nimmutable int L = 1 << 19, H = 512;\nvoid main () {\n\tint n, x;\n\treadf (\" %s %s \", &n, &x);\n\tauto a = readln.splitter.map !(to!int).array;\n\tauto v = a.splitter !(t => t < x).map !(t => t.length + 1).array;\n\tauto k = v.length;\n\tv.length = L;\n\tauto u = v.map !(c => Complex!real (c % H, c / H)).array;\n\treverse (u[0..k]);\n\tauto g = u.fft!real, h = v.fft!real;\n\tg[] *= h[];\n\tauto w = g.inverseFft!real[0..k].retro.map !(c => to!long (c.re.round + H * c.im.round)).array;\n\tw[0] = k.iota.map !(i => (v[i] * (v[i] - 1) / 2)).sum;\n\tw.length = n + 1;\n\twritefln (\"%(%s %)\", w);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int limit = 1 << 19;\n\nvoid main ()\n{\n\tint n, x;\n\twhile (readf (\" %s %s\", &n, &x) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto v = new real [limit];\n\t\tv[] = 0;\n\t\tint pos = 0;\n\t\tint cur = 1;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tif (c < x)\n\t\t\t{\n\t\t\t\tv[pos] = cur;\n\t\t\t\tpos += 1;\n\t\t\t\tcur = 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tcur += 1;\n\t\t\t}\n\t\t}\n\t\tv[pos] = cur;\n\t\tpos += 1;\n\t\tauto u = v.dup;\n\t\treverse (u[0..pos]);\n\t\tdebug {writeln (u.take (n).map !(c => c.round.to !(long)));}\n\t\tdebug {writeln (v.take (n).map !(c => c.round.to !(long)));}\n\n\t\tauto f = new Fft (limit);\n\t\tauto uf = f.fft !(real) (u);\n\t\tauto vf = f.fft !(real) (v);\n\t\tauto wf = uf.dup;\n\t\twf[] *= vf[];\n\t\tauto w = f.inverseFft !(real) (wf);\n\t\tdebug {writeln (w.take (n + 1).map !(c => c.re.round.to !(long)));}\n\t\tauto wr = w.take (n + 1).map !(c => c.re.round.to !(long)).array;\n\t\twr[pos - 1] = 0;\n\t\tforeach (i; 0..pos)\n\t\t{\n\t\t\twr[pos - 1] += cast (long) (v[i] * (v[i] - 1) / 2);\n\t\t}\n\t\treverse (wr[0..pos]);\n\t\twr[pos..$][] = 0;\n\t\twritefln (\"%(%s %)\", wr);\n\t}\n}\n"}], "src_uid": "97e68e5cf05c157b4f83eb07ff003790"} {"nl": {"description": "Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by  - 1. The second operation is to take some suffix and multiply all numbers in it by  - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself.", "output_spec": "The first and the only line of the output should contain the answer to the problem.", "sample_inputs": ["3\n-1 -2 -3", "5\n-4 2 0 5 0", "5\n-1 10 -5 10 -2"], "sample_outputs": ["6", "11", "18"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto prefSum = 0 ~ arr.cumulativeFold!((a, b) => a + b).array;\n \n auto flipSufSum = arr.retro().cumulativeFold!((a, b) => a - b)(0).array.reverse() ~ 0;\n \n int getVal(int idx) { return prefSum[idx] + flipSufSum[idx]; }\n \n auto bestSufFlipIdx = new int[] (n+1);\n bestSufFlipIdx[n] = n;\n foreach_reverse (i; 0 .. n) {\n bestSufFlipIdx[i] = bestSufFlipIdx[i+1];\n \n debug { writeln(i, ' ', getVal(i)); }\n \n if (getVal(i) > getVal(bestSufFlipIdx[i+1])) { bestSufFlipIdx[i] = i; }\n }\n \n debug { prefSum.writeln; flipSufSum.writeln; bestSufFlipIdx.writeln; }\n \n int ans = getVal(bestSufFlipIdx[0]);\n int csumle = 0;\n foreach (i, e; arr.dropBackOne.enumerate(1)) {\n csumle += -e;\n \n int bestIdx = bestSufFlipIdx[i+1];\n \n debug { writeln(i, ' ', csumle, ' ', bestIdx, ' ', prefSum[bestIdx] - prefSum[i], ' ', flipSufSum[bestIdx]); }\n \n ans = max(ans, csumle + getVal(bestIdx) - prefSum[i]);\n }\n \n ans.writeln;\n}"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto prefSum = 0 ~ arr.cumulativeFold!((a, b) => a + b).array;\n \n auto flipSufSum = arr.retro().cumulativeFold!((a, b) => a - b)(0).array.reverse() ~ 0;\n \n auto bestSufFlipIdx = new int[] (n+1);\n bestSufFlipIdx[n] = n;\n foreach_reverse (i; 0 .. n) {\n bestSufFlipIdx[i] = bestSufFlipIdx[i+1];\n \n int getVal(int idx) { return prefSum[idx] + flipSufSum[idx]; }\n \n debug { writeln(i, ' ', getVal(i)); }\n \n if (getVal(i) > getVal(bestSufFlipIdx[i+1])) { bestSufFlipIdx[i] = i; }\n }\n \n debug { prefSum.writeln; flipSufSum.writeln; bestSufFlipIdx.writeln; }\n \n int ans = max(prefSum[n], flipSufSum[0]);\n int csumle = 0;\n foreach (i, e; arr.dropBackOne.enumerate(1)) {\n csumle += -e;\n \n int bestIdx = bestSufFlipIdx[i+1];\n \n debug { writeln(i, ' ', csumle, ' ', bestIdx, ' ', prefSum[bestIdx] - prefSum[i], ' ', flipSufSum[bestIdx]); }\n \n ans = max(ans, csumle + prefSum[bestIdx] - prefSum[i] + flipSufSum[bestIdx]);\n }\n \n ans.writeln;\n}"}], "src_uid": "89237865c97d64406050fd140d4166f9"} {"nl": {"description": "Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.", "input_spec": "First line of input consists of one integer n (1 ≤ n ≤ 3000). Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.", "output_spec": "Output single integer — minimum amount of coins the colonel has to pay.", "sample_inputs": ["4\n1 3 1 4", "5\n1 2 3 2 5"], "sample_outputs": ["1", "2"], "notes": "NoteIn first sample test we can increase factor of first badge by 1.In second sample test we can increase factors of the second and the third badge by 1."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm, std.array, std.conv;\n\nvoid main() {\n\n\treadln.strip;\n\n\tbool[int] arr;\n\n\tint sum;\n\tforeach (el; readln.split.map!(to!int).array.sort!\"a < b\") {\n\t\tint tmp;\n\t\twhile (arr.get(el + tmp, false)) {\n\t\t\t++sum;\n\t\t\t++tmp;\n\t\t}\n\t\tarr[el + tmp] = true;\n\t}\n\n\twriteln(sum);\n}"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.algorithm, std.array, std.conv;\n\nvoid main() {\n\n\treadln.strip;\n\n\tbool[int] arr;\n\n\tint sum;\n\tforeach (el; readln.split.map!(to!int).array.sort!\"a < b\") {\n\t\tint tmp;\n\t\twhile (arr.get(el + tmp, false)) {\n\t\t\t++sum;\n\t\t\t++tmp;\n\t\t}\n\t\tarr[el + tmp] = true;\n\t}\n\n\twriteln(sum);\n\tarr.rehash;\n\twriteln(arr);\n}"}, {"source_code": "import std.stdio, std.string, std.algorithm, std.array, std.conv;\n\nvoid main() {\n\n\treadln.strip;\n\n\tbool[int] arr;\n\n\tint sum;\n\tforeach (el; readln.split.map!(to!int).array.sort!\"a < b\") {\n\t\tif (arr.get(el, false)) {\n\t\t\t++sum;\n\t\t\tarr[el + 1] = true;\n\t\t}\n\t\tarr[el] = true;\n\t}\n\n\twriteln(sum);\n}"}], "src_uid": "53ae714f04fd29721b8bbf77576b7ccf"} {"nl": {"description": "SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the target in turns, and SmallR shoots first. The probability of shooting the target each time is for SmallR while for Zanoes. The one who shoots in the target first should be the winner.Output the probability that SmallR will win the match.", "input_spec": "A single line contains four integers .", "output_spec": "Print a single real number, the probability that SmallR will win the match. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.", "sample_inputs": ["1 2 1 2"], "sample_outputs": ["0.666666666667"], "notes": null}, "positive_code": [{"source_code": "import std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n //stdin.open(\"input.txt\", \"r\");\n //stdout.open(\"output.txt\", \"w\");\n int a;\n int b;\n int c;\n int d;\n readf(\"%d %d %d %d \", &a, &b, &c, &d);\n auto p1 = cast(double)a / b;\n auto p2 = cast(double)c / d;\n auto cur_p = 1.0;\n auto res = 0.0;\n foreach (i; 0..1000000) {\n res += cur_p * p1;\n cur_p *= (1 - p1) * (1 - p2);\n }\n writefln(\"%.20f\", res);\n}"}], "negative_code": [], "src_uid": "7b932b2d3ab65a353b18d81cf533a54e"} {"nl": {"description": "Igor is in the museum and he wants to see as many pictures as possible.Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.", "input_spec": "First line of the input contains three integers n, m and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.", "output_spec": "Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.", "sample_inputs": ["5 6 3\n******\n*..*.*\n******\n*....*\n******\n2 2\n2 5\n4 3", "4 4 1\n****\n*..*\n*.**\n****\n3 2"], "sample_outputs": ["6\n4\n10", "8"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.string;\n\nimmutable int DIRS = 4;\nimmutable int [DIRS] DROW = [-1, 0, +1, 0];\nimmutable int [DIRS] DCOL = [ 0, -1, 0, +1];\n\nvoid main ()\n{\n\tint n, m, k;\n\twhile (readf (\" %s %s %s \", &n, &m, &k) > 0)\n\t{\n\t\tstring [] a;\n\t\tforeach (row; 0..n)\n\t\t{\n\t\t\ta ~= readln.strip;\n\t\t}\n\t\tauto b = new int [] [] (n, m);\n\n\t\tint recur (int row, int col, int mark)\n\t\t{\n\t\t\tint res = 0;\n\t\t\tif (b[row][col])\n\t\t\t{\n\t\t\t\treturn res;\n\t\t\t}\n\t\t\tb[row][col] = mark;\n\t\t\tforeach (dir; 0..DIRS)\n\t\t\t{\n\t\t\t\tint nrow = row + DROW[dir];\n\t\t\t\tint ncol = col + DCOL[dir];\n\t\t\t\tif (a[nrow][ncol] == '.')\n\t\t\t\t{\n\t\t\t\t\tres += recur (nrow, ncol, mark);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres++;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tint [] ans;\n\t\tforeach (z; 0..k)\n\t\t{\n\t\t\tint row, col;\n\t\t\treadf (\" %s %s\", &row, &col);\n\t\t\trow--;\n\t\t\tcol--;\n\t\t\tif (!b[row][col])\n\t\t\t{\n\t\t\t\tans ~= recur (row, col, ans.length + 1);\n\t\t\t}\n\t\t\twriteln (ans[b[row][col] - 1]);\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "cfdcfe449868ed50516f0895bf6a66e1"} {"nl": {"description": "Find the number of ways to divide an array $$$a$$$ of $$$n$$$ integers into any number of disjoint non-empty segments so that, in each segment, there exist at most $$$k$$$ distinct integers that appear exactly once.Since the answer can be large, find it modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \\leq k \\leq n \\leq 10^5$$$) — the number of elements in the array $$$a$$$ and the restriction from the statement. The following line contains $$$n$$$ space-separated integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$) — elements of the array $$$a$$$.", "output_spec": "The first and only line contains the number of ways to divide an array $$$a$$$ modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["3 1\n1 1 2", "5 2\n1 1 2 1 3", "5 5\n1 2 3 4 5"], "sample_outputs": ["3", "14", "16"], "notes": "NoteIn the first sample, the three possible divisions are as follows. $$$[[1], [1], [2]]$$$ $$$[[1, 1], [2]]$$$ $$$[[1, 1, 2]]$$$ Division $$$[[1], [1, 2]]$$$ is not possible because two distinct integers appear exactly once in the second segment $$$[1, 2]$$$."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum MO = 998244353L;\nenum L = 300;\n\nvoid ADD(ref long t, long f) {\n t += f;\n if (t >= MO) {\n t -= MO;\n }\n}\n\nint N, K;\nint[] A;\n\nlong[] dp;\n\nint numBlocks;\nint[] mins;\nint[] keys;\nlong[][] sums;\n\nvoid init() {\n numBlocks = (N + 1 + L - 1) / L;\n mins = new int[numBlocks];\n keys = new int[N + 1];\n sums = new long[][](numBlocks, L + 1);\n}\n\nvoid addDP(int i) {\n debug {\n writeln(\"addDP \", i, \"; \", dp[i]);\n }\n foreach (x; keys[i] .. L + 1) {\n ADD(sums[i / L][x], dp[i]);\n }\n}\n\nvoid rangeAdd(int a, int b, int val) {\n debug {\n writeln(\" mins = \", mins);\n writeln(\" keys = \", keys);\n writeln(\"rangeAdd \", a, \" \", b, \" \", val);\n }\n foreach (e; 0 .. numBlocks) {\n const l = L * e, r = min(L * (e + 1), N);\n const aa = max(a, l), bb = min(b, r);\n if (aa < bb) {\n if (aa == l && bb == r) {\n mins[e] += val;\n } else {\n foreach (i; aa .. bb) {\n keys[i] += val;\n }\n const mn = keys[l .. r].minElement;\n mins[e] += mn;\n foreach (i; l .. r) {\n keys[i] -= mn;\n }\n sums[e][] = 0;\n foreach (i; l .. r) {\n ADD(sums[e][keys[i]], dp[i]);\n }\n foreach (x; 1 .. L + 1) {\n ADD(sums[e][x], sums[e][x - 1]);\n }\n }\n }\n }\n}\n\nlong getDPSum() {\n long ret;\n foreach (e; 0 .. numBlocks) {\n if (mins[e] <= K) {\n ADD(ret, sums[e][min(K - mins[e], L)]);\n }\n }\n debug {\n writeln(\"getDPSum = \", ret);\n }\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n K = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n auto app = new int[N + 1];\n app[] = -1;\n auto prev = new int[N];\n foreach (i; 0 .. N) {\n prev[i] = app[A[i]];\n app[A[i]] = i;\n }\n \n init();\n dp = new long[N + 1];\n dp[0] = 1;\n addDP(0);\n foreach (i; 0 .. N) {\n if (prev[i] >= 0) {\n rangeAdd(prev[prev[i]] + 1, prev[i] + 1, -1);\n }\n rangeAdd(prev[i] + 1, i + 1, +1);\n dp[i + 1] = getDPSum();\n addDP(i + 1);\n }\n writeln(dp[N]);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum MO = 998244353L;\nenum L = 2;\n\nint N, K;\nint[] A;\n\nlong[] dp;\n\nint numBlocks;\nint[] mins;\nint[] keys;\nlong[][] sums;\n\nvoid init() {\n numBlocks = (N + 1 + L - 1) / L;\n mins = new int[numBlocks];\n keys = new int[N + 1];\n sums = new long[][](numBlocks, L + 1);\n}\n\nvoid addDP(int i) {\n debug {\n writeln(\"addDP \", i, \"; \", dp[i]);\n }\n foreach (x; keys[i] .. L + 1) {\n sums[i / L][x] += dp[i];\n }\n}\n\nvoid rangeAdd(int a, int b, int val) {\n debug {\n writeln(\" mins = \", mins);\n writeln(\" keys = \", keys);\n writeln(\"rangeAdd \", a, \" \", b, \" \", val);\n }\n foreach (e; 0 .. numBlocks) {\n const l = L * e, r = min(L * (e + 1), N);\n const aa = max(a, l), bb = min(b, r);\n if (aa < bb) {\n if (aa == l && bb == r) {\n mins[e] += val;\n } else {\n foreach (i; aa .. bb) {\n keys[i] += val;\n }\n const mn = keys[l .. r].minElement;\n mins[e] += mn;\n foreach (i; l .. r) {\n keys[i] -= mn;\n }\n sums[e][] = 0;\n foreach (i; l .. r) {\n sums[e][keys[i]] += dp[i];\n }\n foreach (x; 1 .. L + 1) {\n sums[e][x] += sums[e][x - 1];\n }\n }\n }\n }\n}\n\nlong getDPSum() {\n long ret;\n foreach (e; 0 .. numBlocks) {\n if (mins[e] <= K) {\n ret += sums[e][min(K - mins[e], L)];\n }\n }\n debug {\n writeln(\"getDPSum = \", ret);\n }\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n K = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n auto app = new int[N + 1];\n app[] = -1;\n auto prev = new int[N];\n foreach (i; 0 .. N) {\n prev[i] = app[A[i]];\n app[A[i]] = i;\n }\n \n init();\n dp = new long[N + 1];\n dp[0] = 1;\n addDP(0);\n foreach (i; 0 .. N) {\n if (prev[i] >= 0) {\n rangeAdd(prev[prev[i]] + 1, prev[i] + 1, -1);\n }\n rangeAdd(prev[i] + 1, i + 1, +1);\n dp[i + 1] = getDPSum();\n addDP(i + 1);\n }\n writeln(dp[N]);\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "2c504f538b3e48fab4d8133ef6daedb0"} {"nl": {"description": "Vanya plays a game of balloons on the field of size n × n, where each cell contains a balloon with one of the values 0, 1, 2 or 3. The goal is to destroy a cross, such that the product of all values of balloons in the cross is maximum possible. There are two types of crosses: normal and rotated. For example:**o****o**ooooo**o****o**oro***o*o*o***o***o*o*o***oFormally, the cross is given by three integers r, c and d, such that d ≤ r, c ≤ n - d + 1. The normal cross consists of balloons located in cells (x, y) (where x stay for the number of the row and y for the number of the column), such that |x - r|·|y - c| = 0 and |x - r| + |y - c| < d. Rotated cross consists of balloons located in cells (x, y), such that |x - r| = |y - c| and |x - r| < d.Vanya wants to know the maximum possible product of the values of balls forming one cross. As this value can be large, output it modulo 109 + 7.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows and columns in the table with balloons. The each of the following n lines contains n characters '0', '1', '2' or '3' — the description of the values in balloons.", "output_spec": "Print the maximum possible product modulo 109 + 7. Note, that you are not asked to maximize the remainder modulo 109 + 7, but to find the maximum value and print it this modulo.", "sample_inputs": ["4\n1233\n0213\n2020\n0303", "5\n00300\n00300\n33333\n00300\n00300", "5\n00003\n02030\n00300\n03020\n30000", "5\n21312\n10003\n10002\n10003\n23231", "5\n12131\n12111\n12112\n21311\n21212"], "sample_outputs": ["108", "19683", "108", "3", "24"], "notes": "NoteIn the first sample, the maximum product is achieved for a rotated cross with a center in the cell (3, 3) and radius 1: 2·2·3·3·3 = 108."}, "positive_code": [{"source_code": "import std.stdio;\n\nimmutable int MOD = 1_000_000_007;\nimmutable int MAX_N = 1003;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = new byte [MAX_N * MAX_N];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tauto t = readln;\n\t\t\tforeach (j; 0..n)\n\t\t\t{\n\t\t\t\ta[(i + 1) * MAX_N + (j + 1)] =\n\t\t\t\t cast (byte) (t[j] - '0');\n\t\t\t}\n\t\t}\n\t\treal resR = 0;\n\t\tlong res = 0;\n\t\tforeach (i; 1..n + 1)\n\t\t{\n\t\t\tforeach (j; 1..n + 1)\n\t\t\t{\n\t\t\t\tvoid fun (int d1, int d2, int d3, int d4) ()\n\t\t\t\t{\n\t\t\t\t\tauto p1 = &a[i * MAX_N + j];\n\t\t\t\t\tauto p2 = &a[i * MAX_N + j];\n\t\t\t\t\tauto p3 = &a[i * MAX_N + j];\n\t\t\t\t\tauto p4 = &a[i * MAX_N + j];\n\t\t\t\t\treal curR = *p1;\n\t\t\t\t\tlong cur = *p1;\n\n\t\t\t\t\tif (curR)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor (int step = 0; ; step++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tp1 += d1;\n\t\t\t\t\t\t\tp2 += d2;\n\t\t\t\t\t\t\tp3 += d3;\n\t\t\t\t\t\t\tp4 += d4;\n\t\t\t\t\t\t\tint m = *p1 * *p2 *\n\t\t\t\t\t\t\t *p3 * *p4;\n\t\t\t\t\t\t\tif (!m)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcurR *= m;\n\t\t\t\t\t\t\tcur *= m;\n\t\t\t\t\t\t\tif (!(step & 3))\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tcur %= MOD;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (resR < curR)\n\t\t\t\t\t{\n\t\t\t\t\t\tresR = curR;\n\t\t\t\t\t\tres = cur;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfun !(-MAX_N - 1, -MAX_N + 1,\n\t\t\t\t +MAX_N - 1, +MAX_N + 1) ();\n\t\t\t\tfun !(-MAX_N, -1, +1, +MAX_N) ();\n\t\t\t}\n\t\t}\n\t\twriteln (res % MOD);\n\t}\n}\n"}, {"source_code": "import std.stdio;\nimmutable int MOD = 10 ^^ 9 + 7, MAX_N = 1002;\nvoid main () {\n\tint n;\n\twhile (readf (\" %s \", &n) > 0) {\n\t\tauto a = new byte [MAX_N * MAX_N];\n\t\tforeach (i; 1..n + 1) {\n\t\t\tauto t = readln;\n\t\t\tforeach (j; 1..n + 1) a[i * MAX_N + j] = cast (byte) (t[j - 1] - '0');\n\t\t}\n\t\treal resR = 0; long res = 0;\n\t\tvoid fun (int d1, int d2, int d3, int d4) (int i, int j) {\n\t\t\tauto p1 = &a[i * MAX_N + j], p2 = p1, p3 = p1, p4 = p1;\n\t\t\treal curR = *p1; long cur = *p1;\n\t\t\tif (curR) for (int step = 0; ; step++) {\n\t\t\t\tp1 += d1; p2 += d2; p3 += d3; p4 += d4;\n\t\t\t\tint m = *p1 * *p2 * *p3 * *p4;\n\t\t\t\tif (!m) break;\n\t\t\t\tcurR *= m; cur *= m;\n\t\t\t\tif (!(step & 3)) cur %= MOD;\n\t\t\t}\n\t\t\tif (resR < curR) {resR = curR; res = cur;}\n\t\t}\n\t\tforeach (i; 1..n + 1)\n\t\t\tforeach (j; 1..n + 1) {\n\t\t\t\tfun !(-MAX_N - 1, -MAX_N + 1, +MAX_N - 1, +MAX_N + 1) (i, j);\n\t\t\t\tfun !(-MAX_N, -1, +1, +MAX_N) (i, j);\n\t\t\t}\n\t\twriteln (res % MOD);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MOD = 1_000_000_007;\nimmutable int MAX_N = 1003;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = new byte [MAX_N * MAX_N];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tauto t = readln;\n\t\t\tforeach (j; 0..n)\n\t\t\t{\n\t\t\t\ta[(i + 1) * MAX_N + (j + 1)] =\n\t\t\t\t cast (byte) (t[j] - '0');\n\t\t\t}\n\t\t}\n\t\tdebug {writeln (a.chunks (MAX_N));}\n\t\treal resR = 0;\n\t\tlong res = 0;\n\t\tforeach (i; 1..n + 1)\n\t\t{\n\t\t\tforeach (j; 1..n + 1)\n\t\t\t{\n\t\t\t\tvoid fun (int d1, int d2, int d3, int d4) ()\n\t\t\t\t{\n\t\t\t\t\tauto p1 = &a[i * MAX_N + j];\n\t\t\t\t\tauto p2 = &a[i * MAX_N + j];\n\t\t\t\t\tauto p3 = &a[i * MAX_N + j];\n\t\t\t\t\tauto p4 = &a[i * MAX_N + j];\n\t\t\t\t\treal curR = *p1;\n\t\t\t\t\tlong cur = *p1;\n\n\t\t\t\t\tif (curR)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor (int step = 0; ; step++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tp1 += d1;\n\t\t\t\t\t\t\tp2 += d2;\n\t\t\t\t\t\t\tp3 += d3;\n\t\t\t\t\t\t\tp4 += d4;\n\t\t\t\t\t\t\tint m = *p1 * *p2 *\n\t\t\t\t\t\t\t *p3 * *p4;\n\t\t\t\t\t\t\tif (!m)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcurR *= m;\n\t\t\t\t\t\t\tcur *= m;\n\t\t\t\t\t\t\tif (!(step & 3))\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tcur %= MOD;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (resR < curR)\n\t\t\t\t\t{\n\t\t\t\t\t\tresR = curR;\n\t\t\t\t\t\tres = cur;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfun !(-MAX_N - 1, -MAX_N + 1,\n\t\t\t\t +MAX_N - 1, +MAX_N + 1) ();\n\t\t\t\tfun !(-MAX_N, -1, +1, +MAX_N) ();\n\t\t\t}\n\t\t}\n\n\t\tdebug {writeln (resR);}\n\t\twriteln (res % MOD);\n\t}\n}\n"}], "negative_code": [], "src_uid": "053483902f92e87f753c14e954568629"} {"nl": {"description": "This is an interactive problem.We hid from you a permutation $$$p$$$ of length $$$n$$$, consisting of the elements from $$$1$$$ to $$$n$$$. You want to guess it. To do that, you can give us 2 different indices $$$i$$$ and $$$j$$$, and we will reply with $$$p_{i} \\bmod p_{j}$$$ (remainder of division $$$p_{i}$$$ by $$$p_{j}$$$).We have enough patience to answer at most $$$2 \\cdot n$$$ queries, so you should fit in this constraint. Can you do it?As a reminder, a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "input_spec": "The only line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$) — length of the permutation.", "output_spec": null, "sample_inputs": ["3\n\n1\n\n2\n\n1\n\n0"], "sample_outputs": ["? 1 2\n\n? 3 2\n\n? 1 3\n\n? 2 1\n\n! 1 3 2"], "notes": null}, "positive_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;\n\nalias ll = long;\nalias to!int isz;\nalias rbt = redBlackTree;\nstatic string[] token;\nT rd(T = long)() { while(!token.length) token = readln.chomp.split; string res = token[0]; token.popFront; return res.to!T; }\nT[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\n/*******************************It's a Me, Mario!**********************************************/\n\nint ask(ll x, ll y){\n writeln(\"? \", x, \" \", y);\n stdout.flush();\n int res = rd!int;\n return res;\n}\n\n\nvoid play(){\n int n;\n n = rd!int;\n auto nums = rbt!(int);\n auto seen = rbt!(int);\n bool[int] vis;\n foreach(i; 1.. (n+1)){\n nums.insert(i);\n seen.insert(i);\n vis[i] = 0;\n }\n int[int] fin;\n int cur = 1;\n while(nums.length > 1){\n cur = nums.front;\n nums.removeKey(cur);\n int checknum = nums.front;\n int res = ask(cur, checknum);\n int res2 = ask(checknum, cur);\n if(res > res2){\n vis[cur] = 1;\n fin[cur] = res;\n seen.removeKey(res);\n }else{\n vis[checknum] = 1;\n fin[checknum] = res2;\n seen.removeKey(res2);\n nums.removeKey(checknum);\n nums.insert(cur);\n }\n }\n foreach(i; 1..n+1){\n if(!(i in fin)){\n fin[i] = seen.front;\n }\n }\n write(\"! \");\n foreach(i; 1..n+1){\n write(fin[i], \" \");\n }\n writeln;\n stdout.flush;\n}\n\nint main(){\n ll t = 1;\n /* t = rd; // Toggle! */\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n \n"}], "negative_code": [], "src_uid": "9bfbd68e61f4d2f3d404fd0413aded35"} {"nl": {"description": "Recently, the bear started studying data structures and faced the following problem.You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).Help the bear cope with the problem.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct. The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.", "output_spec": "Print m integers — the answers to the queries on the order the queries appear in the input.", "sample_inputs": ["6\n5 5 7 10 14 15\n3\n2 11\n3 12\n4 4", "7\n2 3 5 7 11 4 8\n2\n8 10\n2 123"], "sample_outputs": ["9\n7\n0", "0\n7"], "notes": "NoteConsider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0. "}, "positive_code": [{"source_code": "import std.stdio, std.string, std.conv;\nimport std.random;\n\nstruct XD\n{\n long[] res;\n int max;\n this(long[] res, int max)\n {\n this.res = res;\n this.max = max;\n }\n};\n\nstruct Q\n{\n int left;\n int right;\n};\n\nXD calc(int[] x)\n{\n int m = 0;\n foreach (i; 0 .. x.length) if (x[i] > m) m = x[i];\n auto count = new int[m + 1];\n foreach (i; 0 .. x.length) ++ count[x[i]];\n auto notPrime = new bool[m + 1];\n auto res = new long[m + 1];\n foreach (i; 2 .. m + 1)\n {\n res[i] = res[i - 1];\n if (!notPrime[i])\n {\n long cnt = 0;\n for (int j = i; j <= m; j += i)\n {\n cnt += count[j];\n if (j > i) notPrime[j] = true;\n }\n res[i] += cnt;\n }\n }\n return XD(res, m);\n}\n\nvoid solve(int[] x, Q[] qs)\n{\n XD xd = calc(x);\n foreach (i; 0 .. qs.length)\n {\n if (qs[i].left > xd.max)\n {\n writeln(0);\n continue;\n }\n if (qs[i].left > xd.max) qs[i].left = xd.max;\n if (qs[i].right > xd.max) qs[i].right = xd.max;\n writeln(xd.res[qs[i].right] - xd.res[qs[i].left - 1]);\n }\n}\n\nvoid main(string[] args)\n{\n int n, m;\n while (scanf(\"%d\", &n) == 1)\n {\n //n = 1000000;\n auto x = new int[n];\n //Random gen;\n //foreach (i; 0 .. n) x[i] = uniform(2, 10000000, gen);\n foreach (i; 0 .. n) scanf(\"%d\", &x[i]);\n //foreach (i; 0 .. n) writeln(x[i], \" \");\n //writeln();\n scanf(\"%d\", &m);\n //m = 50000;\n auto qs = new Q[m];\n foreach (i; 0 .. m) scanf(\"%d%d\", &qs[i].left, &qs[i].right);\n //{\n //qs[i].left = uniform(2, 2000000000, gen);\n //qs[i].right = uniform(qs[i].left, 2000000000, gen);\n //write(qs[i].left, \" \", qs[i].right);\n //}\n //writeln();\n solve(x, qs);\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.conv;\nimport std.random;\n\nstruct XD\n{\n long[] res;\n int max;\n this(long[] res, int max)\n {\n this.res = res;\n this.max = max;\n }\n};\n\nstruct Q\n{\n int left;\n int right;\n};\n\nXD calc(int[] x)\n{\n int m = 0;\n foreach (i; 0 .. x.length) if (x[i] > m) m = x[i];\n auto count = new int[m + 1];\n foreach (i; 0 .. x.length) ++ count[x[i]];\n int[] primes;\n auto notPrime = new bool[m + 1];\n auto res = new long[2];\n foreach (i; 2 .. m + 1)\n {\n if (!notPrime[i])\n {\n primes ~= i;\n int cnt = 0;\n for (int j = i; j <= m; j += i)\n {\n cnt += count[j];\n if (j > i) notPrime[j] = true;\n }\n res ~= res[$ - 1] + cnt;\n }\n else\n {\n res ~= res[$ - 1];\n }\n }\n return XD(res, m);\n}\n\nvoid solve(int[] x, Q[] qs)\n{\n XD xd = calc(x);\n foreach (i; 0 .. qs.length)\n {\n if (qs[i].left > xd.max) qs[i].left = xd.max;\n if (qs[i].right > xd.max) qs[i].right = xd.max;\n writeln(xd.res[qs[i].right] - xd.res[qs[i].left - 1]);\n }\n}\n\nvoid main(string[] args)\n{\n int n, m;\n while (scanf(\"%d\", &n) == 1)\n {\n //n = 1000000;\n auto x = new int[n];\n //Random gen;\n //foreach (i; 0 .. n) x[i] = uniform(2, 10000000, gen);\n foreach (i; 0 .. n) scanf(\"%d\", &x[i]);\n //foreach (i; 0 .. n) writeln(x[i], \" \");\n //writeln();\n scanf(\"%d\", &m);\n //m = 50000;\n auto qs = new Q[m];\n foreach (i; 0 .. m) scanf(\"%d%d\", &qs[i].left, &qs[i].right);\n //{\n //qs[i].left = uniform(2, 2000000000, gen);\n //qs[i].right = uniform(qs[i].left, 2000000000, gen);\n //write(qs[i].left, \" \", qs[i].right);\n //}\n //writeln();\n solve(x, qs);\n }\n}\n"}], "src_uid": "fe42c7f0222497ce3fff51b3676f42d1"} {"nl": {"description": "We guessed a permutation $$$p$$$ consisting of $$$n$$$ integers. The permutation of length $$$n$$$ is the array of length $$$n$$$ where each element from $$$1$$$ to $$$n$$$ appears exactly once. This permutation is a secret for you.For each position $$$r$$$ from $$$2$$$ to $$$n$$$ we chose some other index $$$l$$$ ($$$l < r$$$) and gave you the segment $$$p_l, p_{l + 1}, \\dots, p_r$$$ in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly $$$n-1$$$ segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order.For example, if the secret permutation is $$$p=[3, 1, 4, 6, 2, 5]$$$ then the possible given set of segments can be: $$$[2, 5, 6]$$$ $$$[4, 6]$$$ $$$[1, 3, 4]$$$ $$$[1, 3]$$$ $$$[1, 2, 4, 6]$$$ Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists).You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 \\le n \\le 200$$$) — the length of the permutation. The next $$$n-1$$$ lines describe given segments. The $$$i$$$-th line contains the description of the $$$i$$$-th segment. The line starts with the integer $$$k_i$$$ ($$$2 \\le k_i \\le n$$$) — the length of the $$$i$$$-th segment. Then $$$k_i$$$ integers follow. All integers in a line are distinct, sorted in ascending order, between $$$1$$$ and $$$n$$$, inclusive. It is guaranteed that the required $$$p$$$ exists for each test case. It is also guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200$$$ ($$$\\sum n \\le 200$$$).", "output_spec": "For each test case, print the answer: $$$n$$$ integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$, all $$$p_i$$$ should be distinct) — any suitable permutation (i.e. any permutation corresponding to the test case input).", "sample_inputs": ["5\n6\n3 2 5 6\n2 4 6\n3 1 3 4\n2 1 3\n4 1 2 4 6\n5\n2 2 3\n2 1 2\n2 1 4\n2 4 5\n7\n3 1 2 6\n4 1 3 5 6\n2 1 2\n3 4 5 7\n6 1 2 3 4 5 6\n3 1 3 6\n2\n2 1 2\n5\n2 2 5\n3 2 3 5\n4 2 3 4 5\n5 1 2 3 4 5"], "sample_outputs": ["3 1 4 6 2 5 \n3 2 1 4 5 \n2 1 6 3 5 4 7 \n1 2 \n2 5 3 4 1"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\treadln;\n\tint n;\nmultitest_loop:\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto p = new int [] [n - 1];\n\t\tauto d0 = new int [n];\n\t\tauto hasElement = new bool [] [] (n - 1, n);\n\t\tforeach (i, ref q; p)\n\t\t{\n\t\t\tq = readln.splitter.drop (1).map !(to !(int)).array;\n\t\t\tq[] -= 1;\n\t\t\tforeach (ref v; q)\n\t\t\t{\n\t\t\t\td0[v] += 1;\n\t\t\t\thasElement[i][v] = true;\n\t\t\t}\n\t\t}\n\n\t\tint [] solve (int v, int first)\n\t\t{\n\t\t\tauto d = d0.dup;\n\t\t\tauto pos = new int [n - 1];\n\t\t\tpos[] = 1;\n\t\t\tauto res = new int [n];\n\t\t\tauto total = n.iota.sum;\n\t\t\tforeach_reverse (i; 2..n)\n\t\t\t{\n\t\t\t\tres[i] = v;\n\t\t\t\tdebug {writeln (\"i = \", i, \", v = \", v);}\n\t\t\t\tint j = 0;\n\t\t\t\twhile (pos[j] != 1 || !hasElement[j][v])\n\t\t\t\t{\n\t\t\t\t\tj += 1;\n\t\t\t\t}\n\t\t\t\tpos[j] = i;\n\n\t\t\t\tv = int.max;\n\t\t\t\tforeach (const ref u; p[j])\n\t\t\t\t{\n\t\t\t\t\td[u] -= 1;\n\t\t\t\t\tif (d[u] == 1 && u != first)\n\t\t\t\t\t{\n\t\t\t\t\t\tv = u;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (v == int.max)\n\t\t\t\t{\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t}\n\t\t\tres[1] = total - res.sum - first;\n\t\t\tres[0] = first;\n\t\t\tdebug {writeln (\"pos = \", pos);}\n\t\t\tdebug {writeln (\"res = \", res);}\n\n\t\t\tforeach (j, i; pos)\n\t\t\t{\n\t\t\t\tauto len = p[j].length;\n\t\t\t\tdebug {writeln (res[i - len + 1..i + 1]);}\n\t\t\t\tforeach (k; i - len + 1..i + 1)\n\t\t\t\t{\n\t\t\t\t\tif (!hasElement[j][res[k]])\n\t\t\t\t\t{\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tforeach (first; 0..n)\n\t\t{\n\t\t\tforeach (v; 0..n)\n\t\t\t{\n\t\t\t\tif (v != first && d0[v] == 1)\n\t\t\t\t{\n\t\t\t\t\tauto answer = solve (v, first);\n\t\t\t\t\tif (answer !is null)\n\t\t\t\t\t{\n\t\t\t\t\t\tanswer[] += 1;\n\t\t\t\t\t\twritefln !(\"%(%s %)\") (answer);\n\t\t\t\t\t\tcontinue multitest_loop;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tassert (false);\n\t}\n}\n"}], "negative_code": [], "src_uid": "bb521123f9863345d75cfe10677ab344"} {"nl": {"description": "Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:You have an array $$$a$$$ consisting of $$$n$$$ positive integers. An operation consists of choosing an element and either adding $$$1$$$ to it or subtracting $$$1$$$ from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not $$$1$$$. Find the minimum number of operations needed to make the array good.Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!", "input_spec": "The first line contains an integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$)  — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. ($$$1 \\le a_i \\le 10^{12}$$$)  — the elements of the array.", "output_spec": "Print a single integer  — the minimum number of operations required to make the array good.", "sample_inputs": ["3\n6 2 4", "5\n9 8 7 3 1"], "sample_outputs": ["0", "4"], "notes": "NoteIn the first example, the first array is already good, since the greatest common divisor of all the elements is $$$2$$$.In the second example, we may apply the following operations: Add $$$1$$$ to the second element, making it equal to $$$9$$$. Subtract $$$1$$$ from the third element, making it equal to $$$6$$$. Add $$$1$$$ to the fifth element, making it equal to $$$2$$$. Add $$$1$$$ to the fifth element again, making it equal to $$$3$$$. The greatest common divisor of all elements will then be equal to $$$3$$$, so the array will be good. It can be shown that no sequence of three or less operations can make the array good."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nimport std.functional;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[N] factorize(Z n)\n{\n N[N] res;\n for(N d = 2; d * d <= n; d++)\n while (n % d == 0)\n res[d]++, n /= d;\n if (n != 1)\n res[n]++;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n N minOps(N m)\n {\n if (m == 0)\n return N.max;\n N minOpsPrime(N p)\n {\n N ops = 0;\n foreach(e; a)\n\tops += min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));\n return ops;\n }\n auto factorization = m.factorize;\n N ops = n;\n foreach(prime, order; factorization)\n\tops = min(ops, memoize!minOpsPrime(prime));\n return ops;\n }\n foreach(e; a[0 .. min(18, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 1])\n minops = min(minops, memoize!minOps(t));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nimport std.functional;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n while (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[] factorize(Z n)\n{\n N[] res;\n for(N d = 2; d * d <= n; d++)\n if (n % d == 0)\n {\n\tres ~= d;\n\tn /= d;\n\twhile (n % d == 0)\n\t n /= d;\n }\n if (n != 1)\n res ~= n;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minOpsPrime(N p)\n {\n return a.map!(e => min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p))).sum;\n }\n N minOps(N m)\n {\n return m == 0? N.max : m.factorize.map!(memoize!minOpsPrime).fold!min(N.max);\n }\n ans(a[0..min(18, cast(size_t)(n))]\n .map!(e => [e - 1, e, e + 1]\n\t .map!(memoize!minOps)\n\t .fold!min(N.max))\n .fold!min(N.max));\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nimport std.functional;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[] factorize(Z n)\n{\n N[] res;\n for(N d = 2; d * d <= n; d++)\n if(n % d == 0)\n {\n\tres ~= d;\n\twhile (n % d == 0)\n\t n /= d;\n }\n if (n != 1)\n res ~= n;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n N minOps(N m)\n {\n if (m == 0)\n return N.max;\n N minOpsPrime(N p)\n {\n N ops = 0;\n foreach(e; a)\n\tops += min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));\n return ops;\n }\n auto factorization = m.factorize;\n N ops = n;\n foreach(prime; factorization)\n\tops = min(ops, memoize!minOpsPrime(prime));\n return ops;\n }\n foreach(e; a[0 .. min(18, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 1])\n minops = min(minops, memoize!minOps(t));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nimport std.functional;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[] factorize(Z n)\n{\n N[] res;\n for(N d = 2; d * d <= n; d++)\n if (n % d == 0)\n {\n\tres ~= d;\n\tn /= d;\n\twhile (n % d == 0)\n\t n /= d;\n }\n if (n != 1)\n res ~= n;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n\n N minOpsElem(N e, N p)\n {\n return min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));\n }\n N minOpsPrime(N p)\n {\n return a.map!(e => minOpsElem(e, p)).sum;\n }\n N minOps(N m)\n {\n return m == 0? N.max : m.factorize.map!(memoize!minOpsPrime).fold!min(N.max);\n }\n foreach(e; a[0 .. min(18, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 1])\n minops = min(minops, memoize!minOps(t));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm, std.conv, std.random, std.range, std.stdio, std.string, std.typecons;\nvoid main () {\n\tauto n = readln.strip.to !(int), a = readln.splitter.map !(to !(long)).array;\n\tint [long] d;\n\tforeach (s; 0..30) {\n\t\tauto y = a[uniform (0, n)];\n\t\tforeach (z; [y - 1, y, y + 1]) {\n\t\t\tfor (long c = 2; c * c <= z; c++) if (z % c == 0) {\n\t\t\t\td[c] = 1;\n\t\t\t\twhile (z % c == 0) z /= c;\n\t\t\t}\n\t\t\tif (z > 1) d[z] = 1;\n\t\t}\n\t}\n\tlong r = n;\n\tforeach (k; d.byKey) r = min (r, a.map !(z => z < k ? k - z : min (z % k, k - z % k)).sum);\n\tr.writeln;\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n \nimmutable int limit = 10 ^^ 6;\nimmutable int steps = 10 ^^ 3;\nimmutable int divs = 10 ^^ 3 * 2;\nimmutable int first = 10 ^^ 2 * 2;\n \nint [] p;\n \nvoid prepare ()\n{\n\tauto s = new bool [limit];\n\ts[] = true;\n\ts[0] = false;\n\ts[1] = false;\n\tp = [];\n\tp.reserve (limit / 12);\n\tfor (int v = 2; v < limit; v++)\n\t{\n\t\tif (s[v])\n\t\t{\n\t\t\tp ~= v;\n\t\t\tif (v * 1L * v < limit)\n\t\t\t{\n\t\t\t\tfor (int u = v * v; u < limit; u += v)\n\t\t\t\t{\n\t\t\t\t\ts[u] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n \nvoid main ()\n{\n\trndGen.seed (236426);\n\tprepare ();\n \n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n\t\tsort (a);\n\t\tbool [long] d;\n\t\tforeach (x; p[0..first])\n\t\t{\n\t\t\td[x] = true;\n\t\t}\n\t\tfor (int step = 0; step < steps && d.length < divs; step++)\n\t\t{\n\t\t\tauto z0 = a[uniform (0, n)];\n\t\t\tforeach (z; [z0 - 1, z0, z0 + 1])\n\t\t\t{\n\t\t\t\tforeach (const c; p)\n\t\t\t\t{\n\t\t\t\t\tif (c * 1L * c > z)\n\t\t\t\t\t{\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif (z % c == 0)\n\t\t\t\t\t{\n\t\t\t\t\t\td[c] = true;\n\t\t\t\t\t\tdo\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tz /= c;\n\t\t\t\t\t\t}\n\t\t\t\t\t\twhile (z % c == 0);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (z > 1)\n\t\t\t\t{\n\t\t\t\t\td[z] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n \n\t\tlong res = n;\n\t\tauto e = d.byKey.array;\n\t\tsort (e);\n\t\tdebug {writeln (e);}\n\t\tforeach (const k; e)\n\t\t{\n\t\t\tlong cur = 0;\n\t\t\tforeach (const z; a)\n\t\t\t{\n\t\t\t\tif (z < k)\n\t\t\t\t{\n\t\t\t\t\tcur += k - z;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlong rem = z % k;\n\t\t\t\t\tcur += min (rem, k - rem);\n\t\t\t\t}\n\t\t\t\tif (cur >= res)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (k, \": \", cur);}\n\t\t\tres = min (res, cur);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "// Code By H~$~C\n\nmodule main;\n\nimport core.stdc.stdio, core.stdc.stdlib, core.stdc.string;\nimport std.stdio, std.array, std.string, std.math;\nimport std.algorithm, std.range, std.random;\nimport std.conv, std.typecons;\nalias to!(string) to_string;\nalias to!(int) to_int;\nalias to!(long) to_long;\nalias to!(double) to_double;\nimmutable int inf = 0x3f3f3f3f;\nimmutable long lnf = 0x3f3f3f3f3f3f3f3f;\nimmutable typeof(null) NULL = null;\n\nint n;\nlong[] arr;\n\nvoid main() {\n n = readln.strip.to_int;\n arr = randomShuffle(readln.splitter.map!(to_long).array, rndGen);\n int[long] mp;\n foreach(i; 0 .. min(30, to_int(arr.length))) {\n foreach(x; [arr[i] - 1, arr[i], arr[i] + 1]) {\n for(long p = 2; p * p <= x; p++) {\n if (x % p == 0) {\n mp[p] = 1;\n while (x % p == 0) x /= p;\n }\n }\n if (x > 1) mp[x] = 1;\n }\n }\n long ans = n;\n foreach(k; mp.byKey) {\n ans = min(ans, arr.map!(x => x < k ? k - x : min(x % k, k - x % k)).sum);\n }\n ans.writeln;\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[N] factorize(Z n)\n{\n N[N] res;\n for(N d = 2; d * d <= n; d++)\n while (n % d == 0)\n res[d]++, n /= d;\n if (n != 1)\n res[n]++;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n N minOps(N m)\n {\n if (m == 0)\n return N.max;\n N minOpsPrime(N p)\n {\n bool shallPrint = p == 3;\n N ops = 0;\n foreach(e; a)\n\t ops += min(pmod(e, p) > e? pmod(e, p) : N.max, pmod(-e, p));\n return ops;\n }\n auto factorization = m.factorize;\n N ops = n;\n foreach(prime, order; factorization)\n\tops = min(ops, minOpsPrime(prime));\n return ops;\n }\n foreach(e; a[0 .. min(40, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 2])\n minops = min(minops, minOps(e));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[N] factorize(Z n)\n{\n N[N] res;\n for(N d = 2; d * d <= n; d++)\n while (n % d == 0)\n res[d]++, n /= d;\n if (n != 1)\n res[n]++;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n N minOps(N m)\n {\n if (m == 0)\n return N.max;\n N minOpsPrime(N p)\n {\n bool shallPrint = p == 3;\n N ops = 0;\n foreach(e; a)\n\t ops += min(pmod(e, p) > e? pmod(e, p) : N.max, pmod(-e, p));\n return ops;\n }\n auto factorization = m.factorize;\n N ops = n;\n foreach(prime, order; factorization)\n\tops = min(ops, minOpsPrime(prime));\n return ops;\n }\n foreach(e; a[0 .. min(40, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 1])\n minops = min(minops, minOps(e));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[N] factorize(Z n)\n{\n N[N] res;\n for(N d = 2; d * d <= n; d++)\n while (n % d == 0)\n res[d]++, n /= d;\n if (n != 1)\n res[n]++;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n N minOps(N m)\n {\n if (m == 0)\n return N.max;\n N minOpsPrime(N p)\n {\n bool shallPrint = p == 3;\n N ops = 0;\n foreach(e; a)\n\t ops += min(pmod(e, p) > e? pmod(e, p) : N.max, pmod(-e, p));\n return ops;\n }\n auto factorization = m.factorize;\n N ops = n;\n foreach(prime, order; factorization)\n\tops = min(ops, minOpsPrime(prime));\n return ops;\n }\n foreach(e; a[0 .. min(40, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 1])\n minops = min(minops, minOps(t));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nimport std.random;\nimport std.functional;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nalias Z = long;\nalias N = long;\n\nN[] factorize(Z n)\n{\n N[] res;\n for(N d = 2; d * d <= n; d++)\n if (n % d)\n {\n\tres ~= d;\n\tn /= d;\n\twhile (n % d == 0)\n\t n /= d;\n }\n if (n != 1)\n res ~= n;\n return res;\n}\n\nN pmod(N x, N m)\n{\n return (x%m+m)%m;\n}\n\nvoid main()\n{\n N n; get(n);\n auto a = new N[cast(size_t)(n)]; get(a);\n a = a.randomShuffle;\n N minops = n;\n\n N minOpsElem(N e, N p)\n {\n return min((e > pmod(e, p))? pmod(e, p) : N.max, pmod(-e, p));\n }\n N minOpsPrime(N p)\n {\n return a.map!(e => minOpsElem(e, p)).sum;\n }\n N minOps(N m)\n {\n return m == 0? N.max : m.factorize.map!(memoize!minOpsPrime).fold!min(N.max);\n }\n foreach(e; a[0 .. min(18, cast(size_t)(n))])\n foreach(t; [e - 1, e, e + 1])\n minops = min(minops, memoize!minOps(t));\n ans(minops);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 10 ^^ 6;\nimmutable int steps = 10 ^^ 3 * 2;\nimmutable int divs = 10 ^^ 3 * 4;\nimmutable int first = 10 ^^ 2 * 4;\n\nint [] p;\n\nvoid prepare ()\n{\n\tauto s = new bool [limit];\n\ts[] = true;\n\ts[0] = false;\n\ts[1] = false;\n\tp = [];\n\tp.reserve (limit / 12);\n\tfor (int v = 2; v < limit; v++)\n\t{\n\t\tif (s[v])\n\t\t{\n\t\t\tp ~= v;\n\t\t\tif (v * 1L * v < limit)\n\t\t\t{\n\t\t\t\tfor (int u = v * v; u < limit; u += v)\n\t\t\t\t{\n\t\t\t\t\ts[u] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\trndGen.seed (236426);\n\tprepare ();\n\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n\t\trandomShuffle (a);\n//\t\tsort (a);\n\t\tbool [long] d;\n\t\tforeach (x; p[0..first])\n\t\t{\n\t\t\td[x] = true;\n\t\t}\n\t\tfor (int step = 0; step < steps && d.length < divs; step++)\n\t\t{\n\t\t\tauto z = a[uniform (0, n)];\n\t\t\tforeach (const c; p)\n\t\t\t{\n\t\t\t\tif (c * 1L * c > z)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (z % c == 0)\n\t\t\t\t{\n\t\t\t\t\td[c] = true;\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tz /= c;\n\t\t\t\t\t}\n\t\t\t\t\twhile (z % c == 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (z > 1)\n\t\t\t{\n\t\t\t\td[z] = true;\n\t\t\t}\n\t\t}\n\n\t\tlong res = n;\n\t\tauto e = d.byKey.array;\n\t\tsort (e);\n\t\tdebug {writeln (e);}\n\t\tforeach (const k; e)\n\t\t{\n\t\t\tlong cur = 0;\n\t\t\tforeach (const z; a)\n\t\t\t{\n\t\t\t\tif (z < k)\n\t\t\t\t{\n\t\t\t\t\tcur += k - z;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlong rem = z % k;\n\t\t\t\t\tcur += min (rem, k - rem);\n\t\t\t\t}\n\t\t\t\tif (cur >= res)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (k, \": \", cur);}\n\t\t\tres = min (res, cur);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 10 ^^ 6;\nimmutable int steps = 10 ^^ 3;\nimmutable int divs = 10 ^^ 3 * 2;\nimmutable int first = 10 ^^ 2 * 2;\n\nint [] p;\n\nvoid prepare ()\n{\n\tauto s = new bool [limit];\n\ts[] = true;\n\ts[0] = false;\n\ts[1] = false;\n\tp = [];\n\tp.reserve (limit / 12);\n\tfor (int v = 2; v < limit; v++)\n\t{\n\t\tif (s[v])\n\t\t{\n\t\t\tp ~= v;\n\t\t\tif (v * 1L * v < limit)\n\t\t\t{\n\t\t\t\tfor (int u = v * v; u < limit; u += v)\n\t\t\t\t{\n\t\t\t\t\ts[u] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\trndGen.seed (236426);\n\tprepare ();\n\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n\t\tsort (a);\n\t\tbool [long] d;\n\t\tforeach (x; p[0..first])\n\t\t{\n\t\t\td[x] = true;\n\t\t}\n\t\tfor (int step = 0; step < steps && d.length < divs; step++)\n\t\t{\n\t\t\tauto z = a[uniform (0, n)];\n\t\t\tforeach (const c; p)\n\t\t\t{\n\t\t\t\tif (c * 1L * c > z)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (z % c == 0)\n\t\t\t\t{\n\t\t\t\t\td[c] = true;\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tz /= c;\n\t\t\t\t\t}\n\t\t\t\t\twhile (z % c == 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (z > 1)\n\t\t\t{\n\t\t\t\td[z] = true;\n\t\t\t}\n\t\t}\n\n\t\tlong res = n;\n\t\tauto e = d.byKey.array;\n\t\tsort (e);\n\t\tdebug {writeln (e);}\n\t\tforeach (const k; e)\n\t\t{\n\t\t\tlong cur = 0;\n\t\t\tforeach (const z; a)\n\t\t\t{\n\t\t\t\tif (z < k)\n\t\t\t\t{\n\t\t\t\t\tcur += k - z;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlong rem = z % k;\n\t\t\t\t\tcur += min (rem, k - rem);\n\t\t\t\t}\n\t\t\t\tif (cur >= res)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (k, \": \", cur);}\n\t\t\tres = min (res, cur);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 10 ^^ 6;\nimmutable int steps = 10 ^^ 3 * 2;\nimmutable int divs = 10 ^^ 3 * 6;\nimmutable int first = 10 ^^ 2 * 9;\n\nint [] p;\n\nvoid prepare ()\n{\n\tauto s = new bool [limit];\n\ts[] = true;\n\ts[0] = false;\n\ts[1] = false;\n\tp = [];\n\tp.reserve (limit / 12);\n\tfor (int v = 2; v < limit; v++)\n\t{\n\t\tif (s[v])\n\t\t{\n\t\t\tp ~= v;\n\t\t\tif (v * 1L * v < limit)\n\t\t\t{\n\t\t\t\tfor (int u = v * v; u < limit; u += v)\n\t\t\t\t{\n\t\t\t\t\ts[u] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\trndGen.seed (236426);\n\tprepare ();\n\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n//\t\trandomShuffle (a);\n\t\tsort (a);\n\t\tbool [long] d;\n\t\tforeach (x; p[0..first])\n\t\t{\n\t\t\td[x] = true;\n\t\t}\n\t\tfor (int step = 0; step < steps && d.length < divs; step++)\n\t\t{\n\t\t\tauto z = a[uniform (0, n)];\n\t\t\tforeach (const c; p)\n\t\t\t{\n\t\t\t\tif (c * 1L * c > z)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (z % c == 0)\n\t\t\t\t{\n\t\t\t\t\td[c] = true;\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tz /= c;\n\t\t\t\t\t}\n\t\t\t\t\twhile (z % c == 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (z > 1)\n\t\t\t{\n\t\t\t\td[z] = true;\n\t\t\t}\n\t\t}\n\n\t\tlong res = n;\n\t\tauto e = d.byKey.array;\n\t\tsort (e);\n\t\tdebug {writeln (e);}\n\t\tforeach (const k; e)\n\t\t{\n\t\t\tlong cur = 0;\n\t\t\tforeach (const z; a)\n\t\t\t{\n\t\t\t\tif (z < k)\n\t\t\t\t{\n\t\t\t\t\tcur += k - z;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlong rem = z % k;\n\t\t\t\t\tcur += min (rem, k - rem);\n\t\t\t\t}\n\t\t\t\tif (cur >= res)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (k, \": \", cur);}\n\t\t\tres = min (res, cur);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 10 ^^ 6;\nimmutable int steps = 10 ^^ 3 * 2;\nimmutable int divs = 10 ^^ 3 * 5;\nimmutable int first = 10 ^^ 2 * 8;\n\nint [] p;\n\nvoid prepare ()\n{\n\tauto s = new bool [limit];\n\ts[] = true;\n\ts[0] = false;\n\ts[1] = false;\n\tp = [];\n\tp.reserve (limit / 12);\n\tfor (int v = 2; v < limit; v++)\n\t{\n\t\tif (s[v])\n\t\t{\n\t\t\tp ~= v;\n\t\t\tif (v * 1L * v < limit)\n\t\t\t{\n\t\t\t\tfor (int u = v * v; u < limit; u += v)\n\t\t\t\t{\n\t\t\t\t\ts[u] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\trndGen.seed (1236426);\n\tprepare ();\n\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n//\t\trandomShuffle (a);\n\t\tsort (a);\n\t\tbool [long] d;\n\t\tforeach (x; p[0..first])\n\t\t{\n\t\t\td[x] = true;\n\t\t}\n\t\tfor (int step = 0; step < steps && d.length < divs; step++)\n\t\t{\n\t\t\tauto z = a[uniform (0, n)];\n\t\t\tforeach (const c; p)\n\t\t\t{\n\t\t\t\tif (c * 1L * c > z)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (z % c == 0)\n\t\t\t\t{\n\t\t\t\t\td[c] = true;\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tz /= c;\n\t\t\t\t\t}\n\t\t\t\t\twhile (z % c == 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (z > 1)\n\t\t\t{\n\t\t\t\td[z] = true;\n\t\t\t}\n\t\t}\n\n\t\tlong res = n;\n\t\tauto e = d.byKey.array;\n\t\tsort (e);\n\t\tdebug {writeln (e);}\n\t\tforeach (const k; e)\n\t\t{\n\t\t\tlong cur = 0;\n\t\t\tforeach (const z; a)\n\t\t\t{\n\t\t\t\tif (z < k)\n\t\t\t\t{\n\t\t\t\t\tcur += k - z;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlong rem = z % k;\n\t\t\t\t\tcur += min (rem, k - rem);\n\t\t\t\t}\n\t\t\t\tif (cur >= res)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (k, \": \", cur);}\n\t\t\tres = min (res, cur);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 10 ^^ 6;\nimmutable int steps = 10 ^^ 3 * 2;\nimmutable int divs = 10 ^^ 3 * 4;\nimmutable int first = 10 ^^ 2 * 4;\n\nint [] p;\n\nvoid prepare ()\n{\n\tauto s = new bool [limit];\n\ts[] = true;\n\ts[0] = false;\n\ts[1] = false;\n\tp = [];\n\tp.reserve (limit / 12);\n\tfor (int v = 2; v < limit; v++)\n\t{\n\t\tif (s[v])\n\t\t{\n\t\t\tp ~= v;\n\t\t\tif (v * 1L * v < limit)\n\t\t\t{\n\t\t\t\tfor (int u = v * v; u < limit; u += v)\n\t\t\t\t{\n\t\t\t\t\ts[u] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\trndGen.seed (236426);\n\tprepare ();\n\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n\t\tsort (a);\n\t\tbool [long] d;\n\t\tforeach (x; p[0..first])\n\t\t{\n\t\t\td[x] = true;\n\t\t}\n\t\tfor (int step = 0; step < steps && d.length < divs; step++)\n\t\t{\n\t\t\tauto z = a[uniform (0, n)];\n\t\t\tforeach (const c; p)\n\t\t\t{\n\t\t\t\tif (c * 1L * c > z)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (z % c == 0)\n\t\t\t\t{\n\t\t\t\t\td[c] = true;\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tz /= c;\n\t\t\t\t\t}\n\t\t\t\t\twhile (z % c == 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (z > 1)\n\t\t\t{\n\t\t\t\td[z] = true;\n\t\t\t}\n\t\t}\n\n\t\tlong res = n;\n\t\tauto e = d.byKey.array;\n\t\tsort (e);\n\t\tdebug {writeln (e);}\n\t\tforeach (const k; e)\n\t\t{\n\t\t\tlong cur = 0;\n\t\t\tforeach (const z; a)\n\t\t\t{\n\t\t\t\tif (z < k)\n\t\t\t\t{\n\t\t\t\t\tcur += k - z;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlong rem = z % k;\n\t\t\t\t\tcur += min (rem, k - rem);\n\t\t\t\t}\n\t\t\t\tif (cur >= res)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (k, \": \", cur);}\n\t\t\tres = min (res, cur);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "src_uid": "40a32523f982e24fba2c785fc6a27881"} {"nl": {"description": "Just to remind, girls in Arpa's land are really nice.Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y. Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.", "input_spec": "The first line contains integers n, m and w (1  ≤  n  ≤  1000, , 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.", "output_spec": "Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.", "sample_inputs": ["3 1 5\n3 2 5\n2 4 2\n1 2", "4 2 11\n2 4 6 6\n6 4 2 1\n1 2\n2 3"], "sample_outputs": ["6", "7"], "notes": "NoteIn the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, m, w;\n readf(\"%s %s %s\", &n, &m, &w);\n readln;\n \n auto ws = readln.chomp.split.map!(to!int).array;\n auto bs = readln.chomp.split.map!(to!int).array;\n \n auto grpid = n.iota.array;\n \n foreach (_; 0 .. m) {\n int a, b;\n readf(\"%s %s\", &a, &b);\n readln;\n --a, --b;\n \n int old = grpid[b];\n foreach (ref e; grpid) if (e == old) e = grpid[a];\n }\n \n auto vis = (false).repeat(n).array;\n auto idsForGrp = new int[][] (grpid.dup.sort.uniq.array.length);\n \n debug { grpid.writeln; }\n \n Tuple!(int, int)[] grpsVals;\n foreach (i, e; grpid) {\n if (vis[i]) continue;\n \n int wsm = 0, bsm = 0;\n foreach (j, f; grpid) {\n if (e != f) continue;\n \n idsForGrp[grpsVals.length] ~= j.to!int;\n vis[j] = true;\n wsm += ws[j];\n bsm += bs[j];\n }\n \n grpsVals ~= tuple(wsm, bsm);\n }\n \n debug { grpsVals.writeln; }\n debug { idsForGrp.writeln; }\n \n auto dp = new int[][] (grpsVals.length, w+1);\n foreach (i, t; grpsVals) {\n int grw = t[0], grb = t[1];\n \n if (i > 0) dp[i][] = dp[i-1][];\n \n for (int cw = w; cw - grw >= 0; --cw) {\n int prevVal = i > 0 ? dp[i-1][cw - grw] : 0;\n dp[i][cw] = max(dp[i][cw], prevVal + grb);\n }\n \n foreach (e; idsForGrp[i]) {\n for (int cw = w; cw - ws[e] >= 0; --cw) {\n int prevVal = i > 0 ? dp[i-1][cw - ws[e]] : 0;\n dp[i][cw] = max(dp[i][cw], prevVal + bs[e]);\n }\n }\n }\n \n debug { dp.writeln; }\n \n int ans = dp[$-1].maxElement;\n \n ans.writeln;\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n, m, tw;\n\twhile (readf (\" %s %s %s\", &n, &m, &tw) > 0)\n\t{\n\t\treadln;\n\t\tauto w = readln.split.map !(to !(int)).array;\n\t\tauto b = readln.split.map !(to !(int)).array;\n\t\tauto a = new int [] [n];\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf (\" %s %s\", &u, &v);\n\t\t\tu -= 1;\n\t\t\tv -= 1;\n\t\t\ta[u] ~= v;\n\t\t\ta[v] ~= u;\n\t\t}\n\n\t\tauto d = new bool [n];\n\t\tint [] component;\n\n\t\tvoid recur (int v)\n\t\t{\n\t\t\tif (d[v])\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\t\t\td[v] = true;\n\t\t\tcomponent ~= v;\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\trecur (u);\n\t\t\t}\n\t\t}\n\n\t\tint [] [] c;\n\n\t\tforeach (u; 0..n)\n\t\t{\n\t\t\tif (!d[u])\n\t\t\t{\n\t\t\t\tcomponent.length = 0;\n\t\t\t\tcomponent.assumeSafeAppend ();\n\t\t\t\trecur (u);\n\t\t\t\tc ~= component.dup;\n\t\t\t}\n\t\t}\n\t\tdebug {writeln (c);}\n\n\t\tint [] [2] f;\n\t\tforeach (ref g; f)\n\t\t{\n\t\t\tg = new int [tw + 1];\n\t\t}\n\t\tint e = 0;\n\t\tf[e][] = 0;\n\t\tforeach (cur; c)\n\t\t{\n\t\t\te ^= 1;\n\t\t\tf[e][] = f[!e][];\n\t\t\tint sw = 0;\n\t\t\tint sb = 0;\n\n\t\t\tforeach (k; cur)\n\t\t\t{\n\t\t\t\tforeach (i; w[k]..tw + 1)\n\t\t\t\t{\n\t\t\t\t\tf[e][i] = max (f[e][i],\n\t\t\t\t\t f[!e][i - w[k]] + b[k]);\n\t\t\t\t}\n\t\t\t\tsw += w[k];\n\t\t\t\tsb += b[k];\n\t\t\t}\n\n\t\t\tforeach (i; sw..tw + 1)\n\t\t\t{\n\t\t\t\tf[e][i] = max (f[e][i],\n\t\t\t\t f[!e][i - sw] + sb);\n\t\t\t}\n\t\t}\n\n\t\twriteln (f[e][].reduce !(max));\n\t}\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Dsu(size_t n) {\n int[n] p;\n\n int get(int x) {\n return x == p[x]? x : (p[x] = get(p[x]));\n }\n\n void merge(int x, int y) {\n p[get(x)] = get(y);\n }\n}\n\nalias Girl = Tuple!(int, `weight`, int, `beauty`);\n\nstruct Group {\n int id;\n Girl[ ] girls;\n Girl sum;\n Group* next;\n}\n\nint n;\nGirl[1000] _girls;\nGirl[ ] girls;\nDsu!1000 dsu;\nGroup[1000] groups;\nint[1001][1000] dp;\n\nint dyn(int gid, int wcap) {\n assert(gid < n);\n assert(wcap >= 0);\n if (~dp[gid][wcap])\n return dp[gid][wcap];\n const grp = &groups[gid];\n assert(!grp.girls.empty);\n int result = 0;\n if (grp.next is null) {\n if (wcap >= grp.sum.weight)\n result = grp.sum.beauty;\n else\n foreach (g; grp.girls)\n if (wcap >= g.weight)\n result = max(result, g.beauty);\n } else {\n assert(grp.next == &groups[grp.next.id]);\n result = dyn(grp.next.id, wcap);\n if (wcap >= grp.sum.weight)\n result = max(result, dyn(grp.next.id, wcap - grp.sum.weight) + grp.sum.beauty);\n foreach (g; grp.girls)\n if (wcap >= g.weight)\n result = max(result, dyn(grp.next.id, wcap - g.weight) + g.beauty);\n }\n return (dp[gid][wcap] = result);\n}\n\nvoid main() {\n int m, wl;\n while (read(&n, &m, &wl)) {\n girls = _girls[0 .. n];\n foreach (int i, ref grp; groups) {\n grp.id = i;\n grp.girls = null;\n grp.sum = Girl(0, 0);\n }\n iota(0, n).copy(dsu.p[ ]);\n foreach (ref g; girls)\n read(&g.weight);\n foreach (ref g; girls)\n read(&g.beauty);\n while (m--) {\n int a, b;\n read(&a, &b);\n a--;\n b--;\n dsu.merge(a, b);\n }\n foreach (int i, g; girls) {\n auto grp = &groups[dsu.get(i)];\n grp.girls ~= g;\n grp.sum.weight += g.weight;\n grp.sum.beauty += g.beauty;\n }\n Group* last = null, first = null;\n foreach (ref grp; groups[0 .. n].filter!`!a.girls.empty`) {\n if (first is null)\n first = &grp;\n else\n last.next = &grp;\n last = &grp;\n }\n assert(last !is null);\n last.next = null;\n memset(dp.ptr, 0xFF, dp.sizeof);\n writeln(dyn(first.id, wl));\n }\n}\n"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, m, w;\n readf(\"%s %s %s\", &n, &m, &w);\n readln;\n \n auto ws = readln.chomp.split.map!(to!int).array;\n auto bs = readln.chomp.split.map!(to!int).array;\n \n auto grpid = n.iota.array;\n \n foreach (_; 0 .. m) {\n int a, b;\n readf(\"%s %s\", &a, &b);\n readln;\n --a, --b;\n \n foreach (ref e; grpid) if (e == grpid[b]) e = grpid[a];\n }\n \n auto vis = (false).repeat(n).array;\n auto idsForGrp = new int[][] (grpid.dup.sort.uniq.array.length);\n \n debug { grpid.writeln; }\n \n Tuple!(int, int)[] grpsVals;\n foreach (i, e; grpid) {\n if (vis[i]) continue;\n \n int wsm = 0, bsm = 0;\n foreach (j, f; grpid) {\n if (e != f) continue;\n \n idsForGrp[grpsVals.length] ~= j.to!int;\n vis[j] = true;\n wsm += ws[j];\n bsm += bs[j];\n }\n \n grpsVals ~= tuple(wsm, bsm);\n }\n \n debug { grpsVals.writeln; }\n debug { idsForGrp.writeln; }\n \n auto dp = new int[][] (grpsVals.length, w+1);\n foreach (i, t; grpsVals) {\n int grw = t[0], grb = t[1];\n \n if (i > 0) dp[i][] = dp[i-1][];\n \n for (int cw = w; cw - grw >= 0; --cw) {\n int prevVal = i > 0 ? dp[i-1][cw - grw] : 0;\n dp[i][cw] = max(dp[i][cw], prevVal + grb);\n }\n \n foreach (e; idsForGrp[i]) {\n for (int cw = w; cw - ws[e] >= 0; --cw) {\n int prevVal = i > 0 ? dp[i-1][cw - ws[e]] : 0;\n dp[i][cw] = max(dp[i][cw], prevVal + bs[e]);\n }\n }\n }\n \n debug { dp.writeln; }\n \n int ans = dp[$-1].maxElement;\n \n ans.writeln;\n}"}, {"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Dsu(size_t n) {\n int[n] p;\n\n int get(int x) {\n return x == p[x]? x : (p[x] = get(p[x]));\n }\n\n void merge(int x, int y) {\n p[get(x)] = get(y);\n }\n}\n\nalias Girl = Tuple!(int, `weight`, int, `beauty`);\n\nstruct Group {\n int id;\n Girl[ ] girls;\n Girl sum;\n Group* next;\n}\n\nint n, m, wl;\nGirl[1000] _girls;\nGirl[ ] girls;\nDsu!1000 dsu;\nGroup[1000] groups;\nint[1001][1000] dp;\n\nint dyn(int gid, int curWeight) {\n assert(gid < n);\n if (~dp[gid][curWeight])\n return dp[gid][curWeight];\n const grp = &groups[gid];\n assert (!grp.girls.empty);\n if (grp.next is null) {\n if (curWeight + grp.sum.weight <= wl)\n return (dp[gid][curWeight] = grp.sum.beauty);\n int b = 0;\n foreach (g; grp.girls)\n if (curWeight + g.weight <= wl)\n b = max(b, g.beauty);\n return (dp[gid][curWeight] = b);\n } else {\n int result = 0;\n if (curWeight + grp.sum.weight <= wl)\n result = dyn(grp.next.id, curWeight + grp.sum.weight) + grp.sum.beauty;\n foreach (g; grp.girls)\n if (curWeight + g.weight <= wl)\n result = max(result, dyn(grp.next.id, curWeight + g.weight) + g.beauty);\n return (dp[gid][curWeight] = result);\n }\n}\n\nvoid main() {\n while (read(&n, &m, &wl)) {\n girls = _girls[0 .. n];\n foreach (int i, ref grp; groups) {\n grp.id = i;\n grp.girls = null;\n grp.sum = Girl(0, 0);\n }\n iota(0, n).copy(dsu.p[ ]);\n foreach (ref g; girls)\n read(&g.weight);\n foreach (ref g; girls)\n read(&g.beauty);\n while (m--) {\n int a, b;\n read(&a, &b);\n a--;\n b--;\n dsu.merge(a, b);\n }\n foreach (int i, g; girls) {\n auto grp = &groups[dsu.get(i)];\n grp.girls ~= g;\n grp.sum.weight += g.weight;\n grp.sum.beauty += g.beauty;\n }\n Group* last = null, first = null;\n foreach (ref grp; groups[0 .. n].filter!`!a.girls.empty`) {\n if (last is null)\n first = &grp;\n else\n last.next = &grp;\n last = &grp;\n }\n assert(last !is null);\n last.next = null;\n memset(dp.ptr, 0xFF, dp.sizeof);\n writeln(dyn(first.id, 0));\n }\n}\n"}], "src_uid": "7c96bc1aa4dcabf7560d915823ba22f1"} {"nl": {"description": "Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.The device is powered by two wires \"plus\" and \"minus\". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the \"plus\" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.To understand the problem better please read the notes to the test samples.", "input_spec": "The single line of the input contains a sequence of characters \"+\" and \"-\" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character \"+\", if on the i-th step from the wall the \"plus\" wire runs above the \"minus\" wire, and the character \"-\" otherwise.", "output_spec": "Print either \"Yes\" (without the quotes) if the wires can be untangled or \"No\" (without the quotes) if the wires cannot be untangled.", "sample_inputs": ["-++-", "+-", "++", "-"], "sample_outputs": ["Yes", "No", "Yes", "No"], "notes": "NoteThe first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the \"plus\" wire lower, thus eliminating the two crosses in the middle, and then draw it under the \"minus\" wire, eliminating also the remaining two crosses.In the second testcase the \"plus\" wire makes one full revolution around the \"minus\" wire. Thus the wires cannot be untangled: In the third testcase the \"plus\" wire simply runs above the \"minus\" wire twice in sequence. The wires can be untangled by lifting \"plus\" and moving it higher: In the fourth testcase the \"minus\" wire runs above the \"plus\" wire once. The wires cannot be untangled without moving the device itself: "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tstring s;\n\twhile ((s = readln ()).length > 0)\n\t{\n\t\ts = '<' ~ s.strip () ~ '>';\n\t\tint n = s.length;\n\t\tauto id = iota (n).array;\n\t\tauto next = id.dup;\n\t\tnext[] += 1;\n\t\tauto prev = id.dup;\n\t\tprev[] -= 1;\n\t\tint i = 1;\n\t\twhile (next[i] < n)\n\t\t{\n\t\t\tdebug {writeln (i, ' ', next[i]);}\n\t\t\tif (s[i] == s[next[i]])\n\t\t\t{\n\t\t\t\tdebug {writeln (s[i], ' ', s[next[i]]);}\n\t\t\t\tprev[next[i]] = prev[i];\n\t\t\t\tnext[prev[i]] = next[i];\n\t\t\t\ti = next[i];\n\t\t\t\tprev[next[i]] = prev[i];\n\t\t\t\tnext[prev[i]] = next[i];\n\t\t\t\ti = prev[i];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ti = next[i];\n\t\t\t}\n\t\t}\n\t\twriteln ((i == n - 1 && prev[i] == 0) ? \"Yes\" : \"No\");\n\t}\n}\n"}], "negative_code": [], "src_uid": "89b4a7b4a6160ce784c588409b6ce935"} {"nl": {"description": "There is a sheet of paper that can be represented with a grid of size $$$n \\times m$$$: $$$n$$$ rows and $$$m$$$ columns of cells. All cells are colored in white initially.$$$q$$$ operations have been applied to the sheet. The $$$i$$$-th of them can be described as follows: $$$x_i$$$ $$$y_i$$$ — choose one of $$$k$$$ non-white colors and color the entire row $$$x_i$$$ and the entire column $$$y_i$$$ in it. The new color is applied to each cell, regardless of whether the cell was colored before the operation. The sheet after applying all $$$q$$$ operations is called a coloring. Two colorings are different if there exists at least one cell that is colored in different colors.How many different colorings are there? Print the number modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The first line of the testcase contains four integers $$$n, m, k$$$ and $$$q$$$ ($$$1 \\le n, m, k, q \\le 2 \\cdot 10^5$$$) — the size of the sheet, the number of non-white colors and the number of operations. The $$$i$$$-th of the following $$$q$$$ lines contains a description of the $$$i$$$-th operation — two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i \\le n$$$; $$$1 \\le y_i \\le m$$$) — the row and the column the operation is applied to. The sum of $$$q$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase, print a single integer — the number of different colorings modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["2\n\n1 1 3 2\n\n1 1\n\n1 1\n\n2 2 2 3\n\n2 1\n\n1 1\n\n2 2"], "sample_outputs": ["3\n4"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nstruct ModInt(long mod) {\r\n invariant() {\r\n assert(0 <= val && val < mod);\r\n }\r\n long val;\r\n this(long v) { this.val = (mod * mod + v) % mod; }\r\n ModInt opBinary(string op : \"+\")(int y) const { return ModInt(this.val + y); }\r\n ModInt opBinary(string op : \"-\")(int y) const { return ModInt(this.val - y); }\r\n ModInt opBinary(string op : \"*\")(int y) const { return ModInt(this.val * y); }\r\n ModInt opBinary(string op : \"/\")(int y) const { return ModInt(this.val * inv(y)); }\r\n ModInt opBinary(string op : \"+\")(long y) const { return ModInt(this.val + y); }\r\n ModInt opBinary(string op : \"-\")(long y) const { return ModInt(this.val - y); }\r\n ModInt opBinary(string op : \"*\")(long y) const { return ModInt(this.val * y); }\r\n ModInt opBinary(string op : \"/\")(long y) const { return ModInt(this.val * inv(y)); }\r\n ModInt opBinary(string op : \"+\")(ModInt y) const { return ModInt(this.val + y.val); }\r\n ModInt opBinary(string op : \"-\")(ModInt y) const { return ModInt(this.val - y.val); }\r\n ModInt opBinary(string op : \"*\")(ModInt y) const { return ModInt(this.val * y.val); }\r\n ModInt opBinary(string op : \"/\")(ModInt y) const { return ModInt(this.val * inv(y.val)); }\r\n ModInt opBinaryRight(string op : \"+\")(int y) const { return ModInt(this.val + y); }\r\n ModInt opBinaryRight(string op : \"-\")(int y) const { return ModInt(y - this.val); }\r\n ModInt opBinaryRight(string op : \"*\")(int y) const { return ModInt(this.val * y); }\r\n ModInt opBinaryRight(string op : \"/\")(int y) const { return ModInt(inv(this.val) * y); }\r\n ModInt opBinaryRight(string op : \"+\")(long y) const { return ModInt(this.val + y); }\r\n ModInt opBinaryRight(string op : \"-\")(long y) const { return ModInt(y - this.val); }\r\n ModInt opBinaryRight(string op : \"*\")(long y) const { return ModInt(this.val * y); }\r\n ModInt opBinaryRight(string op : \"/\")(long y) const { return ModInt(inv(this.val) * y); }\r\n void opAssign(int y) { this.val = y; }\r\n void opAssign(long y) { this.val = y; }\r\n void opOpAssign(string op : \"+\")(int y) { this.val = (this.val + mod * mod + y) % mod; }\r\n void opOpAssign(string op : \"-\")(int y) { this.val = (this.val + mod * mod - y) % mod; }\r\n void opOpAssign(string op : \"*\")(int y) { this.val = (this.val * y) % mod; }\r\n void opOpAssign(string op : \"/\")(int y) { this.val = (this.val * inv(y)) % mod; }\r\n void opOpAssign(string op : \"+\")(long y) { this.val = (this.val + mod * mod + y) % mod; }\r\n void opOpAssign(string op : \"-\")(long y) { this.val = (this.val + mod * mod - y) % mod; }\r\n void opOpAssign(string op : \"*\")(long y) { this.val = (this.val * y) % mod; }\r\n void opOpAssign(string op : \"/\")(long y) { this.val = (this.val * inv(y)) % mod; }\r\n void opOpAssign(string op : \"+\")(ModInt y) { this.val = (this.val + mod * mod + y.val) % mod; }\r\n void opOpAssign(string op : \"-\")(ModInt y) { this.val = (this.val + mod * mod - y.val) % mod; }\r\n void opOpAssign(string op : \"*\")(ModInt y) { this.val = (this.val * y.val) % mod; }\r\n void opOpAssign(string op : \"/\")(ModInt y) { this.val = (this.val * inv(y.val)) % mod; }\r\n ModInt opUnary(string op : \"-\")() const { return ModInt(mod - this.val); }\r\n string toString() const { return val.to!string; }\r\n static long extgcd(long a, long b, ref long x, ref long y) {\r\n if (b == 0) {\r\n x = 1;\r\n y = 0;\r\n return a;\r\n }\r\n long d = extgcd(b, a % b, y, x);\r\n y -= a / b * x;\r\n return d;\r\n }\r\n static long inv(long a) {\r\n long x, y;\r\n extgcd(a, mod, x, y);\r\n return (x % mod + mod) % mod;\r\n }\r\n}\r\n\r\nalias MInt = ModInt!998244353L;\r\n\r\nvoid solve() {\r\n int N, M, K, Q; readf(\"%d %d %d %d\\n\", &N, &M,&K, &Q);\r\n auto Y = new int[Q], X = new int[Q];\r\n foreach (i; 0 .. Q) {\r\n readf(\"%d %d\\n\", &Y[i], &X[i]);\r\n }\r\n bool[int] usedY, usedX;\r\n int nY, nX;\r\n MInt ans = 1;\r\n for (int i = Q - 1; i >= 0; i--) {\r\n int y = Y[i], x = X[i];\r\n if ( (y in usedY && x in usedX) || nY == N || nX == M ) continue;\r\n //if ( (y in usedY || nY == N) && (x in usedX || nX == M) ) continue;\r\n ans *= K;\r\n if (y !in usedY) {\r\n usedY[y] = true;\r\n nY++;\r\n }\r\n if (x !in usedX) {\r\n usedX[x] = true;\r\n nX++;\r\n }\r\n }\r\n writeln(ans);\r\n\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nstruct ModInt(long mod) {\r\n invariant() {\r\n assert(0 <= val && val < mod);\r\n }\r\n long val;\r\n this(long v) { this.val = (mod * mod + v) % mod; }\r\n ModInt opBinary(string op : \"+\")(int y) const { return ModInt(this.val + y); }\r\n ModInt opBinary(string op : \"-\")(int y) const { return ModInt(this.val - y); }\r\n ModInt opBinary(string op : \"*\")(int y) const { return ModInt(this.val * y); }\r\n ModInt opBinary(string op : \"/\")(int y) const { return ModInt(this.val * inv(y)); }\r\n ModInt opBinary(string op : \"+\")(long y) const { return ModInt(this.val + y); }\r\n ModInt opBinary(string op : \"-\")(long y) const { return ModInt(this.val - y); }\r\n ModInt opBinary(string op : \"*\")(long y) const { return ModInt(this.val * y); }\r\n ModInt opBinary(string op : \"/\")(long y) const { return ModInt(this.val * inv(y)); }\r\n ModInt opBinary(string op : \"+\")(ModInt y) const { return ModInt(this.val + y.val); }\r\n ModInt opBinary(string op : \"-\")(ModInt y) const { return ModInt(this.val - y.val); }\r\n ModInt opBinary(string op : \"*\")(ModInt y) const { return ModInt(this.val * y.val); }\r\n ModInt opBinary(string op : \"/\")(ModInt y) const { return ModInt(this.val * inv(y.val)); }\r\n ModInt opBinaryRight(string op : \"+\")(int y) const { return ModInt(this.val + y); }\r\n ModInt opBinaryRight(string op : \"-\")(int y) const { return ModInt(y - this.val); }\r\n ModInt opBinaryRight(string op : \"*\")(int y) const { return ModInt(this.val * y); }\r\n ModInt opBinaryRight(string op : \"/\")(int y) const { return ModInt(inv(this.val) * y); }\r\n ModInt opBinaryRight(string op : \"+\")(long y) const { return ModInt(this.val + y); }\r\n ModInt opBinaryRight(string op : \"-\")(long y) const { return ModInt(y - this.val); }\r\n ModInt opBinaryRight(string op : \"*\")(long y) const { return ModInt(this.val * y); }\r\n ModInt opBinaryRight(string op : \"/\")(long y) const { return ModInt(inv(this.val) * y); }\r\n void opAssign(int y) { this.val = y; }\r\n void opAssign(long y) { this.val = y; }\r\n void opOpAssign(string op : \"+\")(int y) { this.val = (this.val + mod * mod + y) % mod; }\r\n void opOpAssign(string op : \"-\")(int y) { this.val = (this.val + mod * mod - y) % mod; }\r\n void opOpAssign(string op : \"*\")(int y) { this.val = (this.val * y) % mod; }\r\n void opOpAssign(string op : \"/\")(int y) { this.val = (this.val * inv(y)) % mod; }\r\n void opOpAssign(string op : \"+\")(long y) { this.val = (this.val + mod * mod + y) % mod; }\r\n void opOpAssign(string op : \"-\")(long y) { this.val = (this.val + mod * mod - y) % mod; }\r\n void opOpAssign(string op : \"*\")(long y) { this.val = (this.val * y) % mod; }\r\n void opOpAssign(string op : \"/\")(long y) { this.val = (this.val * inv(y)) % mod; }\r\n void opOpAssign(string op : \"+\")(ModInt y) { this.val = (this.val + mod * mod + y.val) % mod; }\r\n void opOpAssign(string op : \"-\")(ModInt y) { this.val = (this.val + mod * mod - y.val) % mod; }\r\n void opOpAssign(string op : \"*\")(ModInt y) { this.val = (this.val * y.val) % mod; }\r\n void opOpAssign(string op : \"/\")(ModInt y) { this.val = (this.val * inv(y.val)) % mod; }\r\n ModInt opUnary(string op : \"-\")() const { return ModInt(mod - this.val); }\r\n string toString() const { return val.to!string; }\r\n static long extgcd(long a, long b, ref long x, ref long y) {\r\n if (b == 0) {\r\n x = 1;\r\n y = 0;\r\n return a;\r\n }\r\n long d = extgcd(b, a % b, y, x);\r\n y -= a / b * x;\r\n return d;\r\n }\r\n static long inv(long a) {\r\n long x, y;\r\n extgcd(a, mod, x, y);\r\n return (x % mod + mod) % mod;\r\n }\r\n}\r\n\r\nalias MInt = ModInt!998244353L;\r\n\r\nvoid solve() {\r\n int N, M, K, Q; readf(\"%d %d %d %d\\n\", &N, &M,&K, &Q);\r\n auto Y = new int[Q], X = new int[Q];\r\n foreach (i; 0 .. Q) {\r\n readf(\"%d %d\\n\", &Y[i], &X[i]);\r\n }\r\n bool[int] usedY, usedX;\r\n int nY, nX;\r\n MInt ans = 1;\r\n for (int i = Q - 1; i >= 0; i--) {\r\n int y = Y[i], x = X[i];\r\n if ( (y in usedY || nY == N) && (x in usedX || nX == M) ) continue;\r\n ans *= K;\r\n if (y !in usedY) {\r\n usedY[y] = true;\r\n nY++;\r\n }\r\n if (x !in usedX) {\r\n usedX[x] = true;\r\n nX++;\r\n }\r\n }\r\n writeln(ans);\r\n\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "src_uid": "623a373709e4c4223fbbb9a320f307b1"} {"nl": {"description": "For a permutation P[1... N] of integers from 1 to N, function f is defined as follows: Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.For given N, A, B, find a permutation P of integers from 1 to N such that for 1 ≤ i ≤ N, g(i) equals either A or B.", "input_spec": "The only line contains three integers N, A, B (1 ≤ N ≤ 106, 1 ≤ A, B ≤ N).", "output_spec": "If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.", "sample_inputs": ["9 2 5", "3 2 1"], "sample_outputs": ["6 5 8 3 4 1 9 2 7", "1 2 3"], "notes": "NoteIn the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5 In the second example, g(1) = g(2) = g(3) = 1"}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nstring solve (int n, int a, int b)\n{\n\tint k = n;\n\tint [] p;\n\n\tvoid addCycle (int len)\n\t{\n\t\tint cur = p.length.to !(int) + 1;\n\t\tforeach (i; 1..len)\n\t\t{\n\t\t\tp ~= p.length.to !(int) + 2;\n\t\t}\n\t\tp ~= cur;\n\t}\n\n\twhile (k % b != 0)\n\t{\n\t\tif (k < a)\n\t\t{\n\t\t\treturn \"-1\";\n\t\t}\n\t\tk -= a;\n\t\taddCycle (a);\n\t}\n\twhile (k > 0)\n\t{\n\t\taddCycle (b);\n\t\tk -= b;\n\t}\n\tassert (k == 0);\n\treturn format (\"%(%s %)\", p);\n}\n\nvoid main ()\n{\n\tint n, a, b;\n\twhile (readf (\" %s %s %s\", &n, &a, &b) > 0)\n\t{\n\t\twriteln (solve (n, a, b));\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const A = readInt();\n const B = readInt();\n \n for (int x = 0; A * x <= N; ++x) {\n if ((N - A * x) % B == 0) {\n const y = (N - A * x) / B;\n int[] ans;\n int pos;\n foreach (i; 0 .. x) {\n foreach (j; 0 .. A) {\n ans ~= pos + (j + 1) % A;\n }\n pos += A;\n }\n foreach (i; 0 .. y) {\n foreach (j; 0 .. B) {\n ans ~= pos + (j + 1) % B;\n }\n pos += B;\n }\n foreach (i; 0 .. N) {\n if (i > 0) write(\" \");\n write(ans[i] + 1);\n }\n writeln();\n goto found;\n }\n }\n writeln(-1);\n found:\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "138f7db4a858fb1efe817ee6491f83d9"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ ($$$n \\ge 3$$$) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array $$$[4, 11, 4, 4]$$$ all numbers except one are equal to $$$4$$$).Print the index of the element that does not equal others. The numbers in the array are numbered from one.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 100$$$). It is guaranteed that all the numbers except one in the $$$a$$$ array are the same.", "output_spec": "For each test case, output a single integer — the index of the element that is not equal to others.", "sample_inputs": ["4\n4\n11 13 11 11\n5\n1 4 4 4 4\n10\n3 3 3 3 10 3 3 3 3 3\n3\n20 20 10"], "sample_outputs": ["2\n1\n5\n3"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm, std.conv, std.range;\r\nvoid main()\r\n{\r\n int q;\r\n readf!\"%d\\n\"(q);\r\n foreach (_; 0 .. q)\r\n {\r\n int n;\r\n readf!\"%d\\n\"(n);\r\n int[] a = readln.split.map!(to!int).array, b = a.dup;\r\n b.sort();\r\n foreach (i; 0 .. n)\r\n if (a[i] != b[1])\r\n writeln(i + 1);\r\n }\r\n}"}, {"source_code": "import std.stdio, std.string, std.algorithm, std.conv, std.range;\r\nvoid main()\r\n{\r\n int q;\r\n readf!\"%d\\n\"(q);\r\n foreach (_; 0 .. q)\r\n {\r\n int n;\r\n readf!\"%d\\n\"(n);\r\n int[] a = readln.split.map!(to!int).array;\r\n if (a[0] == a[1])\r\n {\r\n foreach (j; 0 .. n)\r\n if (a[j] != a[0])\r\n writeln(j + 1);\r\n }\r\n else\r\n {\r\n if (a[0] == a[2])\r\n writeln(2);\r\n else\r\n writeln(1);\r\n }\r\n }\r\n}"}, {"source_code": "import std;\r\n\r\nvoid main() {\r\n int t;\r\n readf(\"%d\\n\", t);\r\n\r\n foreach (_; 0 .. t) {\r\n int n;\r\n readf(\"%d\\n\", n);\r\n\r\n auto a = readln.chomp.split.to!(int[]);\r\n\r\n if (a[0] == a[1] && a[1] == a[2]) {\r\n foreach (i; 3 .. n) {\r\n if (a[i] != a[0]) {\r\n writeln(i+1);\r\n }\r\n }\r\n }\r\n else {\r\n if (a[0] == a[1]) writeln(3);\r\n if (a[0] == a[2]) writeln(2);\r\n if (a[1] == a[2]) writeln(1);\r\n }\r\n }\r\n}"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1512/problem/A\n// simulation, implementation\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n\nwhile(t--) {\n int n = readln.chomp.to!int;\n int[] a = readln.split.map!(to!int).array;\n\n int[] counter = new int[101];\n\n foreach(item; a) {\n counter[item] += 1;\n }\n\n for(int i = 0; i < n; ++i) {\n if(counter[a[i]] == 1) {\n (i + 1).writeln;\n break;\n }\n }\n}\n}\n\n"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.algorithm, std.conv, std.range;\r\nvoid main()\r\n{\r\n int q;\r\n readf!\"%d\\n\"(q);\r\n foreach (_; 0 .. q)\r\n {\r\n int n;\r\n readf!\"%d\\n\"(n);\r\n int[] a = readln.split.map!(to!int).array, b = a.dup;\r\n b.sort();\r\n foreach (i; 0 .. n)\r\n if (b[i] != a[1])\r\n writeln(i + 1);\r\n }\r\n}"}], "src_uid": "224a0b09547ec1441474efbd8e06353b"} {"nl": {"description": "We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).", "input_spec": "Input contains several test cases. The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases. The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.", "output_spec": "Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).", "sample_inputs": ["3 2\n1 3\n2 3\n4 4"], "sample_outputs": ["6\n5\n5"], "notes": "Note For K = 2 and length 1 Marmot can eat (R). For K = 2 and length 2 Marmot can eat (RR) and (WW). For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR). For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR). "}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n immutable int MAX = 10^^5+1;\n immutable int MOD = 10^^9+7;\n \n int T, K;\n scanf(\"%d %d\", &T, &K);\n\n auto dp = new int[](MAX);\n foreach (i; 1..MAX) {\n dp[i] = (dp[i-1] + 1) % MOD;\n if (i - K >= 0)\n dp[i] = ((dp[i] + dp[i-K]) % MOD + 1) % MOD;\n }\n\n int a, b;\n foreach (_; 0..T) {\n scanf(\"%d %d\", &a, &b);\n writeln(((dp[b] - dp[a-1]) % MOD + MOD) % MOD);\n }\n}\n"}, {"source_code": "module main;\n\nimport std.stdio, std.string;\n\nstatic auto mod = 1000000007;\n\nvoid init(int[] dp, int[] s, int k)\n{\n foreach (i; 0 .. k)\n {\n dp[i] = 1;\n }\n foreach (i; k .. dp.length)\n {\n dp[i] = (dp[i - 1] + dp[i - k]) % mod;\n }\n s[0] = 0;\n foreach (i; 1 .. s.length)\n {\n s[i] = (s[i - 1] + dp[i]) % mod;\n }\n}\n\nint main(string[] args)\n{\n int k, t;\n while (scanf(\"%d%d\", &t, &k) == 2)\n {\n auto dp = new int[100001];\n auto s = new int[100001];\n init(dp, s, k);\n foreach (i; 0 .. t)\n {\n int a, b;\n scanf(\"%d%d\", &a, &b);\n auto ans = s[b] - s[a - 1];\n if (ans < 0)\n {\n ans += mod;\n }\n writefln(\"%d\", ans);\n }\n }\n return 0;\n}"}, {"source_code": "import std.stdio;\nimport std.exception;\n\nvoid main() {\n\tsize_t t, k;\n\tenforce(readf(\" %s %s\", &t, &k));\n\n\tenum MOD = cast(int)(1e9 + 7);\n\tenum N = 100500;\n\tint[N] z;\n\tz[0] = 1;\n\tforeach (cur; 1..N) {\n\t\tz[cur] = z[cur - 1];\n\t\tif (cur >= k)\n\t\t\tz[cur] += z[cur - k];\n\t\tz[cur] %= MOD;\n\t}\n\tforeach (cur; 1..N) {\n\t\tz[cur] += z[cur - 1];\n\t\tz[cur] %= MOD;\n\t}\n\n\tforeach (test; 0..t) {\n\t\tsize_t a, b;\n\t\tenforce(readf(\" %s %s\", &a, &b));\n\t\tauto ans = (z[b] - z[a - 1] + MOD) % MOD;\n\t\twriteln(ans);\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n immutable int MAX = 10^^5+1;\n immutable int MOD = 10^^9+7;\n \n int T, K;\n scanf(\"%d %d\", &T, &K);\n\n auto dp = new int[](MAX);\n foreach (i; 1..MAX) {\n dp[i] = (dp[i-1] + 1) % MOD;\n if (i - K >= 0)\n dp[i] = ((dp[i] + dp[i-K]) % MOD + 1);\n }\n\n int a, b;\n foreach (_; 0..T) {\n scanf(\"%d %d\", &a, &b);\n writeln(dp[b] - dp[a-1]);\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n immutable int MAX = 10^^5+1;\n immutable int MOD = 10^^9+7;\n \n int T, K;\n scanf(\"%d %d\", &T, &K);\n\n auto dp = new int[](MAX);\n foreach (i; 1..MAX) {\n dp[i] = (dp[i-1] + 1) % MOD;\n if (i - K >= 0)\n dp[i] = ((dp[i] + dp[i-K]) % MOD + 1);\n }\n\n int a, b;\n foreach (_; 0..T) {\n scanf(\"%d %d\", &a, &b);\n writeln((dp[b] - dp[a-1]) % MOD + MOD % MOD);\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n immutable int MAX = 10^^5+1;\n immutable int MOD = 10^^9+7;\n \n int T, K;\n scanf(\"%d %d\", &T, &K);\n\n auto dp = new int[](MAX);\n foreach (i; 1..MAX) {\n dp[i] = (dp[i-1] + 1) % MOD;\n if (i - K >= 0)\n dp[i] = ((dp[i] + dp[i-K]) % MOD + 1) % MOD;\n }\n\n int a, b;\n foreach (_; 0..T) {\n scanf(\"%d %d\", &a, &b);\n writeln((dp[b] - dp[a-1]) % MOD + MOD % MOD);\n }\n}\n"}, {"source_code": "module main;\n\nimport std.stdio, std.string;\n\nvoid init(int[] dp, int[] s, int k)\n{\n static auto mod = 1000000007;\n foreach (i; 0 .. k)\n {\n dp[i] = 1;\n }\n foreach (i; k .. dp.length)\n {\n dp[i] = (dp[i - 1] + dp[i - k]) % mod;\n }\n s[1] = dp[1];\n foreach (i; 2 .. s.length)\n {\n s[i] = (s[i - 1] + dp[i]) % mod;\n }\n}\n\nint main(string[] args)\n{\n int k, t;\n while (scanf(\"%d%d\", &t, &k) == 2)\n {\n auto dp = new int[100001];\n auto s = new int[100001];\n init(dp, s, k);\n foreach (i; 0 .. t)\n {\n int a, b;\n scanf(\"%d%d\", &a, &b);\n writefln(\"%d\", s[b] - s[a - 1]);\n }\n }\n return 0;\n}"}, {"source_code": "import std.stdio;\nimport std.exception;\n\nvoid main() {\n\tsize_t t, k;\n\tenforce(readf(\" %s %s\", &t, &k));\n\n\tenum MOD = cast(int)(1e9 + 7);\n\tenum N = 100500;\n\tint[N] z;\n\tz[0] = 1;\n\tforeach (cur; 1..N) {\n\t\tz[cur] = z[cur - 1];\n\t\tif (cur >= k)\n\t\t\tz[cur] += z[cur - k];\n\t\tz[cur] %= MOD;\n\t}\n\tforeach (cur; 1..N) {\n\t\tz[cur] += z[cur - 1];\n\t\tz[cur] %= MOD;\n\t}\n\n\tforeach (test; 0..t) {\n\t\tsize_t a, b;\n\t\tenforce(readf(\" %s %s\", &a, &b));\n\t\tauto ans = z[b] - z[a - 1];\n\t\twriteln(ans);\n\t}\n}\n"}], "src_uid": "16c016c0735be1815c7b94c5c50516f1"} {"nl": {"description": "You talked to Polycarp and asked him a question. You know that when he wants to answer \"yes\", he repeats Yes many times in a row.Because of the noise, you only heard part of the answer — some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se.Determine if it is true that the given string $$$s$$$ is a substring of YesYesYes... (Yes repeated many times in a row).", "input_spec": "The first line of input data contains the singular $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the test. Each test case is described by a single string of Latin letters $$$s$$$ ($$$1 \\le |s| \\le 50$$$) — the part of Polycarp's answer that you heard, where $$$|s|$$$ — is the length of the string $$$s$$$.", "output_spec": "Output $$$t$$$ lines, each of which is the answer to the corresponding test case. As an answer, output \"YES\" if the specified string $$$s$$$ is a substring of the string YesYesYes...Yes (the number of words Yes is arbitrary), and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["12\n\nYES\n\nesYes\n\ncodeforces\n\nes\n\nse\n\nYesY\n\nesYesYesYesYesYesYe\n\nseY\n\nYess\n\nsY\n\no\n\nYes"], "sample_outputs": ["NO\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nNO\nYES\nNO\nYES"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto s = readln.strip;\r\n\t\tbool ok = false;\r\n\t\tforeach (i; 0..3)\r\n\t\t{\r\n\t\t\tauto t = \"Yes\".cycle.drop (i).take (s.length);\r\n\t\t\tif (equal (s, t))\r\n\t\t\t{\r\n\t\t\t\tok = true;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (ok ? \"YES\" : \"NO\");\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\nimport std.functional;\r\n \r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n \r\nvoid solve() {\r\n auto x = readln.strip();\r\n bool norm = true;\r\nouter: foreach(i, e; x) {\r\n if (i == 0) {\r\n if ((e != 'Y') && (e != 'e') && (e !='s')) {\r\n norm = false;\r\n break outer;\r\n }\r\n }\r\n else {\r\n if ((e == 'Y' && x[i-1] == 's') || (e == 'e' && x[i-1] == 'Y') || (e == 's' && x[i-1] == 'e'))\r\n continue;\r\n else {\r\n norm = false;\r\n break outer;\r\n }\r\n }\r\n }\r\n if (norm)\r\n writeln(\"yes\");\r\n else\r\n writeln(\"no\");\r\n\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n auto s = readln.strip;\n char prevch = s[0];\n bool good = (prevch == 'Y' || prevch == 'e' || prevch == 's');\n foreach (ch ; s[1 .. $]) {\n if ((ch == 'Y' && prevch == 's') ||\n (ch == 'e' && prevch == 'Y') ||\n (ch == 's' && prevch == 'e')) {\n prevch = ch;\n } else {\n good = false;\n break;\n }\n }\n writeln(good ? \"YES\" : \"NO\");\n }\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto s = readln.strip.toLower;\r\n\t\tbool ok = false;\r\n\t\tforeach (i; 0..3)\r\n\t\t{\r\n\t\t\tauto t = \"yes\".cycle.drop (i).take (s.length);\r\n\t\t\tif (equal (s, t))\r\n\t\t\t{\r\n\t\t\t\tok = true;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (ok ? \"YES\" : \"NO\");\r\n\t}\r\n}\r\n"}], "src_uid": "3cd56870a96baf8860e9b7e89008d895"} {"nl": {"description": "You are asked to watch your nephew who likes to play with toy blocks in a strange way.He has $$$n$$$ boxes and the $$$i$$$-th box has $$$a_i$$$ blocks. His game consists of two steps: he chooses an arbitrary box $$$i$$$; he tries to move all blocks from the $$$i$$$-th box to other boxes. If he can make the same number of blocks in each of $$$n - 1$$$ other boxes then he will be happy, otherwise, will be sad. Note that your nephew can only move the blocks from the chosen box to the other boxes; he cannot move blocks from the other boxes.You don't want to make your nephew sad, so you decided to put several extra blocks into some boxes in such a way that no matter which box $$$i$$$ he chooses he won't be sad. What is the minimum number of extra blocks you need to put?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of boxes. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the number of blocks in each box. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum number of blocks you need to put. It can be proved that the answer always exists, i. e. the number of blocks is finite.", "sample_inputs": ["3\n3\n3 2 2\n4\n2 2 3 2\n3\n0 3 0"], "sample_outputs": ["1\n0\n3"], "notes": "NoteIn the first test case, you can, for example, put one extra block into the first box and make $$$a = [4, 2, 2]$$$. If your nephew chooses the box with $$$4$$$ blocks, then we will move two blocks to the second box and two blocks to the third box. If he chooses the box with $$$2$$$ blocks then he will move these two blocks to the other box with $$$2$$$ blocks.In the second test case, you don't need to put any extra blocks, since no matter which box your nephew chooses, he can always make other boxes equal.In the third test case, you should put $$$3$$$ extra blocks. For example, you can put $$$2$$$ blocks in the first box and $$$1$$$ block in the third box. You'll get array $$$a = [2, 3, 1]$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(long)).array;\n\t\tauto m = a.maxElement * (n - 1L);\n\t\tauto t = a.sum (0L);\n\t\tauto s = max (m, t);\n\t\ts += ((n - 1) - (s % (n - 1))) % (n - 1);\n\t\twriteln (s - t);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\n\t\tlong x, tot;\n\t\tforeach (e; a)\n\t\t{\n\t\t\tx.chmax(e);\n\t\t\ttot += e;\n\t\t}\n\t\tauto y = x * (n-1);\n\t\tif (tot <= y)\n\t\t\tans[ti] = y - tot;\n\t\telse\n\t\t\tans[ti] = ((n-1) - (tot % (n-1))) % (n-1);\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\n\t\tlong x, tot;\n\t\tforeach (e; a)\n\t\t{\n\t\t\tx.chmax(e);\n\t\t\ttot += e;\n\t\t}\n\t\tauto y = x * (n-1);\n\t\tif (tot <= y)\n\t\t\tans[ti] = y - tot;\n\t\telse\n\t\t\tans[ti] = (n-1) - (tot % (n-1));\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "src_uid": "e75b88ce4341062c20b6014da1152d29"} {"nl": {"description": "Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for $$$n$$$ flowers and so it looks like a pipe with $$$n$$$ holes. Arkady can only use the water that flows from the first hole.Arkady can block some of the holes, and then pour $$$A$$$ liters of water into the pipe. After that, the water will flow out from the non-blocked holes proportionally to their sizes $$$s_1, s_2, \\ldots, s_n$$$. In other words, if the sum of sizes of non-blocked holes is $$$S$$$, and the $$$i$$$-th hole is not blocked, $$$\\frac{s_i \\cdot A}{S}$$$ liters of water will flow out of it.What is the minimum number of holes Arkady should block to make at least $$$B$$$ liters of water flow out of the first hole?", "input_spec": "The first line contains three integers $$$n$$$, $$$A$$$, $$$B$$$ ($$$1 \\le n \\le 100\\,000$$$, $$$1 \\le B \\le A \\le 10^4$$$) — the number of holes, the volume of water Arkady will pour into the system, and the volume he wants to get out of the first hole. The second line contains $$$n$$$ integers $$$s_1, s_2, \\ldots, s_n$$$ ($$$1 \\le s_i \\le 10^4$$$) — the sizes of the holes.", "output_spec": "Print a single integer — the number of holes Arkady should block.", "sample_inputs": ["4 10 3\n2 2 2 2", "4 80 20\n3 2 1 4", "5 10 10\n1000 1 1 1 1"], "sample_outputs": ["1", "0", "4"], "notes": "NoteIn the first example Arkady should block at least one hole. After that, $$$\\frac{10 \\cdot 2}{6} \\approx 3.333$$$ liters of water will flow out of the first hole, and that suits Arkady.In the second example even without blocking any hole, $$$\\frac{80 \\cdot 3}{10} = 24$$$ liters will flow out of the first hole, that is not less than $$$20$$$.In the third example Arkady has to block all holes except the first to make all water flow out of the first hole."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\n\nvoid main() {\n\tauto nab = readln.chomp.split.map!(to!int);\n\tint n = nab[0];\n\tlong a = nab[1];\n\tlong b = nab[2];\n\tlong[] s = readln.chomp.split.map!(to!long).array;\n\tlong S = s.sum;\n\tlong[] t = std.algorithm.sort!\"a > b\"(s[1..$]).array;\n\tlong f = s[0] * a;\n\tint ans = n - 1;\n\tforeach (i, v; t) {\n\t\tdebug stderr.writefln(\"[%d] %d %d\", i, v, S);\n\t\tif (f >= b * S) {\n\t\t\tans = cast(int) i;\n\t\t\tbreak;\n\t\t}\n\t\tS -= v;\n\t}\n\tans.writeln;\n}\n\n\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\nvoid readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nvoid main()\n{\n int n, a, b; readV(n, a, b);\n int[] s; readA(n, s);\n\n int t = s.sum;\n\n auto s2 = s[1..$];\n s2.sort!\"a > b\";\n\n auto t2 = 0;\n foreach (i; 0..n) {\n if (a.to!long*s[0] >= b.to!long*(t-t2)) {\n writeln(i);\n return;\n }\n t2 += s2[i];\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\n\nvoid main() {\n\tauto nab = readln.chomp.split.map!(to!int);\n\tint n = nab[0];\n\tint a = nab[1];\n\tint b = nab[2];\n\tint[] s = readln.chomp.split.map!(to!int).array;\n\tint S = s.sum;\n\tint[] t = std.algorithm.sort!\"a > b\"(s[1..$]).array;\n\tint f = s[0] * a;\n\tint ans = n - 1;\n\tforeach (i, v; t) {\n\t\tif (f >= b * S) {\n\t\t\tans = cast(int) i;\n\t\t\tbreak;\n\t\t}\n\t\tS -= v;\n\t}\n\tans.writeln;\n}\n\n\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\n\nvoid main() {\n\tauto nab = readln.chomp.split.map!(to!int);\n\tint n = nab[0];\n\tint a = nab[1];\n\tint b = nab[2];\n\tint[] s = readln.chomp.split.map!(to!int).array;\n\tint S = s.sum;\n\tint f = s[0] * a;\n\tint ans = n - 1;\n\tforeach (i, v; s[1..$]) {\n\t\tif (f >= b * S) {\n\t\t\tans = cast(int) i;\n\t\t\tbreak;\n\t\t}\n\t\tS -= v;\n\t}\n\tans.writeln;\n}\n\n\n"}], "src_uid": "fd6b73a2c15f5b009fa350eea9bf0c0a"} {"nl": {"description": "Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.Help Vasya! Calculate the maximum total weight of mushrooms he can collect.", "input_spec": "The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade. The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade. The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.", "output_spec": "Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.", "sample_inputs": ["3\n1 2 3\n6 5 4", "3\n1 1000 10000\n10 100 100000"], "sample_outputs": ["70", "543210"], "notes": "NoteIn the first test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.In the second test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto A = 2.iota.map!(_ => readln.split.map!(to!long).array).array;\n auto B = new long[][](2, N+1);\n\n foreach (i; 0..2)\n foreach (j; 0..N)\n B[i][j+1] = B[i][j] + A[i][j];\n\n long ans = 0;\n long tmp = 0;\n long a = 0;\n long b = 0;\n foreach (i; 0..N) a += i * A[0][i];\n foreach (i; 0..N) a += (N+N-i-1) * A[1][i];\n\n foreach (i; 1..N) b += (i + 1) * A[1][i];\n foreach (i; 1..N) b += (N+N-i) * A[0][i];\n\n foreach (i; 0..N) {\n long t = i*2;\n if (i % 2 == 0) {\n ans = max(ans, tmp + a);\n tmp += t * A[0][i];\n tmp += (t + 1) * A[1][i];\n if (i < N-2) {\n a -= t * A[0][i];\n a -= (t+1) * A[0][i+1];\n a += (B[0][N] - B[0][i+2]) * 2;\n a -= (2 * N - 1) * A[1][i];\n a -= (2 * N - 2) * A[1][i+1];\n a += (B[1][N] - B[1][i+2]) * 2;\n }\n } else {\n ans = max(ans, tmp + b);\n tmp += t * A[1][i];\n tmp += (t + 1) * A[0][i];\n if (i < N-2) {\n b -= t * A[1][i];\n b -= (t+1) * A[1][i+1];\n b += (B[1][N] - B[1][i+2]) * 2;\n b -= (2 * N - 1) * A[0][i];\n b -= (2 * N - 2) * A[0][i+1];\n b += (B[0][N] - B[0][i+2]) * 2;\n }\n }\n if (i == N-1) ans = max(ans, tmp);\n }\n\n ans.writeln;\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n int[][] rws;\n foreach (_; 0 .. 2) rws ~= readln.chomp.split.map!(to!int).array;\n \n debug { rws.writeln; }\n \n auto mxsum = new long[][] (2, n+1);\n mxsum[0][$-1] = mxsum[1][$-1] = 0;\n auto dp = new long[][] (2, n+1);\n dp[0][$-1] = dp[1][$-1] = 0;\n auto totsum = 0L, updown = 0L, downup = 0L;\n foreach_reverse (i; 0 .. n) {\n updown += totsum + cast(long)(2*(n-i) - 1) * rws[1][i];\n downup += totsum + cast(long)(2*(n-i) - 1) * rws[0][i];\n \n dp[0][i] = max(updown, cast(long)rws[1][i] + dp[1][i+1] + 2*totsum);\n dp[1][i] = max(downup, cast(long)rws[0][i] + dp[0][i+1] + 2*totsum);\n \n totsum += rws[0][i] + rws[1][i];\n debug { writeln(dp[0][i], ' ', dp[1][i]); }\n }\n \n dp[0][0].writeln;\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n int[][] rws;\n foreach (_; 0 .. 2) rws ~= readln.chomp.split.map!(to!int).array;\n \n debug { rws.writeln; }\n \n auto dp = new long[][] (2, n+1);\n dp[0][$-1] = dp[1][$-1] = 0;\n auto totsum = 0L, updown = 0L, downup = 0L;\n foreach_reverse (i; 0 .. n) {\n updown += totsum + cast(long)(2*(n-i) - 1) * rws[1][i];\n downup += totsum + cast(long)(2*(n-i) - 1) * rws[0][i];\n \n dp[0][i] = max(updown, cast(long)rws[1][i] + dp[1][i+1] + 2*totsum);\n dp[1][i] = max(downup, cast(long)rws[0][i] + dp[0][i+1] + 2*totsum);\n \n totsum += rws[0][i] + rws[1][i];\n \n debug { writeln(dp[0][i], ' ', dp[1][i]); }\n }\n \n dp[0][0].writeln;\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n int[][] rws;\n foreach (_; 0 .. 2) rws ~= readln.chomp.split.map!(to!int).array;\n \n debug { rws.writeln; }\n \n auto dp = new long[][] (2, n+1);\n dp[0][$-1] = dp[1][$-1] = 0;\n auto totsum = 0L, updown = 0L, downup = 0L;\n \n foreach_reverse (i; 0 .. n)\n {\n updown += totsum + cast(long)(2*(n-i) - 1) * rws[1][i];\n downup += totsum + cast(long)(2*(n-i) - 1) * rws[0][i];\n \n dp[0][i] = max(updown, cast(long)rws[1][i] + dp[1][i+1] + 2*totsum);\n dp[1][i] = max(downup, cast(long)rws[0][i] + dp[0][i+1] + 2*totsum);\n \n totsum += rws[0][i] + rws[1][i];\n \n debug { writeln(dp[0][i], ' ', dp[1][i]); }\n }\n \n dp[0][0].writeln;\n}"}, {"source_code": "void main(){\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int w; rd(w);\n auto a=new long[][](2, w);\n a[0]=readln.split.to!(long[]); \n a[1]=readln.split.to!(long[]);\n\n auto s=new long[](w+1);\n auto sr=new long[][](2, w+1);\n auto sl=new long[][](2, w+1);\n foreach(j; 0..w){\n s[j+1]=s[j];\n if(j%2==0) s[j+1]+=a[0][j]*(j*2)+a[1][j]*(j*2+1);\n else s[j+1]+=a[1][j]*(j*2)+a[0][j]*(j*2+1);\n }\n auto sub=new long[][](2, w+1);\n foreach(i; 0..2)foreach(j; 0..w) sub[i][j+1]=sub[i][j]+a[i][j];\n foreach(i; 0..2)for(int j=w-1; j>=0; j--){\n sr[i][j]=sr[i][j+1];\n sr[i][j]-=(sub[i][w]-sub[i][j+1]);\n sr[i][j]+=a[i][j]*(j*2); // ちょっとサボる \n\n sl[i][j]=sl[i][j+1];\n sl[i][j]-=(sub[i][w]-sub[i][j+1]);\n sl[i][j]+=a[i][j]*(w*2-1);\n }\n long mx=0;\n for(int j=0; j<=w; j++){\n if(j%2==0){\n mx=max(mx, s[j]+sr[0][j]+sl[1][j]);\n // writeln(s[j]+sr[0][j]+sl[1][j]);\n }else{\n mx=max(mx, s[j]+sr[1][j]+sl[0][j]);\n // writeln(s[j]+sr[1][j]+sl[0][j]);\n }\n }\n writeln(mx);\n}\n\n/* \n s[j]:=j列目までジグザグ (1-indexed)\n j=0, 1, ..., w\n s[0]=0\n sr[i][j]:=i行をj列から右端まで (0-indexed)\n sl[i][j]:=i行を右端からj列まで (0-indexed)\n\n sl[*][w]=sr[*][w]=0\n\n j列までジグザグ j=0, 1, ..., w\n jが偶数\n 0行目をj+1列目から右端まで、1行目を右端からj+1列目まで\n s[j]+sr[0][j]+sl[1][j]\n jが奇数\n s[j]+sr[1][j]+sl[0][j]\n\n*/\n\nvoid rd(T...)(ref T x){\n import std.stdio, std.string, std.conv;\n auto l=readln.split;\n assert(l.length==x.length);\n foreach(i, ref e; x) e=l[i].to!(typeof(e));\n}"}, {"source_code": "void main(){\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int w; rd(w);\n auto a=new long[][](2, w);\n a[0]=readln.split.to!(long[]); \n a[1]=readln.split.to!(long[]);\n\n auto s=new long[](w+1);\n auto sr=new long[][](2, w+1);\n auto sl=new long[][](2, w+1);\n foreach(j; 0..w){\n s[j+1]=s[j];\n if(j%2==0) s[j+1]+=a[0][j]*(j*2)+a[1][j]*(j*2+1);\n else s[j+1]+=a[1][j]*(j*2)+a[0][j]*(j*2+1);\n }\n auto sub=new long[][](2, w+1);\n foreach(i; 0..2)foreach(j; 0..w) sub[i][j+1]=sub[i][j]+a[i][j];\n foreach(i; 0..2)for(int j=w-1; j>=0; j--){\n sr[i][j]=sr[i][j+1];\n sr[i][j]-=(sub[i][w]-sub[i][j+1]);\n sr[i][j]+=a[i][j]*(j*2); // ちょっとサボる \n\n sl[i][j]=sl[i][j+1];\n sl[i][j]-=(sub[i][w]-sub[i][j+1]);\n sl[i][j]+=a[i][j]*(w*2-1);\n }\n long mx=0;\n for(int j=0; j<=w; j++){\n if(j%2==0) mx=max(mx, s[j]+sr[0][j]+sl[1][j]);\n else mx=max(mx, s[j]+sr[1][j]+sl[0][j]);\n }\n writeln(mx);\n}\n\nvoid rd(T...)(ref T x){\n import std.stdio, std.string, std.conv;\n auto l=readln.split;\n assert(l.length==x.length);\n foreach(i, ref e; x) e=l[i].to!(typeof(e));\n}"}], "negative_code": [], "src_uid": "948429d3788b212e7763774f8cab097b"} {"nl": {"description": "You are given a rectangular table 3 × n. Each cell contains an integer. You can move from one cell to another if they share a side.Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 105)  — the number of columns in the table. Next three lines contain n integers each  — the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 ≤ aij ≤ 109) of the table.", "output_spec": "Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.", "sample_inputs": ["3\n1 1 1\n1 -1 1\n1 1 1", "5\n10 10 10 -1 -1\n-1 10 10 10 10\n-1 10 10 10 10"], "sample_outputs": ["7", "110"], "notes": "NoteThe path for the first example: The path for the second example: "}, "positive_code": [{"source_code": "import std.stdio, std.string;\nimport std.algorithm;\n\nlong saiki(int idx, int pos, int n, long[][] dp, int[][] a)\n{\n if (dp[idx][pos] != -1)\n {\n return dp[idx][pos];\n }\n if (idx == n - 1)\n {\n long val = 0;\n for (int i = 2; i >= 0; -- i)\n {\n val += a[i][n - 1];\n dp[idx][i] = val;\n }\n return dp[idx][pos];\n }\n auto ndp = new long[][](2, 3);\n auto sum = new long[2];\n foreach (i; 0 .. 2)\n {\n sum[i] = 0;\n foreach (j; 0 .. 3)\n {\n sum[i] += a[j][idx + i];\n if (idx + i + 1 < n)\n {\n ndp[i][j] = saiki(idx + i + 1, j, n, dp, a);\n }\n }\n }\n auto res = long.min;\n if (pos == 1)\n {\n res = max(res, ndp[0][pos] + a[pos][idx]);\n res = max(res, ndp[0][0] + a[pos][idx] + a[0][idx]);\n res = max(res, ndp[0][2] + a[pos][idx] + a[2][idx]);\n }\n else\n {\n res = max(res, ndp[0][pos] + a[pos][idx]);\n res = max(res, ndp[0][1] + a[pos][idx] + a[1][idx]);\n if (pos == 0) res = max(res, ndp[0][2] + sum[0]);\n else res = max(res, ndp[0][0] + sum[0]);\n if (idx + 2 < n)\n {\n if (pos == 0) res = max(res, ndp[1][2] + sum[0] + sum[1]);\n else res = max(res, ndp[1][0] + sum[0] + sum[1]); \n }\n if (idx == n - 2 && pos == 0)\n {\n res = max(res, sum[0] + sum[1]);\n }\n }\n dp[idx][pos] = res;\n return res;\n}\n\nvoid solve(int[][] a, int n)\n{\n auto dp = new long[][](n, 3);\n foreach (i; 0 .. n)\n {\n fill(dp[i], -1);\n }\n auto ans = saiki(0, 0, n, dp, a);\n writeln(ans);\n}\n\nint main(string[] args)\n{\n int n;\n while (readf(\"%d\\n\", &n) == 1)\n {\n auto a = new int[][](3, n);\n foreach (i; 0 .. 3)\n {\n foreach (j; 0 .. n)\n {\n readf(\" %d\", &a[i][j]);\n }\n readln;\n }\n solve(a, n);\n }\n return 0;\n}"}, {"source_code": "import std.stdio, std.string;\nimport std.algorithm;\n\nlong saiki(int idx, int pos, int n, long[][] dp, int[][] a)\n{\n if (dp[idx][pos] != -1)\n {\n return dp[idx][pos];\n }\n if (idx == n - 1)\n {\n long val = 0;\n for (int i = 2; i >= 0; -- i)\n {\n val += a[i][n - 1];\n dp[idx][i] = val;\n }\n return dp[idx][pos];\n }\n long[3][2] ndp;\n long[2] sum;\n foreach (i; 0 .. 2)\n {\n sum[i] = 0;\n foreach (j; 0 .. 3)\n {\n sum[i] += a[j][idx + i];\n if (idx + i + 1 < n)\n {\n ndp[i][j] = saiki(idx + i + 1, j, n, dp, a);\n }\n }\n }\n auto res = long.min;\n if (pos == 1)\n {\n res = max(res, ndp[0][pos] + a[pos][idx]);\n res = max(res, ndp[0][0] + a[pos][idx] + a[0][idx]);\n res = max(res, ndp[0][2] + a[pos][idx] + a[2][idx]);\n }\n else\n {\n res = max(res, ndp[0][pos] + a[pos][idx]);\n res = max(res, ndp[0][1] + a[pos][idx] + a[1][idx]);\n res = max(res, ndp[0][2 - pos] + sum[0]);\n if (idx + 2 < n)\n {\n if (pos == 0) res = max(res, ndp[1][2] + sum[0] + sum[1]);\n else res = max(res, ndp[1][0] + sum[0] + sum[1]); \n }\n if (idx == n - 2 && pos == 0)\n {\n res = max(res, sum[0] + sum[1]);\n }\n }\n dp[idx][pos] = res;\n return res;\n}\n\nvoid solve(int[][] a, int n)\n{\n auto dp = new long[][](n, 3);\n foreach (i; 0 .. n)\n {\n fill(dp[i], -1);\n }\n auto ans = saiki(0, 0, n, dp, a);\n writeln(ans);\n}\n\nint main(string[] args)\n{\n int n;\n while (readf(\"%d\\n\", &n) == 1)\n {\n auto a = new int[][](3, n);\n foreach (i; 0 .. 3)\n {\n foreach (j; 0 .. n)\n {\n readf(\" %d\", &a[i][j]);\n }\n readln;\n }\n solve(a, n);\n }\n return 0;\n}"}], "negative_code": [], "src_uid": "301a6dd54b3d404e98f5e1ee014d5c89"} {"nl": {"description": "A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).For a positive integer $$$n$$$, we call a permutation $$$p$$$ of length $$$n$$$ good if the following condition holds for every pair $$$i$$$ and $$$j$$$ ($$$1 \\le i \\le j \\le n$$$) — $$$(p_i \\text{ OR } p_{i+1} \\text{ OR } \\ldots \\text{ OR } p_{j-1} \\text{ OR } p_{j}) \\ge j-i+1$$$, where $$$\\text{OR}$$$ denotes the bitwise OR operation. In other words, a permutation $$$p$$$ is good if for every subarray of $$$p$$$, the $$$\\text{OR}$$$ of all elements in it is not less than the number of elements in that subarray. Given a positive integer $$$n$$$, output any good permutation of length $$$n$$$. We can show that for the given constraints such a permutation always exists.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first and only line of every test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$).", "output_spec": "For every test, output any good permutation of length $$$n$$$ on a separate line. ", "sample_inputs": ["3\n1\n3\n7"], "sample_outputs": ["1\n3 1 2\n4 3 5 2 7 1 6"], "notes": "NoteFor $$$n = 3$$$, $$$[3,1,2]$$$ is a good permutation. Some of the subarrays are listed below. $$$3\\text{ OR }1 = 3 \\geq 2$$$ $$$(i = 1,j = 2)$$$ $$$3\\text{ OR }1\\text{ OR }2 = 3 \\geq 3$$$ $$$(i = 1,j = 3)$$$ $$$1\\text{ OR }2 = 3 \\geq 2$$$ $$$(i = 2,j = 3)$$$ $$$1 \\geq 1$$$ $$$(i = 2,j = 2)$$$ Similarly, you can verify that $$$[4,3,5,2,7,1,6]$$$ is also good."}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string read_string() {\n while (tokens.empty) {\n tokens = readln.split;\n }\n auto token = tokens.front;\n tokens.popFront;\n return token;\n }\n \n int read_int() {\n return read_string.to!int;\n }\n\n double read_double() {\n return read_string.to!double;\n }\n \n string[] tokens;\n}\n\nvoid main() {\n IO cin;\n int t = 1;\n t = cin.read_int;\n while (t--) {\n int n = cin.read_int;\n for (int i = 1; i <= n; i++) {\n write(i, \" \");\n }\n writeln();\n } \n}"}, {"source_code": "import std.algorithm, std.conv, std.range, std.stdio, std.string;\n\nvoid main () {\n\tforeach (test; 0..readln.strip.to !(int))\n\t\treadln.strip.to !(int).iota.map !(q{a + 1}).writefln !(\"%(%s %)\");\n}\n"}, {"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n \nstruct IO {\n string read_string() {\n while (tokens.empty) {\n tokens = readln.split;\n }\n auto token = tokens.front;\n tokens.popFront;\n return token;\n }\n \n int read_int() {\n return read_string.to!int;\n }\n \n double read_double() {\n return read_string.to!double;\n }\n \n string[] tokens;\n}\n \nvoid main() {\n IO cin;\n int t = 1;\n t = cin.read_int;\n while (t--) {\n int n = cin.read_int;\n for (int i = 1; i <= n; i++) {\n write(i, \" \");\n }\n writeln();\n } \n}\n/* CODED BY:-\n ___________________________________\n| ___ |\n| /\\ /\\ \\ / | | |___ |__| | \n| /~~\\ /~~\\ | |__| ___| | | |\n|___________________________________|\n \n*/"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\t\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tans[ti] ~= i+1;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\te.map!(to!string).join(\" \").writeln;\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "1b3ac752bc9c0b5e20a76f028d4b3c15"} {"nl": {"description": "You are given a grid with $$$n$$$ rows and $$$m$$$ columns. Rows and columns are numbered from $$$1$$$ to $$$n$$$, and from $$$1$$$ to $$$m$$$. The intersection of the $$$a$$$-th row and $$$b$$$-th column is denoted by $$$(a, b)$$$. Initially, you are standing in the top left corner $$$(1, 1)$$$. Your goal is to reach the bottom right corner $$$(n, m)$$$.You can move in four directions from $$$(a, b)$$$: up to $$$(a-1, b)$$$, down to $$$(a+1, b)$$$, left to $$$(a, b-1)$$$ or right to $$$(a, b+1)$$$.You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach $$$(n, m)$$$?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of the test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^9$$$) — the size of the grid.", "output_spec": "For each test case, print a single integer: $$$-1$$$ if it is impossible to reach $$$(n, m)$$$ under the given conditions, otherwise the minimum number of moves.", "sample_inputs": ["6\n\n1 1\n\n2 1\n\n1 3\n\n4 2\n\n4 6\n\n10 5"], "sample_outputs": ["0\n1\n-1\n6\n10\n17"], "notes": "NoteTest case $$$1$$$: $$$n=1$$$, $$$m=1$$$, and initially you are standing in $$$(1, 1)$$$ so $$$0$$$ move is required to reach $$$(n, m) = (1, 1)$$$.Test case $$$2$$$: you should go down to reach $$$(2, 1)$$$.Test case $$$3$$$: it is impossible to reach $$$(1, 3)$$$ without moving right two consecutive times, or without leaving the grid.Test case $$$4$$$: an optimal moving sequence could be: $$$(1, 1) \\to (1, 2) \\to (2, 2) \\to (2, 1) \\to (3, 1) \\to (3, 2) \\to (4, 2)$$$. It can be proved that this is the optimal solution. So the answer is $$$6$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD;\r\n\t\tauto m = RD;\r\n\r\n\t\tif (n > m)\r\n\t\t\tswap(n, m);\r\n\r\n\t\tif (n == 1)\r\n\t\t\tans[ti] = m == 1 ? 0 : m == 2 ? 1 : -1;\r\n\t\telse\r\n\t\t{\r\n\t\t\tm -= n;\r\n\t\t\tans[ti] = (n-1) * 2;\r\n\t\t\tans[ti] += (m / 2) * 4 + (m % 2);\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "6f0d3a7971ffc2571838ecd8bf14238d"} {"nl": {"description": "Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all $$$m$$$ taxi drivers in the city, who every day transport the rest of the city residents — $$$n$$$ riders.Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $$$i$$$ the number $$$a_{i}$$$ — the number of riders that would call the $$$i$$$-th taxi driver when all drivers and riders are at their home?The taxi driver can neither transport himself nor other taxi drivers.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n,m \\le 10^5$$$) — number of riders and taxi drivers. The second line contains $$$n + m$$$ integers $$$x_1, x_2, \\ldots, x_{n+m}$$$ ($$$1 \\le x_1 < x_2 < \\ldots < x_{n+m} \\le 10^9$$$), where $$$x_i$$$ is the coordinate where the $$$i$$$-th resident lives. The third line contains $$$n + m$$$ integers $$$t_1, t_2, \\ldots, t_{n+m}$$$ ($$$0 \\le t_i \\le 1$$$). If $$$t_i = 1$$$, then the $$$i$$$-th resident is a taxi driver, otherwise $$$t_i = 0$$$. It is guaranteed that the number of $$$i$$$ such that $$$t_i = 1$$$ is equal to $$$m$$$.", "output_spec": "Print $$$m$$$ integers $$$a_1, a_2, \\ldots, a_{m}$$$, where $$$a_i$$$ is the answer for the $$$i$$$-th taxi driver. The taxi driver has the number $$$i$$$ if among all the taxi drivers he lives in the $$$i$$$-th smallest coordinate (see examples for better understanding).", "sample_inputs": ["3 1\n1 2 3 10\n0 0 1 0", "3 2\n2 3 4 5 6\n1 0 0 0 1", "1 4\n2 4 6 10 15\n1 1 1 1 0"], "sample_outputs": ["3", "2 1", "0 0 0 1"], "notes": "NoteIn the first example, we have only one taxi driver, which means an order from any of $$$n$$$ riders will go to him.In the second example, the first taxi driver lives at the point with the coordinate $$$2$$$, and the second one lives at the point with the coordinate $$$6$$$. Obviously, the nearest taxi driver to the rider who lives on the $$$3$$$ coordinate is the first one, and to the rider who lives on the coordinate $$$5$$$ is the second one. The rider who lives on the $$$4$$$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.In the third example, we have one rider and the taxi driver nearest to him is the fourth one."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.conv;\nimport std.range, std.algorithm, std.array, std.typecons, std.container;\nimport std.math, std.numeric, core.bitop;\n\n\nvoid main() {\n int n, m;\n scan(n, m);\n auto x = readln.split.to!(int[]);\n auto t = readln.split.to!(int[]);\n\n auto r = iota(n + m).filter!(i => !t[i]).map!(i => x[i]).array;\n auto d = iota(n + m).filter!(i => t[i]).map!(i => x[i]).array;\n\n auto ans = new int[](m);\n int last = n;\n\n foreach (i ; 0 .. m - 1) {\n int cnt;\n\n while (!r.empty && (r.front - d[i]) <= (d[i+1] - r.front)) {\n r.popFront();\n cnt++;\n }\n\n ans[i] = cnt;\n last -= cnt;\n }\n\n ans[m-1] = last;\n\n writefln(\"%(%s %)\", ans);\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nvoid scan(T...)(ref T args) {\n import std.stdio : readln;\n import std.algorithm : splitter;\n import std.conv : to;\n import std.range.primitives;\n\n auto line = readln().splitter();\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}"}], "negative_code": [], "src_uid": "56ea328f84b2930656ff5eb9b8fda8e0"} {"nl": {"description": "Your task is to maintain a queue consisting of lowercase English letters as follows: \"push $$$c$$$\": insert a letter $$$c$$$ at the back of the queue; \"pop\": delete a letter from the front of the queue. Initially, the queue is empty. After each operation, you are asked to count the number of distinct palindromic substrings in the string that are obtained by concatenating the letters from the front to the back of the queue.Especially, the number of distinct palindromic substrings of the empty string is $$$0$$$. A string $$$s[1..n]$$$ of length $$$n$$$ is palindromic if $$$s[i] = s[n-i+1]$$$ for every $$$1 \\leq i \\leq n$$$. The string $$$s[l..r]$$$ is a substring of string $$$s[1..n]$$$ for every $$$1 \\leq l \\leq r \\leq n$$$. Two strings $$$s[1..n]$$$ and $$$t[1..m]$$$ are distinct, if at least one of the following holds. $$$n \\neq m$$$; $$$s[i] \\neq t[i]$$$ for some $$$1 \\leq i \\leq \\min\\{n,m\\}$$$. ", "input_spec": "The first line is an integer $$$q$$$ ($$$1 \\leq q \\leq 10^6$$$), indicating the number of operations. Then $$$q$$$ lines follow. Each of the following lines contains one of the operations as follows. \"push $$$c$$$\": insert a letter $$$c$$$ at the back of the queue, where $$$c$$$ is a lowercase English letter; \"pop\": delete a letter from the front of the queue. It is guaranteed that no \"pop\" operation will be performed when the queue is empty.", "output_spec": "After each operation, print the number of distinct palindromic substrings in the string presented in the queue. ", "sample_inputs": ["12\npush a\npop\npush a\npush a\npush b\npush b\npush a\npush a\npop\npop\npop\npush b"], "sample_outputs": ["1\n0\n1\n2\n3\n4\n5\n6\n5\n4\n3\n4"], "notes": "NoteLet $$$s_k$$$ be the string presented in the queue after the $$$k$$$-th operation, and let $$$c_k$$$ be the number of distinct palindromic substrings of $$$s_k$$$. The following table shows the details of the example. $$$k$$$$$$s_k$$$$$$c_k$$$$$$1$$$$$$a$$$$$$1$$$$$$2$$$$$$\\textsf{empty}$$$$$$0$$$$$$3$$$$$$a$$$$$$1$$$$$$4$$$$$$aa$$$$$$2$$$$$$5$$$$$$aab$$$$$$3$$$$$$6$$$$$$aabb$$$$$$4$$$$$$7$$$$$$aabba$$$$$$5$$$$$$8$$$$$$aabbaa$$$$$$6$$$$$$9$$$$$$abbaa$$$$$$5$$$$$$10$$$$$$bbaa$$$$$$4$$$$$$11$$$$$$baa$$$$$$3$$$$$$12$$$$$$baab$$$$$$4$$$ It is worth pointing out that After the $$$2$$$-nd operation, the string is empty and thus has no substrings. So the answer is $$$0$$$; After the $$$8$$$-th operation, the string is \"$$$aabbaa$$$\". The $$$6$$$ distinct palindromic substrings are \"$$$a$$$\", \"$$$aa$$$\", \"$$$aabbaa$$$\", \"$$$abba$$$\", \"$$$b$$$\", and \"$$$bb$$$\". "}, "positive_code": [{"source_code": "import std;\r\nimmutable int N=1000005;\r\n \r\nstruct node {\r\n\tint fail,len,pos,tot;\r\n int[26] to;\r\n}\r\n\r\nstruct S {\r\n node[] t;\r\n this(int k) { this.t = new node[k]; }\r\n}\r\nint cnt=1;\r\nint cur=1;\r\nint n=0;\r\nint[N] a;\r\nint l=1;\r\nint ans=0;\r\nint[][N] d;\r\n\r\nvoid extend(int x, ref S z) {\r\n\twhile (a[n-z.t[cur].len-1]!=a[n]) {\r\n\t\tcur=z.t[cur].fail;\r\n\t}\r\n\tif (!z.t[cur].to[x]) {\r\n\t\tint u=z.t[cur].fail,v=++cnt;\r\n\t\twhile (a[n-z.t[u].len-1]!=a[n]) {\r\n\t\t\tu=z.t[u].fail;\r\n\t\t}\r\n\t\tz.t[v].fail=z.t[u].to[x];\r\n\t\tz.t[cur].to[x]=v;\r\n\t\tz.t[v].len=z.t[cur].len+2;\r\n\t}\r\n\tcur=z.t[cur].to[x];\r\n}\r\nvoid cover(int x,int y, ref S z) {\r\n\tif (x<=1) {\r\n\t\treturn;\r\n\t}\r\n\tif (!z.t[x].pos) {\r\n\t\tans+=!z.t[x].pos;\r\n\t\tz.t[z.t[x].fail].tot++;\r\n\t}\r\n\tif (z.t[x].posn-l+1) {\r\n\t\tcur=z.t[cur].fail;\r\n\t}\r\n\tcover(cur,n,z);\r\n}\r\n\r\nvoid pop(ref S z) {\r\n\tforeach (x;d[l]) {\r\n\t\tif (z.t[x].pos-z.t[x].len+1!=l||z.t[x].tot) {\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tcover(z.t[x].fail,z.t[x].pos,z);\r\n\t\tz.t[x].pos=0;\r\n ans--;\r\n z.t[z.t[x].fail].tot--;\r\n\t}\r\n\tl++;\r\n}\r\n \r\nvoid main() {\r\n auto z = S(N);\r\n\ta[0]=-1;\r\n\tz.t[0].len=0;\r\n z.t[1].len=-1;\r\n\tz.t[0].fail=1;\r\n z.t[1].fail=0;\r\n\tint Q;\r\n\treadf!\" %d \"(Q);\r\n\twhile (Q--) {\r\n\t\tauto s = readln.strip.split();\r\n\t\tif (s[0]==\"push\") {\r\n\t\t\tpush(s[1][0]-'a',z);\r\n\t\t} else {\r\n\t\t\tpop(z);\r\n\t\t}\r\n writeln(ans);\r\n\t}\r\n\t\r\n}\r\n"}], "negative_code": [{"source_code": "import std;\r\nimmutable int N=1000000;\r\n \r\nstruct node {\r\n\tint fail,len,pos,tot;\r\n int[26] to;\r\n}\r\n\r\nstruct S {\r\n node[] t;\r\n this(int k) { this.t = new node[k]; }\r\n}\r\nint cnt=1;\r\nint cur=1;\r\nint n=0;\r\nint[N] a;\r\nint l=1;\r\nint ans=0;\r\nint[][N] d;\r\n\r\nvoid extend(int x, ref S z) {\r\n\twhile (a[n-z.t[cur].len-1]!=a[n]) {\r\n\t\tcur=z.t[cur].fail;\r\n\t}\r\n\tif (!z.t[cur].to[x]) {\r\n\t\tint u=z.t[cur].fail,v=++cnt;\r\n\t\twhile (a[n-z.t[u].len-1]!=a[n]) {\r\n\t\t\tu=z.t[u].fail;\r\n\t\t}\r\n\t\tz.t[v].fail=z.t[u].to[x];\r\n\t\tz.t[cur].to[x]=v;\r\n\t\tz.t[v].len=z.t[cur].len+2;\r\n\t}\r\n\tcur=z.t[cur].to[x];\r\n}\r\nvoid cover(int x,int y, ref S z) {\r\n\tif (x<=1) {\r\n\t\treturn;\r\n\t}\r\n\tif (!z.t[x].pos) {\r\n\t\tans+=!z.t[x].pos;\r\n\t\tz.t[z.t[x].fail].tot++;\r\n\t}\r\n\tif (z.t[x].posn-l+1) {\r\n\t\tcur=z.t[cur].fail;\r\n\t}\r\n\tcover(cur,n,z);\r\n}\r\n\r\nvoid pop(ref S z) {\r\n\tforeach (x;d[l]) {\r\n\t\tif (z.t[x].pos-z.t[x].len+1!=l||z.t[x].tot) {\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tcover(z.t[x].fail,z.t[x].pos,z);\r\n\t\tz.t[x].pos=0;\r\n ans--;\r\n z.t[z.t[x].fail].tot--;\r\n\t}\r\n\tl++;\r\n}\r\n \r\nvoid main() {\r\n auto z = S(N);\r\n\ta[0]=-1;\r\n\tz.t[0].len=0;\r\n z.t[1].len=-1;\r\n\tz.t[0].fail=1;\r\n z.t[1].fail=0;\r\n\tint Q;\r\n\treadf!\" %d \"(Q);\r\n\twhile (Q--) {\r\n\t\tauto s = readln.strip.split();\r\n\t\tif (s[0]==\"push\") {\r\n\t\t\tpush(s[1][0]-'a',z);\r\n\t\t} else {\r\n\t\t\tpop(z);\r\n\t\t}\r\n writeln(ans);\r\n\t}\r\n\t\r\n}\r\n"}], "src_uid": "fd5fd38a69a0f645c5e092a15de67f88"} {"nl": {"description": "You are given a matrix of size $$$n \\times n$$$ filled with lowercase English letters. You can change no more than $$$k$$$ letters in this matrix.Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is $$$2n - 1$$$.Find the lexicographically smallest string that can be associated with a path after changing letters in at most $$$k$$$ cells of the matrix.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$, if the first different letter in $$$a$$$ and $$$b$$$ is smaller in $$$a$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2000$$$, $$$0 \\le k \\le n^2$$$) — the size of the matrix and the number of letters you can change. Each of the next $$$n$$$ lines contains a string of $$$n$$$ lowercase English letters denoting one row of the matrix.", "output_spec": "Output the lexicographically smallest string that can be associated with some valid path after changing no more than $$$k$$$ letters in the matrix.", "sample_inputs": ["4 2\nabcd\nbcde\nbcad\nbcde", "5 3\nbwwwz\nhrhdh\nsepsp\nsqfaf\najbvw", "7 6\nypnxnnp\npnxonpm\nnxanpou\nxnnpmud\nnhtdudu\nnpmuduh\npmutsnz"], "sample_outputs": ["aaabcde", "aaaepfafw", "aaaaaaadudsnz"], "notes": "NoteIn the first sample test case it is possible to change letters 'b' in cells $$$(2, 1)$$$ and $$$(3, 1)$$$ to 'a', then the minimum path contains cells $$$(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)$$$. The first coordinate corresponds to the row and the second coordinate corresponds to the column."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport std.range;\nimport std.numeric;\n\nvoid lower(T)(ref T dest, T source)\n{\n dest = min(dest, source);\n}\nvoid increase(T)(ref T dest, T source)\n{\n dest = max(dest, source);\n}\n\nvoid main(string[] args)\n{\n inputFile = stdin;\n auto n = next!int;\n auto k = next!int;\n auto imat = next!string(n);\n auto mat = new char[][](n, n);\n foreach(i; 0 .. n)\n foreach(j; 0 .. n)\n {\n\tmat[i][j] = imat[i][j];\n }\n auto minuse = new int[][](n, n);\n minuse[0][0] = mat[0][0] != 'a';\n foreach(s; 1 .. 2 * n - 1)\n {\n int i;\n int j;\n i = min(s, n - 1);\n j = s - i;\n for(; i >= 0 && j < n; i--, j++)\n\t{\n\t minuse[i][j] = int.max;\n\t if (i - 1 >= 0) lower(minuse[i][j], minuse[i - 1][j] + (mat[i][j] != 'a'));\n\t if (j - 1 >= 0) lower(minuse[i][j], minuse[i][j - 1] + (mat[i][j] != 'a'));\n\t}\n }\n foreach(i; 0 .. n)\n foreach(j; 0 .. n)\n {\n\tif (minuse[i][j] <= k)\n\t mat[i][j] = 'a';\n }\n debug foreach(i; 0 .. n)\n {\n writeln(mat[i]);\n }\n auto used = new bool[][](n, n);\n used[0][0] = true;\n write(mat[0][0]);\n foreach(s; 1 .. 2 * n - 1)\n {\n int i;\n int j;\n i = min(s, n - 1);\n j = s - i;\n char minchar = char.max;\n for(; i >= 0 && j < n; i--, j++)\n\t{\n\t if (i - 1 >= 0 && used[i - 1][j] ||\n\t j - 1 >= 0 && used[i][j - 1])\n\t minchar = min(minchar, mat[i][j]);\n\t}\n write(minchar);\n i = min(s, n - 1);\n j = s - i;\n for(; i >= 0 && j < n; i--, j++)\n\t{\n\t if (i - 1 >= 0 && used[i - 1][j] ||\n\t j - 1 >= 0 && used[i][j - 1])\n\t if (mat[i][j] == minchar)\n\t used[i][j] = true;\n\t}\n }\n writeln;\n}\n ulong bitCnt(long x)\n{\n ulong cnt = 0;\n while(x) {cnt += x & 1; x>>=1;}\n return cnt;\n}\nalias Arr(T) = Mat!(T, 1);\nstruct Mat(T, size_t dimension)\n{\n T[] _baseArray;\n T[] _slice;\n size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}\n size_t[dimension] _indexer;\n this(T[] baseArray, size_t[dimension] sizes)\n {\n _baseArray = baseArray;\n _sizes = sizes;\n _indexer[dimension - 1] = 1;\n static foreach_reverse(i; 0 .. dimension - 1)\n _indexer[i] = _indexer[i + 1] * _sizes[i + 1];\n auto requiredSize = _indexer[0] * _sizes[0];\n debug assert(_baseArray.length >= requiredSize);\n _slice = _baseArray[0 .. requiredSize];\n }\n this(size_t[dimension] sizes)\n {\n size_t reqSize = 1;\n static foreach(i; 0 .. dimension)\n reqSize *= sizes[i];\n this(new T[](reqSize), sizes);\n }\n ref auto opIndex()\n {\n pragma(inline, true);\n return _slice[];\n }\n ref auto opIndex(I...)(I i)\n {\n pragma(inline, true);\n debug assert(i.length <= dimension);\n size_t index = mixin(iota(0, i.length).map!(j => text(\"i[\", j, \"]*_indexer[\", j, \"]\")).join(\"+\"));\n static if (i.length == dimension)\n {\n\treturn _slice[index];\n }\n else\n {\n\tenum sliceDimension = dimension - i.length;\n\tsize_t[sliceDimension] remSizes = _sizes[i.length .. $];\n\tauto basePortion = _slice[index .. index + _indexer[i.length - 1]];\n \treturn Mat!(T, sliceDimension)(basePortion, remSizes);\n }\n }\n static if (dimension == 2)\n {\n auto identity()\n {\n\tdebug assert(dim(0) == dim(1));\n\tauto n = dim(0);\n\tauto id = Mat!(T, 2)([n, n]);\n\tforeach(k; 0 .. n)\n\t id[k, k] = T(1);\n\treturn id;\n }\n bool canMultiply(Mat!(T, 2) b)\n {\n\treturn this.dim(1) == b.dim(0);\n }\n auto opBinary(string op)(Mat!(T, 2) b)\n\tif (op == \"*\")\n\t {\n\t debug assert(canMultiply(b));\n\t auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);\n\t foreach(i; 0 .. res.dim(0))\n\t foreach(k; 0 .. this.dim(1))\n\t\tforeach(j; 0 .. res.dim(1))\n\t\t res[i, j] = res[i, j] + this[i, k] * b[k, j];\n\t return res;\n\t }\n auto opBinary(string op)(ulong exp)\n\tif (op == \"^^\")\n\t {\n\t return this.pow(identity, exp);\n\t }\n mixin interiorBinPow;\n }\n}\n\nstruct Mod(T, ulong mod)\n{\n alias This = Mod!(T, mod);\n T _rep; T rep() {return _rep;}\n this(W)(W w)\n {\n _rep = ((cast(T) w)%mod + mod) % mod;\n }\n static auto plainConstruct(T t)\n {\n pragma(inline, true);\n debug assert(t >= 0 && t < mod);\n auto res = Mod!(T, mod).init;\n res._rep = t;\n return res;\n }\n string toString()\n {\n return to!string(rep);\n }\n debug invariant\n {\n assert(_rep >= 0 && _rep < mod);\n }\n auto opBinary(string op)(This b)\n {\n static if (op == \"+\")\n {\n\tT resrep = _rep + b._rep;\n\tif (resrep >= mod) resrep -= mod;\n\treturn This.plainConstruct(resrep);\n }\n else static if (op == \"-\")\n {\n\tT resrep = _rep - b._rep;\n\tif (resrep < 0) resrep += mod;\n\treturn This.plainConstruct(resrep);\n }\n else static if (op == \"*\")\n {\n\treturn This(_rep * b._rep);\n }\n }\n}\n\nmixin template interiorBinPow()\n{\n alias ThisType = typeof(this);\n auto pow(ThisType res, ulong exp)\n {\n if (exp < 0) debug assert(0);\n auto pow = this;\n while(exp)\n\t{\n\t if (exp & 1)\n\t res = res * pow;\n\t pow = pow * pow;\n\t exp >>= 1;\n\t}\n return res;\n }\n}\n\n// INPUT\nFile inputFile;\nDList!string _words;\nvoid _wordsFill()\n{\n while (_words.empty) _words.insertBack(inputFile.readln.split); \n}\nstring popWord()\n{\n _wordsFill;\n auto word = _words.front;\n _words.removeFront;\n return word;\n}\nstring peekWord()\n{\n _wordsFill;\n return _words.front;\n}\nT next(T)()\n{\n return to!T(popWord);\n}\nT peek(T)()\n{\n return to!T(peekWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport std.range;\nimport std.numeric;\n\nvoid lower(T)(ref T dest, T source)\n{\n dest = min(dest, source);\n}\nvoid increase(T)(ref T dest, T source)\n{\n dest = max(dest, source);\n}\n\nvoid main(string[] args)\n{\n inputFile = stdin;\n auto n = next!int;\n auto k = next!int;\n auto imat = next!string(n);\n auto mat = new char[][](n, n);\n foreach(i; 0 .. n)\n foreach(j; 0 .. n)\n {\n\tmat[i][j] = imat[i][j];\n }\n auto visited = new bool[][](n, n);\n auto adjlist(Tuple!(int, int) i)\n {\n auto adjs = new Tuple!(int, int)[](0);\n if (i[0] + 1 < n) adjs ~= tuple(i[0] + 1, i[1]);\n if (i[1] + 1 < n) adjs ~= tuple(i[0], i[1] + 1);\n return adjs;\n }\n auto radjlist(Tuple!(int, int) i)\n {\n auto adjs = new Tuple!(int, int)[](0);\n if (i[0] - 1 >= 0) adjs ~= tuple(i[0] - 1, i[1]);\n if (i[1] - 1 >= 0) adjs ~= tuple(i[0], i[1] - 1);\n return adjs;\n }\n auto toplace = new int[][](n, n);\n toplace[0][0] = k;\n auto q = DList!(Tuple!(int, int))();\n q.insertBack(tuple(0, 0));\n while(!q.empty)\n {\n auto i = q.front;\n q.removeFront;\n if (toplace[i[0]][i[1]] == 0) continue;\n foreach(adj; adjlist(i))\n\t{\n\t auto togive = toplace[i[0]][i[1]] - (mat[i[0]][i[1]] != 'a');\n\t increase(toplace[adj[0]][adj[1]], togive);\n\t if (!visited[adj[0]][adj[1]])\n\t {\n\t visited[adj[0]][adj[1]] = true;\n\t q.insertBack(adj);\n\t }\n\t}\n }\n debug foreach(i; 0 .. n)\n {\n writeln(toplace[i]);\n }\n foreach(i; 0 .. n)\n foreach(j; 0 .. n)\n {\n\tif (toplace[i][j] > 0)\n\t mat[i][j] = 'a';\n }\n debug foreach(i; 0 .. n)\n {\n writeln(mat[i]);\n }\n auto used = new bool[][](n, n);\n used[0][0] = true;\n write(mat[0][0]);\n foreach(s; 1 .. 2 * n - 1)\n {\n int i;\n int j;\n i = min(s, n - 1);\n j = s - i;\n char minchar = char.max;\n for(; i >= 0 && j < n; i--, j++)\n\t{\n\t if (i - 1 >= 0 && used[i - 1][j] ||\n\t j - 1 >= 0 && used[i][j - 1])\n\t minchar = min(minchar, mat[i][j]);\n\t}\n write(minchar);\n i = min(s, n - 1);\n j = s - i;\n for(; i >= 0 && j < n; i--, j++)\n\t{\n\t if (mat[i][j] == minchar)\n\t used[i][j] = true;\n\t}\n }\n writeln;\n}\n ulong bitCnt(long x)\n{\n ulong cnt = 0;\n while(x) {cnt += x & 1; x>>=1;}\n return cnt;\n}\nalias Arr(T) = Mat!(T, 1);\nstruct Mat(T, size_t dimension)\n{\n T[] _baseArray;\n T[] _slice;\n size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}\n size_t[dimension] _indexer;\n this(T[] baseArray, size_t[dimension] sizes)\n {\n _baseArray = baseArray;\n _sizes = sizes;\n _indexer[dimension - 1] = 1;\n static foreach_reverse(i; 0 .. dimension - 1)\n _indexer[i] = _indexer[i + 1] * _sizes[i + 1];\n auto requiredSize = _indexer[0] * _sizes[0];\n debug assert(_baseArray.length >= requiredSize);\n _slice = _baseArray[0 .. requiredSize];\n }\n this(size_t[dimension] sizes)\n {\n size_t reqSize = 1;\n static foreach(i; 0 .. dimension)\n reqSize *= sizes[i];\n this(new T[](reqSize), sizes);\n }\n ref auto opIndex()\n {\n pragma(inline, true);\n return _slice[];\n }\n ref auto opIndex(I...)(I i)\n {\n pragma(inline, true);\n debug assert(i.length <= dimension);\n size_t index = mixin(iota(0, i.length).map!(j => text(\"i[\", j, \"]*_indexer[\", j, \"]\")).join(\"+\"));\n static if (i.length == dimension)\n {\n\treturn _slice[index];\n }\n else\n {\n\tenum sliceDimension = dimension - i.length;\n\tsize_t[sliceDimension] remSizes = _sizes[i.length .. $];\n\tauto basePortion = _slice[index .. index + _indexer[i.length - 1]];\n \treturn Mat!(T, sliceDimension)(basePortion, remSizes);\n }\n }\n static if (dimension == 2)\n {\n auto identity()\n {\n\tdebug assert(dim(0) == dim(1));\n\tauto n = dim(0);\n\tauto id = Mat!(T, 2)([n, n]);\n\tforeach(k; 0 .. n)\n\t id[k, k] = T(1);\n\treturn id;\n }\n bool canMultiply(Mat!(T, 2) b)\n {\n\treturn this.dim(1) == b.dim(0);\n }\n auto opBinary(string op)(Mat!(T, 2) b)\n\tif (op == \"*\")\n\t {\n\t debug assert(canMultiply(b));\n\t auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);\n\t foreach(i; 0 .. res.dim(0))\n\t foreach(k; 0 .. this.dim(1))\n\t\tforeach(j; 0 .. res.dim(1))\n\t\t res[i, j] = res[i, j] + this[i, k] * b[k, j];\n\t return res;\n\t }\n auto opBinary(string op)(ulong exp)\n\tif (op == \"^^\")\n\t {\n\t return this.pow(identity, exp);\n\t }\n mixin interiorBinPow;\n }\n}\n\nstruct Mod(T, ulong mod)\n{\n alias This = Mod!(T, mod);\n T _rep; T rep() {return _rep;}\n this(W)(W w)\n {\n _rep = ((cast(T) w)%mod + mod) % mod;\n }\n static auto plainConstruct(T t)\n {\n pragma(inline, true);\n debug assert(t >= 0 && t < mod);\n auto res = Mod!(T, mod).init;\n res._rep = t;\n return res;\n }\n string toString()\n {\n return to!string(rep);\n }\n debug invariant\n {\n assert(_rep >= 0 && _rep < mod);\n }\n auto opBinary(string op)(This b)\n {\n static if (op == \"+\")\n {\n\tT resrep = _rep + b._rep;\n\tif (resrep >= mod) resrep -= mod;\n\treturn This.plainConstruct(resrep);\n }\n else static if (op == \"-\")\n {\n\tT resrep = _rep - b._rep;\n\tif (resrep < 0) resrep += mod;\n\treturn This.plainConstruct(resrep);\n }\n else static if (op == \"*\")\n {\n\treturn This(_rep * b._rep);\n }\n }\n}\n\nmixin template interiorBinPow()\n{\n alias ThisType = typeof(this);\n auto pow(ThisType res, ulong exp)\n {\n if (exp < 0) debug assert(0);\n auto pow = this;\n while(exp)\n\t{\n\t if (exp & 1)\n\t res = res * pow;\n\t pow = pow * pow;\n\t exp >>= 1;\n\t}\n return res;\n }\n}\n\n// INPUT\nFile inputFile;\nDList!string _words;\nvoid _wordsFill()\n{\n while (_words.empty) _words.insertBack(inputFile.readln.split); \n}\nstring popWord()\n{\n _wordsFill;\n auto word = _words.front;\n _words.removeFront;\n return word;\n}\nstring peekWord()\n{\n _wordsFill;\n return _words.front;\n}\nT next(T)()\n{\n return to!T(popWord);\n}\nT peek(T)()\n{\n return to!T(peekWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}], "src_uid": "24afabd9cbbe287ea83c780f1797297c"} {"nl": {"description": "Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $$$k$$$ have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array $$$a$$$ of length $$$n$$$. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between $$$1$$$ and $$$n$$$ inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 50$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 100$$$). The second line of each test case contains $$$n$$$ space-separated integers ($$$1 \\le a_i \\le n$$$) — the array that Phoenix currently has. This array may or may not be already beautiful.", "output_spec": "For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines. The first line should contain the length of the beautiful array $$$m$$$ ($$$n \\le m \\le 10^4$$$). You don't need to minimize $$$m$$$. The second line should contain $$$m$$$ space-separated integers ($$$1 \\le b_i \\le n$$$) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array $$$a$$$. You may print integers that weren't originally in array $$$a$$$. If there are multiple solutions, print any. It's guaranteed that if we can make array $$$a$$$ beautiful, we can always make it with resulting length no more than $$$10^4$$$.", "sample_inputs": ["4\n4 2\n1 2 2 1\n4 3\n1 2 2 1\n3 2\n1 2 3\n4 4\n4 3 4 2"], "sample_outputs": ["5\n1 2 1 2 1\n4\n1 2 2 1\n-1\n7\n4 3 2 1 4 3 2"], "notes": "NoteIn the first test case, we can make array $$$a$$$ beautiful by inserting the integer $$$1$$$ at index $$$3$$$ (in between the two existing $$$2$$$s). Now, all subarrays of length $$$k=2$$$ have the same sum $$$3$$$. There exists many other possible solutions, for example: $$$2, 1, 2, 1, 2, 1$$$ $$$1, 2, 1, 2, 1, 2$$$ In the second test case, the array is already beautiful: all subarrays of length $$$k=3$$$ have the same sum $$$5$$$.In the third test case, it can be shown that we cannot insert numbers to make array $$$a$$$ beautiful.In the fourth test case, the array $$$b$$$ shown is beautiful and all subarrays of length $$$k=4$$$ have the same sum $$$10$$$. There exist other solutions also."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto nk = readln.split.to!(int[]);\n auto N = nk[0];\n auto K = nk[1];\n auto as = readln.split.to!(int[]);\n auto ns = new bool[](100);\n foreach (a; as) ns[a-1] = true;\n int c;\n foreach (n; ns) if (n) ++c;\n if (c > K) {\n writeln(\"-1\");\n continue;\n }\n int[] ii;\n foreach (i, n; ns) if (n) ii ~= i.to!int+1;\n while (ii.length < K) ii ~= ii[0];\n int[] ms;\n size_t i;\n foreach (a; as) {\n while (a != ii[i]) {\n ms ~= ii[i++];\n i %= ii.length;\n }\n ms ~= a;\n ++i;\n i %= ii.length;\n }\n writeln(ms.length);\n writeln(ms.to!(string[]).join(\" \"));\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\treadln;\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tbool [int] vis;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tvis[c] = true;\n\t\t}\n\t\tif (vis.length > k)\n\t\t{\n\t\t\twriteln (-1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (n * k);\n\t\t\twritefln !(\"%(%s %)\")\n\t\t\t (vis.byKey.cycle.take (k).repeat (n).joiner);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto a = RDA!int;\n\n\t\tbool[int] set;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tset[a[i]] = true;\n\t\t}\n\t\tif (set.keys.length > k) continue;\n\n\t\tauto len = (10^^4) / k;\n\t\tlen *= k;\n\t\tans[ti].length = len;\n\t\tauto b = set.keys.dup;\n\t\twhile (b.length < k)\n\t\t\tb ~= 1;\n\t\tforeach (i; 0..len)\n\t\t{\n\t\t\tauto pos = i % k;\n\t\t\tans[ti][i] = b[pos];\n\t\t}\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(-1);\n\t\telse\n\t\t{\n\t\t\twriteln(e.length);\n\t\t\te.map!(to!string).join(\" \").writeln;\n\t\t}\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\treadln;\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tbool [int] vis;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tvis[c] = true;\n\t\t}\n\t\tif (vis.length > k)\n\t\t{\n\t\t\twriteln (-1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (n * vis.length);\n\t\t\twritefln !(\"%(%s %)\") (vis.byKey.repeat (n).joiner);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto a = RDA!int;\n\n\t\tbool[int] set;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tset[a[i]] = true;\n\t\t}\n\t\tif (set.keys.length > k) continue;\n\n\t\tauto len = (10^^4) / k;\n\t\tans[ti].length = len;\n\t\tauto b = set.keys.dup;\n\t\twhile (b.length < k)\n\t\t\tb ~= 1;\n\t\tforeach (i; 0..len)\n\t\t{\n\t\t\tauto pos = i % k;\n\t\t\tans[ti][i] = b[pos];\n\t\t}\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(-1);\n\t\telse\n\t\t{\n\t\t\twriteln(e.length);\n\t\t\te.map!(to!string).join(\" \").writeln;\n\t\t}\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto a = RDA!int;\n\n\t\tbool[int] set;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tset[a[i]] = true;\n\t\t}\n\t\tif (set.keys.length > k) continue;\n\n\t\tauto len = (10^^4) / k;\n\t\tans[ti].length = len;\n\t\tforeach (i; 0..len)\n\t\t{\n\t\t\tauto pos = i % k;\n\t\t\tans[ti][i] = a[pos];\n\t\t}\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(-1);\n\t\telse\n\t\t{\n\t\t\twriteln(e.length);\n\t\t\te.map!(to!string).join(\" \").writeln;\n\t\t}\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto a = RDA!int;\n\n\t\tbool[int] set;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tset[a[i]] = true;\n\t\t}\n\t\tif (set.keys.length > k) continue;\n\n\t\tauto len = (10^^4) / k;\n\t\tans[ti].length = len;\n\t\tauto keys = set.keys;\n\t\tforeach (i; 0..len)\n\t\t{\n\t\t\tauto pos = i % keys.length;\n\t\t\tans[ti][i] = keys[pos];\n\t\t}\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(-1);\n\t\telse\n\t\t{\n\t\t\twriteln(e.length);\n\t\t\te.map!(to!string).join(\" \").writeln;\n\t\t}\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto a = RDA!int;\n\n\t\tans[ti] = a.dup;\n\t\tint i = k;\n\t\twhile (i < ans[ti].length)\n\t\t{\n\t\t\tif (ans[ti][i] == ans[ti][i-k])\n\t\t\t{\n\t\t\t\t++i;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tbool ok;\n\t\t\tforeach (j; i-k..i)\n\t\t\t{\n\t\t\t\tif (ans[ti][j] == ans[ti][i])\n\t\t\t\t{\n\t\t\t\t\tok = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok)\n\t\t\t{\n\t\t\t\tans[ti] = ans[ti][0..i] ~ ans[ti][i-k] ~ ans[ti][i..$];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tans[ti].length = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t++i;\n\t\t\tdebug writeln(ans[ti]);\n\t\t}\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(-1);\n\t\telse\n\t\t{\n\t\t\twriteln(e.length);\n\t\t\te.map!(to!string).join(\" \").writeln;\n\t\t}\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "src_uid": "80d4b2d01215b12ebd89b8ee2d1ac6ed"} {"nl": {"description": "You are given an integer $$$x$$$ of $$$n$$$ digits $$$a_1, a_2, \\ldots, a_n$$$, which make up its decimal notation in order from left to right.Also, you are given a positive integer $$$k < n$$$.Let's call integer $$$b_1, b_2, \\ldots, b_m$$$ beautiful if $$$b_i = b_{i+k}$$$ for each $$$i$$$, such that $$$1 \\leq i \\leq m - k$$$.You need to find the smallest beautiful integer $$$y$$$, such that $$$y \\geq x$$$. ", "input_spec": "The first line of input contains two integers $$$n, k$$$ ($$$2 \\leq n \\leq 200\\,000, 1 \\leq k < n$$$): the number of digits in $$$x$$$ and $$$k$$$. The next line of input contains $$$n$$$ digits $$$a_1, a_2, \\ldots, a_n$$$ ($$$a_1 \\neq 0$$$, $$$0 \\leq a_i \\leq 9$$$): digits of $$$x$$$.", "output_spec": "In the first line print one integer $$$m$$$: the number of digits in $$$y$$$. In the next line print $$$m$$$ digits $$$b_1, b_2, \\ldots, b_m$$$ ($$$b_1 \\neq 0$$$, $$$0 \\leq b_i \\leq 9$$$): digits of $$$y$$$.", "sample_inputs": ["3 2\n353", "4 2\n1234"], "sample_outputs": ["3\n353", "4\n1313"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto s = readln.chomp;\n \n dchar[] ans;\n foreach (i; 0 .. n) { ans ~= s[i % k]; }\n \n debug { ans.writeln; }\n \n foreach (i; k .. n) {\n if (ans[i] > s[i]) { break; }\n if (ans[i] < s[i]) {\n int start = k-1;\n while (s[start] == '9') { --start; }\n foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[start] + 1; }\n foreach (nxt; start + 1 .. k) {\n foreach (j; n.iota.drop(nxt).stride(k)) { ans[j] = '0'; }\n }\n break;\n }\n }\n \n ans.length.writeln;\n ans.writeln;\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const K = readInt();\n const A = readToken();\n \n auto b = new char[N];\n foreach (i; 0 .. N) {\n b[i] = A[i % K];\n }\n if (A > b) {\n b[K - 1] += 1;\n foreach_reverse (i; 0 .. K) {\n if (b[i] > '9') {\n b[i] -= 10;\n b[i - 1] += 1;\n }\n }\n foreach (i; K .. N) {\n b[i] = b[i % K];\n }\n }\n writeln(N);\n writeln(b);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto s = readln.chomp;\n \n dchar[] ans;\n foreach (i; 0 .. n) { ans ~= s[i % k]; }\n \n debug { ans.writeln; }\n \n foreach (i; k .. n) {\n if (ans[i] > s[i]) { break; }\n if (ans[i] < s[i]) {\n int start = k-1;\n while (s[start] == '9') { --start; }\n foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[i]; }\n break;\n }\n }\n \n ans.length.writeln;\n ans.writeln;\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto s = readln.chomp;\n \n dchar[] ans;\n foreach (i; 0 .. n) { ans ~= s[i % k]; }\n \n debug { ans.writeln; }\n \n foreach (i; k .. n) {\n if (ans[i] > s[i]) { break; }\n if (ans[i] < s[i]) {\n int start = k-1;\n while (s[start] == '9') { --start; }\n foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[start] + 1; }\n break;\n }\n }\n \n ans.length.writeln;\n ans.writeln;\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto s = readln.chomp;\n \n dchar[] ans;\n foreach (i; 0 .. n) { ans ~= s[i % k]; }\n \n debug { ans.writeln; }\n \n foreach (i; k .. n) {\n if (ans[i] > s[i]) { break; }\n if (ans[i] < s[i]) {\n int start = k-1;\n while (s[start] == '9') { --start; }\n foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[i]; }\n break;\n }\n }\n \n ans.writeln;\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const K = readInt();\n const A = readToken();\n \n auto b = new char[N];\n foreach (i; 0 .. N) {\n b[i] = A[i % K];\n }\n if (A > b) {\n b[K - 1] += 1;\n foreach_reverse (i; 0 .. K) {\n if (b[i] > '9') {\n b[i] -= 10;\n b[i - 1] += 1;\n }\n }\n foreach (i; K .. N) {\n b[i] = b[i % K];\n }\n }\n writeln(b);\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "646b898ae6b801f7403ad0e9984c88f6"} {"nl": {"description": "Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.", "output_spec": "Print a single integer — the minimum total time needed to turn off all the lights.", "sample_inputs": ["2 2\n0010\n0100", "3 4\n001000\n000010\n000010", "4 3\n01110\n01110\n01110\n01110"], "sample_outputs": ["5", "12", "18"], "notes": "NoteIn the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1] + 2;\n string[] A;\n bool ok = false;\n foreach (i; 0..N) {\n auto t = readln.chomp;\n if (!ok && t.map!(tt => tt == '0').all) continue;\n ok = true;\n A ~= t;\n }\n N = A.length.to!int;\n A.reverse();\n\n int[] L = new int[](N);\n int[] R = new int[](N);\n foreach (i; 0..N) {\n L[i] = M-1, R[i] = 0;\n foreach (j; 0..M) {\n if (A[i][j] == '1') {\n L[i] = min(L[i], j);\n R[i] = max(R[i], j);\n }\n }\n }\n\n if (N == 0) {\n writeln(0);\n return;\n } else if (N == 1) {\n writeln(R[0]);\n return;\n }\n\n auto dp = new int[][](N, 2);\n\n dp[0][0] = R[0] * 2;\n dp[0][1] = M - 1;\n\n foreach (i; 1..N-1) {\n dp[i][0] = min(dp[i-1][0] + R[i] * 2 + 1, dp[i-1][1] + M);\n dp[i][1] = min(dp[i-1][1] + (M - L[i] - 1) * 2 + 1, dp[i-1][0] + M);\n }\n\n int ans = min(dp[N-2][0] + R[N-1] + 1, dp[N-2][1] + (M - L[N-1] - 1) + 1);\n ans.writeln;\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n\n Tuple!(int, int)[] floors;\n foreach (_; 0 .. n) {\n auto s = readln.chomp;\n if (s.all!(x => x == '0')) floors ~= tuple(m+1, 0);\n else {\n auto le = s.countUntil!(x => x == '1').to!int;\n auto r = m+1 - s.retro.countUntil!(x => x == '1').to!int;\n floors ~= tuple(le, r);\n }\n }\n \n debug { floors.writeln; }\n \n auto ansle = 0, ansr = 0;\n foreach (t; floors) {\n auto le = t[0], r = t[1];\n auto nowansle = r + (ansle > 0 ? 1 + min(r + ansle, m+1 - r + ansr) : 0);\n auto nowansr = m+1 - le + (ansle > 0 ? 1 + min(le + ansle, m+1 - le + ansr) : 0);\n \n ansle = nowansle, ansr = nowansr;\n debug { writeln(ansle, ' ', ansr); }\n }\n \n ansle.writeln;\n}"}], "negative_code": [], "src_uid": "55070f8e5dba8a6ec669a53a169e557d"} {"nl": {"description": "Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t): where is obtained from string s, by applying left circular shift i times. For example, ρ(\"AGC\", \"CGT\") =  h(\"AGC\", \"CGT\") + h(\"AGC\", \"GTC\") + h(\"AGC\", \"TCG\") +  h(\"GCA\", \"CGT\") + h(\"GCA\", \"GTC\") + h(\"GCA\", \"TCG\") +  h(\"CAG\", \"CGT\") + h(\"CAG\", \"GTC\") + h(\"CAG\", \"TCG\") =  1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: .Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 105). The second line of the input contains a single string of length n, consisting of characters \"ACGT\".", "output_spec": "Print a single number — the answer modulo 109 + 7.", "sample_inputs": ["1\nC", "2\nAG", "3\nTTT"], "sample_outputs": ["1", "4", "1"], "notes": "NotePlease note that if for two distinct strings t1 and t2 values ρ(s, t1) и ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.In the first sample, there is ρ(\"C\", \"C\") = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0.In the second sample, ρ(\"AG\", \"AG\") = ρ(\"AG\", \"GA\") = ρ(\"AG\", \"AA\") = ρ(\"AG\", \"GG\") = 4.In the third sample, ρ(\"TTT\", \"TTT\") = 27"}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\nimmutable int MD = 10 ^^ 9 + 7;\n\nvoid main()\n{\n readln;\n \n auto s = readln.chomp;\n \n int [char] cnt;\n s.each!(c => ++cnt[c]);\n \n auto mx = cnt.values.maxCount[1];\n \n debug { mx.writeln; }\n \n int ans = 1;\n foreach (_; 0 .. s.length) ans = ans.to!long * mx % MD;\n ans.writeln;\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MOD = 1_000_000_007;\nimmutable string LETTERS = \"ACGT\";\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tstring s = readln ().strip ();\n\t\tint [] a;\n\t\tint m;\n\t\tforeach (c; LETTERS)\n\t\t{\n\t\t\ta ~= s.count (c);\n\t\t\tm = max (m, a[$ - 1]);\n\t\t}\n\t\tstring t;\n\t\tforeach (i; 0..LETTERS.length)\n\t\t{\n\t\t\tif (a[i] == m)\n\t\t\t{\n\t\t\t\tt ~= LETTERS[i];\n\t\t\t}\n\t\t}\n\n\t\tlong res = 1;\n\t\tforeach (i; 0..s.length)\n\t\t{\n\t\t\tres = (res * t.length) % MOD;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nimmutable long MO = 10^^9 + 7;\n\nint N;\nstring S;\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tN = readInt;\n\t\tS = readToken;\n\t\t\n\t\tint[] cnt = new int[26];\n\t\tforeach (s; S) {\n\t\t\t++cnt[s - 'A'];\n\t\t}\n\t\t\n\t\tconst num = cnt.count(cnt.reduce!max);\n\t\tlong ans = 1;\n\t\tforeach (i; 0 .. N) {\n\t\t\t(ans *= num) %= MO;\n\t\t}\n\t\twriteln(ans);\n\t\t\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [], "src_uid": "f35c042f23747988f65c5b5e8d5ddacd"} {"nl": {"description": "You are given two integers $$$A$$$ and $$$B$$$, calculate the number of pairs $$$(a, b)$$$ such that $$$1 \\le a \\le A$$$, $$$1 \\le b \\le B$$$, and the equation $$$a \\cdot b + a + b = conc(a, b)$$$ is true; $$$conc(a, b)$$$ is the concatenation of $$$a$$$ and $$$b$$$ (for example, $$$conc(12, 23) = 1223$$$, $$$conc(100, 11) = 10011$$$). $$$a$$$ and $$$b$$$ should not contain leading zeroes.", "input_spec": "The first line contains $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case contains two integers $$$A$$$ and $$$B$$$ $$$(1 \\le A, B \\le 10^9)$$$.", "output_spec": "Print one integer — the number of pairs $$$(a, b)$$$ such that $$$1 \\le a \\le A$$$, $$$1 \\le b \\le B$$$, and the equation $$$a \\cdot b + a + b = conc(a, b)$$$ is true.", "sample_inputs": ["3\n\n1 11\n\n4 2\n\n191 31415926"], "sample_outputs": ["1\n0\n1337"], "notes": "NoteThere is only one suitable pair in the first test case: $$$a = 1$$$, $$$b = 9$$$ ($$$1 + 9 + 1 \\cdot 9 = 19$$$)."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;\nimport std.typecons, std.functional, std.traits,std.concurrency;\nimport std.algorithm, std.container;\nimport core.bitop, core.time, core.memory;\nimport std.datetime;\nimport std.bitmanip;\nimport std.regex;\n\nvoid main()\n{\n auto N = scanElem;\n foreach(e;scanElem!(long,long)(N))\n {\n auto a = e[0];\n auto b = e[1];\n auto n = b.to!string.length;\n if(b.to!string.any!\"a!='9'\")n--;\n writeln(a*n);\n }\n}\n\n//辞書順順列はiota(1,N),nextPermituionを使う\n\nenum INF = long.max/3;\nenum MOD = 10^^9+7;\n\nT binarySearch(alias F, T)(T ok, T ng, long iter=100)\n{\n alias FF = unaryFun!F;\n foreach(_;0..iter)\n {\n auto n = (ok+ng)/2;\n if(FF(n)){\n ok = n;\n }else{\n ng = n;\n }\n static if(isIntegral!T){\n if(abs(ok-ng)==1)return ok;\n }\n }\n return ok;\n}\n\n//最小を返す\nreal ternarySearch(alias F)(real l, real r, long iter=200)\n{\n alias FF = unaryFun!F;\n foreach(_;0..iter)\n {\n auto nl = lerp(l, r, 1/real(3));\n auto nr = lerp(l, r, 2/real(3));\n if(FF(nl)11)debugPrint!(l,r,nl,nr,()=>FF(nl),()=>FF(nr));\n if(FF(nl)FF(i));\n res = min(FF(i), res);\n }\n return res;\n}\n\nCommonType!(A,B,T) lerp(A,B,T)(A a, B b, T t) pure\n{\n alias C = CommonType!(A,B,T);\n return (C(b)-a)*t+a;\n}\n\nstruct Vector{\n real x, y;\n\n real magnitude()pure\n {\n return sqrt(sqrMagnitude);\n }\n real sqrMagnitude()pure\n {\n return x*x+y*y;\n }\n\n Vector opBinary(string op, T)(inout T v)\n if(isNumeric!T)\n {\n return Vector(mixin(\"x\"~op~\"v\"), mixin(\"y\"~op~\"v\"));\n }\n Vector opBinary(string op)(inout Vector v)\n {\n return Vector(mixin(\"x\"~op~\"v.x\"), mixin(\"y\"~op~\"v.y\"));\n }\n void opOpAssign(string op, T)(inout T v)\n {\n this = mixin(\"this\"~op~\"v\");\n }\n\n static Vector lerp(Vector a, Vector b, real t)pure\n {\n return (b-a)*t+a;\n }\n static Vector normalized(Vector a)pure\n {\n auto r = a.magnitude();\n return Vector(a.x / r, a.y / r);\n }\n static real distance(Vector a, Vector b)pure\n {\n return (a-b).magnitude;\n }\n static real dot(Vector a, Vector b)pure\n {\n return a.x*b.x+a.y*b.y;\n }\n static real cross(Vector a, Vector b)pure\n {\n return a.x*b.y-b.x*a.y;\n }\n}\n\n//ascii文字のみ\nlong[] suffixArray(Char)(const(Char)[] s) pure\nif(isSomeChar!Char)\n// in{assert(s.all!\"0<=a&&a<127\");}\n// do\n{\n s ~= '\\0';\n long[] p = new long[s.length];\n long[] c = new long[s.length];\n {\n long[127] cnt;\n foreach(i;0..s.length)cnt[s[i]]++;\n foreach(i;1..cnt.length)cnt[i] += cnt[i-1];\n foreach(i;0..s.length)p[--cnt[s[i]]] = i;\n long classes=1;\n foreach(i;1..p.length){\n classes += s[p[i]]!=s[p[i-1]]?1:0;\n c[p[i]] = classes-1;\n }\n }\n long[] pn = new long[s.length];\n long[] cn = new long[s.length];\n for(long n=0;(1L<mod(a-(1L< 0){\n// int d = dfs(e.to, t, min(f, e.cap));\n// if(d>0){\n// e.cap -= d;\n// G[e.to][e.rev].cap +=d ;\n// return d;\n// }\n// }\n// }\n// return 0;\n// }\n// int flow = 0;\n// for(;;){\n// int f = dfs(s,t,INF);\n// if(f==0)return flow;\n// flow += f;\n// }\n// }\n\nstruct CombTable2(long MOD)\n{\n static long[] fac;\n static long[] finv;\n static long[] inv;\n this(long n)\n {\n fac = new long[n];\n finv = new long[n];\n inv = new long[n];\n\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n foreach(i;2..n){\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n }\n\n long comb(long n, long k)\n {\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n }\n}\nstruct CombTable(long MOD, long n)\n{\n static long[n] fac;\n static long[n] finv;\n static long[n] inv;\n static this()\n {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n foreach(i;2..n){\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n }\n\n static long comb(long n, long k)\n {\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n }\n}\n\nvoid outLine(List...)(List list)\n{\n foreach(i, v;list)\n {\n static if(isFloatingPoint!(typeof(v)))\n {\n writef(\"%.12f\", v);\n }else{\n write(v);\n }\n if(i+1!=list.length)write(' ');\n }\n writeln;\n}\n\nvoid end(List...)(List list)\n{\n outLine(list);\n end;\n}\nvoid end()\n{\n import core.stdc.stdlib;\n exit(0);\n}\n\nlong sequenceSum(long n) pure\n{\n return n*(n+1)/2;\n}\n\n//HL分解\nstruct HeavyLightDecomposition\n{\n immutable int root = 1;\n\n int[][] edge;\n int[] vid;\n int[] invid;\n int[] parent;\n int[] depth;\n int[] subCount;\n int[] head;\n\n this(long n, long root = 1)\n {\n this(n.to!int, root.to!int);\n }\n this(int n, int root = 1)\n {\n this.root = root;\n n++;\n edge.length = n;\n vid.length = n;\n invid.length = n;\n parent.length = n; parent[] = -1;\n depth.length = n;\n head.length = n; head[root] = root;\n subCount.length = n; subCount[] = 1;\n }\n\n void addEdge(long u, long v)\n {\n addEdge(u.to!int, v.to!int);\n }\n void addEdge(int u, int v)\n {\n edge[u] ~= v;\n edge[v] ~= u;\n }\n\n void build()\n {\n void dfs(int v){\n foreach(ref u; edge[v])\n {\n if(u==parent[v]) continue;\n parent[u] = v;\n depth[u] = depth[v] + 1;\n dfs(u);\n subCount[v] += subCount[u];\n if(edge[v][0]==parent[v]||subCount[u]>subCount[edge[v][0]])\n swap(u, edge[v][0]);\n }\n }\n void dfsHead(int v, ref int pos){\n invid[pos] = v;\n vid[v] = pos;\n pos++;\n foreach(ref u; edge[v])\n {\n if(u==parent[v]) continue;\n\n head[u] = u == edge[v][0] ? head[v] : u;\n dfsHead(u, pos);\n }\n }\n dfs(root);\n int pos;\n dfsHead(root, pos);\n }\n\n long lca(long u, long v)\n {\n return lca(u.to!int, v.to!int);\n }\n int lca(int u, int v)\n {\n while(true){\n if(vid[u]>vid[v]) swap(u,v);\n if(head[u]==head[v]) return u;\n v = parent[head[v]];\n }\n }\n\n long distance(long u, long v) { return distance(u.to!int, v.to!int); }\n long distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u,v)]; }\n\n //idxのn個上の親を返す\n //バグあるかも\n long nParent(long n, long idx){\n return nParent(n.to!int, idx.to!int);\n }\n int nParent(int n, int idx){\n auto u = 0;\n auto v = idx;\n while(true)\n {\n if(vid[u]>vid[v]) swap(u,v);\n\n immutable _u = max(vid[head[v]], vid[u]);\n immutable _v = vid[v] + 1;\n if(_v<=_u+n){\n n -= _v-_u;\n }else{\n return invid[_v-n-1];\n }\n\n if(head[u]==head[v]) return -1;\n v = parent[head[v]];\n }\n }\n\n void each(long u, long v, void delegate(long u, long v) pred)\n {\n each(u.to!int, v.to!int, pred);\n }\n void each(int u, int v, void delegate(int u, int v) pred)\n {\n while(true)\n {\n if(vid[u]>vid[v]) swap(u,v);\n pred(max(vid[head[v]], vid[u]), vid[v] + 1);\n if(head[u]==head[v]) return;\n v = parent[head[v]];\n }\n }\n\n void each(alias pred)(long u, long v)\n if(is(typeof(binaryFun!pred(0L,0L))))\n {\n while(true)\n {\n if(vid[u]>vid[v]) swap(u,v);\n binaryFun!pred(max(vid[head[v]], vid[u]), vid[v] + 1);\n if(head[u]==head[v]) return;\n v = parent[head[v]];\n }\n }\n}\n\n// struct HLD{\n\n// long[][] G;\n// long[] vid, head, sub, par, dep, inv, type;\n\n// void dfs_sz(long v) {\n// foreach(ref u; G[v])\n// if(u==par[v]) swap(u,G[v].back());\n// if(~par[v]) G[v].popBack;\n\n// foreach(ref u; G[v]){\n// par[u]=v;\n// dep[u]=dep[v]+1;\n// dfs_sz(u);\n// sub[v]+=sub[u];\n// if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]);\n// }\n// }\n\n// void dfs_hld(long v,long c,ref long pos) {\n// vid[v]=pos++;\n// inv[vid[v]]=v;\n// type[v]=c;\n// foreach(u; G[v]){\n// if(u==par[v]) continue;\n// head[u]=(u==G[v][0]?head[v]:u);\n// dfs_hld(u,c,pos);\n// }\n// }\n\n// this(long n){\n// G = new long[][n];\n// vid = new long[n]; vid[] = -1;\n// head = new long[n];\n// sub = new long[n]; sub[] = 1;\n// par = new long[n]; par[] = -1;\n// dep = new long[n];\n// inv = new long[n];\n// type = new long[n];\n// }\n\n// void add_edge(long u,long v) {\n// G[u] ~= v;\n// G[v] ~= u;\n// }\n\n// void build(long[] rs = [0]) {\n// long c=0,pos=0;\n// foreach(r; rs){\n// dfs_sz(r);\n// head[r]=r;\n// dfs_hld(r,c++,pos);\n// }\n// }\n\n// long lca(long u,long v){\n// while(1){\n// if(vid[u]>vid[v]) swap(u,v);\n// if(head[u]==head[v]) return u;\n// v=par[head[v]];\n// }\n// }\n\n// long distance(long u,long v){\n// return dep[u]+dep[v]-2*dep[lca(u,v)];\n// }\n\n// // for_each(vertex)\n// // [l, r) <- attention!!\n// void for_each(F)(long u, long v, F f) {\n// while(1){\n// if(vid[u]>vid[v]) swap(u,v);\n// f(max(vid[head[v]],vid[u]),vid[v]+1);\n// if(head[u]!=head[v]) v=par[head[v]];\n// else break;\n// }\n// }\n\n// T for_each(T, Q, F)(long u,long v,T ti,Q q,F f)\n// {\n// T l=ti,r=ti;\n// while(1){\n// if(vid[u]>vid[v]){\n// swap(u,v);\n// swap(l,r);\n// }\n// l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1));\n// if(head[u]!=head[v]) v=par[head[v]];\n// else break;\n// }\n// return f(l,r);\n// }\n\n// // for_each(edge)\n// // [l, r) <- attention!!\n// void for_each_edge(F)(long u, long v,F f) {\n// while(1){\n// if(vid[u]>vid[v]) swap(u,v);\n// if(head[u]!=head[v]){\n// f(vid[head[v]],vid[v]+1);\n// v=par[head[v]];\n// }else{\n// if(u!=v) f(vid[u]+1,vid[v]+1);\n// break;\n// }\n// }\n// }\n// }\n\nstruct SegTree(T, T UNIT, alias pred){\n int n;\n long size;\n T* arr;\n alias F = binaryFun!pred;\n\n this(long size)\n {\n this.size = size;\n n=1;\n while(n=0&&k>=1)\n arr[k]=F(arr[(k<<1)|0],arr[(k<<1)|1]);\n }\n\n T query(long a, long b)\n {\n assert(a>=0&&a=1&&b<=size);\n\n T vl=UNIT,vr=UNIT;\n for(long l=a+n,r=b+n;l>=1,r>>=1)\n {\n if(l&1) vl=F(vl,arr[l++]);\n if(r&1) vr=F(arr[--r],vr);\n }\n return F(vl,vr);\n }\n}\n\nbool isInf(Num)(Num v) pure @nogc\nif(isIntegral!Num)\n{\n return v>=INF/2;\n}\n\nUnqual!M mod(N,M)(N n, M mod) pure @nogc\nif(isIntegral!N&&isIntegral!M)\n{\n return (n%mod+mod)%mod;\n}\n\nlong pow(long a, long n) {\n long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a;\n a = a * a;\n n >>= 1;\n }\n return res;\n}\n\nUnqual!M powmod(A,N,M)(A _a, N _n, M mod)\n{\n Unqual!A a = _a;\n Unqual!N n = _n;\n M res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n}\n\nalias MInt = ModInt!MOD;\n\nstruct ModInt(alias Mod)\nif(isIntegral!(typeof(Mod)) && isPrime(Mod))\n{\n int value;\n\n this(ModInt!Mod v)\n {\n value = v.value;\n }\n this(T)(T v)\n if(isIntegral!T)\n {\n value = mod(v, Mod);\n }\n\n ModInt!Mod opBinaryRight(string op, T)(inout T v) const\n if((op==\"+\"||op==\"-\"||op==\"*\")&&isIntegral!T)\n {\n return typeof(this)(mixin(\"v\"~op~\"value\"));\n }\n ModInt!Mod opBinary(string op, T)(inout T v) const\n if((op==\"+\"||op==\"-\"||op==\"*\")&&(is(T==ModInt!Mod)||isIntegral!T))\n {\n return typeof(this)(mixin(\"value\"~op~\"v\"));\n }\n\n ModInt!Mod opBinaryRight(string op, T)(inout T v) const\n if((op==\"/\")&&isIntegral!T)\n {\n return v * (this^^(Mod-2));\n }\n ModInt!Mod opBinary(string op, T)(inout T v) const\n if((op==\"/\")&&(is(T==ModInt!Mod)||isIntegral!T))\n {\n return this * (typeof(this)(v)^^(Mod-2));\n }\n\n ModInt!Mod opBinary(string op, T)(inout T v) const\n if((op==\"^^\")&&isIntegral!T)\n {\n return typeof(this)(powmod(value, v, Mod));\n }\n\n void opAssign(T)(inout T v)\n if(isIntegral!T)\n {\n this = typeof(this)(v);\n }\n\n void opOpAssign(string op, T)(inout T v)\n {\n this = mixin(\"this\"~op~\"v\");\n }\n\n bool opEquals(T)(const T v) const\n if(is(T==ModInt!Mod)||isIntegral!T)\n {\n return value == typeof(this)(v).value;\n }\n\n long opCast() const {\n return value;\n }\n\n string toString() const {\n return value.to!string;\n }\n}\nunittest\n{\n assert(is(ModInt!MOD));\n assert(is(ModInt!17));\n assert(!is(ModInt!0));\n assert(!is(ModInt!10));\n}\n\nunittest\n{\n alias MInt = ModInt!13;\n MInt value;\n value = MInt(14) + MInt(18);\n assert(value==6);\n value = 14 - MInt(28);\n assert(value==12);\n value = MInt(17) * -19;\n assert(value==2);\n\n value = MInt(7) / 4;\n assert(value==5);\n value = 8 / MInt(4);\n assert(value==2);\n value = 9;\n value /= 4;\n assert(value==12);\n\n assert(MInt(3) ^^ 9 == 1);\n\n value = 29-MInt(16);\n assert(value==0);\n value = MInt(13)*5;\n assert(value==0);\n value = 0;\n assert(value==0);\n\n value = 3;\n value += MInt(11);\n assert(value==1);\n value -= 7;\n assert(value==7);\n value *= MInt(4);\n assert(value==2);\n value = 23;\n assert(value == cast(long)value);\n}\nunittest\n{\n ModInt!MOD value;\n value = MOD-1;\n assert(value==MOD-1);\n value = MOD;\n assert(value==0);\n}\n\nstruct Grid(T){\n private T[] grid;\n size_t width, height;\n private size_t stride;\n\n private this(T[] g, size_t w, size_t h, size_t s)\n {\n grid = g;\n width = w;\n height = h;\n stride = s;\n }\n\n this(long w, long h, T init = T.init)\n {\n auto arr = new T[(w*h).to!int];\n arr[] = init;\n this(arr, w.to!size_t, h.to!size_t, w.to!size_t);\n }\n\n // void fill(T elem){\n // grid[] = elem;\n // }\n\n bool isInRange(long x, long y) const nothrow\n {\n return \n x>=0 &&\n x=0 &&\n y= 0 && end <= this.opDollar!dim); }\n body{\n return [start, end];\n }\n\n Grid!T transpose(){\n auto grid = Grid!T(height, width);\n\n foreach(w; 0..width)\n foreach(h; 0..height)\n {\n grid[h, w] = this[w, h];\n }\n return grid;\n }\n\n long opDollar(long dim : 0)() const { return width; }\n long opDollar(long dim : 1)() const { return height; }\n\n string toString() pure {\n long count;\n foreach(y; 0..height)\n {\n foreach(x; 0..width)\n {\n long eCount = this[x, y].to!string.length;\n static if(is(typeof(this[x,y].isInf)))\n {\n if(this[x,y].isInf)\n {\n eCount = 1;\n }\n }\n static if(is(typeof(this[x,y])==bool))\n {\n eCount = 1;\n }\n count = max(count, eCount);\n }\n }\n count++;\n\n string res = \"\\n\";\n foreach(y; 0..height)\n {\n foreach(x; 0..width)\n {\n string joinStr = this[x, y].to!string;\n static if(is(typeof(this[x,y].isInf)))\n {\n if(this[x,y].isInf)\n {\n joinStr = \"*\";\n }\n }\n static if(is(typeof(this[x,y])==bool))\n {\n joinStr = this[x,y]?\"+\":\"-\";\n }\n foreach(i;0..count-joinStr.length)\n {\n res ~= ' ';\n }\n res ~= joinStr;\n }\n res ~= '\\n';\n }\n return res;\n }\n}\n\nstruct UnionFind{\n private int[] arr;\n int rootCount;\n\n @disable this();\n this(long n){\n arr.length = n.to!int;\n arr[] = -1;\n rootCount = n.to!int;\n }\n\n void merge(long a, long b)\n {\n merge(a.to!int, b.to!int);\n }\n void merge(int a, int b)\n {\n if(same(a,b)) return;\n arr[root(a)] = root(b);\n rootCount--;\n }\n\n bool same(long a, long b)\n {\n return same(a.to!int, b.to!int);\n }\n bool same(int a, int b)\n {\n return root(a)==root(b);\n }\n\n private int root(int i)\n {\n if(arr[i] == -1) return i;\n return arr[i] = root(arr[i]);\n }\n}\n\nunittest{\n assert(is(typeof(UnionFind(10))));\n assert(!is(typeof(UnionFind())));\n\n auto uf = UnionFind(10);\n uf.merge(2,3);\n assert(uf.same(2,3));\n uf.merge(3,4);\n uf.merge(1,5);\n uf.merge(5,6);\n uf.merge(6,7);\n assert(!uf.same(4,7));\n uf.merge(1,2);\n assert(uf.same(4,7));\n}\n\nvoid debugPrint(List...)()\n{\n void _debugPrintElem(alias elem, float rad)()\n {\n import std.experimental.color;\n import std.experimental.color.lab;\n import std.experimental.color.rgb;\n import std.experimental.color.xyz;\n\n enum color = LCh!float(80f, 100f, rad);\n enum rgb = color.convertColor!(Lab!float).convertColor!(XYZ!float).convertColor!(RGB8).tristimulus;\n enum r = rgb[0].value;\n enum g = rgb[1].value;\n enum b = rgb[2].value;\n\n stderr.writef!\"\\033[38;2;%s;%s;%sm\"(r, g, b);\n static if(isSomeFunction!elem)\n {\n stderr.write(\"elem: \", elem(), \" \");\n }else{\n enum name = __traits(identifier, elem);\n stderr.write(name, \": \");\n stderr.write(elem, \" \");\n }\n }\n void _debugPrint(int i, float rad, List...)()\n {\n _debugPrintElem!(List[0], i * rad)();\n static if(List.length>1)\n {\n _debugPrint!(i+1, rad, List[1..$])();\n }\n }\n\n debug(Local)\n {\n _debugPrint!(0, 360f/List.length, List)();\n stderr.writeln(\"\\033[0m\");\n }\n}\n\n\nstruct Stack(Elem){\n private Elem[] array;\n private size_t endIdx;\n\n void insertBack(Elem e)\n {\n if(endIdx==array.length){\n array.length = array.length*2+10;\n }\n (array.ptr)[endIdx++] = e;\n }\n\n void insertBack(Range)(Range range)\n if(is(typeof(array[0] = range.popFront)))\n {\n if(endIdx+range.length>array.length){\n array.length = (endIdx+range.length)*2+10;\n }\n foreach(ref e;range)\n {\n (array.ptr)[endIdx++] = e;\n }\n }\n\n Elem[] opSlice(){\n return array[0..endIdx];\n }\n\n void popBack()\n {\n assert(endIdx!=0);\n endIdx--;\n }\n\n ref Elem back()\n {\n assert(endIdx!=0);\n return (array.ptr)[endIdx-1];\n }\n\n size_t count() const \n {\n return endIdx; \n }\n\n bool empty() const \n {\n return endIdx==0;\n }\n\n void clear()\n {\n endIdx = 0;\n }\n}\n\nGrid!T scanGrid(T=long)(long w, long h, dchar t='.')\n{\n auto grid = Grid!T(w,h);\n foreach(y;0..h)\n {\n foreach(x;0..w)\n {\n grid[x, y] = scanElem!T;\n }\n }\n\n return grid;\n}\n\nGrid!bool scanGridBool(long w, long h, dchar t='.')\n{\n auto grid = Grid!bool(w,h);\n foreach(y;0..h.to!size_t)\n {\n auto line = scanString;\n foreach(x;0..w.to!size_t)\n {\n grid[x, y] = line[x.to!size_t]==t;\n }\n }\n\n return grid;\n}\n\nT[] scanLineArray(T = long)()\n{\n static char[] scanBuf;\n readln(scanBuf);\n return scanBuf.split.to!(T[]);\n}\n\nchar scanChar()\n{\n int c = ' ';\n while (isWhite(c) && c != -1)\n {\n c = getchar;\n }\n return cast(char)c;\n}\n\nT[] scanElem(T=long)(long size)\n{\n T[] list = new T[size.to!size_t];\n foreach(i;0..size.to!size_t)\n {\n list[i] = scanElem!T;\n }\n return list;\n}\n\nT scanElem(T)()\nif(is(T==struct))\n{\n T res;\n foreach(ref field; res.tupleof){\n field = scanElem!(typeof(field));\n }\n return res;\n}\n\nTuple!List[] scanElem(List...)(long size)\n{\n auto list = new Tuple!List[size.to!size_t];\n foreach(i;0..size.to!size_t)\n {\n list[i] = scanElem!List;\n }\n return list;\n}\nTuple!List scanElem(List...)()\n{\n List res;\n foreach(i, e; List){\n res[i] = scanElem!e;\n }\n return tuple(res);\n}\n\nT scanElem(T = long)()\nif(isBasicType!T||isSomeString!T)\n{\n import core.stdc.stdlib;\n static auto scanBuf = appender!(char[])([]);\n\n scanBuf.clear;\n\n int c = ' ';\n while (isWhite(c) && c != -1)\n {\n c = getchar;\n }\n while (!isWhite(c) && c != -1)\n {\n scanBuf ~= cast(char) c;\n c = getchar;\n }\n return scanBuf.data.to!T;\n}\n\nstring scanString(){\n return scanElem!string;\n}\n\nCommonType!(A,B) gcd(A,B)(A a, B b) pure\nif(isIntegral!A&&isIntegral!B)\n{\n if(b==0)return a;\n return gcd(b, a % b);\n}\n\nCommonType!(A,B) lcm(A,B)(A a, B b) pure\nif(isIntegral!A&&isIntegral!B)\n{\n return a / gcd(a, b) * b;\n}\n\nstruct Factor\n{\n long n;\n long c;\n}\n\n//素因数分解\nFactor[] factors(long n) pure\n{\n Factor[] res;\n for (long i = 2; i ^^ 2 <= n; i++)\n {\n if (n % i != 0)\n continue;\n\n long c;\n while (n % i == 0)\n {\n n = n / i;\n c++;\n }\n res ~= Factor(i, c);\n }\n if (n != 1)\n res ~= Factor(n, 1);\n\n return res;\n}\n//約数をすべて列挙\nlong[] divisors(long n) pure\n{\n long[] list;\n void func(Factor[] fs, long n)\n {\n if(fs.empty){\n list ~= n;\n return;\n }\n\n foreach(c; 0..fs[0].c+1)\n {\n func(fs[1..$], n * (fs[0].n ^^ c));\n }\n }\n\n func(factors(n), 1);\n sort(list);\n return list;\n}\n//nまでの素数のリスト\nlong[] primes(long n) pure\n{\n if(n<2)return [];\n\n auto table = new long[cast(size_t)n+1];\n\n long[] res;\n for(size_t i = 2;i<=n;i++)\n {\n if(table[i]==-1) continue;\n for(size_t a = i;a c >= lo);\n if (!p.empty) {\n return false;\n }\n if (!rawRead ()) {\n return true;\n }\n }\n }\n template next(T) if (isSigned!T) {\n T next () {\n if (seekByte (45)) {\n return 0;\n }\n T res;\n ubyte b = nextByte!false ();\n if (b == 45) {\n while (true) {\n b = nextByte!true ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte!true ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n T next () {\n if (seekByte (48)) {\n return 0;\n }\n T res = nextByte!false () - 48;\n while (true) {\n ubyte b = nextByte!true ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n T[] nextA(T) (in int n) {\n auto a = uninitializedArray!(T[]) (n);\n foreach (i; 0 .. n) {\n a[i] = next!T;\n }\n return a;\n }\n}\n\nlong test (in int a, in int b) {\n long res;\n long d = 10;\n while (d - 1 <= b) {\n res += a;\n d *= 10;\n }\n return res;\n}\n\n//a * b + a + b = conc (a, b)\n//a * (b + 1) + b = conc (a, b)\n\nvoid main() {\n auto r = new InputReader ();\n immutable nt = r.next!uint ();\n auto ans = new long[nt];\n foreach (tid; 0 .. nt) {\n int a = r.next!int;\n int b = r.next!int;\n ans[tid] = test (a, b); \n }\n writefln! (\"%(%s\\n%)\")(ans);\n}\n\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto A = RD;\n\t\tauto B = RD;\n\t\tauto x = B;\n\t\tlong digit;\n\t\twhile (x != 0)\n\t\t{\n\t\t\tx /= 10;\n\t\t\t++digit;\n\t\t}\n\t\tdebug writeln(B, \":\", 10^^(digit)-1);\n\t\tif (B == 10^^(digit)-1)\n\t\t{\n\t\t\tans[ti] = A * digit;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tans[ti] = A * (digit-1);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "dc67dd2102c70ea476df642b863ae8d3"} {"nl": {"description": "PolandBall has an undirected simple graph consisting of n vertices. Unfortunately, it has no edges. The graph is very sad because of that. PolandBall wanted to make it happier, adding some red edges. Then, he will add white edges in every remaining place. Therefore, the final graph will be a clique in two colors: white and red. Colorfulness of the graph is a value min(dr, dw), where dr is the diameter of the red subgraph and dw is the diameter of white subgraph. The diameter of a graph is a largest value d such that shortest path between some pair of vertices in it is equal to d. If the graph is not connected, we consider its diameter to be -1.PolandBall wants the final graph to be as neat as possible. He wants the final colorfulness to be equal to k. Can you help him and find any graph which satisfies PolandBall's requests?", "input_spec": "The only one input line contains two integers n and k (2 ≤ n ≤ 1000, 1 ≤ k ≤ 1000), representing graph's size and sought colorfulness.", "output_spec": "If it's impossible to find a suitable graph, print -1. Otherwise, you can output any graph which fulfills PolandBall's requirements. First, output m — the number of red edges in your graph. Then, you should output m lines, each containing two integers ai and bi, (1 ≤ ai, bi ≤ n, ai ≠ bi) which means that there is an undirected red edge between vertices ai and bi. Every red edge should be printed exactly once, you can print the edges and the vertices of every edge in arbitrary order. Remember that PolandBall's graph should remain simple, so no loops or multiple edges are allowed.", "sample_inputs": ["4 1", "5 2"], "sample_outputs": ["-1", "4\n1 2\n2 3\n3 4\n4 5"], "notes": "NoteIn the first sample case, no graph can fulfill PolandBall's requirements.In the second sample case, red graph is a path from 1 to 5. Its diameter is 4. However, white graph has diameter 2, because it consists of edges 1-3, 1-4, 1-5, 2-4, 2-5, 3-5."}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n;\n\tint k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\tdebug {writeln (n, \" \", k, \":\");}\n\t\tif (k < 2)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (k > 3)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 2)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 3)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 4)\n\t\t{\n\t\t\tif (k == 3)\n\t\t\t{\n\t\t\t\twriteln (3);\n\t\t\t\twriteln (1, \" \", 2);\n\t\t\t\twriteln (2, \" \", 3);\n\t\t\t\twriteln (3, \" \", 4);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\twriteln (-1);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tif (k == 3)\n\t\t{\n\t\t\tif (n == 5)\n\t\t\t{\n\t\t\t\twriteln (5);\n\t\t\t\twriteln (1, \" \", 2);\n\t\t\t\twriteln (2, \" \", 3);\n\t\t\t\twriteln (3, \" \", 4);\n\t\t\t\twriteln (2, \" \", 5);\n\t\t\t\twriteln (3, \" \", 5);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint [2] [] ans;\n\t\t\t\tint lo = n / 2;\n\t\t\t\tfor (int i = 0; i < lo; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (int j = i + 1; j < lo; j++)\n\t\t\t\t\t{\n\t\t\t\t\t\tans ~= [i, j];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tans ~= [lo - 1, lo];\n\t\t\t\tfor (int i = lo; i < n; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (int j = i + 1; j < n; j++)\n\t\t\t\t\t{\n\t\t\t\t\t\tans ~= [i, j];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\twriteln (ans.length);\n\t\t\t\tforeach (c; ans)\n\t\t\t\t{\n\t\t\t\t\twriteln (c[0] + 1, \" \", c[1] + 1);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\twriteln (n - 1);\n\t\tfor (int i = 1; i < n; i++)\n\t\t{\n\t\t\twriteln (i, \" \", i + 1);\n\t\t}\n\t\tcontinue;\n\t}\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n;\n\tint k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\tdebug {writeln (n, \" \", k, \":\");}\n\t\tif (k < 2)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (k > 3)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 2)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 3)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 4)\n\t\t{\n\t\t\tif (k == 3)\n\t\t\t{\n\t\t\t\twriteln (3);\n\t\t\t\twriteln (1, \" \", 2);\n\t\t\t\twriteln (2, \" \", 3);\n\t\t\t\twriteln (3, \" \", 4);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\twriteln (-1);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tif (k == 3)\n\t\t{\n\t\t\tif (n == 5)\n\t\t\t{\n\t\t\t\twriteln (5);\n\t\t\t\twriteln (1, \" \", 2);\n\t\t\t\twriteln (2, \" \", 3);\n\t\t\t\twriteln (3, \" \", 4);\n\t\t\t\twriteln (2, \" \", 5);\n\t\t\t\twriteln (3, \" \", 5);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\twriteln (-1);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\twriteln (n - 1);\n\t\tfor (int i = 1; i < n; i++)\n\t\t{\n\t\t\twriteln (i, \" \", i + 1);\n\t\t}\n\t\tcontinue;\n\t}\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n;\n\tint k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\tdebug {writeln (n, \" \", k, \":\");}\n\t\tif (k < 2)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (k > 3)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 2)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 3)\n\t\t{\n\t\t\twriteln (-1);\n\t\t\tcontinue;\n\t\t}\n\t\tif (n == 4)\n\t\t{\n\t\t\tif (k == 3)\n\t\t\t{\n\t\t\t\twriteln (3);\n\t\t\t\twriteln (1, \" \", 2);\n\t\t\t\twriteln (2, \" \", 3);\n\t\t\t\twriteln (3, \" \", 4);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\twriteln (-1);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tif (k == 3)\n\t\t{\n\t\t\tif (n == 5)\n\t\t\t{\n\t\t\t\twriteln (5);\n\t\t\t\twriteln (1, \" \", 2);\n\t\t\t\twriteln (2, \" \", 3);\n\t\t\t\twriteln (3, \" \", 4);\n\t\t\t\twriteln (2, \" \", 5);\n\t\t\t\twriteln (3, \" \", 5);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint [2] [] ans;\n\t\t\t\tint lo = n / 2;\n\t\t\t\tfor (int i = 0; i < lo; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (int j = i + 1; j < lo; j++)\n\t\t\t\t\t{\n\t\t\t\t\t\tans ~= [i, j];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (int i = lo; i < n; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (int j = i + 1; j < n; j++)\n\t\t\t\t\t{\n\t\t\t\t\t\tans ~= [i, j];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\twriteln (ans.length);\n\t\t\t\tforeach (c; ans)\n\t\t\t\t{\n\t\t\t\t\twriteln (c[0] + 1, \" \", c[1] + 1);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\twriteln (n - 1);\n\t\tfor (int i = 1; i < n; i++)\n\t\t{\n\t\t\twriteln (i, \" \", i + 1);\n\t\t}\n\t\tcontinue;\n\t}\n}\n"}], "src_uid": "ed040061e0e9fd41a7cd05bbd8ad32dd"} {"nl": {"description": "Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached $$$100$$$ million subscribers. In order to celebrate this, he posted a video with an interesting problem he couldn't solve yet. Can you help him?You're given a tree — a connected undirected graph consisting of $$$n$$$ vertices connected by $$$n - 1$$$ edges. The tree is rooted at vertex $$$1$$$. A vertex $$$u$$$ is called an ancestor of $$$v$$$ if it lies on the shortest path between the root and $$$v$$$. In particular, a vertex is an ancestor of itself.Each vertex $$$v$$$ is assigned its beauty $$$x_v$$$ — a non-negative integer not larger than $$$10^{12}$$$. This allows us to define the beauty of a path. Let $$$u$$$ be an ancestor of $$$v$$$. Then we define the beauty $$$f(u, v)$$$ as the greatest common divisor of the beauties of all vertices on the shortest path between $$$u$$$ and $$$v$$$. Formally, if $$$u=t_1, t_2, t_3, \\dots, t_k=v$$$ are the vertices on the shortest path between $$$u$$$ and $$$v$$$, then $$$f(u, v) = \\gcd(x_{t_1}, x_{t_2}, \\dots, x_{t_k})$$$. Here, $$$\\gcd$$$ denotes the greatest common divisor of a set of numbers. In particular, $$$f(u, u) = \\gcd(x_u) = x_u$$$.Your task is to find the sum$$$$$$ \\sum_{u\\text{ is an ancestor of }v} f(u, v). $$$$$$As the result might be too large, please output it modulo $$$10^9 + 7$$$.Note that for each $$$y$$$, $$$\\gcd(0, y) = \\gcd(y, 0) = y$$$. In particular, $$$\\gcd(0, 0) = 0$$$.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 100\\,000$$$) — the number of vertices in the tree. The following line contains $$$n$$$ integers $$$x_1, x_2, \\dots, x_n$$$ ($$$0 \\le x_i \\le 10^{12}$$$). The value $$$x_v$$$ denotes the beauty of vertex $$$v$$$. The following $$$n - 1$$$ lines describe the edges of the tree. Each of them contains two integers $$$a, b$$$ ($$$1 \\le a, b \\le n$$$, $$$a \\neq b$$$) — the vertices connected by a single edge.", "output_spec": "Output the sum of the beauties on all paths $$$(u, v)$$$ such that $$$u$$$ is ancestor of $$$v$$$. This sum should be printed modulo $$$10^9 + 7$$$.", "sample_inputs": ["5\n4 5 6 0 8\n1 2\n1 3\n1 4\n4 5", "7\n0 2 3 0 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"], "sample_outputs": ["42", "30"], "notes": "NoteThe following figure shows all $$$10$$$ possible paths for which one endpoint is an ancestor of another endpoint. The sum of beauties of all these paths is equal to $$$42$$$: "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\n\nimmutable int mod = 10 ^^ 9 + 7;\nimmutable int NA = -1;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto x = readln.splitter.map !(to !(long)).array;\n\t\tauto g = new int [] [n];\n\t\tforeach (i; 0..n - 1)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf (\" %s %s\", &u, &v);\n\t\t\tu -= 1;\n\t\t\tv -= 1;\n\t\t\tg[u] ~= v;\n\t\t\tg[v] ~= u;\n\t\t}\n\n\t\tint res = 0;\n\n\t\tvoid recur (int v, int p, int [long] s)\n\t\t{\n\t\t\tint [long] t;\n\t\t\tt[x[v]] = 1;\n\t\t\tforeach (k, w; s)\n\t\t\t{\n\t\t\t\tauto r = gcd (k, x[v]);\n\t\t\t\tt[r] += w;\n\t\t\t}\n\t\t\tforeach (k, w; t)\n\t\t\t{\n\t\t\t\tres = (res + k * w) % mod;\n\t\t\t}\n\t\t\tforeach (u; g[v])\n\t\t\t{\n\t\t\t\tif (u != p)\n\t\t\t\t{\n\t\t\t\t\trecur (u, v, t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\trecur (0, NA, null);\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.conv, std.numeric, std.range, std.stdio, std.string;\n\nvoid main () {\n\tauto n = readln.strip.to !(int);\n\tauto x = readln.splitter.map !(to !(long)).array;\n\tauto g = new int [] [n];\n\tforeach (i; 0..n - 1) {\n\t\tint u, v;\n\t\treadf (\" %s %s\", &u, &v);\n\t\tu -= 1;\n\t\tv -= 1;\n\t\tg[u] ~= v;\n\t\tg[v] ~= u;\n\t}\n\n\tint res = 0;\n\n\tvoid recur (int v, int p, int [long] s) {\n\t\tint [long] t;\n\t\tt[x[v]] = 1;\n\t\tforeach (k, w; s) t[gcd (k, x[v])] += w;\n\t\tforeach (k, w; t) res = (res + k * w) % (10 ^^ 9 + 7);\n\t\tforeach (u; g[v]) if (u != p) recur (u, v, t);\n\t}\n\n\trecur (0, -1, null);\n\twriteln (res);\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return read.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\tlong[] xs = rlong(n);\n\t\n\tNode[] nodes;\n\tforeach(i; 0 .. n) nodes ~= new Node(i, xs[i]);\n\t\n\tforeach(i; 0 .. n - 1){\n\t\tint a = rint - 1, b = rint - 1;\n\t\tnodes[a].nodes ~= nodes[b];\n\t\tnodes[b].nodes ~= nodes[a];\n\t}\n\t\n\tnodes[0].treefy(null);\n\t\n\tFinite ans = nodes[0].calc;\n\t\n\tans.writeln;\n}\n/*\nTree DP.\n\n*/\nclass Node{\n\tint id;\n\tstatic long k;\n\tNode[] nodes;\n\tNode[] kids;\n\tNode parent;\n\tbool isVisited;\n\tlong value;\n\tthis(int id, long value){\n\t\tthis.id = id;\n\t\tthis.value = value;\n\t}\n\tvoid treefy(Node parent){\n\t\tthis.parent = parent;\n\t\tforeach(nd; nodes) if(!parent || nd.id != parent.id) this.kids ~= nd, nd.treefy(this);\n\t}\n\tP[] points;\n\tFinite calc(){\n\t\tFinite res = Finite(0);\n\t\tthis.points = [];\n\t\tP[] parpoints = [];\n\t\tif(parent) parpoints = parent.points;\n\t\tforeach(p; parpoints){\n\t\t\tlong d = gcd(p.value, this.value);\n\t\t\tif(points.length && points[$ - 1].value == d){\n\t\t\t\tpoints[$ - 1] = P(d, points[$ - 1].count + p.count);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpoints ~= P(d, p.count);\n\t\t\t}\n\t\t}\n\t\tpoints ~= P(this.value, 1);\n\t\t\n\t\tforeach(p; this.points){\n\t\t\tres += p.value * p.count;\n\t\t}\n\t\tlog(\"id:\", (id + 1), \"value:\", value, \"points:\", points, \"res:\", res);\n\t\t\n\t\tforeach(kid; kids){\n\t\t\tres += kid.calc();\n\t\t}\n\t\t\n\t\treturn res;\n\t}\n\toverride string toString(){\n\t\treturn \"id:\" ~ id.to!string ~ \" value:\" ~ value.to!string ~ \" kids:\" ~ kids.map!(x => x.id).to!string;\n\t}\n}\nstruct P{\n\tlong value, count;\n}\n\n// --------\n\nimport std.conv;\nstruct Finite{\n\tulong value; static ulong mod = 1_000_000_007;\n\tprivate static ulong[] _inv = [0, 1], _frac = [1, 1], _invfrac = [1, 1];\n\tthis(ulong v){ value = v % mod; }\n\tbool opCast(T: bool)(){ return value != 0; }\n\tFinite opUnary(string s){ ulong v;\n\t\tif(s == \"+\") v = value;\n\t\telse if(s == \"-\") v = mod - value;\n\t\telse if(s == \"++\") v = value + 1;\n\t\telse if(s == \"--\") v = value + mod - 1;\n\t\telse assert(0, \"Operator unary \" ~ s ~ \" not implemented\");\n\t\treturn Finite(v);\n\t}\n\tFinite opBinary(string s)(Finite b){\n\t\treturn opBinary!s(b.value);\n\t}\n\tFinite opBinary(string s)(ulong u){ ulong v;\n\t\tif(s == \"+\") v = value + u;\n\t\telse if(s == \"-\") v = value + mod - u;\n\t\telse if(s == \"*\") v = value * u;\n\t\telse if(s == \"/\") v = value * inv(u);\n\t\telse assert(0, \"Operator \" ~ s ~ \" not implemented\");\n\t\treturn Finite(v);\n\t}\n\tFinite opBinaryRight(string s)(ulong u){ ulong v;\n\t\tif(s == \"+\") v = u + value;\n\t\telse if(s == \"-\") v = u + mod - value;\n\t\telse if(s == \"*\") v = u * value;\n\t\telse if(s == \"/\") v = u * invvalue;\n\t\telse assert(0, \"Operator \" ~ s ~ \" not implemented\");\n\t\treturn Finite(v);\n\t}\n\tFinite opAssign(ulong v){ value = v; return this; }\n\tFinite opOpAssign(string s)(Finite b){\n\t\treturn opOpAssign!s(b.value);\n\t}\n\tFinite opOpAssign(string s)(ulong v){\n\t\tif(s == \"+\") value = (value + v) % mod;\n\t\telse if(s == \"-\") value = (value + mod - v) % mod;\n\t\telse if(s == \"*\") value = (value * v) % mod;\n\t\telse if(s == \"/\") value = (value * inv(v)) % mod;\n\t\telse assert(0, \"Operator \" ~ s ~ \"= not implemented\");\n\t\treturn this;\n\t}\n\tbool opEquals(Finite b){\n\t\treturn(value == b.value);\n\t}\n\tstring toString(){ return value.to!string; }\n\tulong inv(ulong v){\n\t\tassert(v > 0);\n\t\tassert(v < mod);\n\t\twhile(v >= _inv.length){\n\t\t\t_inv ~= _inv[mod % $] * (mod - mod / _inv.length) % mod;\n\t\t}\n\t\treturn _inv[v.to!uint];\n\t}\n\tulong invvalue(){\n\t\treturn inv(value);\n\t}\n\tstatic Finite opCall(ulong v){\n\t\treturn Finite(v);\n\t}\n\t\n}\n\n// ----------\n\nlong gcd(long a, long b){ // OK with 0, non-negative\n\tif(a < 0) return gcd(-a, b);\n\tif(b < 0) return gcd(a, -b);\n\tif(a == 0) return b;\n\tif(b == 0) return a;\n\tif(a % b == 0) return b;\n\tif(a < b) return gcd(b, a);\n\telse return gcd(b, a % b);\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(long M) {\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt opUnary(string op)() const if (op == \"-\") { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const { return mixin(\"ModInt(this) \" ~ op ~ \"= a\"); }\n ModInt opBinaryRight(string op)(long a) const { return mixin(\"ModInt(a) \" ~ op ~ \"= this\"); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10L^^9 + 7;\nalias Mint = ModInt!MO;\n\nenum E = 17;\n\nint N;\nlong[] X;\nint[] A, B;\n\nint[][] G;\nint[][] par;\n\nvoid dfs(int u, int p) {\n par[0][u] = p;\n foreach (v; G[u]) {\n if (v != p) {\n dfs(v, u);\n }\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n X = new long[N];\n foreach (u; 0 .. N) {\n X[u] = readLong();\n }\n A = new int[N - 1];\n B = new int[N - 1];\n foreach (i; 0 .. N - 1) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n G = new int[][N];\n foreach (i; 0 .. N - 1) {\n G[A[i]] ~= B[i];\n G[B[i]] ~= A[i];\n }\n par = new int[][](E, N + 1);\n dfs(0, N);\n par[0][N] = N;\n foreach (e; 0 .. E - 1) {\n foreach (u; 0 .. N + 1) {\n par[e + 1][u] = par[e][par[e][u]];\n }\n }\n \n auto ds = new long[][](E, N);\n foreach (u; 0 .. N) {\n const v = par[0][u];\n ds[0][u] = (v == N) ? 0 : X[v];\n }\n foreach (e; 0 .. E - 1) {\n foreach (u; 0 .. N) {\n const v = par[e][u];\n ds[e + 1][u] = (v == N) ? ds[e][u] : gcd(ds[e][u], ds[e][v]);\n }\n }\n debug {\n foreach (e; 0 .. 4) {\n writeln(par[e]);\n writeln(ds[e]);\n }\n }\n \n Mint ans;\n foreach (u; 0 .. N) {\n long g = X[u];\n int v = u;\n for (; ; ) {\n long sum;\n foreach_reverse (e; 0 .. E) {\n const w = par[e][v];\n if (w != N) {\n // gcd(g, ds[e][v]) == g\n if ((g == 0) ? (ds[e][v] == 0) : (ds[e][v] % g == 0)) {\n sum |= 1 << e;\n v = w;\n }\n }\n }\n ans += g * Mint(sum + 1);\n v = par[0][v];\n if (v == N) {\n break;\n }\n g = gcd(g, X[v]);\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "5179d7554a08d713da7597db41f0ed43"} {"nl": {"description": "Slime has a sequence of positive integers $$$a_1, a_2, \\ldots, a_n$$$.In one operation Orac can choose an arbitrary subsegment $$$[l \\ldots r]$$$ of this sequence and replace all values $$$a_l, a_{l + 1}, \\ldots, a_r$$$ to the value of median of $$$\\{a_l, a_{l + 1}, \\ldots, a_r\\}$$$.In this problem, for the integer multiset $$$s$$$, the median of $$$s$$$ is equal to the $$$\\lfloor \\frac{|s|+1}{2}\\rfloor$$$-th smallest number in it. For example, the median of $$$\\{1,4,4,6,5\\}$$$ is $$$4$$$, and the median of $$$\\{1,7,5,8\\}$$$ is $$$5$$$.Slime wants Orac to make $$$a_1 = a_2 = \\ldots = a_n = k$$$ using these operations.Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.", "input_spec": "The first line of the input is a single integer $$$t$$$: the number of queries. The first line of each query contains two integers $$$n\\ (1\\le n\\le 100\\,000)$$$ and $$$k\\ (1\\le k\\le 10^9)$$$, the second line contains $$$n$$$ positive integers $$$a_1,a_2,\\dots,a_n\\ (1\\le a_i\\le 10^9)$$$ The total sum of $$$n$$$ is at most $$$100\\,000$$$.", "output_spec": "The output should contain $$$t$$$ lines. The $$$i$$$-th line should be equal to 'yes' if it is possible to make all integers $$$k$$$ in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.", "sample_inputs": ["5\n5 3\n1 5 2 6 1\n1 6\n6\n3 2\n1 2 3\n4 3\n3 1 2 3\n10 3\n1 2 3 4 5 6 7 8 9 10"], "sample_outputs": ["no\nyes\nyes\nno\nyes"], "notes": "NoteIn the first query, Orac can't turn all elements into $$$3$$$.In the second query, $$$a_1=6$$$ is already satisfied.In the third query, Orac can select the complete array and turn all elements into $$$2$$$.In the fourth query, Orac can't turn all elements into $$$3$$$.In the fifth query, Orac can select $$$[1,6]$$$ at first and then select $$$[2,10]$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nbool solve (R) (R a, int n, int k)\n{\n\tif (a.length == 1)\n\t{\n\t\treturn k == a.front;\n\t}\n\tif (!a.canFind (k))\n\t{\n\t\treturn false;\n\t}\n\tif (a.length == 2)\n\t{\n\t\treturn sum (a) >= 2 * k;\n\t}\n\tforeach (i; 0..n - 2)\n\t{\n\t\tif ((a[i] >= k) + (a[i + 1] >= k) + (a[i + 2] >= k) >= 2)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid main ()\n{\n\treadln;\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\twriteln (solve (a, n, k) ? \"yes\" : \"no\");\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nbool solve(int[] as) {\n const n = cast(int)(as.length);\n const cnt1 = as.count(1);\n if (cnt1 == 0) {\n return false;\n }\n if (cnt1 == n) {\n return true;\n }\n foreach (i; 0 .. n - 2 + 1) {\n if (as[i] >= 1 && as[i + 1] >= 1) {\n return true;\n }\n }\n foreach (i; 0 .. n - 3 + 1) {\n if (as[i] >= 1 && as[i + 2] >= 1) {\n return true;\n }\n }\n return false;\n}\n\nvoid main() {\n debug {\n enum lim = 8;\n foreach (n; 1 .. lim + 1) {\n auto graph = new int[][3^^n];\n foreach (u; 0 .. 3^^n) {\n auto as = new int[n];\n foreach (i; 0 .. n) {\n as[i] = u / 3^^i % 3;\n }\n foreach (i; 0 .. n) foreach (j; i + 1 .. n + 1) {\n auto bs = as[i .. j].dup;\n bs.sort;\n auto cs = as.dup;\n cs[i .. j] = bs[($ + 1) / 2 - 1];\n int v;\n foreach_reverse (k; 0 .. n) {\n v = v * 3 + cs[k];\n }\n graph[v] ~= u;\n }\n }\n int start;\n foreach_reverse (i; 0 .. n) {\n start = start * 3 + 1;\n }\n DList!int que;\n auto vis = new bool[3^^n];\n vis[start] = true;\n que ~= start;\n for (; !que.empty; ) {\n const u = que.front;\n que.removeFront;\n foreach (v; graph[u]) {\n if (!vis[v]) {\n vis[v] = true;\n que ~= v;\n }\n }\n }\n foreach (u; 0 .. 3^^n) {\n auto as= new int[n];\n foreach (i; 0 .. n) {\n as[i] = u / 3^^i % 3;\n }\n const res = solve(as);\n if (vis[u] != res) {\n foreach (i; 0 .. n) {\n write(as[i]);\n }\n writeln();\n writeln(vis[u], \" \", res);\n assert(false);\n }\n }\n }\n }\n \n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const K = readInt();\n auto A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n auto ss = new int[N];\n foreach (i; 0 .. N) {\n ss[i] = (A[i] < K) ? 0 : (A[i] == K) ? 1 : 2;\n }\n const ans = solve(ss);\n writeln(ans ? \"yes\" : \"no\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto nk = readln.split.to!(int[]);\n auto N = nk[0];\n auto K = nk[1];\n int[] as;\n bool has;\n foreach (a; readln.split.to!(int[])) {\n if (a == K) has = true;\n as ~= a;\n }\n if (!has) {\n writeln(\"no\");\n continue;\n }\n if (N == 1) {\n writeln(has ? \"yes\" : \"no\");\n continue;\n }\n foreach (i, a; as[0..$-1]) {\n if (a == K) {\n if (as[i+1] >= K) goto ok;\n if (i+2 < N && as[i+2] >= K) goto ok;\n } else if (a > K) {\n if (as[i+1] >= K) goto ok;\n if (i+2 < N && as[i+2] >= K) goto ok;\n }\n }\n writeln(\"no\");\n continue;\n ok:\n writeln(\"yes\");\n }\n}"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nbool solve (R) (R a, int n, int k)\n{\n\tif (a.length == 1)\n\t{\n\t\treturn k == a.front;\n\t}\n\tif (!a.canFind (k))\n\t{\n\t\treturn false;\n\t}\n\tforeach (i; 0..n - 2)\n\t{\n\t\tif ((a[i] >= k) + (a[i + 1] >= k) + (a[i + 2] >= k) >= 2)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid main ()\n{\n\treadln;\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\twriteln (solve (a, n, k) ? \"yes\" : \"no\");\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nbool solve (R) (R a, int k)\n{\n\tif (a.length < 2)\n\t{\n\t\treturn k == a.front;\n\t}\n\tint lo = 0;\n\tint me = 0;\n\tint hi = 0;\n\t// k = 3\n\t// 3 x\n\t// 3 1 x\n\t// 3 1 3 v\n\t// 3 5 v\n\tforeach (ref c; a.find (k))\n\t{\n\t\tlo += (c < k);\n\t\tme += (c == k);\n\t\thi += (c > k);\n\t\tint n = lo + me + hi;\n\t\twriteln (lo, \" \", me, \" \", hi);\n\t\tif (n > 1 && lo <= (n - 1) / 2 && lo + me > (n - 1) / 2)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\tif (c == k)\n\t\t{\n\t\t\tlo = 0;\n\t\t\tme = 1;\n\t\t\thi = 0;\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid main ()\n{\n\treadln;\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\twriteln (solve (a, k) || solve (a.retro, k) ? \"yes\" : \"no\");\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nbool solve (R) (R a, int k)\n{\n\tif (a.length < 2)\n\t{\n\t\treturn k == a.front;\n\t}\n\tint lo = 0;\n\tint me = 0;\n\tint hi = 0;\n\tforeach (ref c; a.find (k))\n\t{\n\t\tlo += (c < k);\n\t\tme += (c == k);\n\t\thi += (c > k);\n\t\tint n = lo + me + hi;\n\t\tif (n > 1 && lo <= (n - 1) / 2 && lo + me > (n - 1) / 2)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\tif (c == k)\n\t\t{\n\t\t\tlo = 0;\n\t\t\tme = 1;\n\t\t\thi = 0;\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid main ()\n{\n\treadln;\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\twriteln (solve (a, k) || solve (a.retro, k) ? \"yes\" : \"no\");\n\t}\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto nk = readln.split.to!(int[]);\n auto N = nk[0];\n auto K = nk[1];\n int[] as;\n size_t[] kis = [0];\n foreach (i, a; readln.split.to!(int[])) {\n as ~= a;\n if (a == K) kis ~= i;\n }\n if (N == 1) {\n writeln(as[0] == K ? \"yes\" : \"no\");\n continue;\n }\n if (kis.length == 1) {\n writeln(\"no\");\n continue;\n }\n foreach (i, s; kis[1..$]) {\n int x;\n foreach_reverse (j; kis[i]+1..s) {\n if (as[j] >= K) {\n ++x;\n } else {\n --x;\n }\n if (x >= 0) goto ok;\n }\n }\n kis = kis[1..$] ~ N.to!size_t;\n foreach (i, s; kis[0..$-1]) {\n int x;\n foreach (j; s+1..kis[i+1]) {\n if (as[j] >= K) {\n ++x;\n } else {\n --x;\n }\n if (x >= 0) goto ok;\n }\n }\n writeln(\"no\");\n continue;\n ok:\n writeln(\"yes\");\n }\n}"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto nk = readln.split.to!(int[]);\n auto N = nk[0];\n auto K = nk[1];\n int[] as;\n size_t[] kis = [0];\n foreach (i, a; readln.split.to!(int[])) {\n as ~= a;\n if (a == K) kis ~= i;\n }\n if (N == 1) {\n writeln(as[0] == K ? \"yes\" : \"no\");\n continue;\n }\n if (kis.length == 1) {\n writeln(\"no\");\n continue;\n }\n foreach (i, s; kis[1..$]) {\n int x;\n foreach_reverse (j; kis[i]..s) {\n if (as[j] >= K) {\n ++x;\n } else {\n --x;\n }\n if (x >= 0) goto ok;\n }\n }\n kis ~= (N-1).to!size_t;\n kis = kis[1..$];\n foreach (i, s; kis[0..$-1]) {\n int x;\n foreach (j; s+1..kis[i+1]+1) {\n if (as[j] >= K) {\n ++x;\n } else {\n --x;\n }\n if (x >= 0) goto ok;\n }\n }\n writeln(\"no\");\n continue;\n ok:\n writeln(\"yes\");\n }\n}"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto nk = readln.split.to!(int[]);\n auto N = nk[0];\n auto K = nk[1];\n int[] as;\n size_t[] kis;\n foreach (i, a; readln.split.to!(int[])) {\n as ~= a;\n if (a == K) kis ~= i;\n }\n if (N == 1) {\n writeln(as[0] == K ? \"yes\" : \"no\");\n continue;\n }\n if (kis.empty) {\n writeln(\"no\");\n continue;\n }\n if (kis[0] != 0) {\n int x;\n foreach_reverse (i; 0..kis[0]) {\n if (as[i] >= K) {\n ++x;\n } else {\n --x;\n }\n if (x >= 0) goto ok;\n }\n }\n kis ~= (N-1).to!size_t;\n foreach (i, s; kis[0..$-1]) {\n int x;\n foreach (j; s+1..kis[i+1]+1) {\n if (as[j] >= K) {\n ++x;\n } else {\n --x;\n }\n if (x >= 0) goto ok;\n }\n }\n writeln(\"no\");\n continue;\n ok:\n writeln(\"yes\");\n }\n}"}], "src_uid": "2d988fe01f91847dcad52111c468c0ba"} {"nl": {"description": "Asya loves animals very much. Recently, she purchased $$$n$$$ kittens, enumerated them from $$$1$$$ and $$$n$$$ and then put them into the cage. The cage consists of one row of $$$n$$$ cells, enumerated with integers from $$$1$$$ to $$$n$$$ from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were $$$n - 1$$$ partitions originally. Initially, each cell contained exactly one kitten with some number.Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day $$$i$$$, Asya: Noticed, that the kittens $$$x_i$$$ and $$$y_i$$$, located in neighboring cells want to play together. Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after $$$n - 1$$$ days the cage contained a single cell, having all kittens.For every day, Asya remembers numbers of kittens $$$x_i$$$ and $$$y_i$$$, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into $$$n$$$ cells.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 150\\,000$$$) — the number of kittens. Each of the following $$$n - 1$$$ lines contains integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$, $$$x_i \\ne y_i$$$) — indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens $$$x_i$$$ and $$$y_i$$$ were in the different cells before this day.", "output_spec": "For every cell from $$$1$$$ to $$$n$$$ print a single integer — the index of the kitten from $$$1$$$ to $$$n$$$, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.", "sample_inputs": ["5\n1 4\n2 5\n3 1\n4 5"], "sample_outputs": ["3 1 4 2 5"], "notes": "NoteThe answer for the example contains one of several possible initial arrangements of the kittens.The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. "}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto uf = new UnionFind(N);\n\n foreach (_; 0..N-1) {\n auto s = readln.split.map!(to!int);\n uf.unite(s[0]-1, s[1]-1);\n }\n\n uf.group[uf.find(0)].map!(i => (i + 1).to!string).join(\" \").writeln;\n}\n\nclass UnionFind {\n int N;\n int[] table;\n int[][] group;\n\n this(int n) {\n N = n;\n table = new int[](N);\n fill(table, -1);\n group = N.iota.map!(i => [i]).array;\n }\n\n int find(int x) {\n return table[x] < 0 ? x : (table[x] = find(table[x]));\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (table[x] > table[y]) swap(x, y);\n group[x] ~= group[y];\n group[y] = [];\n table[x] += table[y];\n table[y] = x;\n }\n}\n"}], "negative_code": [], "src_uid": "2c665f95370058bc7fdec405db7d155d"} {"nl": {"description": "Let's define a balanced multiset the following way. Write down the sum of all elements of the multiset in its decimal representation. For each position of that number check if the multiset includes at least one element such that the digit of the element and the digit of the sum at that position are the same. If that holds for every position, then the multiset is balanced. Otherwise it's unbalanced.For example, multiset $$$\\{20, 300, 10001\\}$$$ is balanced and multiset $$$\\{20, 310, 10001\\}$$$ is unbalanced: The red digits mark the elements and the positions for which these elements have the same digit as the sum. The sum of the first multiset is $$$10321$$$, every position has the digit required. The sum of the second multiset is $$$10331$$$ and the second-to-last digit doesn't appear in any number, thus making the multiset unbalanced.You are given an array $$$a_1, a_2, \\dots, a_n$$$, consisting of $$$n$$$ integers.You are asked to perform some queries on it. The queries can be of two types: $$$1~i~x$$$ — replace $$$a_i$$$ with the value $$$x$$$; $$$2~l~r$$$ — find the unbalanced subset of the multiset of the numbers $$$a_l, a_{l + 1}, \\dots, a_r$$$ with the minimum sum, or report that no unbalanced subset exists. Note that the empty multiset is balanced.For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$) — the number of elements in the array and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i < 10^9$$$). Each of the following $$$m$$$ lines contains a query of one of two types: $$$1~i~x$$$ ($$$1 \\le i \\le n$$$, $$$1 \\le x < 10^9$$$) — replace $$$a_i$$$ with the value $$$x$$$; $$$2~l~r$$$ ($$$1 \\le l \\le r \\le n$$$) — find the unbalanced subset of the multiset of the numbers $$$a_l, a_{l + 1}, \\dots, a_r$$$ with the lowest sum, or report that no unbalanced subset exists. It is guaranteed that there is at least one query of the second type.", "output_spec": "For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.", "sample_inputs": ["4 5\n300 10001 20 20\n2 1 3\n1 1 310\n2 1 3\n2 3 3\n2 3 4"], "sample_outputs": ["-1\n330\n-1\n40"], "notes": "NoteAll the subsets of multiset $$$\\{20, 300, 10001\\}$$$ are balanced, thus the answer is -1.The possible unbalanced subsets in the third query are $$$\\{20, 310\\}$$$ and $$$\\{20, 310, 10001\\}$$$. The lowest sum one is $$$\\{20, 310\\}$$$. Note that you are asked to choose a subset, not a subsegment, thus the chosen elements might not be adjancent in the array.The fourth query includes only the empty subset and subset $$$\\{20\\}$$$. Both of them are balanced.The last query includes the empty subset and the subsets $$$\\{20\\}$$$, $$$\\{20\\}$$$ and $$$\\{20, 20\\}$$$. Only $$$\\{20, 20\\}$$$ is unbalanced, its sum is $$$40$$$. Note that you are asked to choose a multiset, thus it might include equal elements."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1];\n auto A = readln.split.map!(to!int).array;\n\n alias SegTree = SegmentTree!(int[2], (a,b)=>a[0]<=b[0]?a:b, [int.max, 0]);\n auto st = new SegTree[](9);\n foreach (i; 0..9) st[i] = new SegTree(N);\n\n pragma(inline, true)\n void set(int idx, int orgval) {\n int val = orgval;\n foreach (i; 0..9) {\n st[i].modify(idx, val % 10 == 0 ? [int.max, idx] : [orgval, idx]);\n val /= 10;\n }\n }\n\n pragma(inline, true)\n int calc(int left, int right) {\n int ans = int.max;\n foreach (i; 0..9) {\n auto t1 = st[i].query(left, right + 1);\n auto m1 = t1[0];\n auto idx = t1[1];\n if (m1 == int.max) continue;\n auto m2 = min(st[i].query(left, idx)[0], st[i].query(idx + 1, right + 1)[0]);\n if (m2 == int.max) continue;\n ans = min(ans, m1 + m2);\n }\n return ans;\n }\n\n foreach (i; 0..N) set(i, A[i]);\n\n while (M--) {\n auto q = readln.split.map!(to!int).array;\n if (q[0] == 1) {\n auto idx = q[1] - 1;\n auto val = q[2];\n set(idx, val);\n } else {\n auto left = q[1] - 1;\n auto right = q[2] - 1;\n int ans = calc(left, right);\n (ans == int.max ? -1 : ans).writeln;\n }\n }\n}\n\nclass SegmentTree(T, alias op, T e) {\n int n;\n T[] table;\n\n this(int n) {\n this.n = n;\n table = new T[](2 * n);\n table[] = e;\n }\n\n void modify(int p, T value) {\n for (table[p += n] = value; p > 1; p >>= 1) table[p>>1] = op(table[p], table[p^1]);\n }\n\n T query(int l, int r) { //[l, r)\n if (r <= l) return e;\n T res = e;\n for (l += n, r += n; l < r; l >>= 1, r >>= 1) {\n if (l&1) res = op(res, table[l++]);\n if (r&1) res = op(res, table[--r]);\n }\n return res;\n }\n}"}], "negative_code": [], "src_uid": "c01a5ed6a598a1c443611a8f1b1a3db7"} {"nl": {"description": "One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company. Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 =  - 1, for all other people the condition 1 ≤ pi < i is fulfilled.", "output_spec": "Print a single integer — the maximum possible efficiency of the workgroup.", "sample_inputs": ["7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2"], "sample_outputs": ["17"], "notes": "NoteIn the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\n\nimmutable int NA = -1;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [] [n];\n\t\tauto p = new int [n];\n\t\tauto c = new int [n];\n\t\tint r = NA;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %s %s\", &p[i], &c[i]);\n\t\t\tif (p[i] == NA)\n\t\t\t{\n\t\t\t\tr = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tp[i]--;\n\t\t\t\ta[p[i]] ~= i;\n\t\t\t}\n\t\t}\n\t\tassert (r != NA);\n\n\t\tauto d = new long [2] [n];\n\n\t\tforeach_reverse (v; 0..n)\n\t\t{\n\t\t\td[v][0] = 0;\n\t\t\td[v][1] = long.min / 2;\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\tauto d0 = d[v][0];\n\t\t\t\tauto d1 = d[v][1];\n\t\t\t\td[v][0] = max (d0 + d[u][0], d1 + d[u][1]);\n\t\t\t\td[v][1] = max (d0 + d[u][1], d1 + d[u][0]);\n\t\t\t}\n\t\t\td[v][1] = max (d[v][1], d[v][0] + c[v]);\n\t\t}\n\n\t\twriteln (max (d[r][0], d[r][1]));\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\n\nimmutable int NA = -1;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [] [n];\n\t\tauto p = new int [n];\n\t\tauto c = new int [n];\n\t\tint r = NA;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %s %s\", &p[i], &c[i]);\n\t\t\tif (p[i] == NA)\n\t\t\t{\n\t\t\t\tr = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tp[i]--;\n\t\t\t\ta[p[i]] ~= i;\n\t\t\t}\n\t\t}\n\t\tassert (r != NA);\n\n\t\tauto d = new long [2] [n];\n\n\t\tvoid recur (int v)\n\t\t{\n\t\t\td[v][0] = 0;\n\t\t\td[v][1] = long.min / 2;\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\trecur (u);\n\t\t\t\tauto d0 = d[v][0];\n\t\t\t\tauto d1 = d[v][1];\n\t\t\t\td[v][0] = max (d0 + d[u][0], d1 + d[u][1]);\n\t\t\t\td[v][1] = max (d0 + d[u][1], d1 + d[u][0]);\n\t\t\t}\n\t\t\tauto d0 = d[v][0];\n\t\t\tauto d1 = d[v][1];\n\t\t\td[v][0] = max (d0, d1);\n\t\t\td[v][1] = d0 + c[v];\n\t\t}\n\n\t\trecur (r);\n\t\twriteln (max (d[r][0], d[r][1]));\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\n\nimmutable int NA = -1;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [] [n];\n\t\tauto p = new int [n];\n\t\tauto c = new long [n];\n\t\tint r = NA;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %s %s\", &p[i], &c[i]);\n\t\t\tif (p[i] == NA)\n\t\t\t{\n\t\t\t\tr = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tp[i]--;\n\t\t\t\ta[p[i]] ~= i;\n\t\t\t}\n\t\t}\n\t\tassert (r != NA);\n\n\t\tlong res = 0;\n\t\tauto d = new long [n];\n\n\t\tvoid recur (int v)\n\t\t{\n\t\t\tlong [] z;\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\trecur (u);\n\t\t\t\tz ~= d[u];\n\t\t\t}\n\t\t\tsort (z);\n\t\t\td[v] = c[v] + sum (z[$ & 1..$]);\n\t\t\tres = max (res, d[v]);\n\t\t}\n\n\t\trecur (r);\n\t\twriteln (res);\n\t}\n}\n"}], "src_uid": "3b4100b844e925af971061df42521712"} {"nl": {"description": "A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: at first, the text of the comment is written; after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.For example, if the comments look like: then the first comment is written as \"hello,2,ok,0,bye,0\", the second is written as \"test,0\", the third comment is written as \"one,1,two,2,a,0,b,0\". The whole comments feed is written as: \"hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\". For a given comments feed in the format specified above print the comments in a different format: at first, print a integer d — the maximum depth of nesting comments; after that print d lines, the i-th of them corresponds to nesting level i; for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. ", "input_spec": "The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. ", "output_spec": "Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.", "sample_inputs": ["hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0", "a,5,A,0,a,0,A,0,a,0,A,0", "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0"], "sample_outputs": ["3\nhello test one \nok bye two \na b", "2\na \nA a A a A", "4\nA K M \nB F H L N O \nC D G I P \nE J"], "notes": "NoteThe first example is explained in the statements. "}, "positive_code": [{"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nint pos;\nstring s;\nstring[ ][ ] result;\nint resDepth;\n\nvoid parse(int depth) {\n assert(pos < s.length);\n resDepth = max(resDepth, depth);\n if (depth == result.length)\n result ~= cast(string[ ])null;\n int comma = cast(int)(s.length - s[pos .. $].find(',').length);\n result[depth] ~= s[pos .. comma];\n pos = comma + 1;\n comma = cast(int)(s.length - s[pos .. $].find(',').length);\n int count = s[pos .. comma].to!int;\n pos = comma + 1;\n foreach (i; 0 .. count)\n parse(depth + 1);\n}\n\nvoid main() {\n while ((s = readln()).length > 1) {\n s = s[0 .. $ - 1];\n result = null;\n resDepth = 0;\n pos = 0;\n while (pos < s.length)\n parse(0);\n writeln(resDepth + 1);\n writefln(\"%(%-(%s %)\\n%)\", result);\n }\n}\n"}, {"source_code": "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,\nstd.random,std.uni,std.traits,std.variant;\nimport core.stdc.stdio:freopen;\nimport std.stdio;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;\nalias redBlackTree rbt;\nalias BigInt big;\nalias pair!(int,int) pii;\nalias pair!(long,long) pll;\nalias BoyerMooreFinder strfind;\nimmutable int mod=1000000007;\n@property pure nothrow T lcm (T)(auto ref T a,auto ref T b)\n{\n\treturn a/gcd(a,b)*b;\n}\npure nothrow @property X binpow(X,Y)(X base,Y exp)\nif(is(typeof(exp&1)))\n{\n\tif(exp<0)return X(0);\n\tX res=X(1);\n\twhile(exp>0)\n\t{\n\t\tif(exp&1)res*=base;\n\t\tbase*=base;\n\t\texp>>=1;\n\t}\n\treturn res;\n}\npure nothrow @property auto binpow(X,Y,Z)(X base,Y exp,in Z mm)\nif(is(typeof(exp&1)))\n{\n\tif(mm==0) return binpow(base,exp);\n\tif(exp<0)return X(0);\n\tauto res=X(1)%mm;\n\twhile(exp>0)\n\t{\n\t\tif(exp&1)res=(res*base)%mm;\n\t\tbase*=base;\n\t\tbase%=mm;\n\t\texp>>=1;\n\t}\n\treturn res;\n}\n@property void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}\n@property void putarr(X)(X a,in int n,in int m)\n{\n\tforeach(i;0..n)\n\t{\n\t\tforeach(j;0..m)write(a[i][j],' ');\n\t\twriteln;\n\t}\n}\n@property bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}\nbool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nstruct pair(X,Y)\n{\n\tX first;\n\tY second;\n\talias first fi;\n\talias second se;\n\tthis(X a_,Y b_){\n\t\tfi=a_;\n\t\tse=b_;\n\t}\n\tthis(this){fi=fi;se=se;}\n\tthis(A,B)(auto ref pair!(A,B) p)\n\t{\n\t\tfi=p.fi;\n\t\tse=p.se;\n\t}\n\tint opCmp(in pair!(X,Y) s_) const pure nothrow @safe\n\t{\n\t\tint cmp=0-(fis_.fi);\n\t\tif(cmp!=0)return cmp;\n\t\telse return (0-(ses_.se));\n\t}\n};\npair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe\n{\n\tpair!(X,Y) pp;\n\tpp.fi=x_;\n\tpp.se=y_;\n\treturn pp;\n}\nX sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}\nX cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}\nnothrow @nogc @system void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow @nogc @system void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\nauto arread(T)(){return readln.split.map!(to!(T)).array;}\nnothrow @property pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t{\n\t\tauto m=(l+r)>>1;\n\t\tif(!(a[m]1)\n\t{\n\t\tauto m=(l+r)>>1;\n\t\tif(g1)\n\t{\n\t\tauto m=(l+r)>>1;\n\t\tif(!(a[m] 0)\n\t{\n\t\treadln;\n\n\t\talias Entry = Tuple !(int, q{s}, int, q{m});\n\t\tauto t = new Entry [maxN];\n\t\tint total = 0;\n\n\t\tvoid add (int pos, int delta)\n\t\t{\n\t\t\tfor (pos += medN; pos > 0; pos >>= 1)\n\t\t\t{\n\t\t\t\tt[pos].s += delta;\n\t\t\t\tif (pos < medN)\n\t\t\t\t{\n\t\t\t\t\tt[pos].m = min (t[pos * 2 + 0].m,\n\t\t\t\t\t t[pos * 2 + 0].s +\n\t\t\t\t\t t[pos * 2 + 1].m);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tt[pos].m = min (0, t[pos].s);\n\t\t\t\t}\n\t\t\t}\n\t\t\ttotal += delta;\n\t\t}\n\n\t\tauto x = new int [n];\n\t\tforeach (r; 0..n)\n\t\t{\n\t\t\tauto v = readln.splitter.map !(to !(int)).array;\n\t\t\tint pos = v[0] - 1;\n\t\t\tint type = v[1];\n\t\t\tif (type == 0)\n\t\t\t{\n\t\t\t\tadd (pos, -1);\n\t\t\t}\n\t\t\telse if (type == 1)\n\t\t\t{\n\t\t\t\tx[pos] = v[2];\n\t\t\t\tadd (pos, +1);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert (false);\n\t\t\t}\n\n\t\t\tint level = 0;\n\t\t\tint cur = 1;\n\t\t\twhile (cur < medN)\n\t\t\t{\n\t\t\t\tint next0 = cur * 2 + 0;\n\t\t\t\tint next1 = cur * 2 + 1;\n\t\t\t\tif (level + t[next0].s + t[next1].m < total)\n\t\t\t\t{\n\t\t\t\t\tlevel += t[next0].s;\n\t\t\t\t\tcur = next1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tcur = next0;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (level == total - 1)\n\t\t\t{\n\t\t\t\twriteln (x[cur - medN]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\twriteln (-1);\n\t\t\t}\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "3e8433d848e7a0be5b38ea0377e35b2b"} {"nl": {"description": "Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and repeatedly presses the keys that need to be pressed only once. For example, instead of the phrase \"how are you\" he can type \"hhoow aaaare yyoouu\". Polycarp decided to automate the process of correcting such errors. He decided to write a plug-in to the text editor that will remove pairs of identical consecutive letters (if there are any in the text). Of course, this is not exactly what Polycarp needs, but he's got to start from something! Help Polycarp and write the main plug-in module. Your program should remove from a string all pairs of identical letters, which are consecutive. If after the removal there appear new pairs, the program should remove them as well. Technically, its work should be equivalent to the following: while the string contains a pair of consecutive identical letters, the pair should be deleted. Note that deleting of the consecutive identical letters can be done in any order, as any order leads to the same result. ", "input_spec": "The input data consists of a single line to be processed. The length of the line is from 1 to 2·105 characters inclusive. The string contains only lowercase Latin letters. ", "output_spec": "Print the given string after it is processed. It is guaranteed that the result will contain at least one character.", "sample_inputs": ["hhoowaaaareyyoouu", "reallazy", "abacabaabacabaa"], "sample_outputs": ["wre", "rezy", "a"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.container;\n\nvoid main() {\n string s = stdin.readln;\n Array!char a;\n foreach (c; s) {\n if (a.empty) { a.insert(c); }\n else if (a.back == c) { a.removeBack; }\n else { a.insert(c); }\n }\n foreach (c; a) {\n write(c);\n }\n}\n"}, {"source_code": "module cf_81A;\n\nimport std.stdio;\n\nvoid main() {\n string text;\n\n readf(\"%s\\n\", &text);\n\n string optText = \"\";\n\n while (text.length > 0) {\n if (optText.length == 0) {\n optText ~= text[0];\n } else if (optText[$ - 1] == text[0]) {\n optText = optText[0 .. $ - 1];\n } else {\n optText ~= text[0];\n }\n\n text = text[1 .. $];\n }\n\n writeln(optText);\n}"}], "negative_code": [], "src_uid": "26f1b8c3cb83603c904e1508df4067f4"} {"nl": {"description": "This is an interactive problem.Anton and Harris are playing a game to decide which of them is the king of problemsetting.There are three piles of stones, initially containing $$$a$$$, $$$b$$$, and $$$c$$$ stones, where $$$a$$$, $$$b$$$, and $$$c$$$ are distinct positive integers. On each turn of the game, the following sequence of events takes place: The first player chooses a positive integer $$$y$$$ and provides it to the second player. The second player adds $$$y$$$ stones to one of the piles, with the condition that he cannot choose the same pile in two consecutive turns. The second player loses if, at any point, two of the piles contain the same number of stones. The first player loses if $$$1000$$$ turns have passed without the second player losing.Feeling confident in his skills, Anton decided to let Harris choose whether he wants to go first or second. Help Harris defeat Anton and become the king of problemsetting!", "input_spec": "The first line of input contains three distinct positive integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \\le a, b, c \\le 10^9$$$)  — the initial number of stones in piles $$$1$$$, $$$2$$$, and $$$3$$$ respectively.", "output_spec": null, "sample_inputs": ["5 2 6\n\n\n3\n\n0"], "sample_outputs": ["First\n2\n\n3"], "notes": "NoteIn the sample input, the piles initially have $$$5$$$, $$$2$$$, and $$$6$$$ stones. Harris decides to go first and provides the number $$$2$$$ to Anton. Anton adds $$$2$$$ stones to the third pile, which results in $$$5$$$, $$$2$$$, and $$$8$$$.In the next turn, Harris chooses $$$3$$$. Note that Anton cannot add the stones to the third pile since he chose the third pile in the previous turn. Anton realizes that he has no valid moves left and reluctantly recognizes Harris as the king."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto a = readln.splitter.map !(to !(long)).array;\n\tauto p = 3.iota.array;\n\twriteln (\"First\");\n\tstdout.flush ();\n\twhile (true)\n\t{\n\t\tschwartzSort !(i => a[i]) (p);\n\t\tlong delta = a[p[2]] - a[p[0]] + a[p[2]] - a[p[1]];\n\t\tif (a[p[2]] - a[p[1]] == a[p[1]] - a[p[0]])\n\t\t{\n\t\t\tdelta = a[p[2]] - a[p[1]];\n\t\t}\n\t\twriteln (delta);\n\t\tstdout.flush ();\n\t\tauto k = readln.strip.to !(int);\n\t\tif (k == 0)\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\ta[k - 1] += delta;\n\t\tdebug {stderr.writeln (a);}\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n auto A = new long[3];\n foreach (i; 0 .. 3) {\n A[i] = readLong();\n }\n writeln(\"First\");\n stdout.flush;\n \n enum ini = 10L^^10;\n writeln(ini);\n stdout.flush;\n const i0 = readInt() - 1;\n if (i0 == -1) {\n return;\n }\n A[i0] += ini;\n debug {\n writeln(\"A = \", A);\n }\n \n auto a = A.dup;\n a.sort;\n const magic = (a[1] - a[0]) + 2 * (a[2] - a[1]);\n writeln(magic);\n stdout.flush;\n const i1 = readInt() - 1;\n if (i1 == -1) {\n return;\n }\n A[i1] += magic;\n debug {\n writeln(\"A = \", A);\n }\n \n auto b = A.dup;\n b.sort;\n assert(b[1] - b[0] == b[2] - b[1]);\n const diff = b[1] - b[0];\n writeln(diff);\n stdout.flush;\n const i2 = readInt() - 1;\n if (i2 == -1) {\n return;\n }\n assert(false);\n}\n"}], "negative_code": [], "src_uid": "351c6fb0a9d1bbb29387c5b7ce8c7f28"} {"nl": {"description": "Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the rating of i-th student as ai. After the contest ends, every student will end up with some positive integer position. GukiZ expects that his students will take places according to their ratings. He thinks that each student will take place equal to . In particular, if student A has rating strictly lower then student B, A will get the strictly better position than B, and if two students have equal ratings, they will share the same position. GukiZ would like you to reconstruct the results by following his expectations. Help him and determine the position after the end of the contest for each of his students if everything goes as expected.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students. The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).", "output_spec": "In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.", "sample_inputs": ["3\n1 3 3", "1\n1", "5\n3 5 3 4 5"], "sample_outputs": ["3 1 1", "1", "4 1 4 3 1"], "notes": "NoteIn the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating.In the second sample, first student is the only one on the contest.In the third sample, students 2 and 5 share the first position with highest rating, student 4 is next with third position, and students 1 and 3 are the last sharing fourth position."}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string read_string() {\n while (tokens.empty) {\n tokens = readln.split;\n }\n auto token = tokens.front;\n tokens.popFront;\n return token;\n }\n \n int read_int() {\n return read_string.to!int;\n }\n\n double read_double() {\n return read_string.to!double;\n }\n \n string[] tokens;\n}\n\nint count_greater(ref int[] a1, int n) {\n int count = 0;\n for (int i = 0; i < a1.length; i++) {\n if (a1[i] > n) {\n count++;\n }\n }\n return count;\n}\n\nvoid main() {\n IO cin;\n int t = 1;\n // int t = cin.read_int;\n while (t--) {\n int n = cin.read_int;\n int[] arr = new int[n];\n foreach (ref i; arr) {\n i = cin.read_int;\n }\n for (int i = 0; i < n; i++) {\n write(count_greater(arr, arr[i]) + 1, \" \");\n }\n writeln();\n } \n}"}, {"source_code": "version (all) {\nimport std.math,\n std.conv,\n std.stdio,\n std.ascii,\n std.range,\n std.array,\n std.regex,\n std.format,\n std.bigint,\n std.traits,\n std.random,\n std.string,\n std.numeric,\n std.variant,\n std.typecons,\n std.container,\n std.algorithm,\n std.typetuple,\n std.exception,\n core.checkedint;\n}\n\nvoid main() {\n\n\treadln.strip;\n\tauto a = readln.split.map!(to!uint).array;\n\n\tforeach (el; a) {\n\t\twrite(1 + a.filter!(c => c > el).array.length, ' ');\n\t}\n\n\twriteln();\n}"}, {"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.conv;\nimport std.array;\nimport std.range;\nimport std.ascii; \nimport std.functional;\n\nalias isEven = unaryFun!(\"(a & 1) == 0\");\nalias isOdd = unaryFun!(\"(a & 1) != 0\");\n\nvoid readInput() \n{\n\tsize_t lineSize;\n\n\tauto studentCount = stdin.readln.chomp().to!int();\n\t\n\tauto grades = stdin.readln().split().map!(a => to!int(a)).array;\n\t \n\tint[int] indexHolder;\n\tauto realGrades = grades.dup;\n\tgrades.sort!\"a > b\"();\n\tint lastGrade = 0;\n\tint lastElem = 0;\n\n\tforeach (i, elem; grades)\n\t{\n\t\tif ( lastElem != elem)\n\t\t{\n\t\t\tlastElem = elem;\n\t\t\tlastGrade = i + 1;\n\t\t\tindexHolder[elem] = lastGrade;\n\t\t}\n\t} \n\n\twriteln(realGrades.map!( a => to!string(indexHolder[a])).joiner(\" \"));\n\t\n}\n\nvoid main( string[] args )\n{\n\treadInput();\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.conv;\nimport std.array;\nimport std.range;\nimport std.ascii; \nimport std.functional;\n\nalias isEven = unaryFun!(\"(a & 1) == 0\");\nalias isOdd = unaryFun!(\"(a & 1) != 0\");\n\nvoid readInput() \n{\n\tsize_t lineSize;\n\n\tauto studentCount = stdin.readln.chomp().to!int();\n\t\n\tauto grades = stdin.readln().split().map!(a => to!int(a)).array;\n\t \n\tint[int] indexHolder;\n\tauto realGrades = grades.dup;\n\tgrades.sort!\"a > b\"();\n\tint lastGrade = 0;\n\tint lastElem = 0;\n\n\tforeach (i, elem; grades)\n\t{\n\t\tif ( lastElem != elem)\n\t\t{\n\t\t\tlastElem = elem;\n\t\t\tlastGrade = i + 1;\n\t\t\tindexHolder[elem] = lastGrade;\n\t\t}\n\t} \n\n\twriteln(realGrades.map!( a => indexHolder[a]));\n\t\n}\n\nvoid main( string[] args )\n{\n\treadInput();\n}"}, {"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.conv;\nimport std.array;\nimport std.range;\nimport std.ascii; \nimport std.functional;\n\nalias isEven = unaryFun!(\"(a & 1) == 0\");\nalias isOdd = unaryFun!(\"(a & 1) != 0\");\n\nvoid readInput() \n{\n\tsize_t lineSize; \n\n\tauto studentCount = stdin.readln.chomp().to!int();\n\t\n\tauto grades = stdin.readln().split().map!(a => to!int(a)).array;\n\t\n\tint[int] indexHolder;\n\tauto realGrades = grades.dup;\n\tgrades.sort!\"a > b\"();\n\tint lastGrade = 0;\n\tint lastElem = 0;\n\n\tforeach (i, elem; grades)\n\t{\n\t\tif ( lastElem != elem)\n\t\t{\n\t\t\tlastElem = elem;\n\t\t\tlastGrade = i + 1;\n\t\t\tindexHolder[elem] = lastGrade;\n\t\t}\n\t} \n\n\twriteln(realGrades.map!( a => indexHolder[a]));\n\t\n}\n\nvoid main( string[] args )\n{\n\treadInput();\n}"}, {"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.conv;\nimport std.array;\nimport std.range;\nimport std.ascii; \nimport std.functional;\n\nalias isEven = unaryFun!(\"(a & 1) == 0\");\nalias isOdd = unaryFun!(\"(a & 1) != 0\");\n\nvoid readInput() \n{\n\tsize_t lineSize; ; \n\n\tauto studentCount = stdin.readln.chomp().to!int();\n\t\n\tauto grades = stdin.readln().split().map!(a => to!int(a)).array;\n\t\n\tint[int] indexHolder;\n\tauto realGrades = grades.dup;\n\tgrades.sort!\"a > b\"();\n\tint lastGrade = 0;\n\tint lastElem = 0;\n\n\tforeach (i, elem; grades)\n\t{\n\t\tif ( lastElem != elem)\n\t\t{\n\t\t\tlastElem = elem;\n\t\t\tlastGrade = i + 1;\n\t\t\tindexHolder[elem] = lastGrade;\n\t\t}\n\t} \n\n\twriteln(realGrades.map!( a => indexHolder[a]));\n\t\n}\n\nvoid main( string[] args )\n{\n\treadInput();\n}"}], "src_uid": "a5edbf422616cdac35e95a4f07efc7fd"} {"nl": {"description": "$$$2^k$$$ teams participate in a playoff tournament. The tournament consists of $$$2^k - 1$$$ games. They are held as follows: first of all, the teams are split into pairs: team $$$1$$$ plays against team $$$2$$$, team $$$3$$$ plays against team $$$4$$$ (exactly in this order), and so on (so, $$$2^{k-1}$$$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $$$2^{k-1}$$$ teams remain. If only one team remains, it is declared the champion; otherwise, $$$2^{k-2}$$$ games are played: in the first one of them, the winner of the game \"$$$1$$$ vs $$$2$$$\" plays against the winner of the game \"$$$3$$$ vs $$$4$$$\", then the winner of the game \"$$$5$$$ vs $$$6$$$\" plays against the winner of the game \"$$$7$$$ vs $$$8$$$\", and so on. This process repeats until only one team remains.For example, this picture describes the chronological order of games with $$$k = 3$$$: Let the string $$$s$$$ consisting of $$$2^k - 1$$$ characters describe the results of the games in chronological order as follows: if $$$s_i$$$ is 0, then the team with lower index wins the $$$i$$$-th game; if $$$s_i$$$ is 1, then the team with greater index wins the $$$i$$$-th game; if $$$s_i$$$ is ?, then the result of the $$$i$$$-th game is unknown (any team could win this game). Let $$$f(s)$$$ be the number of possible winners of the tournament described by the string $$$s$$$. A team $$$i$$$ is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team $$$i$$$ is the champion.You are given the initial state of the string $$$s$$$. You have to process $$$q$$$ queries of the following form: $$$p$$$ $$$c$$$ — replace $$$s_p$$$ with character $$$c$$$, and print $$$f(s)$$$ as the result of the query. ", "input_spec": "The first line contains one integer $$$k$$$ ($$$1 \\le k \\le 18$$$). The second line contains a string consisting of $$$2^k - 1$$$ characters — the initial state of the string $$$s$$$. Each character is either ?, 0, or 1. The third line contains one integer $$$q$$$ ($$$1 \\le q \\le 2 \\cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, the $$$i$$$-th line contains an integer $$$p$$$ and a character $$$c$$$ ($$$1 \\le p \\le 2^k - 1$$$; $$$c$$$ is either ?, 0, or 1), describing the $$$i$$$-th query.", "output_spec": "For each query, print one integer — $$$f(s)$$$.", "sample_inputs": ["3\n0110?11\n6\n5 1\n6 ?\n7 ?\n1 ?\n5 ?\n1 1"], "sample_outputs": ["1\n2\n3\n3\n5\n4"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.array, std.range, core.bitop;\n\nint[] phasebase;\nint[] fights;\nbyte[] a;\nlong[long] cache;\n\nlong solve(int phase, int fightnum)\n{\n byte outcome = a[phasebase[phase] + fightnum];\n if (phase == 0)\n return outcome == 3 ? 2 : 1;\n\n long cacheid = cast(long)fightnum * 20 + cast(long)phase;\n long result = cache.get(cacheid, -1);\n if (result != -1)\n return result;\n result = 0;\n\n if (outcome & 1) {\n result += solve(phase - 1, fightnum * 2 + 0);\n }\n if (outcome & 2) {\n result += solve(phase - 1, fightnum * 2 + 1);\n }\n cache[cacheid] = result;\n return result;\n}\n\nvoid invalidate_cache(int idx)\n{\n int phase = 0;\n while (true) {\n if (idx >= phasebase[phase] && idx < phasebase[phase] + fights[phase]) {\n int fightnum = idx - phasebase[phase];\n\n while (phase < phasebase.length) {\n long cacheid = cast(long)fightnum * 20 + cast(long)phase;\n cache[cacheid] = -1;\n fightnum /= 2;\n phase++;\n }\n return;\n }\n phase++;\n }\n}\n\nvoid main()\n{\n int k, t;\n readf!\" %d \"(k);\n a = readln.strip.split(\"\").map!((x) => (x[0] == '?') ? cast(byte)3 : (x[0] == '0' ? cast(byte)1 : cast(byte)2)).array;\n// writeln(a);\n assert(k == bsf(a.length + 1));\n k = bsf(a.length + 1);\n int pb = 0;\n while (k != 0) {\n phasebase ~= pb;\n k--;\n fights ~= 1 << k;\n pb += (1 << k);\n }\n// writeln(phasebase);\n// writeln(fights);\n// writeln(\"---\");\n readf!\" %d \"(t);\n while (t--) {\n int i;\n readf!\" %d\"(i);\n string x = readln.strip;\n a[i - 1] = (x[0] == '?') ? cast(byte)3 : (x[0] == '0' ? cast(byte)1 : cast(byte)2);\n// writeln(a);\n\n // cache.clear();\n invalidate_cache(i - 1);\n\n writeln(solve(bsf(a.length + 1) - 1, 0));\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tint k;\r\n\twhile (readf !(\" %s\") (k) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto s = readln.strip.dup ~ '*';\r\n\t\treverse (s);\r\n\t\tauto m = 1 << k;\r\n\t\tauto f = new int [m * 2];\r\n\t\tf[] = 1;\r\n\r\n\t\tvoid calc (int pos)\r\n\t\t{\r\n\t\t\tf[pos] = (s[pos] == '0') ? f[pos * 2 + 1] :\r\n\t\t\t (s[pos] == '1') ? f[pos * 2 + 0] :\r\n\t\t\t f[pos * 2 + 0] + f[pos * 2 + 1];\r\n\t\t}\r\n\r\n\t\tforeach_reverse (pos; 1..m)\r\n\t\t{\r\n\t\t\tcalc (pos);\r\n\t\t}\r\n\r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (r; 0..q)\r\n\t\t{\r\n\t\t\tauto t = readln.split;\r\n\t\t\tauto pos = t[0].to !(int);\r\n\t\t\tpos = m - pos;\r\n\t\t\ts[pos] = t[1][0];\r\n\t\t\tfor ( ; pos > 0; pos >>= 1)\r\n\t\t\t{\r\n\t\t\t\tcalc (pos);\r\n\t\t\t}\r\n\t\t\twriteln (f[1]);\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.string;\nimport std.range;\nimport std.algorithm;\nimport core.stdc.stdio;\n\nint main()\n{\n int k = readInt!int;\n char[] s = readString.dup.reverse;\n int n = cast(int)s.length;\n int q = readInt!int;\n\n long[] pos = new long[](n + 1);\n void calculate(int v)\n {\n if (v * 2 > s.length)\n {\n if (s[v - 1] == '0' || s[v - 1] == '1') pos[v] = 1;\n else pos[v] = 2;\n return;\n }\n int left = 2 * v;\n int right = 2 * v + 1;\n calculate(left);\n calculate(right);\n if (s[v - 1] == '1')\n {\n pos[v] = pos[left];\n }\n else if (s[v - 1] == '0')\n {\n pos[v] = pos[right];\n }\n else\n {\n pos[v] = pos[right] + pos[left];\n }\n }\n calculate(1);\n foreach(qi; 0 .. q)\n {\n int p = n - readInt!int + 1;\n string ch = readString;\n s[p - 1] = ch[0];\n while (p >= 1)\n {\n if (p * 2 >= n)\n {\n if (s[p - 1] == '0' || s[p - 1] == '1') pos[p] = 1;\n else pos[p] = 2;\n goto done;\n }\n if (s[p - 1] == '1')\n {\n pos[p] = pos[2 * p];\n }\n else if (s[p - 1] == '0')\n {\n pos[p] = pos[2 * p + 1];\n }\n else\n {\n pos[p] = pos[2 * p] + pos[2 * p + 1];\n }\n done:\n p /= 2;\n }\n pos[1].writeln;\n }\n return 0;\n}\n\n\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n currChar = getchar();\n}\nchar topChar()\n{\n return cast(char) currChar;\n}\nvoid popChar()\n{\n currChar = getchar();\n}\nauto readInt(T)()\n{\n T num = 0;\n int sgn = 0;\n while (topChar.isWhite) popChar;\n while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }\n while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }\n if (sgn) return -num; return num;\n}\nstring readString()\n{\n string res = \"\";\n while (topChar.isWhite) popChar;\n while (!topChar.isWhite) { res ~= topChar; popChar; }\n return res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n long rep;\n this(long num)\n {\n rep = num;\n }\n Z!m opBinary(string operator)(Z!m rhs)\n {\n static if (operator == \"+\")\n {\n long result = rhs.rep + this.rep;\n if (result >= m) result -= m;\n return Z!m(result);\n }\n else static if (operator == \"-\")\n {\n long result = this.rep - rhs.rep;\n if (result < 0) result += m;\n return Z!m(result);\n }\n else static if (operator == \"*\")\n {\n long result = this.rep * rhs.rep;\n if (result >= m) result %= m;\n return Z!m(result);\n } else static assert(text(\"Operator \", operator, \" not supported\"));\n }\n Z!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n {\n assert(exponent >= 0);\n Z!m base = this;\n Z!m result = 1;\n while (exponent)\n {\n if (exponent & 1)\n result = result * base;\n base = base * base;\n exponent >>= 1;\n }\n return result;\n }\n invariant\n {\n assert(rep >= 0 && rep < m);\n }\n}\n"}], "negative_code": [], "src_uid": "0eee4b8f074e02311329d5728138c7fe"} {"nl": {"description": "You are given $$$n$$$ strings $$$a_1, a_2, \\ldots, a_n$$$: all of them have the same length $$$m$$$. The strings consist of lowercase English letters.Find any string $$$s$$$ of length $$$m$$$ such that each of the given $$$n$$$ strings differs from $$$s$$$ in at most one position. Formally, for each given string $$$a_i$$$, there is no more than one position $$$j$$$ such that $$$a_i[j] \\ne s[j]$$$.Note that the desired string $$$s$$$ may be equal to one of the given strings $$$a_i$$$, or it may differ from all the given strings.For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing two positive integers $$$n$$$ ($$$1 \\le n \\le 10$$$) and $$$m$$$ ($$$1 \\le m \\le 10$$$) — the number of strings and their length. Then follow $$$n$$$ strings $$$a_i$$$, one per line. Each of them has length $$$m$$$ and consists of lowercase English letters.", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer (if it exists) is a string of length $$$m$$$ consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print \"-1\" (\"minus one\", without quotes).", "sample_inputs": ["5\n2 4\nabac\nzbab\n2 4\naaaa\nbbbb\n3 3\nbaa\naaa\naab\n2 2\nab\nbb\n3 1\na\nb\nc"], "sample_outputs": ["abab\n-1\naaa\nab\nz"], "notes": "NoteThe first test case was explained in the statement.In the second test case, the answer does not exist."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.range;\nimport std.algorithm;\n\nvoid main() {\n\tint tc;\n\tscanf(\"%d\\n\", &tc);\n\n\twhile (tc--) {\t\n\t\tint n, m;\n\t\tscanf(\"%d%d\\n\", &n, &m);\n\n\t\tstring[] arr = new string[n];\n\t\tfor (int i = 0; i < n; i++) \n\t\t\tarr[i] = stdin.byLine.front.to!string;\n\n\t\tstring answer = null;\n\t\tcheck: foreach(s; arr) {\n\t\t\tforeach(i; iota(m)) {\n\t\t\t\tfor (char c = 'a'; c <= 'z'; c++) {\n\t\t\t\t\tchar[] test_string = s.dup;\n\t\t\t\t\ttest_string[i] = c;\n\n\t\t\t\t\t// test the string \n\t\t\t\t\tbool good = true;\n\t\t\t\t\tforeach (t; arr) {\n\t\t\t\t\t\tint d = 0;\n\t\t\t\t\t\tforeach (j, e; t) d += e != test_string[j];\n\t\t\t\t\t\tif (d > 1) {\n\t\t\t\t\t\t\tgood = false;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (good) {\n\t\t\t\t\t\tanswer = test_string.dup;\n\t\t\t\t\t\tbreak check;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (answer == null) writeln(-1);\n\t\telse answer.writeln;\n\t}\n}\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n \n outer: while (t--) {\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n string[] input;\n foreach (i; 0 .. n) {\n input ~= readln.chomp;\n }\n \n auto ans = input[0].to!(dchar[]);\n foreach (col; 0 .. m) {\n \n foreach (row; 0 .. n) {\n ans[col] = input[row][col];\n \n bool ok = true;\n foreach (compareidx; 0 .. n) {\n ok &= zip(ans, input[compareidx]).filter!\"a[0] != a[1]\".array.length <= 1;\n }\n \n if (ok) {\n writeln(ans);\n continue outer;\n }\n }\n \n ans[col] = input[0][col];\n }\n \n writeln(-1);\n }\n}"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nalias type = multi;\nstruct TestCase\n{\n mixin prettyPrinting;\n\n long n, m;\n @Dim(\"n\") string[] a;\n\n void solve(long tc = -1)\n {\n bool test(string str)\n {\n return iota(0, n)\n .all!(j => iota(0, m).count!(i => a.at(j).at(i) != str.at(i)) <= 1);\n }\n\n char[] ts = a.at(0).dup;\n foreach(i; 0 .. m)\n {\n foreach(c; 'a' .. cast(char)('z' + 1))\n {\n ts.at(i) = c;\n if (test(cast(string) ts))\n {\n writeln(ts);\n return;\n }\n }\n ts.at(i) = a.at(0).at(i);\n }\n writeln(-1);\n }\n}\n\nstruct Interval\n{\n long l, h;\n\n bool empty()\n {\n return l > h;\n }\n}\n\nInterval intersect(Interval a, Interval b)\n{\n return Interval(max(a.l, b.l), min(a.h, b.h));\n}\n\nstruct If\n{\n string condition;\n}\nenum ignore = If(\"false\");\nstruct Dim\n{\n string dimensions;\n int levels;\n this(string dimensions)\n {\n this.dimensions = dimensions;\n levels = cast(int)dimensions.count(\",\") + 1;\n }\n}\n\ntemplate getArrayLevel(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));\n else\n enum getArrayLevel = 0;\n}\n\nstatic foreach(fieldName; 'a' .. cast(char)('z' + 1))\n {\n static if (fieldName != 'o')\n mixin(\"enum d\", fieldName, \" = Dim(\\\"\", fieldName, \"\\\");\");\n }\n\n\nmixin template prettyPrinting()\n{\n string toString()\n {\n alias structType = typeof(this);\n auto res = appender!(string)();\n res.put(structType.stringof);\n res.put(\"(\");\n static foreach(fieldName; FieldNameTuple!structType)\n {\n res.put(text(\" \", fieldName, \": \", mixin(fieldName)));\n }\n res.put(\" )\");\n res.put(\"\\n\");\n return res[];\n }\n}\n\nstruct Graph(N)\n{\n size_t size;\n this(S)(S size)\n {\n adjacencyList.length = cast(size_t) size;\n this.size = cast(size_t) size;\n this.visited = makeSlice!bool(size);\n }\n N[][] adjacencyList;\n alias ne = neighbours;\n N[] neighbours(N n)\n {\n return adjacencyList.at(n);\n }\n alias bcon = biconnect;\n void biconnect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n adjacencyList.at(b) ~= a;\n }\n alias con = connect;\n void connect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n }\n void connect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n connect(edge[0], edge[1]);\n }\n }\n void biconnect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n biconnect(edge[0], edge[1]);\n }\n }\n alias deg = degree!long;\n auto degree(T)(N a)\n {\n return cast(T) adjacencyList.at(a).length;\n }\n\n auto visited = new bool[0];\n void dfs(Visitor)(long startNode)\n {\n auto visitor = Visitor.init;\n void internalDfs(long node)\n {\n visited.set(node, true);\n static if (is(typeof({visitor.n = node;})))\n visitor.n = node;\n static if (is(typeof({visitor.pre;})))\n visitor.pre;\n foreach(w; ne(node))\n if (!visited.at(w))\n {\n static if (is(typeof({visitor.w = w;})))\n visitor.w = w;\n static if (is(typeof(visitor.pren)))\n visitor.pren;\n internalDfs(w);\n static if (is(typeof(visitor.postn)))\n visitor.postn;\n }\n static if (is(typeof(visitor.post)))\n visitor.post;\n }\n internalDfs(startNode);\n }\n void dfs(long startNode)\n {\n struct DefVisitor {}\n dfs!DefVisitor(startNode);\n }\n}\n\nDList!string _words;\n\ntemplate removeArray(W, int times)\n{\n static if (times == 0)\n alias removeArray = W;\n else static if (times == 1)\n alias removeArray = typeof(function(){W w; return w[0];}());\n else\n {\n static assert(isDynamicArray!W);\n alias removeArray = removeArray!(removeArray!(W, 1), times - 1);\n }\n}\n\ntemplate baseType(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));\n else\n alias baseType = W;\n}\n\nauto makeSlice(E, W, T...)(W size, T otherSizes)\n{\n static if (T.length == 0)\n {\n E[] res;\n res.length = cast(size_t) size;\n return res;\n }\n else\n {\n typeof(makeSlice!E(otherSizes))[] res;\n res.length = cast(size_t) size;\n foreach(ref row; res)\n row = makeSlice!E(otherSizes);\n return res;\n }\n}\n\nvoid read(T...)(ref T t)\n{\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new string[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto m = RD!int;\n\t\tauto s = new string[](n);\n\t\tforeach (i; 0..n)\n\t\t\ts[i] = RD!string;\n\t\t\n\t\tif (n == 1)\n\t\t{\n\t\t\tans[ti] = s[0];\n\t\t\tcontinue;\n\t\t}\n\n\t\tauto used = new bool[][](m, 26);\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tforeach (j; 0..m)\n\t\t\t{\n\t\t\t\tauto num = s[i][j]-'a';\n\t\t\t\tused[j][num] = true;\n\t\t\t}\n\t\t}\n\n\t\tint[][string] dp;\n\t\tdp[\"\"] = new int[](n);\n\t\tforeach (i; 0..m)\n\t\t{\n\t\t\tint[][string] ndp;\n\t\t\tforeach (j; 0..26)\n\t\t\t{\n\t\t\t\tif (used[i][j] == false) continue;\n\t\t\t\tauto c = cast(char)('a'+j);\n\t\t\t\tforeach (key; dp.keys)\n\t\t\t\t{\n\t\t\t\t\tauto str = key ~ c;\n\t\t\t\t\tauto cnt = dp[key].dup;\n\t\t\t\t\tbool ok = true;\n\t\t\t\t\tforeach (k; 0..n)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (s[k][i] != c)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t++cnt[k];\n\t\t\t\t\t\t\tif (cnt[k] >= 2)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tok = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (ok)\n\t\t\t\t\t{\n\t\t\t\t\t\tndp[str] = cnt;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp = ndp;\n\t\t}\n\t\tif (!dp.empty)\n\t\t{\n\t\t\tans[ti] = dp.keys[0];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(-1);\n\t\telse\n\t\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "8d4cdf75f726b59a8fcc374e1417374a"} {"nl": {"description": "You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \\le u, v \\le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree.", "output_spec": "Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property.", "sample_inputs": ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"], "sample_outputs": ["1", "-1", "4", "0"], "notes": "NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto G = new int[][](N);\n foreach (_; 0..N-1) {\n auto s = readln.split.map!(to!int);\n G[s[0]-1] ~= s[1]-1;\n G[s[1]-1] ~= s[0]-1;\n }\n\n if (N % 2 == 1) {\n writeln(-1);\n return;\n }\n\n auto d = new int[](N);\n\n int dfs(int n, int p) {\n d[n] = 1;\n foreach (m; G[n]) if (m != p) d[n] += dfs(m, n);\n return d[n];\n }\n\n dfs(0, -1);\n auto ans = d.map!(a => 1 - a % 2).sum;\n writeln(ans - 1);\n}\n"}], "negative_code": [], "src_uid": "711896281f4beff55a8826771eeccb81"} {"nl": {"description": "Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills: a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1.For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards).Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.", "output_spec": "If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).", "sample_inputs": ["3\n4 1 7", "1\n10", "4\n1 3 5 9", "4\n4 3 4 5", "2\n2 4"], "sample_outputs": ["2\n-2 10", "-1", "1\n7", "0", "3\n0 3 6"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n if (xs.back - xs.front == 0) {\n writeln(1);\n writeln(xs.back);\n } else {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n }\n } else {\n int d = (xs.back - xs.front);\n writeln(2);\n writeln(xs.front - d, \" \", xs.back + d);\n }\n return;\n }\n int[] ds;\n foreach (i; 0 .. xs.length - 1) {\n ds ~= xs[i + 1] - xs[i];\n }\n int[] k = ds.sort.uniq.array;\n switch (k.length) {\n case 1:\n if (k[0] == 0) {\n writeln(1);\n writeln(xs.front);\n } else {\n writeln(2);\n writeln(xs.front - k[0], \" \", xs.back + k[0]);\n }\n return;\n case 2:\n int[int] c;\n foreach (d; ds) {\n c[d]++;\n }\n if (c.length > 2) {\n writeln(0);\n return;\n }\n if (c.length == 2) {\n int k1 = c.keys.reduce!max;\n int k2 = c.keys.reduce!min;\n if (c[k1] != 1) {\n writeln(0);\n return;\n }\n }\n if (k[0] * 2 != k[1]) {\n writeln(0);\n return;\n }\n int d = k[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n default:\n writeln(0);\n return;\n }\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [n];\n\t\tforeach (ref x; a)\n\t\t{\n\t\t\treadf (\" %s\", &x);\n\t\t}\n\t\tsort !(\"a < b\", SwapStrategy.stable) (a);\n\n\t\tlong res = 0;\n\t\tlong [] ans;\n\t\tif (n == 1)\n\t\t{\n\t\t\tres = -1;\n\t\t}\n\t\telse if (n == 2)\n\t\t{\n\t\t\tif (a[0] == a[1])\n\t\t\t{\n\t\t\t\tres = 1;\n\t\t\t\tans ~= a[0];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint d = a[1] - a[0];\n\t\t\t\tres = 2 + (d % 2 == 0);\n\t\t\t\tans ~= a[0] - d;\n\t\t\t\tif (d % 2 == 0)\n\t\t\t\t{\n\t\t\t\t\tans ~= (a[0] + a[1]) >> 1;\n\t\t\t\t}\n\t\t\t\tans ~= a[1] + d;\n\t\t\t}\n\t\t}\n\t\telse if (a[$ - 1] == a[1])\n\t\t{\n\t\t\tres = 1;\n\t\t\tans ~= a[0];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbool [int] e;\n\t\t\te[a[1] - a[0]] = true;\n\t\t\te[a[2] - a[1]] = true;\n\t\t\tforeach (d, val; e)\n\t\t\t{\n\t\t\t\tans = [];\n\t\t\t\tint holes = 0;\n\t\t\t\tfor (int i = 1; i < n; i++)\n\t\t\t\t{\n\t\t\t\t\tif (a[i] - a[i - 1] == d)\n\t\t\t\t\t{\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tif (a[i] - a[i - 1] == d * 2)\n\t\t\t\t\t{\n\t\t\t\t\t\tans ~= (a[i] + a[i - 1]) >> 1;\n\t\t\t\t\t\tholes++;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tholes += 2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (holes == 0)\n\t\t\t\t{\n\t\t\t\t\tans ~= a[0] - d;\n\t\t\t\t\tans ~= a[$ - 1] + d;\n\t\t\t\t}\n\t\t\t\tres = max (0, 2 - holes);\n\t\t\t\tif (res > 0)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t\tif (res > 0)\n\t\t{\n\t\t\twritefln (\"%(%s %)\", ans);\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n if (xs.back - xs.front == 0) {\n writeln(1);\n writeln(xs.back);\n } else {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n }\n } else {\n int d = (xs.back - xs.front);\n writeln(2);\n writeln(xs.front - d, \" \", xs.back + d);\n }\n return;\n }\n int[] ds;\n foreach (i; 0 .. xs.length - 1) {\n ds ~= xs[i + 1] - xs[i];\n }\n int[] k = ds.sort.uniq.array;\n switch (k.length) {\n case 1:\n if (k[0] == 0) {\n writeln(1);\n writeln(xs.front);\n } else {\n writeln(2);\n writeln(xs.front - k[0], \" \", xs.back + k[0]);\n }\n return;\n case 2:\n int[int] c;\n foreach (d; ds) {\n c[d]++;\n }\n if (c.length > 2) {\n writeln(0);\n return;\n }\n if (c.length == 2) {\n bool f = false;\n foreach (key, value; c) {\n if (value == 1) {\n f = true;\n }\n }\n if (!f) {\n writeln(0);\n return;\n }\n }\n if (k[0] * 2 != k[1]) {\n writeln(0);\n return;\n }\n int d = k[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n default:\n writeln(0);\n return;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n } else {\n writeln(2);\n writeln(xs.front, xs.back);\n }\n return;\n }\n xs.sort;\n int[] ds;\n foreach (i; 0 .. xs.length - 1) {\n ds ~= xs[i + 1] - xs[i];\n }\n int[] k = ds.sort.uniq.array;\n switch (k.length) {\n case 1:\n writeln(2);\n writeln(xs.front - k[0], \" \", xs.back + k[0]);\n return;\n case 2:\n if (k[0] * 2 != k[1]) {\n writeln(0);\n return;\n }\n int d = k[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n default:\n writeln(0);\n return;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n if (xs.back - xs.front == 0) {\n writeln(1);\n writeln(xs.back);\n } else {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n }\n } else {\n writeln(2);\n writeln(xs.front, xs.back);\n }\n return;\n }\n xs.sort;\n int[] ds;\n foreach (i; 0 .. xs.length - 1) {\n ds ~= xs[i + 1] - xs[i];\n }\n int[] k = ds.sort.uniq.array;\n switch (k.length) {\n case 1:\n if (k[0] == 0) {\n writeln(1);\n writeln(xs.front);\n } else {\n writeln(2);\n writeln(xs.front - k[0], \" \", xs.back + k[0]);\n }\n return;\n case 2:\n if (k[0] * 2 != k[1]) {\n writeln(0);\n return;\n }\n int d = k[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n default:\n writeln(0);\n return;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n if (xs.back - xs.front == 0) {\n writeln(1);\n writeln(xs.back);\n } else {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n }\n } else {\n int d = (xs.back - xs.front);\n writeln(2);\n writeln(xs.front - d, \" \", xs.back + d);\n }\n return;\n }\n int[] ds;\n foreach (i; 0 .. xs.length - 1) {\n ds ~= xs[i + 1] - xs[i];\n }\n int[] k = ds.sort.uniq.array;\n switch (k.length) {\n case 1:\n if (k[0] == 0) {\n writeln(1);\n writeln(xs.front);\n } else {\n writeln(2);\n writeln(xs.front - k[0], \" \", xs.back + k[0]);\n }\n return;\n case 2:\n if (k[0] * 2 != k[1]) {\n writeln(0);\n return;\n }\n int d = k[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n default:\n writeln(0);\n return;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n } else {\n writeln(2);\n writeln(xs.front, xs.back);\n }\n return;\n }\n xs.sort;\n int d = xs[1] - xs[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n writeln(2);\n writeln(xs.front - d, \" \", xs.back + d);\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n if (xs.length == 1) {\n (-1).writeln;\n return;\n }\n if (xs.length == 2) {\n if ((xs.back - xs.front) % 2 == 0) {\n if (xs.back - xs.front == 0) {\n writeln(1);\n writeln(xs.back);\n } else {\n int d = (xs.back - xs.front) / 2;\n writeln(3);\n writeln(xs.front - 2 * d, \" \", xs.front + d, \" \", xs.back + 2 * d);\n }\n } else {\n int d = (xs.back - xs.front);\n writeln(2);\n writeln(xs.front - d, \" \", xs.back + d);\n }\n return;\n }\n int[] ds;\n foreach (i; 0 .. xs.length - 1) {\n ds ~= xs[i + 1] - xs[i];\n }\n int[] k = ds.sort.uniq.array;\n switch (k.length) {\n case 1:\n if (k[0] == 0) {\n writeln(1);\n writeln(xs.front);\n } else {\n writeln(2);\n writeln(xs.front - k[0], \" \", xs.back + k[0]);\n }\n return;\n case 2:\n int[int] c;\n foreach (d; ds) {\n c[d]++;\n }\n if (c.length > 2) {\n writeln(0);\n return;\n }\n if (c.length == 2) {\n int k1 = c.keys.reduce!min;\n int k2 = c.keys.reduce!max;\n if (c[k1] != 1) {\n writeln(0);\n return;\n }\n }\n if (k[0] * 2 != k[1]) {\n writeln(0);\n return;\n }\n int d = k[0];\n foreach (i; 0 .. xs.length - 1) {\n if (xs[i + 1] - xs[i] != d) {\n if (xs[i + 1] - xs[i] == 2 * d) {\n 1.writeln;\n ((xs[i + 1] + xs[i]) / 2).writeln;\n } else {\n 0.writeln;\n }\n return;\n }\n }\n default:\n writeln(0);\n return;\n }\n}\n"}], "src_uid": "e3ca8338beb8852c201be72650e9aabd"} {"nl": {"description": "Polycarp has created his own training plan to prepare for the programming contests. He will train for $$$n$$$ days, all days are numbered from $$$1$$$ to $$$n$$$, beginning from the first.On the $$$i$$$-th day Polycarp will necessarily solve $$$a_i$$$ problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems.Determine the index of day when Polycarp will celebrate the equator.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$) — the number of days to prepare for the programming contests. The second line contains a sequence $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10\\,000$$$), where $$$a_i$$$ equals to the number of problems, which Polycarp will solve on the $$$i$$$-th day.", "output_spec": "Print the index of the day when Polycarp will celebrate the equator.", "sample_inputs": ["4\n1 3 2 1", "6\n2 2 2 2 2 2"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first example Polycarp will celebrate the equator on the evening of the second day, because up to this day (inclusive) he will solve $$$4$$$ out of $$$7$$$ scheduled problems on four days of the training.In the second example Polycarp will celebrate the equator on the evening of the third day, because up to this day (inclusive) he will solve $$$6$$$ out of $$$12$$$ scheduled problems on six days of the training."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimport std.math;\n\n\nvoid main(){\n\n\tint[200010] a;\n\n\tint n;\n\n\treadf(\"%s\", &n);\n\treadln;\n\t\n\tint ss = 0;\n\n\tfor (int i = 1; i < n; ++i){\n\t\treadf(\"%s \", &a[i]);\n\t\tss += a[i];\n\t}\n\n\treadf(\"%s\", &a[n]);\n\tss += a[n];\n\treadln;\n\n\n\tss = (ss + 1) / 2;\n\tint tt = 0;\n\tfor (int i = 1; i <= n; ++i){\n\t\ttt += a[i];\n\t\tif (tt >= ss){\n\t\t\twriteln(i);\n\t\t\treturn;\n\t\t}\n\t}\n}\n\n"}, {"source_code": "import std.algorithm.iteration;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main()\n{\n int n;\n readf!\"%d\\n\"(n);\n\n auto a = readln.stripRight.split.map!(a => a.parse!int);\n auto s = cumulativeFold!\"a + b\"(a, 0).array;\n // find the first value >= sum/2\n writeln(s.assumeSorted.lowerBound((s[$-1] + 1) / 2).length + 1);\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimport std.math;\n\nvoid main(){\n\n\tint[200010] a;\n\tint n;\n\n\treadf(\"%s\", &n);\n\treadln;\n\t\n\tint ss = 0;\n\n\tfor (int i = 1; i < n; ++i){\n\t\treadf(\"%s \", &a[i]);\n\t\tss += a[i];\n\t}\n\n\treadf(\"%s\", &a[n]);\n\tss += a[n];\n\treadln;\n\n\tss = (ss + 1) / 2;\n\tint tt = 0;\n\tfor (int i = 1; i <= n; ++i){\n\t\ttt += a[i];\n\t\tif (tt >= ss){\n\t\t\twriteln(i);\n\t\t\treturn;\n\t\t}\n\t}\n}\n\n"}], "negative_code": [], "src_uid": "241157c465fe5dd96acd514010904321"} {"nl": {"description": "Given three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$, find the medium number between all of them.The medium number is the number that is neither the minimum nor the maximum of the given three numbers. For example, the median of $$$5,2,6$$$ is $$$5$$$, since the minimum is $$$2$$$ and the maximum is $$$6$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 6840$$$) — the number of test cases. The description of each test case consists of three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \\leq a, b, c \\leq 20$$$).", "output_spec": "For each test case, output a single integer — the medium number of the three numbers.", "sample_inputs": ["9\n\n5 2 6\n\n14 3 4\n\n20 2 1\n\n1 2 3\n\n11 19 12\n\n10 8 20\n\n6 20 3\n\n4 1 3\n\n19 8 4"], "sample_outputs": ["5\n4\n2\n2\n12\n10\n6\n3\n8"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.algorithm;\r\nimport std.math;\r\n//import std.conv;\r\n//import std.numeric;\r\n//import std.range;\r\nimport std.array;\r\nimport std.bigint;\r\nimport std.string;\r\n\r\nvoid sol(){\r\n\r\n int a, b, c;\r\n scanf(\"%d %d %d\", &a, &b, &c);\r\n\r\n if(a>b){\r\n swap(a,b);\r\n }\r\n if(b>c){\r\n swap(b,c);\r\n }\r\n if(a>b){\r\n swap(a,b);\r\n }\r\n\r\n writeln(b);\r\n}\r\n\r\nvoid main(){\r\n \r\n int t;\r\n readf!\"%d\"(t);\r\n while (t--){\r\n sol();\r\n }\r\n}"}, {"source_code": "import std.stdio, std.conv, std.string, std.algorithm, std.array;\r\n\r\nvoid main()\r\n{\r\n auto t = readln().strip().to!int;\r\n foreach (i; 0..t)\r\n {\r\n auto testCase = readln().strip();\r\n auto numbers = testCase.split().map!(s=>s.to!int).array.sort;\r\n writefln(\"%d\", numbers[1]);\r\n }\r\n}\r\n"}, {"source_code": "import std;\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n auto a = readln.splitter.map!(to!long).array.sort.array;\n writeln(a[1]);\n }\n}\n"}], "negative_code": [], "src_uid": "63c2142461c93ae4c962eac1ecb5b192"} {"nl": {"description": "You are given $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ and an integer $$$k$$$. Find the maximum value of $$$i \\cdot j - k \\cdot (a_i | a_j)$$$ over all pairs $$$(i, j)$$$ of integers with $$$1 \\le i < j \\le n$$$. Here, $$$|$$$ is the bitwise OR operator.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$)  — the number of test cases. The first line of each test case contains two integers $$$n$$$ ($$$2 \\le n \\le 10^5$$$) and $$$k$$$ ($$$1 \\le k \\le \\min(n, 100)$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer  — the maximum possible value of $$$i \\cdot j - k \\cdot (a_i | a_j)$$$.", "sample_inputs": ["4\n3 3\n1 1 3\n2 2\n1 2\n4 3\n0 1 2 3\n6 6\n3 2 0 0 5 6"], "sample_outputs": ["-1\n-4\n3\n12"], "notes": "NoteLet $$$f(i, j) = i \\cdot j - k \\cdot (a_i | a_j)$$$.In the first test case, $$$f(1, 2) = 1 \\cdot 2 - k \\cdot (a_1 | a_2) = 2 - 3 \\cdot (1 | 1) = -1$$$. $$$f(1, 3) = 1 \\cdot 3 - k \\cdot (a_1 | a_3) = 3 - 3 \\cdot (1 | 3) = -6$$$. $$$f(2, 3) = 2 \\cdot 3 - k \\cdot (a_2 | a_3) = 6 - 3 \\cdot (1 | 3) = -3$$$. So the maximum is $$$f(1, 2) = -1$$$.In the fourth test case, the maximum is $$$f(3, 4) = 12$$$."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.functional;\r\nimport std.container;\r\nimport std.typecons;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\nimport core.bitop;\r\nimport core.stdc.stdio : scanf;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\nvoid chmin(T)(ref T a, in T b) { a = min(a, b); }\r\nvoid chmax(T)(ref T a, in T b) { a = max(a, b); }\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n\r\nalias P = Tuple!(long, \"i\", long, \"j\");\r\n\r\nvoid solve() {\r\n int N; long K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readarray!int;\r\n\r\n long xmax = -(1L<<56);\r\n for (int j = N; j >= 1; j--) {\r\n for (int i = j-1; i >= 1; i--) {\r\n if (xmax >= cast(long)(i) * j) break;\r\n long x = cast(long)(i) * j - K * (A[i-1] | A[j-1]);\r\n xmax.chmax(x);\r\n }\r\n }\r\n writeln(xmax);\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.functional;\r\nimport std.container;\r\nimport std.typecons;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\nimport core.bitop;\r\nimport core.stdc.stdio : scanf;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\nvoid chmin(T)(ref T a, in T b) { a = min(a, b); }\r\nvoid chmax(T)(ref T a, in T b) { a = max(a, b); }\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n\r\nalias P = Tuple!(long, \"i\", long, \"j\");\r\n\r\nvoid solve() {\r\n int N; long K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readarray!int;\r\n\r\n if (N >= 500) {\r\n bool[P] used;\r\n auto Q = heapify!((a, b) => a.i*a.j < b.i*b.j)(new P[0], 0);\r\n Q.insert(P(N-1, N));\r\n long xmax = -(1L<<56);\r\n while (! Q.empty) {\r\n auto c = Q.front; Q.removeFront;\r\n long i = c.i, j = c.j;\r\n if (i * j < xmax) break;\r\n long x = i * j - K * (A[cast(int)(i-1)] | A[cast(int)(j-1)]);\r\n xmax.chmax(x);\r\n if (i + 1 < j) {\r\n auto n = P(i, j-1);\r\n if (n !in used) {\r\n used[n] = true;\r\n Q.insert(n);\r\n }\r\n }\r\n if (i > 1) {\r\n auto n = P(i-1, j);\r\n if (n !in used) {\r\n used[n] = true;\r\n Q.insert(n);\r\n }\r\n }\r\n }\r\n writeln(xmax);\r\n } else {\r\n long xmax = -(1L<<56);\r\n for (int j = N; j >= 1; j--) {\r\n for (int i = j-1; i >= 1; i--) {\r\n if (xmax >= i * j) break;\r\n long x = i * j - K * (A[i-1] | A[j-1]);\r\n xmax.chmax(x);\r\n }\r\n }\r\n writeln(xmax);\r\n }\r\n}\r\n"}, {"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main(string[ ] args) {\r\n int t;\r\n readf!\"%s\"(t);\r\n readln;\r\n\r\n while (t--) {\r\n int n, k;\r\n readf!\"%s %s\"(n, k);\r\n readln;\r\n\r\n auto a = readln.chomp.split.map!(to!int).array;\r\n \r\n immutable int reach = 110;\r\n\r\n long ans = long.min;\r\n for (int i = n-1; i >= 0 && i >= n - reach; --i) {\r\n for (int j = i-1; j >= 0 && j >= n - reach; --j) {\r\n auto cur = (i+1).to!long * (j+1).to!long - k * (a[i] | a[j]);\r\n ans = max(cur, ans);\r\n }\r\n }\r\n\r\n ans.writeln;\r\n }\r\n}"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport std.range;\nimport core.stdc.stdio;\n\nvoid main()\n{\n\tint t = readInt!int;\n\twhile (t--) solve;\n}\n\nvoid solve()\n{\n\tint n = readInt!int;\n\tint k = readInt!int;\n\tauto a = new int[](n); foreach(ref ai; a) ai = readInt!int;\n\tlong mx = long.min;\n\tforeach(j; n - 100 .. n)\n\t{\n\t\tforeach(i; 0 .. j)\n\t\t{\n\t\t\tmx = max(mx, cast(long)(i+1)*cast(long)(j+1) - cast(long)k * (a[i]|a[j]));\n\t\t}\n\t}\n\tmx.writeln;\n}\n\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n\tcurrChar = getchar();\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD;\r\n\t\tauto a = RDA;\r\n\r\n\t\tans[ti] = long.min;\r\n\t\tforeach_reverse (i; 0..n)\r\n\t\t{\r\n\t\t\tauto x = k * a[i];\r\n\t\t\tforeach_reverse (j; 0..i)\r\n\t\t\t{\r\n\t\t\t\tlong y = cast(long)(i+1) * (j+1);\r\n\t\t\t\tif (y - x <= ans[ti]) break;\r\n\t\t\t\tauto z = y - k * (a[i]|a[j]);\r\n\t\t\t\tans[ti].chmax(z);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.functional;\r\nimport std.container;\r\nimport std.typecons;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\nimport core.bitop;\r\nimport core.stdc.stdio : scanf;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\nvoid chmin(T)(ref T a, in T b) { a = min(a, b); }\r\nvoid chmax(T)(ref T a, in T b) { a = max(a, b); }\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n\r\nalias P = Tuple!(long, \"i\", long, \"j\");\r\n\r\nvoid solve() {\r\n int N; long K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readarray!int;\r\n\r\n long xmax = -(1L<<56);\r\n for (int j = N; j >= 1; j--) {\r\n for (int i = j-1; i >= 1; i--) {\r\n if (xmax >= i * j) break;\r\n long x = i * j - K * (A[i-1] | A[j-1]);\r\n xmax.chmax(x);\r\n }\r\n }\r\n writeln(xmax);\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.functional;\r\nimport std.container;\r\nimport std.typecons;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\nimport core.bitop;\r\nimport core.stdc.stdio : scanf;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\nvoid chmin(T)(ref T a, in T b) { a = min(a, b); }\r\nvoid chmax(T)(ref T a, in T b) { a = max(a, b); }\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n\r\nalias P = Tuple!(long, \"i\", long, \"j\");\r\n\r\nvoid solve() {\r\n int N; long K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readarray!int;\r\n bool[P] used;\r\n DList!P Q;\r\n Q.insert(P(N-1, N));\r\n long xmax = -(1L<<56);\r\n while (! Q.empty) {\r\n auto c = Q.front; Q.removeFront;\r\n long i = c.i, j = c.j;\r\n if (i * j < xmax) break;\r\n long x = i * j - K * (A[cast(int)(i-1)] | A[cast(int)(j-1)]);\r\n xmax.chmax(x);\r\n if (i + 1 < j) {\r\n auto n = P(i, j-1);\r\n if (n !in used) {\r\n used[n] = true;\r\n Q.insert(n);\r\n }\r\n }\r\n if (i > 1) {\r\n auto n = P(i-1, j);\r\n if (n !in used) {\r\n used[n] = true;\r\n Q.insert(n);\r\n }\r\n }\r\n }\r\n writeln(xmax);\r\n}\r\n"}, {"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main(string[ ] args) {\r\n int t;\r\n readf!\"%s\"(t);\r\n readln;\r\n\r\n while (t--) {\r\n int n, k;\r\n readf!\"%s %s\"(n, k);\r\n readln;\r\n\r\n auto a = readln.chomp.split.map!(to!int).array;\r\n \r\n immutable int reach = 100;\r\n\r\n long ans = long.min;\r\n for (int i = n-1; i >= 0 && i >= n - reach; --i) {\r\n for (int j = i-1; j >= 0 && j >= n - reach; --j) {\r\n auto cur = (i+1).to!long * (j+1).to!long - k * (a[i] | a[j]);\r\n ans = max(cur, ans);\r\n }\r\n }\r\n\r\n ans.writeln;\r\n }\r\n}"}, {"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main(string[ ] args) {\r\n int t;\r\n readf!\"%s\"(t);\r\n readln;\r\n\r\n while (t--) {\r\n int n, k;\r\n readf!\"%s %s\"(n, k);\r\n readln;\r\n\r\n auto a = readln.chomp.split.map!(to!int).array;\r\n\r\n long ans = long.min;\r\n for (int i = n-1; i >= 0 && i >= n - 10; --i) {\r\n for (int j = i-1; j >= 0 && j >= n - 10; --j) {\r\n auto cur = (i+1).to!long * (j+1).to!long - k * (a[i] | a[j]);\r\n ans = max(cur, ans);\r\n }\r\n }\r\n\r\n ans.writeln;\r\n }\r\n}"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport std.range;\nimport core.stdc.stdio;\n\nvoid main()\n{\n\tint t = readInt!int;\n\twhile (t--) solve;\n}\n\nvoid solve()\n{\n\tint n = readInt!int;\n\tint k = readInt!int;\n\tauto a = new int[](n); foreach(ref ai; a) ai = readInt!int;\n\tif (n <= 1000)\n\t{\n\t\tlong mx = long.min;\n\t\tforeach(i; 0 .. n)\n\t\t\tforeach(j; i + 1 .. n)\n\t\t\t\tmx = max(mx, cast(long)(i+1)*cast(long)(j+1) - cast(long)k * (a[i]|a[j]));\n\t\tmx.writeln;\n\t}\n\telse\n\t{\n\t\tlong mx = long.min;\n\t\tforeach(j; n - 3 .. n)\n\t\t{\n\t\t\tforeach(i; 0 .. j)\n\t\t\t{\n\t\t\t\tmx = max(mx, cast(long)(i+1)*cast(long)(j+1) - cast(long)k * (a[i]|a[j]));\n\t\t\t}\n\t\t}\n\t\tmx.writeln;\n\t}\n}\n\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n\tcurrChar = getchar();\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD;\r\n\t\tauto a = RDA;\r\n\r\n\t\tans[ti] = long.min;\r\n\t\tforeach_reverse (i; 0..n)\r\n\t\t{\r\n\t\t\tauto x = k * a[i];\r\n\t\t\tforeach_reverse (j; 0..i)\r\n\t\t\t{\r\n\t\t\t\tlong y = (i+1) * (j+1);\r\n\t\t\t\tif (y - x <= ans[ti]) break;\r\n\t\t\t\tauto z = y - k * (a[i]|a[j]);\r\n\t\t\t\tans[ti].chmax(z);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD;\r\n\t\tauto a = RDA;\r\n\r\n\t\tans[ti] = long.min;\r\n\t\tforeach_reverse (i; 0..n)\r\n\t\t{\r\n\t\t\tauto x = k * a[i];\r\n\t\t\tforeach_reverse (j; 0..i)\r\n\t\t\t{\r\n\t\t\t\tauto y = (i+1) * (j+1);\r\n\t\t\t\tif (y - x <= ans[ti]) break;\r\n\t\t\t\tauto z = y - k * (a[i]|a[j]);\r\n\t\t\t\tans[ti].chmax(z);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "src_uid": "3a45b6acdcf3800d1cb4ef8ac96ed4cf"} {"nl": {"description": "Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places.Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij.Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.", "input_spec": "The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.", "output_spec": "Print the only integer a — the cost of the dinner for Jack and Emma.", "sample_inputs": ["3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "3 3\n1 2 3\n2 3 1\n3 1 2"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2.In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1."}, "positive_code": [{"source_code": "\nmodule foo1;\nimport std.stdio;\nimport std.string;\nimport std.conv;\nimport std.regex;\nimport std.file;\nimport std.math;\nimport std.algorithm;\n//import foo2;\n\nvoid main()\n{\n auto str = split(strip(readln()),\" \");\n long[] ans;\n for(int i = 0; i < to!int(str[0], 10); i++)\n {\n auto x = split(strip(readln()),\" \");\n long[] res;\n foreach(idx, element; x)\n {\n\n res ~= to!long(element, 10);\n\n }\n res.sort();\n\n ans ~= res[0];\n\n }\n ans.sort();\n writeln(ans[$-1]);\n\n}\n\n\n"}], "negative_code": [{"source_code": "module foo2;\n\nimport std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.conv;\n\n//import foo1;\n\n\nvoid main()\n{\n string arr;\n string[] str1 = split(readln());\n for(int i = 0; i < to!int(str1[0]); i++)\n {\n string inp = strip(readln);\n arr ~= \" \" ~ min(inp);\n }\n writeln(max(arr));\n}\n\nstring min(string str)\n{\n string[] a = split(str);\n string min = a[0];\n foreach(element; a[1 .. $])\n {\n if(min > element)\n min = element;\n }\n return min;\n}\nstring max(string str)\n{\n string[] a = split(str);\n string max = a[0];\n foreach(element; a[1 .. $])\n {\n if(max < element)\n max = element;\n }\n return max;\n}"}, {"source_code": "\nmodule foo1;\nimport std.stdio;\nimport std.string;\nimport std.conv;\nimport std.regex;\nimport std.file;\nimport std.math;\nimport std.algorithm;\n//import foo2;\n\nvoid main()\n{\n auto str = split(strip(readln()),\" \");\n string res[];\n //writeln(n,\" \",m);\n for(int i = 0; i < to!int(str[0], 10); i++)\n {\n auto x = split(strip(readln()),\" \");\n x.sort();\n res ~= x[0];\n\n }\n res.sort();\n writeln(res[$-1]);\n\n}\n\n\n"}], "src_uid": "f2142bc2f44e5d8b77f8561c29038a73"} {"nl": {"description": "Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were $$$n$$$ types of candies, there are $$$a_i$$$ candies of the type $$$i$$$ ($$$1 \\le i \\le n$$$).Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose any of them). To get the maximum pleasure from eating, Vlad does not want to eat two candies of the same type in a row.Help him figure out if he can eat all the candies without eating two identical candies in a row.", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of input test cases. The following is a description of $$$t$$$ test cases of input, two lines for each. The first line of the case contains the single number $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of types of candies in the package. The second line of the case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$) — the number of candies of the type $$$i$$$. It is guaranteed that the sum of $$$n$$$ for all cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Output $$$t$$$ lines, each of which contains the answer to the corresponding test case of input. As an answer, output \"YES\" if Vlad can eat candy as planned, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["6\n2\n2 3\n1\n2\n5\n1 6 2 4 3\n4\n2 2 2 1\n3\n1 1000000000 999999999\n1\n1"], "sample_outputs": ["YES\nNO\nNO\nYES\nYES\nYES"], "notes": "NoteIn the first example, it is necessary to eat sweets in this order: a candy of the type $$$2$$$, it is the most frequent, now $$$a = [2, 2]$$$; a candy of the type $$$1$$$, there are the same number of candies of the type $$$2$$$, but we just ate one, now $$$a = [1, 2]$$$; a candy of the type $$$2$$$, it is the most frequent, now $$$a = [1, 1]$$$; a candy of the type $$$1$$$, now $$$a = [0, 1]$$$; a candy of the type $$$2$$$, now $$$a = [0, 0]$$$ and the candy has run out.In the second example, all the candies are of the same type and it is impossible to eat them without eating two identical ones in a row.In the third example, first of all, a candy of the type $$$2$$$ will be eaten, after which this kind will remain the only kind that is the most frequent, and you will have to eat a candy of the type $$$2$$$ again."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array ~ 0;\r\n\t\tsort !(q{a > b}) (a);\r\n\t\twriteln (a[0] - a[1] > 1 ? \"NO\" : \"YES\");\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "8b926a19f380a56018308668c17c6928"} {"nl": {"description": "Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.Initially, on day $$$1$$$, there is one bacterium with mass $$$1$$$.Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass $$$m$$$ splits, it becomes two bacteria of mass $$$\\frac{m}{2}$$$ each. For example, a bacterium of mass $$$3$$$ can split into two bacteria of mass $$$1.5$$$.Also, every night, the mass of every bacteria will increase by one.Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly $$$n$$$. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\le 10^9$$$) — the sum of bacteria masses that Phoenix is interested in. ", "output_spec": "For each test case, if there is no way for the bacteria to exactly achieve total mass $$$n$$$, print -1. Otherwise, print two lines. The first line should contain an integer $$$d$$$  — the minimum number of nights needed. The next line should contain $$$d$$$ integers, with the $$$i$$$-th integer representing the number of bacteria that should split on the $$$i$$$-th day. If there are multiple solutions, print any.", "sample_inputs": ["3\n9\n11\n2"], "sample_outputs": ["3\n1 0 2 \n3\n1 1 2\n1\n0"], "notes": "NoteIn the first test case, the following process results in bacteria with total mass $$$9$$$: Day $$$1$$$: The bacterium with mass $$$1$$$ splits. There are now two bacteria with mass $$$0.5$$$ each. Night $$$1$$$: All bacteria's mass increases by one. There are now two bacteria with mass $$$1.5$$$. Day $$$2$$$: None split. Night $$$2$$$: There are now two bacteria with mass $$$2.5$$$. Day $$$3$$$: Both bacteria split. There are now four bacteria with mass $$$1.25$$$. Night $$$3$$$: There are now four bacteria with mass $$$2.25$$$. The total mass is $$$2.25+2.25+2.25+2.25=9$$$. It can be proved that $$$3$$$ is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.$$$ $$$In the second test case, the following process results in bacteria with total mass $$$11$$$: Day $$$1$$$: The bacterium with mass $$$1$$$ splits. There are now two bacteria with mass $$$0.5$$$. Night $$$1$$$: There are now two bacteria with mass $$$1.5$$$. Day $$$2$$$: One bacterium splits. There are now three bacteria with masses $$$0.75$$$, $$$0.75$$$, and $$$1.5$$$. Night $$$2$$$: There are now three bacteria with masses $$$1.75$$$, $$$1.75$$$, and $$$2.5$$$. Day $$$3$$$: The bacteria with mass $$$1.75$$$ and the bacteria with mass $$$2.5$$$ split. There are now five bacteria with masses $$$0.875$$$, $$$0.875$$$, $$$1.25$$$, $$$1.25$$$, and $$$1.75$$$. Night $$$3$$$: There are now five bacteria with masses $$$1.875$$$, $$$1.875$$$, $$$2.25$$$, $$$2.25$$$, and $$$2.75$$$. The total mass is $$$1.875+1.875+2.25+2.25+2.75=11$$$. It can be proved that $$$3$$$ is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.$$$ $$$In the third test case, the bacterium does not split on day $$$1$$$, and then grows to mass $$$2$$$ during night $$$1$$$."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n\n while (t--) {\n int n;\n readf(\"%s\", &n);\n readln;\n \n int mx = 0;\n while (n >= (1 << mx)) {\n n -= (1 << mx);\n ++mx;\n }\n \n int[] arr;\n foreach (b; 0 .. mx) {\n arr ~= (1 << b);\n if (n >= (1 << b) && n < (1 << (b+1))) { arr ~= n; }\n }\n \n int[] ans;\n foreach (prv, nxt; lockstep(arr, arr.dropOne)) {\n ans ~= nxt - prv;\n }\n \n ans.length.writeln;\n ans.map!(to!string).join(\" \").writeln;\n }\n}"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto N = readln.chomp.to!long;\n long[] as;\n long b = 1;\n N -= 1;\n while (N) {\n if (N <= b*2) {\n as ~= N-b;\n break;\n }\n if (N <= b*4) {\n as ~= N/2-b;\n b = N/2;\n } else {\n as ~= b;\n b *= 2;\n }\n N -= b;\n }\n writeln(as.length);\n writeln(as.to!(string[]).join(\" \"));\n }\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\treadln;\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\tauto steps = bsr (n);\n\t\tint cur = 1;\n\t\tint total = cur;\n\t\tint [] ans;\n\t\tforeach (step; 0..steps)\n\t\t{\n\t\t\tint next = (n - total) / (steps - step);\n\t\t\tnext = min (next, cur * 2);\n\t\t\tans ~= next - cur;\n\t\t\tcur = next;\n\t\t\ttotal += next;\n\t\t}\n\t\twriteln (steps);\n\t\twritefln !(\"%(%s %)\") (ans);\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto N = readln.chomp.to!long;\n long[] as;\n long b = 1;\n if (N%2 == 0) {\n as ~= 0;\n N -= 2;\n } else {\n N -= 1;\n }\n while (N) {\n if (N == b) {\n as ~= 0;\n break;\n } else if (N == b*2) {\n as ~= b;\n break;\n }\n if (N/2 <= b*2) {\n as ~= N/2-b;\n b = N/2;\n N /= 2;\n } else {\n as ~= b;\n b *= 2;\n N -= b;\n }\n }\n writeln(as.length);\n writeln(as.to!(string[]).join(\" \"));\n }\n}"}], "src_uid": "e21f235ffe7f26d9a7af12a7f3f9a2fd"} {"nl": {"description": "There is a grid, consisting of $$$n$$$ rows and $$$m$$$ columns. The rows are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns are numbered from $$$1$$$ to $$$m$$$ from left to right. The $$$i$$$-th column has the bottom $$$a_i$$$ cells blocked (the cells in rows $$$1, 2, \\dots, a_i$$$), the remaining $$$n - a_i$$$ cells are unblocked.A robot is travelling across this grid. You can send it commands — move up, right, down or left. If a robot attempts to move into a blocked cell or outside the grid, it explodes.However, the robot is broken — it executes each received command $$$k$$$ times. So if you tell it to move up, for example, it will move up $$$k$$$ times ($$$k$$$ cells). You can't send it commands while the robot executes the current one.You are asked $$$q$$$ queries about the robot. Each query has a start cell, a finish cell and a value $$$k$$$. Can you send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command $$$k$$$ times?The robot must stop in the finish cell. If it visits the finish cell while still executing commands, it doesn't count.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 10^9$$$; $$$1 \\le m \\le 2 \\cdot 10^5$$$) — the number of rows and columns of the grid. The second line contains $$$m$$$ integers $$$a_1, a_2, \\dots, a_m$$$ ($$$0 \\le a_i \\le n$$$) — the number of blocked cells on the bottom of the $$$i$$$-th column. The third line contains a single integer $$$q$$$ ($$$1 \\le q \\le 2 \\cdot 10^5$$$) — the number of queries. Each of the next $$$q$$$ lines contain five integers $$$x_s, y_s, x_f, y_f$$$ and $$$k$$$ ($$$a[y_s] < x_s \\le n$$$; $$$1 \\le y_s \\le m$$$; $$$a[y_f] < x_f \\le n$$$; $$$1 \\le y_f \\le m$$$; $$$1 \\le k \\le 10^9$$$) — the row and the column of the start cell, the row and the column of the finish cell and the number of times each your command is executed. The start and the finish cell of each query are unblocked.", "output_spec": "For each query, print \"YES\" if you can send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command $$$k$$$ times. Otherwise, print \"NO\".", "sample_inputs": ["11 10\n9 0 0 10 3 4 8 11 10 8\n6\n1 2 1 3 1\n1 2 1 3 2\n4 3 4 5 2\n5 3 11 5 3\n5 3 11 5 2\n11 9 9 10 1"], "sample_outputs": ["YES\nNO\nNO\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nclass SegmentTree(T, T unit, alias binop) {\r\n int n;\r\n T[] dat;\r\n this(int n_) {\r\n n = 1;\r\n while (n < n_) n *= 2;\r\n dat = new T[2*n+2];\r\n dat[] = unit;\r\n }\r\n void update(int k, in T a) {\r\n k += n - 1;\r\n dat[k] = a;\r\n while (k > 0) {\r\n k = (k - 1) / 2;\r\n dat[k] = binop(dat[k * 2 + 1], dat[k * 2 + 2]);\r\n }\r\n }\r\n T query(int a, int b) const {\r\n return query(a, b, 0, 0, n);\r\n }\r\n T query(int a, int b, int k, int l, int r) const {\r\n if (r <= a || b <= l) return unit;\r\n if (a <= l && r <= b) return dat[k];\r\n T vl = query(a, b, k * 2 + 1, l, (l + r) / 2),\r\n vr = query(a, b, k * 2 + 2, (l + r) / 2, r);\r\n return binop(vl, vr);\r\n }\r\n}\r\n\r\nenum long INF = 1L<<56;\r\n\r\nvoid main() {\r\n long N, M; readf(\"%d %d\\n\", &N, &M);\r\n auto A = readln.chomp.split(\" \").map!(to!long).array;\r\n auto rmq = new SegmentTree!(long, -INF, (a, b) => max(a, b))(cast(int)(M + 1));\r\n for (int i = 1; i <= M; i++) {\r\n rmq.update(i, A[i-1]);\r\n }\r\n long Q = readln.chomp.to!long;\r\n foreach (q; 0 .. Q) {\r\n bool ans_query() {\r\n long xs, ys, xf, yf, k;\r\n readf(\"%d %d %d %d %d\\n\", &ys, &xs, &yf, &xf, &k);\r\n if (xs > xf) {\r\n swap(xs, xf);\r\n swap(ys, yf);\r\n }\r\n long dst = (N - ys) / k;\r\n long yst = ys + k * dst;\r\n if (rmq.query(cast(int)xs, cast(int)(xf + 1)) < yst) {\r\n // ok\r\n return (yst - ys) % k == 0 && (yst - yf) % k == 0 && (xf - xs) % k == 0;\r\n } else {\r\n return false;\r\n }\r\n }\r\n writeln(ans_query() ? \"YES\" : \"NO\");\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "19f12c275f2b389ed5a1af66808d1bbd"} {"nl": {"description": "The Hedgehog recently remembered one of his favorite childhood activities, — solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces.All puzzle pieces turn out to be of the same size X × Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A × B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size.", "input_spec": "The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters.", "output_spec": "In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers — the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly — by the length X.", "sample_inputs": ["2 4\nABDC\nABDC", "2 6\nABCCBA\nABCCBA"], "sample_outputs": ["3\n2 1", "1\n2 6"], "notes": "NoteThe picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4)."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n \nvoid main()\n{\n\n int a, b;\n readf(\"%s %s\", &a, &b);\n readln;\n \n dchar[][] arr;\n \n foreach (i; 0 .. a) {\n arr ~= readln.chomp.to!(dchar[]);\n }\n \n debug { arr.writeln; }\n\n int anscnt = 0;\n int ansx = a, ansy = b;\n bool smaller(int x, int y) { \n return x*y < ansx*ansy || (x*y == ansx*ansy && x < ansx);\n }\n \n bool isOk(int x, int y) {\n struct Piece {\n dchar[][] e;\n \n bool different(Piece rhs) {\n auto e2 = rhs.e.dup;\n \n dchar[][] rot90(dchar[][] a) {\n auto res = new dchar[][] (a[0].length, a.length);\n foreach (i; 0 .. a.length) {\n foreach (j; 0 .. a[0].length) {\n res[j][a.length - 1 - i] = a[i][j];\n }\n }\n return res;\n }\n \n foreach (_; 0 .. 4) {\n e2 = rot90(e2);\n \n bool sameRot(const dchar[][] a, const dchar[][] b) {\n if (a.length != b.length) { return false; }\n if (a[0].length != b[0].length) { return false; }\n \n foreach (i; 0 .. a.length) {\n foreach (j; 0 .. a[0].length) {\n if (a[i][j] != b[i][j]) { return false; }\n }\n }\n \n return true;\n }\n \n if (sameRot(e, e2)) { return false; }\n }\n \n return true;\n }\n }\n \n Piece[] ps;\n foreach (stx; a.iota.array.stride(x)) {\n foreach (sty; b.iota.array.stride(y)) {\n Piece p;\n foreach (r; stx .. stx + x) {\n p.e ~= arr[r][sty .. sty + y];\n }\n ps ~= p;\n }\n }\n \n debug { ps.map!(p => p.e).writeln; }\n \n foreach (i; 0 .. ps.length) {\n foreach (j; i+1 .. ps.length) {\n if (!ps[i].different(ps[j])) { return false; }\n }\n }\n \n return true;\n }\n \n foreach (x; 1 .. a+1) {\n if (a % x != 0) { continue; }\n \n foreach (y; 1 .. b+1) {\n if (b % y != 0) { continue; }\n \n if (isOk(x, y)) {\n anscnt += 1;\n if (smaller(x, y)) {\n ansx = x;\n ansy = y;\n }\n }\n }\n }\n \n writeln(anscnt);\n writeln(ansx, ' ', ansy);\n}"}], "negative_code": [], "src_uid": "4de8b72f9ce12554cae8b6a83b3f023e"} {"nl": {"description": "You are given an undirected unweighted graph consisting of $$$n$$$ vertices and $$$m$$$ edges.You have to write a number on each vertex of the graph. Each number should be $$$1$$$, $$$2$$$ or $$$3$$$. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd.Calculate the number of possible ways to write numbers $$$1$$$, $$$2$$$ and $$$3$$$ on vertices so the graph becomes beautiful. Since this number may be large, print it modulo $$$998244353$$$.Note that you have to write exactly one number on each vertex.The graph does not have any self-loops or multiple edges.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 3 \\cdot 10^5$$$) — the number of tests in the input. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 3 \\cdot 10^5, 0 \\le m \\le 3 \\cdot 10^5$$$) — the number of vertices and the number of edges, respectively. Next $$$m$$$ lines describe edges: $$$i$$$-th line contains two integers $$$u_i$$$, $$$ v_i$$$ ($$$1 \\le u_i, v_i \\le n; u_i \\neq v_i$$$) — indices of vertices connected by $$$i$$$-th edge. It is guaranteed that $$$\\sum\\limits_{i=1}^{t} n \\le 3 \\cdot 10^5$$$ and $$$\\sum\\limits_{i=1}^{t} m \\le 3 \\cdot 10^5$$$.", "output_spec": "For each test print one line, containing one integer — the number of possible ways to write numbers $$$1$$$, $$$2$$$, $$$3$$$ on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo $$$998244353$$$.", "sample_inputs": ["2\n2 1\n1 2\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"], "sample_outputs": ["4\n0"], "notes": "NotePossible ways to distribute numbers in the first test: the vertex $$$1$$$ should contain $$$1$$$, and $$$2$$$ should contain $$$2$$$; the vertex $$$1$$$ should contain $$$3$$$, and $$$2$$$ should contain $$$2$$$; the vertex $$$1$$$ should contain $$$2$$$, and $$$2$$$ should contain $$$1$$$; the vertex $$$1$$$ should contain $$$2$$$, and $$$2$$$ should contain $$$3$$$. In the second test there is no way to distribute numbers."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nimmutable long MOD = 998244353;\n\nlong powmod(long a, long x, long m) {\n long ret = 1;\n while (x) {\n if (x % 2) ret = ret * a % m;\n a = a * a % m;\n x /= 2;\n }\n return ret;\n}\n\nvoid solve() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1];\n auto G = new int[][](N);\n foreach (_; 0..M) {\n s = readln.split.map!(to!int);\n G[s[0]-1] ~= s[1]-1;\n G[s[1]-1] ~= s[0]-1;\n }\n\n long ans = 1;\n auto cnt = new long[](2);\n auto C = new int[](N);\n fill(C, -1);\n\n bool dfs(int n, int c) {\n if (C[n] != -1)\n return C[n] == c;\n C[n] = c;\n cnt[c] += 1;\n foreach (m; G[n]) {\n if (C[m] != -1) {\n if (C[m] == (c^1))\n continue;\n else\n return false;\n }\n if (!dfs(m, c^1))\n return false;\n }\n return true;\n }\n\n foreach (i; 0..N) {\n if (C[i] != -1) continue;\n cnt[0] = cnt[1] = 0;\n if (dfs(i, 0)) {\n long tmp = powmod(2, cnt[0], MOD) + powmod(2, cnt[1], MOD);\n tmp %= MOD;\n ans = ans * tmp % MOD;\n } else {\n writeln(0);\n return;\n }\n }\n\n ans.writeln;\n}\n\nvoid main() {\n auto Q = readln.chomp.to!int;\n while (Q--) solve;\n}\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{\n immutable int MD = 998244353;\n immutable int MXN = 3 * 10 ^^ 5;\n \n auto pw = new int[] (MXN + 1);\n pw[0] = 1;\n foreach (i; 1 .. MXN+1) { pw[i] = pw[i-1] * 2 % MD; }\n \n int t;\n readf(\"%s\", &t);\n readln;\n \n outer: while (t--) {\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto g = new int[][] (n+1);\n foreach (i; 0 .. m) {\n int u, v;\n readf(\"%s %s\", &u, &v);\n readln;\n \n g[u] ~= v;\n g[v] ~= u;\n }\n \n auto clr = new int[] (n+1);\n clr[] = 0;\n int ans = 1;\n foreach (i; 1 .. n+1) {\n if (clr[i] != 0) { continue; }\n \n auto res = new int[] (3);\n res[] = 0;\n \n bool dfs(int v, int c) {\n clr[v] = c;\n res[c] += 1;\n auto ok = true;\n foreach (u; g[v]) {\n if (clr[u] == 0) {\n ok &= dfs(u, 3 - c);\n } \n else if (clr[u] == 3 - c) { continue; }\n else { return false; }\n }\n \n return ok;\n }\n \n if (!dfs(i, 1)) {\n writeln(0);\n continue outer;\n }\n \n debug { writeln(i, ' ', res); }\n \n int cur = (pw[res[1]] + pw[res[2]]) % MD;\n ans = ans.to!long * cur % MD;\n }\n \n ans.writeln;\n }\n}"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nimmutable long MOD = 998244353;\n\nlong powmod(long a, long x, long m) {\n long ret = 1;\n while (x) {\n if (x % 2) ret = ret * a % m;\n a = a * a % m;\n x /= 2;\n }\n return ret;\n}\n\nvoid solve() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1];\n auto G = new int[][](N);\n foreach (_; 0..M) {\n s = readln.split.map!(to!int);\n G[s[0]-1] ~= s[1]-1;\n G[s[1]-1] ~= s[0]-1;\n }\n\n long ans = 0;\n auto cnt = new long[](2);\n auto C = new int[](N);\n fill(C, -1);\n\n bool dfs(int n, int c) {\n if (C[n] != -1)\n return C[n] == c;\n C[n] = c;\n cnt[c] += 1;\n foreach (m; G[n]) {\n if (C[m] != -1) {\n if (C[m] != (c^1))\n return false;\n else\n continue;\n }\n if (!dfs(m, c^1))\n return false;\n }\n return true;\n }\n\n foreach (i; 0..N) {\n if (C[i] != -1) continue;\n cnt[0] = cnt[1] = 0;\n if (dfs(i, 0)) {\n ans += powmod(2, cnt[0], MOD) + powmod(2, cnt[1], MOD);\n ans %= MOD;\n } else {\n writeln(0);\n return;\n }\n }\n\n ans.writeln;\n}\n\nvoid main() {\n auto Q = readln.chomp.to!int;\n while (Q--) solve;\n}\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{\n immutable int MD = 998244353;\n immutable int MXN = 3 * 10 ^^ 5;\n \n auto pw = new int[] (MXN + 1);\n pw[0] = 1;\n foreach (i; 1 .. MXN+1) { pw[i] = pw[i-1] * 2 % MD; }\n \n int t;\n readf(\"%s\", &t);\n readln;\n \n while (t--) {\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto g = new int[][] (n+1);\n foreach (i; 0 .. m) {\n int u, v;\n readf(\"%s %s\", &u, &v);\n readln;\n \n g[u] ~= v;\n g[v] ~= u;\n }\n \n auto clr = new int[] (n+1);\n clr[] = 0;\n \n bool dfs(int v, int c) {\n clr[v] = c;\n bool ok = true;\n foreach (u; g[v]) {\n if (clr[u] == 0) {\n ok &= dfs(u, 3 - c);\n } \n else if (clr[u] == 3 - c) { continue; }\n else { return false; }\n }\n \n return ok;\n }\n \n if (!dfs(1, 1)) {\n writeln(0);\n continue;\n }\n \n debug { writeln(clr); }\n \n auto side = clr.count!(x => x == 1).to!int;\n int ans = (pw[side] + pw[n - side]) % MD;\n ans.writeln;\n }\n}"}], "src_uid": "332340a793eb3ec14131948e2b6bdf2f"} {"nl": {"description": "A frog is currently at the point $$$0$$$ on a coordinate axis $$$Ox$$$. It jumps by the following algorithm: the first jump is $$$a$$$ units to the right, the second jump is $$$b$$$ units to the left, the third jump is $$$a$$$ units to the right, the fourth jump is $$$b$$$ units to the left, and so on.Formally: if the frog has jumped an even number of times (before the current jump), it jumps from its current position $$$x$$$ to position $$$x+a$$$; otherwise it jumps from its current position $$$x$$$ to position $$$x-b$$$. Your task is to calculate the position of the frog after $$$k$$$ jumps.But... One more thing. You are watching $$$t$$$ different frogs so you have to answer $$$t$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of queries. Each of the next $$$t$$$ lines contain queries (one query per line). The query is described as three space-separated integers $$$a, b, k$$$ ($$$1 \\le a, b, k \\le 10^9$$$) — the lengths of two types of jumps and the number of jumps, respectively.", "output_spec": "Print $$$t$$$ integers. The $$$i$$$-th integer should be the answer for the $$$i$$$-th query.", "sample_inputs": ["6\n5 2 3\n100 1 4\n1 10 5\n1000000000 1 6\n1 1 1000000000\n1 1 999999999"], "sample_outputs": ["8\n198\n-17\n2999999997\n0\n1"], "notes": "NoteIn the first query frog jumps $$$5$$$ to the right, $$$2$$$ to the left and $$$5$$$ to the right so the answer is $$$5 - 2 + 5 = 8$$$.In the second query frog jumps $$$100$$$ to the right, $$$1$$$ to the left, $$$100$$$ to the right and $$$1$$$ to the left so the answer is $$$100 - 1 + 100 - 1 = 198$$$.In the third query the answer is $$$1 - 10 + 1 - 10 + 1 = -17$$$.In the fourth query the answer is $$$10^9 - 1 + 10^9 - 1 + 10^9 - 1 = 2999999997$$$.In the fifth query all frog's jumps are neutralized by each other so the answer is $$$0$$$.The sixth query is the same as the fifth but without the last jump so the answer is $$$1$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.format;\nimport std.math;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int t = readln.strip.to!int;\n\n while (t--)\n {\n long a, b, k;\n readf!\" %s %s %s \"(a, b, k);\n\n long ans = k / 2 * (a - b);\n if (k % 2)\n ans += a;\n\n writefln!\"%s\"(ans);\n }\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n \n while (t--) {\n int a, b, k;\n readf(\"%s %s %s\", &a, &b, &k);\n readln;\n \n auto diff = a - b;\n auto ans = cast(long)diff * (k/2);\n \n if (k % 2 == 1) ans += a;\n \n ans.writeln;\n }\n}"}, {"source_code": "import std.stdio;\nint main()\n{\n\tint t=0;\n\treadf(\" %d\", &t);\n\tfor (int i=0; i v ^^ 2 + v) lo += (v + 1) ^^ 2 - d;\r\n\t\t\telse hi = min (hi, lo + v ^^ 2 + v - d);\r\n\t\t\tif (lo > hi) {\r\n\t\t\t\ta.swapAt (i, uniform (0, i + 1));\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (lo <= hi) {\r\n\t\t\twriteln (lo);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.random;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable long limit = 2 * 10 ^^ 6 + 5;\r\n\r\nvoid main ()\r\n{\r\n\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto used = new bool [limit];\r\n\t\tauto a = readln.splitter.map !(to !(long)).array;\r\n\r\n\t\trndGen.seed (263_473_470);\r\n\t\tlong sel = a.front;\r\n\t\tlong cur = sel;\r\n\t\ta.popFront ();\r\n\t\trandomShuffle (a);\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tlong base = cast (long) (sqrt (cur * 1.0));\r\n\t\t\tlong lo = cast (long) (cur - base * 1L * base);\r\n\t\t\tlong hi = base;\r\n\t\t\tlong delta = cur - sel;\r\n\t\t\tforeach (i, ref c; a)\r\n\t\t\t{\r\n\t\t\t\tlong d = c + delta;\r\n\t\t\t\tlong v = cast (long) (sqrt (d * 1.0));\r\n\t\t\t\tlong w = v * 1L * v;\r\n\t\t\t\tif (d > w + v)\r\n\t\t\t\t{\r\n\t\t\t\t\tlong e = w + v * 2 + 1;\r\n\t\t\t\t\tlong add = cast (long) (e - d);\r\n\t\t\t\t\tcur += add;\r\n\t\t\t\t\tdelta += add;\r\n\t\t\t\t\tlo += add;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\thi = min (hi,\r\n\t\t\t\t\t lo + cast (long) (w + v - d));\r\n\t\t\t\t}\r\n\t\t\t\tif (lo > hi)\r\n\t\t\t\t{\r\n\t\t\t\t\ta.swapAt (i, uniform (0, i + 1));\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (lo <= hi)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tcur = (base + 1L) ^^ 2;\r\n\t\t}\r\n\t\twriteln (cur - sel);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.random;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable long limit = 2 * 10 ^^ 6 + 5;\r\n\r\nvoid main ()\r\n{\r\n\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto used = new bool [limit];\r\n\t\tauto a = readln.splitter.map !(to !(long)).array;\r\n\r\n\t\trndGen.seed (263_473_470);\r\n\t\tlong sel = a.front;\r\n\t\tlong cur = sel;\r\n\t\ta.popFront ();\r\n\t\trandomShuffle (a);\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tlong base = cast (long) (sqrt (cur * 1.0));\r\n\t\t\tlong lo = cast (long) (cur - base * 1L * base);\r\n\t\t\tlong hi = base;\r\n\t\t\tlong delta = cur - sel;\r\n\t\t\tforeach (i, ref c; a)\r\n\t\t\t{\r\n\t\t\t\tlong d = c + delta;\r\n\t\t\t\tlong v = cast (long) (sqrt (d * 1.0));\r\n\t\t\t\tlong w = v * 1L * v;\r\n\t\t\t\tif (d > w + v)\r\n\t\t\t\t{\r\n\t\t\t\t\tlong e = w + v * 2 + 1;\r\n\t\t\t\t\tlong add = cast (long) (e - d);\r\n\t\t\t\t\tcur += add;\r\n\t\t\t\t\tdelta += add;\r\n\t\t\t\t\tlo += add;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\thi = min (hi,\r\n\t\t\t\t\t lo + cast (long) (w + v - d));\r\n\t\t\t\t}\r\n\t\t\t\tif (lo > hi)\r\n\t\t\t\t{\r\n\t\t\t\t\tswap (a[0], a[i]);\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (lo <= hi)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tcur = (base + 1L) ^^ 2;\r\n\t\t}\r\n\t\twriteln (cur - sel);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.random;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable long limit = 2 * 10 ^^ 6 + 5;\r\n\r\nvoid main ()\r\n{\r\n\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto used = new bool [limit];\r\n\t\tauto a = readln.splitter.map !(to !(long)).array;\r\n\r\n\t\trndGen.seed (263_473_470);\r\n\t\tlong sel = a.front;\r\n\t\tlong cur = sel;\r\n\t\ta.popFront ();\r\n\t\trandomShuffle (a);\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tlong base = cast (long) (sqrt (cur * 1.0));\r\n\t\t\tlong lo = cast (long) (cur - base * 1L * base);\r\n\t\t\tlong hi = base;\r\n\t\t\tlong delta = cur - sel;\r\n\t\t\tforeach (ref c; a)\r\n\t\t\t{\r\n\t\t\t\tlong d = c + delta;\r\n\t\t\t\tlong v = cast (long) (sqrt (d * 1.0));\r\n\t\t\t\tlong w = v * 1L * v;\r\n\t\t\t\tif (d > w + v)\r\n\t\t\t\t{\r\n\t\t\t\t\tlong e = w + v * 2 + 1;\r\n\t\t\t\t\tlong add = cast (long) (e - d);\r\n\t\t\t\t\tcur += add;\r\n\t\t\t\t\tdelta += add;\r\n\t\t\t\t\tlo += add;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\thi = min (hi, cast (long) (w + v - d));\r\n\t\t\t\t}\r\n\t\t\t\tif (lo > hi)\r\n\t\t\t\t{\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (lo <= hi)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tcur = (base + 1L) ^^ 2;\r\n\t\t}\r\n\t\twriteln (cur - sel);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.random;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int limit = 2 * 10 ^^ 6 + 5;\r\n\r\nvoid main ()\r\n{\r\n\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto used = new bool [limit];\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\trndGen.seed (263_473_470);\r\n\t\tlong sel = a.front;\r\n\t\tlong cur = sel;\r\n\t\ta.popFront ();\r\n\t\trandomShuffle (a);\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tint base = cast (int) (sqrt (cur * 1.0));\r\n\t\t\tint lo = cast (int) (cur - base * 1L * base);\r\n\t\t\tint hi = base;\r\n\t\t\tlong delta = cur - sel;\r\n\t\t\tdebug {writefln !(\"considering %s [%s..%s]\")\r\n\t\t\t (cur, lo, hi);}\r\n\t\t\tforeach (ref c; a)\r\n\t\t\t{\r\n\t\t\t\tlong d = c + delta;\r\n\t\t\t\tint v = cast (int) (sqrt (d * 1.0));\r\n\t\t\t\tlong w = v * 1L * v;\r\n\t\t\t\tdebug {writefln\r\n\t\t\t\t !(\"lo = %s, %s -> %s in [%s..%s]\")\r\n\t\t\t\t (lo, c, d, w, w + v);}\r\n\t\t\t\tif (d > w + v)\r\n\t\t\t\t{\r\n\t\t\t\t\tlong e = w + v * 2 + 1;\r\n\t\t\t\t\tint add = cast (int) (e - d);\r\n\t\t\t\t\tdebug {writeln (\"add \", add);}\r\n\t\t\t\t\tcur += add;\r\n\t\t\t\t\tdebug {writeln (\"cur = \", cur);}\r\n\t\t\t\t\tdelta += add;\r\n\t\t\t\t\tlo += add;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\thi = min (hi, cast (int) (w + v - d));\r\n\t\t\t\t}\r\n\t\t\t\tif (lo > hi)\r\n\t\t\t\t{\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (lo <= hi)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tcur = (base + 1L) ^^ 2;\r\n\t\t\tdebug {writeln (\"cur = \", cur);}\r\n\t\t}\r\n\t\twriteln (cur - sel);\r\n\t}\r\n}\r\n"}], "src_uid": "6f31e2bc222314236d35c9642a60812e"} {"nl": {"description": "A string is binary, if it consists only of characters \"0\" and \"1\".String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string \"010\" has six substrings: \"0\", \"1\", \"0\", \"01\", \"10\", \"010\". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters \"1\".", "input_spec": "The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.", "output_spec": "Print the single number — the number of substrings of the given string, containing exactly k characters \"1\". Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["1\n1010", "2\n01010", "100\n01010"], "sample_outputs": ["6", "4", "0"], "notes": "NoteIn the first sample the sought substrings are: \"1\", \"1\", \"10\", \"01\", \"10\", \"010\".In the second sample the sought substrings are: \"101\", \"0101\", \"1010\", \"01010\"."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.typecons;\nimport std.algorithm;\nimport std.array;\nimport std.range;\nimport std.math;\n\nvoid main()\n{\n int n;\n long r;\n auto k = readln().chomp().to!int();\n auto str = readln().chomp();\n auto dp = new long[str.length + 1];\n dp[0] = 1;\n\n foreach (s; str) {\n if (s == '1') n++;\n if (n >= k) r += dp[n - k];\n dp[n]++; \n }\n\n writeln(r);\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.typecons;\nimport std.algorithm;\nimport std.array;\nimport std.range;\nimport std.math;\n\nvoid main()\n{\n int n, r;\n auto k = readln().chomp().to!int();\n auto str = readln().chomp();\n auto dp = new long[str.length];\n\n foreach (s; str) {\n if (s == '1') n++;\n if (n >= k) r += dp[n - k];\n dp[n]++; \n }\n\n writeln(r);\n}"}, {"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.typecons;\nimport std.algorithm;\nimport std.array;\nimport std.range;\nimport std.math;\n\nvoid main()\n{\n int n, r;\n auto k = readln().chomp().to!int();\n auto str = readln().chomp();\n auto dp = new long[str.length];\n dp[0] = 1;\n\n foreach (s; str) {\n if (s == '1') n++;\n if (n >= k) r += dp[n - k];\n dp[n]++; \n }\n\n writeln(r);\n}"}], "src_uid": "adc43f273dd9b3f1c58b052a34732a50"} {"nl": {"description": "The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both x and y, it is denoted as . For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows: Given all the numbers of the GCD table G, restore array a.", "input_spec": "The first line contains number n (1 ≤ n ≤ 500) — the length of array a. The second line contains n2 space-separated numbers — the elements of the GCD table of G for array a. All the numbers in the table are positive integers, not exceeding 109. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array a.", "output_spec": "In the single line print n positive integers — the elements of array a. If there are multiple possible solutions, you are allowed to print any of them.", "sample_inputs": ["4\n2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2", "1\n42", "2\n1 1 1 1"], "sample_outputs": ["4 3 6 2", "42", "1 1"], "notes": null}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n int [int] cnt;\n \n arr.each!(x => ++cnt[x]);\n \n int[] ans;\n foreach (i; 0 .. n) {\n auto cur = cnt.keys.maxElement();\n ans ~= cur;\n cnt[cur] -= 1;\n if (cnt[cur] == 0) cnt.remove(cur);\n \n foreach (j; 0 .. i) {\n auto now = gcd(ans[i], ans[j]);\n cnt[now] -= 2;\n if (cnt[now] == 0) cnt.remove(now);\n }\n }\n \n ans.sort();\n //debug { ans.writeln; }\n ans.writefln!(\"%(%s %)\");\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tauto a = readln.split.map !(to !(int)).array;\n\t\tsort (a);\n\t\tint [] r;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tauto cur = a.minPos !(q{a > b}).front;\n\t\t\tint [] b = [cur];\n\t\t\tforeach (p; r)\n\t\t\t{\n\t\t\t\tb ~= gcd (p, cur);\n\t\t\t}\n\t\t\tb ~= b;\n\t\t\tb ~= int.max;\n\t\t\tsort (b);\n\t\t\tfor (int j = 0, p = 0; j < a.length; j++)\n\t\t\t{\n\t\t\t\tif (b.front == a[j])\n\t\t\t\t{\n\t\t\t\t\tb.popFront ();\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ta[p] = a[j];\n\t\t\t\t\tp++;\n\t\t\t\t}\n\t\t\t}\n\t\t\ta.length -= r.length * 2 + 1;\n\t\t\tr ~= cur;\n\t\t}\n\t\twritefln (\"%(%s %)\", r);\n\t}\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n int [int] cnt;\n \n arr.each!(x => ++cnt[x]);\n \n int[] ans;\n foreach (i; 0 .. n) {\n auto cur = cnt.keys.maxElement();\n ans ~= cur;\n cnt[cur] -= 1;\n if (cnt[cur] == 0) cnt.remove(cur);\n \n foreach (j; 0 .. i) {\n auto now = gcd(ans[i], ans[j]);\n cnt[now] -= 2;\n if (cnt[now] == 0) cnt.remove(cur);\n }\n }\n \n ans.sort();\n //debug { ans.writeln; }\n ans.writefln!(\"%(%s %)\");\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n int [int] cnt;\n \n arr.each!(x => ++cnt[x]);\n \n int[] ans;\n foreach (i; 0 .. n) {\n auto cur = cnt.keys.maxElement();\n ans ~= cur;\n cnt[cur] -= 1;\n if (cnt[cur] == 0) cnt.remove(cur);\n \n foreach (j; 0 .. i) {\n auto now = gcd(ans[i], ans[j]);\n cnt[now] -= 2;\n if (cnt[now] == 0) cnt.remove(cur);\n }\n }\n \n //debug { ans.writeln; }\n ans.writefln!(\"%(%s %)\");\n}"}], "src_uid": "71dc07f0ea8962f23457af1d6509aeee"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that $$$a_1 < a_2 < \\dots < a_n$$$ holds.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array.", "output_spec": "For each test case, output \"YES\" (without quotes) if the array satisfies the condition, and \"NO\" (without quotes) otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["3\n\n4\n\n1 1 1 1\n\n5\n\n8 7 1 3 4\n\n1\n\n5"], "sample_outputs": ["NO\nYES\nYES"], "notes": "NoteIn the first test case any rearrangement will keep the array $$$[1,1,1,1]$$$, which is not strictly increasing.In the second test case, you can make the array $$$[1,3,4,7,8]$$$."}, "positive_code": [{"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\\n\", &t);\r\n foreach(_; 0..t) {\r\n int n;\r\n scanf(\"%d\\n\", &n);\r\n auto arr = readln.split.to!(long[]);\r\n if (arr.length == 1)\r\n writeln(\"YES\");\r\n else {\r\n if (arr.length == uniq(arr.sort).array.length)\r\n writeln(\"YES\");\r\n else\r\n writeln(\"NO\");\r\n }\r\n } \r\n}\r\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n long n;\n readf!\" %d \"(n);\n auto a = readln.splitter.map!(to!long).array.sort.array;\n long prev = 0;\n bool good = true;\n foreach (x ; a) {\n if (x == prev) {\n good = false;\n break;\n }\n prev = x;\n }\n writeln(good ? \"YES\" : \"NO\");\n }\n}\n"}, {"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nvoid solve() {\r\n int N = read!int;\r\n auto A = readarray!int;\r\n sort(A);\r\n bool f() {\r\n for (int i = 0; i + 1 < N; i++) {\r\n if (A[i] == A[i + 1]) return false;\r\n }\r\n return true;\r\n }\r\n writeln(f() ? \"YES\" : \"NO\");\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "negative_code": [{"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\\n\", &t);\r\n foreach(_; 0..t) {\r\n int n;\r\n scanf(\"%d\\n\", &n);\r\n auto arr = readln.split.to!(long[]);\r\n if (arr.length == 1)\r\n writeln(\"YES\");\r\n else {\r\n if (arr.length == uniq(arr).array.length)\r\n writeln(\"YES\");\r\n else\r\n writeln(\"NO\");\r\n }\r\n } \r\n}\r\n"}], "src_uid": "288f147bb8a3c30b0bb712a01c65109b"} {"nl": {"description": "BerOilGasDiamondBank has branches in n cities, at that n is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly n / 2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol d is used as the separator of words in the calendar. The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we \"glue\"(concatinate) all the n / 2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are \"bertown!berville\", \"newberville!bera\", then the resulting line is \"bertown!bervillenewberville!bera\". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line.Help BerOilGasDiamondBank and construct the required calendar.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 104, n is even) which is the number of branches. Then follow n lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol d (d has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different.", "output_spec": "Print n / 2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the \"<\" operator in the modern programming languages.", "sample_inputs": ["4\nb\naa\nhg\nc\n.", "2\naa\na\n!", "2\naa\na\n|"], "sample_outputs": ["aa.b\nc.hg", "a!aa", "aa|a"], "notes": null}, "positive_code": [{"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.variant,\n\tstd.stdio,std.bitmanip;\nimport core.stdc.stdio : freopen;\nalias RedBlackTree Rbt;\nalias redBlackTree rbt;\nalias BigInt big;\nalias pair!(int,int) pii;\nalias pair!(long,long) pll;\nalias boyerMooreFinder strfind;\nalias make_pair mp;\nalias binary_search bins;\nalias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});\npair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)\n{\n\tpair!(X,Y) pp;\n\tpp.fi=x_;\n\tpp.se=y_;\n\treturn pp;\n}\nimmutable int mod=10^^9+7;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b)\n\t{\n\t\treturn a/gcd(a,b)*b;\n\t}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) \n\t\tif(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\t//debug writeln('}'>'z');\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tauto ar=new string[n];\n\t\tint l;\n\t\tforeach(i;0..n){ar[i]=readln.strip;l+=ar[i].length;}\n\t\tl/=(n/2);\n\t\tchar d;\n\t\tread(&d);\n\t\tif(d>'z')\n\t\t{\n\t\t\tbool cmp(string a,string b)\n\t\t\t{\n\t\t\t\tif(a.length<=b.length)\n\t\t\t\t{\n\t\t\t\t\tif(a==b[0..a.length])return false;\n\t\t\t\t\telse return a1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tauto ar=new string[n];\n\t\tint l;\n\t\tforeach(i;0..n){ar[i]=readln.strip;l+=ar[i].length;}\n\t\tl/=n/2;\n\t\tdebug writeln(l);\n\t\tchar d;\n\t\tread(&d);\n\t\tif(d>'z')\n\t\t{\n\t\t\tbool cmp(string a,string b)\n\t\t\t{\n\t\t\t\tif(a.length<=b.length)\n\t\t\t\t{\n\t\t\t\t\tif(a==b[0..a.length])return false;\n\t\t\t\t\telse return a1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\tloop:while(read(&n))\n\t{\n\t\tauto ar=new string[n];\n\t\tforeach(i;0..n)ar[i]=readln.strip;\n\t\tchar d;\n\t\tread(&d);\n\t\tif(d>'z')\n\t\t{\n\t\t\tbool cmp(string a,string b)\n\t\t\t{\n\t\t\t\tif(a.length<=b.length)\n\t\t\t\t{\n\t\t\t\t\tif(a==b[0..a.length])return false;\n\t\t\t\t\telse return a parse!int(a));\n\n int sum = 0;\n foreach (i; 0 .. m) {\n sum += maxAns[i] * point[i];\n }\n\n writeln(sum);\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1];\n auto S = N.iota.map!(_ => readln.chomp).array;\n auto A = readln.split.map!(to!long).array;\n auto cnt = new long[](5);\n long ans = 0;\n\n foreach (i; 0..M) {\n cnt[] = 0;\n foreach (j; 0..N) cnt[S[j][i]-'A'] += 1;\n ans += cnt.reduce!max * A[i];\n }\n\n ans.writeln;\n}\n"}, {"source_code": "import std.algorithm.searching : maxElement;\nimport std.range : generate, takeExactly;\nimport std.stdio : readf, readln, writeln;\nimport std.string : strip;\n\nvoid main()\n{\n int n, m; // number of students, number of questions\n readf!\"%d %d\\n\"(n, m);\n auto s = new string[n];\n foreach (i; 0..n) s[i] = readln.strip;\n\n auto total = 0;\n\n // read points for each question\n foreach (i; 0..m) {\n int a;\n char c;\n readf!\"%d%c\"(a, c);\n\n auto counts = new int[26];\n\n foreach (j; 0..n) {\n counts[s[j][i] - 'A']++;\n }\n\n total += counts.maxElement * a;\n }\n\n writeln(total);\n}\n"}], "negative_code": [], "src_uid": "2022f53e5a88d5833e133dc3608a122c"} {"nl": {"description": "In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions: overall, there must be m dances; exactly three people must take part in each dance; each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland). The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance. You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.", "input_spec": "The first line contains two space-separated integers n (3 ≤ n ≤ 105) and m (1 ≤ m ≤ 105) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.", "output_spec": "Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.", "sample_inputs": ["7 3\n1 2 3\n1 4 5\n4 6 7", "9 3\n3 6 9\n2 5 8\n1 4 7", "5 2\n4 1 5\n3 1 2"], "sample_outputs": ["1 2 3 3 2 2 1", "1 1 1 2 2 2 3 3 3", "2 3 1 1 3"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n int N, M; scanf(\"%d %d\\n\", &N, &M);\n auto F = new int[][M];\n foreach (i; 0 .. M) {\n auto s = readln.chomp.split(\" \").map!(to!int).array;\n foreach (ref e; s) e--;\n F[i] = s;\n }\n\n auto X = new int[N];\n foreach (i; 0 .. M) {\n auto used = new bool[4];\n foreach (j; 0 .. 3) {\n int p = F[i][j];\n if (X[p] != 0) {\n used[ X[p] ] = true;\n }\n }\n foreach (j; 0 .. 3) {\n int p = F[i][j];\n if (X[p] != 0) continue;\n foreach (int k; 1 .. 4) {\n if (!used[k]) {\n X[p] = k;\n used[k] = true;\n break;\n }\n }\n }\n }\n writefln(\"%(%s %)\", X);\n}\n"}, {"source_code": "#!/usr/bin/env rdmd\nimport std.stdio, std.string, std.conv;\nimport std.algorithm, std.array;\n\nvoid main()\n{\n for(string S; (S=readln().chomp()).length; )\n {\n auto nm = S.split().map!(to!int)();\n auto n = nm[0], m = nm[1];\n auto d = new int[3][m];\n foreach(ref a;d)\n a[] = array(readln().split().map!(to!int)().map!(\"a-1\")());\n auto c = new int[n];\n foreach(ref a;d)\n {\n auto o = 0;\n foreach(i;0..3)\n if(c[a[i]]!=0)\n o=c[a[i]]+2-i;\n foreach(i;0..3)\n c[a[i]]=(i+o)%3+1;\n }\n writeln(c.map!(to!string)().join(\" \"));\n }\n}\n"}], "negative_code": [], "src_uid": "ee523bb4da5cb794e05fb62b7da8bb89"} {"nl": {"description": "Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together.More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves: Value x and some direction are announced, and all boys move x positions in the corresponding direction. Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even. Your task is to determine the final position of each boy.", "input_spec": "The first line of the input contains two integers n and q (2 ≤ n ≤ 1 000 000, 1 ≤ q ≤ 2 000 000) — the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that n is even. Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n ≤ x ≤ n), where 0 ≤ x ≤ n means all boys moves x girls in clockwise direction, while  - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type.", "output_spec": "Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves.", "sample_inputs": ["6 3\n1 2\n2\n1 2", "2 3\n1 1\n2\n1 -2", "4 2\n2\n1 3"], "sample_outputs": ["4 3 6 5 2 1", "1 2", "1 4 3 2"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10 ^^ 9 + 23;\n\nvoid main()\n{\n int n, q;\n readf(\"%s %s\", &n, &q);\n readln;\n\n long[2] mv;\n foreach (_; 0 .. q) {\n int t, x;\n readf(\"%s\", &t);\n if (t == 1) { readf(\" %s\", &x); }\n readln;\n \n if (t == 1) {\n mv[0] += x;\n mv[1] += x;\n } else {\n if (mv[0] % 2 == 0) { mv[0] -= 1; }\n else { mv[0] += 1; }\n \n if (mv[1] % 2 == 0) { mv[1] += 1; }\n else { mv[1] -= 1; }\n }\n }\n \n foreach (ref e; mv) { e = ((e % n) + n) % n; }\n \n auto ans = new int[] (n);\n foreach (i; 1 .. n+1) {\n auto cmv = mv[i.to!int % 2];\n int npos = (i.to!int - 1 + cmv) % n;\n ans[npos] = i;\n }\n \n ans.writefln!\"%(%s %)\";\n}"}], "negative_code": [], "src_uid": "e30ac55354792bdad2ef41aba8838806"} {"nl": {"description": "Let's denote correct match equation (we will denote it as CME) an equation $$$a + b = c$$$ there all integers $$$a$$$, $$$b$$$ and $$$c$$$ are greater than zero.For example, equations $$$2 + 2 = 4$$$ (||+||=||||) and $$$1 + 2 = 3$$$ (|+||=|||) are CME but equations $$$1 + 2 = 4$$$ (|+||=||||), $$$2 + 2 = 3$$$ (||+||=|||), and $$$0 + 1 = 1$$$ (+|=|) are not.Now, you have $$$n$$$ matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!For example, if $$$n = 2$$$, you can buy two matches and assemble |+|=||, and if $$$n = 5$$$ you can buy one match and assemble ||+|=|||. Calculate the minimum number of matches which you have to buy for assembling CME.Note, that you have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of queries. The only line of each query contains one integer $$$n$$$ ($$$2 \\le n \\le 10^9$$$) — the number of matches.", "output_spec": "For each test case print one integer in single line — the minimum number of matches which you have to buy for assembling CME. ", "sample_inputs": ["4\n2\n5\n8\n11"], "sample_outputs": ["2\n1\n0\n1"], "notes": "NoteThe first and second queries are explained in the statement.In the third query, you can assemble $$$1 + 3 = 4$$$ (|+|||=||||) without buying matches.In the fourth query, buy one match and assemble $$$2 + 4 = 6$$$ (||+||||=||||||)."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto q = RD!int;\n\tauto ans = new long[](q);\n\tforeach (i; 0..q)\n\t{\n\t\tauto n = RD!int;\n\t\tif (n == 2)\n\t\t{\n\t\t\tans[i] = 2;\n\t\t}\n\t\telse\n\t\t\tans[i] = n % 2 == 0 ? 0 : 1;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush();\n\tdebug readln();\n}"}, {"source_code": "import std.stdio;\nint main()\n{\n\tint t=0;\n\treadf(\" %d\", &t);\n\tfor (int i=0; i b) { swap(r, b); }\n \n auto g = gcd(r, b);\n \n r /= g;\n b /= g;\n \n auto m = (b-1+r-1) / r;\n \n debug { writeln(m, ' ', g); }\n \n writeln(m < k ? \"OBEY\" : \"REBEL\");\n }\n}"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint t = rint;\n\tforeach(_; 0 .. t){\n\t\tstring ans = \"OBEY\";\n\t\tlong r = rlong, b = rlong, k = rlong;\n\t\tlong g = gcd(r, b);\n\t\tr /= g, b /= g;\n\t\tif(r > b && (r - 2) / b + 1 >= k) ans = \"REBEL\";\n\t\tif(b > r && (b - 2) / r + 1 >= k) ans = \"REBEL\";\n\t\tans.writeln;\n\t}\n\n}\nlong gcd(long a, long b){\n\tif(a < 0) a = -a;\n\tif(b == 0) return a;\n\tif(b < 0) return gcd(a, -b);\n\tif(a % b == 0) return b;\n\tif(a < b) return gcd(b, a);\n\telse return gcd(b, a % b);\n}\n"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int t;\n readf(\"%s\", &t);\n readln;\n\n while (t--) {\n int r, b, k;\n readf(\"%s %s %s\", &r, &b, &k);\n readln;\n \n if (r > b) { swap(r, b); }\n \n auto g = gcd(r, b);\n \n auto m = b / r;\n \n if (g == r && r != b) { --m; }\n \n debug { writeln(m, ' ', g); }\n \n writeln(m < k ? \"OBEY\" : \"REBEL\");\n }\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int t;\n readf(\"%s\", &t);\n readln;\n\n while (t--) {\n int r, b, k;\n readf(\"%s %s %s\", &r, &b, &k);\n readln;\n \n if (r > b) { swap(r, b); }\n \n auto g = gcd(r, b);\n \n auto m = b / r;\n \n debug { writeln(m, ' ', g); }\n \n writeln(m < k ? \"OBEY\" : \"REBEL\");\n }\n}"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint t = rint;\n\tforeach(_; 0 .. t){\n\t\tstring ans = \"OBAY\";\n\t\tlong r = rlong, b = rlong, k = rlong;\n\t\tlong g = gcd(r, b);\n\t\tr /= g, b /= g;\n\t\tif(r > b && (r - 2) / b + 1 >= k) ans = \"REBEL\";\n\t\tif(b > r && (b - 2) / r + 1 >= k) ans = \"REBEL\";\n\t\tans.writeln;\n\t}\n\n}\nlong gcd(long a, long b){\n\tif(a < 0) a = -a;\n\tif(b == 0) return a;\n\tif(b < 0) return gcd(a, -b);\n\tif(a % b == 0) return b;\n\tif(a < b) return gcd(b, a);\n\telse return gcd(b, a % b);\n}\n"}], "src_uid": "be141f316d6e5d9d8f09192913f4be47"} {"nl": {"description": "There are $$$n$$$ boxes with different quantities of candies in each of them. The $$$i$$$-th box has $$$a_i$$$ candies inside.You also have $$$n$$$ friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don't want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.What's the minimum total number of candies you have to eat to satisfy the requirements?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the number of boxes you have. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^7$$$) — the quantity of candies in each box.", "output_spec": "For each test case, print a single integer denoting the minimum number of candies you have to eat to satisfy the requirements.", "sample_inputs": ["5\n\n5\n\n1 2 3 4 5\n\n6\n\n1000 1000 5 1000 1000 1000\n\n10\n\n1 2 3 5 1 2 7 9 13 5\n\n3\n\n8 8 8\n\n1\n\n10000000"], "sample_outputs": ["10\n4975\n38\n0\n0"], "notes": "NoteFor the first test case, you can eat $$$1$$$ candy from the second box, $$$2$$$ candies from the third box, $$$3$$$ candies from the fourth box and $$$4$$$ candies from the fifth box. Now the boxes have $$$[1, 1, 1, 1, 1]$$$ candies in them and you ate $$$0 + 1 + 2 + 3 + 4 = 10$$$ candies in total so the answer is $$$10$$$.For the second test case, the best answer is obtained by making all boxes contain $$$5$$$ candies in them, thus eating $$$995 + 995 + 0 + 995 + 995 + 995 = 4975$$$ candies in total."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.string;\r\nimport std.algorithm;\r\n\r\nint[] rtln(T)() { return to!(T[])(strip(readln).split(\" \")); }\r\n\r\nvoid main() {\r\n int t, n, m;\r\n readf(\"%d\\n\", t);\r\n while(t--) {\r\n\t\treadln;\r\n\t\tint sum;\r\n auto b = rtln!int;\r\n\t\tint min = b.minElement;\r\n\t\tforeach (x; b)\r\n\t\t\t\tsum += (x - min);\r\n\t\twriteln(sum);\r\n }\r\n}\r\n"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N);\r\n\r\n auto mi = A.minElement;\r\n return A.sum - mi*N;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve().writeln;\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\twriteln (a.sum - a.minElement * n);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "20dd260775ea71b1fb5b42bcac90a6f2"} {"nl": {"description": "The only difference between the easy and the hard versions is the maximum value of $$$k$$$.You are given an infinite sequence of form \"112123123412345$$$\\dots$$$\" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $$$1$$$ to $$$1$$$, the second one — from $$$1$$$ to $$$2$$$, the third one — from $$$1$$$ to $$$3$$$, $$$\\dots$$$, the $$$i$$$-th block consists of all numbers from $$$1$$$ to $$$i$$$. So the first $$$56$$$ elements of the sequence are \"11212312341234512345612345671234567812345678912345678910\". Elements of the sequence are numbered from one. For example, the $$$1$$$-st element of the sequence is $$$1$$$, the $$$3$$$-rd element of the sequence is $$$2$$$, the $$$20$$$-th element of the sequence is $$$5$$$, the $$$38$$$-th element is $$$2$$$, the $$$56$$$-th element of the sequence is $$$0$$$.Your task is to answer $$$q$$$ independent queries. In the $$$i$$$-th query you are given one integer $$$k_i$$$. Calculate the digit at the position $$$k_i$$$ of the sequence.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. The $$$i$$$-th of the following $$$q$$$ lines contains one integer $$$k_i$$$ $$$(1 \\le k_i \\le 10^9)$$$ — the description of the corresponding query.", "output_spec": "Print $$$q$$$ lines. In the $$$i$$$-th line print one digit $$$x_i$$$ $$$(0 \\le x_i \\le 9)$$$ — the answer to the query $$$i$$$, i.e. $$$x_i$$$ should be equal to the element at the position $$$k_i$$$ of the sequence.", "sample_inputs": ["5\n1\n3\n20\n38\n56", "4\n2132\n506\n999999999\n1000000000"], "sample_outputs": ["1\n2\n5\n2\n0", "8\n2\n9\n8"], "notes": "NoteAnswers on queries from the first example are described in the problem statement."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int q;\n readf(\"%s\", &q);\n readln;\n\n immutable int MXK = 10 ^^ 9;\n immutable int MX = 10 ^^ 5;\n \n auto sz = new long[] (MX);\n sz[0] = 0;\n foreach (i; 1 .. MX) {\n sz[i] = sz[i-1] + i.to!string.length.to!int;\n }\n \n debug { sz.take(20).writeln; }\n \n while (q--) {\n int x;\n readf(\"%s\", &x);\n readln;\n \n auto idx = 0;\n while (x > sz[idx]) { \n x -= sz[idx];\n ++idx;\n }\n \n debug { writeln(\"x : \", x); }\n \n int start = 1;\n int m = 9;\n while (start.to!string.length.to!long * m < x) {\n x -= start.to!string.length * m;\n start *= 10;\n m *= 10;\n }\n \n debug { writeln(x, ' ', start); }\n \n int v = start;\n while (v.to!string.length < x) {\n x -= v.to!string.length;\n ++v;\n }\n \n auto str = v.to!string;\n \n debug { str.writeln; }\n \n str[x-1].writeln;\n }\n}"}], "negative_code": [], "src_uid": "7e03c9e316f36c1d9487286237e24c6f"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. Indices of the array start from zero (i. e. the first element is $$$a_0$$$, the second one is $$$a_1$$$, and so on).You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of $$$a$$$ with borders $$$l$$$ and $$$r$$$ is $$$a[l; r] = a_l, a_{l + 1}, \\dots, a_{r}$$$.Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements $$$a_0, a_2, \\dots, a_{2k}$$$ for integer $$$k = \\lfloor\\frac{n-1}{2}\\rfloor$$$ should be maximum possible).You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_0, a_1, \\dots, a_{n-1}$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer on the separate line — the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of $$$a$$$.", "sample_inputs": ["4\n8\n1 7 3 4 7 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1"], "sample_outputs": ["26\n5\n37\n5"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.container;\nimport std.range;\nimport std.algorithm;\n\nlong calculate_max_sum(long[] arr) {\n\tlong current = 0;\n\tlong best_res = 0; \n\tint left = 0;\n\t\n\tfor (int i = 0; i < arr.length; i++) {\n\t\tcurrent += arr[i];\n\t\twhile (left <= i && arr[left] < 0) {\n\t\t\tcurrent -= arr[left];\n\t\t\tleft++;\n\t\t}\n\t\t\n\t\tif (current < 0) {\n\t\t\tcurrent = 0; \n\t\t\tleft = i + 1;\n\t\t}\n\t\t\n\t\tbest_res = max(best_res, current);\n\t}\n\t\n\treturn best_res;\n}\n\nvoid main() {\n\tint t;\n\tscanf(\"%d\", &t);\n\t\n\twhile (t--) {\n\t\tint n; \n\t\tscanf(\"%d\", &n);\n\t\t\n\t\tlong[] arr = new long[n];\n\t\tforeach (i; iota(n)) scanf(\"%lld\", &arr[i]);\n\t\t\n\t\t// calculate the sum for even elems\n\t\tlong sum = 0; \n\t\tforeach (i, e; arr) sum += ((i + 1) % 2) * e;\n\t\t\n\t\t// sum-diff-array starting at first element \n\t\tlong[] a = new long[n / 2];\n\t\tfor (int i = 0; i + 1 < n; i += 2) {\n\t\t\ta[i / 2] = arr[i + 1] - arr[i];\n\t\t}\n\t\t\n\t\t// starting at second \n\t\tint n_odd = (n - 1) / 2;\n\t\tlong[] b = new long[n_odd];\n\t\tfor (int i = 1; i + 1 < n; i += 2) {\n\t\t\tb[i / 2] = arr[i] - arr[i + 1];\n\t\t}\n\t\t\n\t\twriteln(sum + max(calculate_max_sum(a), calculate_max_sum(b)));\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tlong [] [] s = [[0], [0], [0]];\n\t\tforeach (i, c; a)\n\t\t{\n\t\t\ts[0] ~= s[0][$ - 1] + !(i & 1) * c;\n\t\t\ts[1] ~= s[1][$ - 1] + (i & 1) * c;\n\t\t\ts[2] ~= s[1][$ - 1] - s[0][$ - 1];\n\t\t}\n\t\tlong res = 0;\n\t\tlong [2] lo = [long.max / 2, 0];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tlo[i & 1] = min (lo[i & 1], s[2][i + 1]);\n\t\t\tres = max (res, s[2][i + 1] - lo[i & 1]);\n\t\t}\n\t\twriteln (res + s[0].back);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\n\t\tauto b = new long[](n+1);\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (i % 2)\n\t\t\t{\n\t\t\t\tb[i+1] = b[i] - a[i];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tb[i+1] = b[i] + a[i];\n\t\t\t}\n\t\t}\n\n\t\tauto c0 = new long[](n+1);\n\t\tauto c1 = new long[](n+1);\n\t\tc0[] = long.max;\n\t\tc1[] = long.max;\n\t\tlong tot;\n\t\tforeach_reverse (i; 0..n)\n\t\t{\n\t\t\tif (i % 2)\n\t\t\t{\n\t\t\t\tc1[i] = min(c1[i+1], b[i+1]);\n\t\t\t\tc0[i] = c0[i+1];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tc0[i] = min(c0[i+1], b[i+1]);\n\t\t\t\tc1[i] = c1[i+1];\n\t\t\t\ttot += a[i];\n\t\t\t}\n\t\t}\n\t\tdebug writeln(b);\n\t\tdebug writeln(c0);\n\t\tdebug writeln(c1);\n\n\t\tans[ti] = tot;\n\t\tforeach (i; 0..n-1)\n\t\t{\n\t\t\tlong x;\n\t\t\tif (i % 2)\n\t\t\t{\n\t\t\t\tx = -(c0[i+1] - b[i]);\n\t\t\t\tdebug writeln(\"i:\", i, \" \", c0[i+1], \" \", b[i]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tx = -(c1[i+1] - b[i]);\n\t\t\t\tdebug writeln(\"i:\", i, \" \", c1[i+1], \" \", b[i]);\n\t\t\t}\n\n\t\t\tans[ti].chmax(tot+x);\n\t\t\tdebug writeln(\"i:\", i, \" \", ans[ti]);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "3696b769bfd32cba34e739b74e13693f"} {"nl": {"description": "Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n.Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.", "input_spec": "The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.", "output_spec": "Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.", "sample_inputs": ["2\n0 1\n1 0", "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0"], "sample_outputs": ["2 1", "2 5 4 1 3"], "notes": "NoteIn the first case, the answer can be {1, 2} or {2, 1}.In the second case, another possible answer is {2, 4, 5, 1, 3}."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.numeric;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tint [] [] a;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\ta ~= readln.split.map !(to !(int)).array;\n\t\t}\n\n\t\tauto p = new int [n];\nmain_loop:\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tforeach (j; 0..n)\n\t\t\t{\n\t\t\t\tforeach (k; j + 1..n)\n\t\t\t\t{\n\t\t\t\t\tif (a[i][j] == a[i][k])\n\t\t\t\t\t{\n\t\t\t\t\t\tp[i] = a[i][j];\n\t\t\t\t\t\tcontinue main_loop;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint v = n - 1;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (p[i] == 0)\n\t\t\t{\n\t\t\t\tp[i] = v;\n\t\t\t\tv++;\n\t\t\t}\n\t\t}\n\n\t\twritefln (\"%(%s %)\", p);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.range, std.string, std.conv;\n\nvoid main() {\n int n = readln.chomp.to!int;\n int[][] t;\n for (int i = 0; i < n; i++) {\n t ~= readln.chomp.split.map!(to!int).array;\n }\n\n int[] ans;\n foreach (e; t) {\n ans ~= e.minPos!(\"a > b\")[0];\n }\n\n ans[ans.length - ans.minPos!(\"a > b\").length] = ans.length.to!int;\n\n writeln(ans.map!(to!string).join(\" \"));\n}"}, {"source_code": "import std.stdio;\nint n,i,j,k;\nint[50][50] v;\nint[50] ans;\nvoid main()\n{\n\tscanf(\" %d\",&n);\n\tfor(i=0;i 0)\n\t{\n\t\tauto f = new int [MAX_L];\n\t\tf[] = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tstring t = readln ().strip ();\n\t\t\tassert (t.length == 3);\n\t\t\tint [3] b;\n\t\t\tforeach (j, c; t)\n\t\t\t{\n\t\t\t\tb[j] = 1 << (c - 'a');\n\t\t\t}\n\t\t\tsort (b[]);\n\t\t\tif (b[0] == b[2])\n\t\t\t{\n\t\t\t\tf[b[0]] += 1;\n\t\t\t}\n\t\t\telse if (b[0] == b[1] || b[1] == b[2])\n\t\t\t{\n\t\t\t\tf[b[0]] += 1;\n\t\t\t\tf[b[2]] += 1;\n\t\t\t\tf[b[0] + b[2]] -= 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tf[b[0]] += 1;\n\t\t\t\tf[b[1]] += 1;\n\t\t\t\tf[b[2]] += 1;\n\t\t\t\tf[b[0] + b[1]] -= 1;\n\t\t\t\tf[b[0] + b[2]] -= 1;\n\t\t\t\tf[b[1] + b[2]] -= 1;\n\t\t\t\tf[b[0] + b[1] + b[2]] += 1;\n\t\t\t}\n\t\t}\n\n\t\tfor (int v = 1; v < MAX_L; v <<= 1)\n\t\t{\n\t\t\timmutable int m = MAX_L - 1 - v;\n\t\t\tint s = m;\n\t\t\tdo\n\t\t\t{\n\t\t\t\tf[s ^ v] += f[s];\n\t\t\t\ts = (s - 1) & m;\n\t\t\t}\n\t\t\twhile (s != m);\n\t\t}\n\n\t\tlong res = 0;\n\t\tforeach (s; 0..MAX_L)\n\t\t{\n\t\t\tlong cur = f[s];\n\t\t\tdebug {if (s < 16) {writeln (s, ' ', cur);}}\n\t\t\tcur *= cur;\n\t\t\tres ^= cur;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "595c586b764a05289e3b82cbbbbe5e83"} {"nl": {"description": "The Great Mushroom King descended to the dwarves, but not everyone managed to see him. Only the few chosen ones could see the King.We know that only LCM(k2l + 1, k2l + 1 + 1, ..., k2r + 1) dwarves can see the Great Mushroom King. Numbers k, l, r are chosen by the Great Mushroom King himself in some complicated manner which is unclear to common dwarves. The dwarven historians decided to document all visits of the Great Mushroom King. For each visit the dwarven historians know three integers ki, li, ri, chosen by the Great Mushroom King for this visit. They also know a prime number pi. Help them to count the remainder of dividing the number of dwarves who can see the King, by number pi, for each visit.", "input_spec": "The first line contains the single integer t (1 ≤ t ≤ 105) — the number of the King's visits. Each of the following t input lines contains four space-separated integers ki, li, ri and pi (1 ≤ ki ≤ 106; 0 ≤ li ≤ ri ≤ 1018; 2 ≤ pi ≤ 109) — the numbers, chosen by the Great Mushroom King and the prime module, correspondingly. It is guaranteed that for all visits number pi is prime. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "For each visit print the answer on a single line — the remainder of dividing the number of the dwarves who can see the King this time, by number pi. Print the answers for the visits in the order, in which the visits are described in the input.", "sample_inputs": ["2\n3 1 10 2\n5 0 4 3"], "sample_outputs": ["0\n0"], "notes": "NoteWe consider that LCM(a1, a2, ..., an) represents the least common multiple of numbers a1, a2, ..., an.We consider that x0 = 1, for any x."}, "positive_code": [{"source_code": "import std.stdio;\n\nlong power(long a, long b, long mod) {\n if (b && ! (a % mod)) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod, b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n int T; readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0); else writeln(1);\n continue;\n }\n ans = (power(k, power(2, l, p - 1) + p - 1, p) + p - 1) % p;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1) + p - 1, p) + p - 1) * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n if (b && ! (a % mod)) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n ans = (power(k, power(2, l, p - 1) + p - 1, p) + p - 1) % p;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1) + p - 1, p) + p - 1) % p * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nlong power(long a, long b, long mod) {\n if (b && ! (a % mod)) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod, b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n int T; readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, q, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p), q = p - 1;\n if (p == 2) {\n if (k & 1) writeln(0); else writeln(1);\n continue;\n }\n ans = (power(k, power(2, l, q) + q, p) + q) % p;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, q) + q, p) + q) * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}"}], "negative_code": [{"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n ans = power(k, power(2, l, p - 1), p) - 1;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1), p) - 1) * inv(ans, p);\n if (k & 1) ans *= power((p >> 1) + 1, r - l, p);\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n ans = power(k, power(2, l, p), p) - 1;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p), p) - 1) * inv(ans, p);\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nlong power(long a, long b, long mod) {\n if (b && ! (a % mod)) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod, b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n int T; readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0); else writeln(1);\n continue;\n }\n ans = power(k, power(2, l, p - 1) + p - 1, p) + p - 1;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1) + p - 1, p) + p - 1) * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n ans = power(k, power(2, l, p), p) - 1;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p), p) - 1) * inv(ans, p);\n if (k & 1) ans *= power((p >> 1) + 1, r - l, p);\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n if (! a) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n k %= p;\n ans = power(k, power(2, l, p - 1), p) + p - 1;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1), p) + p - 1) * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n if (! (a % mod)) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n ans = (power(k, power(2, l, p - 1), p) + p - 1) % p;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1), p) + p - 1) % p * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nint T;\n\nlong power(long a, long b, long mod) {\n if (! a) return 0;\n long ret = 1;\n while (b) {\n if (b & 1) (ret *= a) %= mod;\n (a *= a) %= mod;\n b >>= 1;\n }\n return ret;\n}\n\nlong inv(long a, long p) {\n return power(a, p - 2, p);\n}\n\nint main() {\n readf(\" %d\", &T);\n while (T--) {\n long k, l, r, p, ans;\n readf(\" %d %d %d %d\", &k, &l, &r, &p);\n if (p == 2) {\n if (k & 1) writeln(0);\n else writeln(1);\n continue;\n }\n k %= p;\n ans = (power(k, power(2, l, p - 1), p) + p - 1) % p;\n if (! ans) ans = power(2, r - l + 1, p);\n else ans = (power(k, power(2, r + 1, p - 1), p) + p - 1) % p * inv(ans, p) % p;\n if (k & 1) (ans *= power((p >> 1) + 1, r - l, p)) %= p;\n writeln(ans);\n }\n return 0;\n}\n"}], "src_uid": "1c0dbbcfbf5e9ded42e86660272dc8e3"} {"nl": {"description": "Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively \"Loves\" and \"Doesn't love\", at that Marina always starts with \"Loves\". There are n camomiles growing in the field, possessing the numbers of petals equal to a1, a2, ... an. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be \"Loves\". Help her do that; find the maximal number of petals possible in the bouquet.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100), which is the number of flowers growing in the field. The second line contains n integers ai (1 ≤ ai ≤ 100) which represent the number of petals on a given i-th camomile.", "output_spec": "Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in \"Loves\". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.", "sample_inputs": ["1\n1", "1\n2", "3\n5 6 7"], "sample_outputs": ["1", "0", "13"], "notes": null}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string[] tk;\n string readString() {\n while (tk.empty) \n tk = readln.split;\n auto tkt = tk.front;\n tk.popFront;\n return tkt;\n }\n int readInt() { \n return readString.to!int; \n }\n double readDouble() { \n return readString.to!double; \n }\n}\n\nvoid main() {\n IO cin;\n int n_Cases = 1;\n // n_Cases = cin.readInt;\n \n\tfor (int case_i = 1; case_i <= n_Cases; case_i++) {\n int n = cin.readInt;\n int[] arr = new int[n];\n int sum = 0;\n int smallestOdd = int.max;\n foreach (ref i; arr) {\n i = cin.readInt;\n sum += i;\n if ((i % 2) && i < smallestOdd) {\n smallestOdd = i;\n }\n }\n writeln((sum & 1 ? sum : sum - (smallestOdd == int.max ? sum : smallestOdd)));\n\t} \n}"}], "negative_code": [], "src_uid": "a5d56056fd66713128616bc7c2de8b22"} {"nl": {"description": "Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!", "input_spec": "The first line contains single integer t (1 ≤ t ≤ 10) — the number of chats. The t chats follow. Each chat is given in the following format. The first line of each chat description contains single integer n (1 ≤ n ≤ 100) — the number of users in the chat. The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer m (1 ≤ m ≤ 100) — the number of messages in the chat. The next m line contain the messages in the following formats, one per line: <username>:<text> — the format of a message with known sender. The username should appear in the list of usernames of the chat. <?>:<text> — the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text \"Vasya, masha13 and Kate!\" can mention users \"Vasya\", \"masha13\", \"and\" and \"Kate\", but not \"masha\". It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.", "output_spec": "Print the information about the t chats in the following format: If it is not possible to recover senders, print single line \"Impossible\" for this chat. Otherwise print m messages in the following format: <username>:<text> If there are multiple answers, print any of them.", "sample_inputs": ["1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course."], "sample_outputs": ["netman: Hello, Vladik!\nVladik: Hi", "Impossible", "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course."], "notes": null}, "positive_code": [{"source_code": "import std.stdio,std.range,std.algorithm,std.regex,std.conv,std.string;\nbool input(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\nvoid main()\n{\n\tint t;\n\twhile(input(&t))\n\t{\n\t\tforeach(gg;0..t)\n\t\t{\n\t\t\tint n;\n\t\t\tinput(&n);\n\t\t\tauto aut=arread!string;\n\t\t\tint m;\n\t\t\tinput(&m);\n\t\t\tauto a=new int[m], g=new int[][m],mes=new string[m];\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tauto s=readln.strip;\n\t\t\t\tmes[i]=s;\n\t\t\t\tif(s[0]=='?')\n\t\t\t\t{\n\t\t\t\t\ta[i]=-1;\n\t\t\t\t\tauto f=s[2..$].splitter(ctRegex!(\"[ ?.,:!]+\"));\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tauto f=s.splitter(ctRegex!(\"[ ?.,:!]+\"));;\n\t\t\t\t\ta[i]=aut.countUntil(f.front);\n\t\t\t\t\tf.popFront;\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint[105][105] dp, table;\n\t\t\tforeach(ref i;table)i[]=-1;\n\t\t\tint go(int p,int prev)\n\t\t\t{\n\t\t\t\tif(p==m)return true;\n\t\t\t\tint ans=table[p][prev];\n\t\t\t\tif(ans!=-1)return ans;\n\t\t\t\tans=0;\n\t\t\t\tforeach(i;0..n)\n\t\t\t\t{\n\t\t\t\t\tif((p!=0 && prev==i) || (a[p]!=-1 && a[p]!=i) || g[p].count(i)>0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}, {"source_code": "import std.stdio,std.range,std.algorithm,std.string,std.regex,std.conv;\nbool input(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\n@property auto arread(T)(){return readln.split.map!(to!(T)).array;}\nvoid main()\n{\n\tint t;\n\tloop:while(input(&t))\n\t{\n\t\tforeach(gg;0..t)\n\t\t{\n\t\t\tint n;\n\t\t\tinput(&n);\n\t\t\treadln;\n\t\t\tauto aut=arread!string;\n\t\t\tint m;\n\t\t\tinput(&m);\n\t\t\treadln;\n\t\t\tauto a=new int[m];\n\t\t\tauto g=new int[][m];\n\t\t\tauto mes=new string[m];\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tauto s=readln.strip;\n\t\t\t\tmes[i]=s;\n\t\t\t\tif(s[0]=='?')\n\t\t\t\t{\n\t\t\t\t\ta[i]=-1;\n\t\t\t\t\tauto f=s[2..$].strip.split(regex(\"[ ?.,:!]+\"));\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tauto f=s.strip.split(regex(\"[ ?.,:!]+\"));\n\t\t\t\t\ta[i]=aut.countUntil(f.front);\n\t\t\t\t\tf.popFront;\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint[105][105] dp, table;\n\t\t\tforeach(ref i;table)i[]=-1;\n\t\t\tint go(int p,int prev)\n\t\t\t{\n\t\t\t\tif(p==m)return true;\n\t\t\t\tint ans=table[p][prev];\n\t\t\t\tif(ans!=-1)return ans;\n\t\t\t\tans=0;\n\t\t\t\tforeach(i;0..n)\n\t\t\t\t{\n\t\t\t\t\tif((p!=0 && prev==i) || (a[p]!=-1 && a[p]!=i) || g[p].count(i)>0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.variant;\nimport core.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\nimport std.stdio;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;\nalias redBlackTree rbt;\nalias BigInt big;\nalias pair!(int,int) pii;\nalias pair!(long,long) pll;\nalias BoyerMooreFinder strfind;\nalias mp=make_pair;\nalias bins=binary_search;\nalias orsq=orient_square;\nimmutable int mod=1000000007;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b)\n\t{\n\t\treturn a/gcd(a,b)*b;\n\t}\n\tX binpow(X,Y)(X base,Y exp)\n\t\tif(is(typeof(exp&1)))\n\t{\n\t\tif(exp<0)return X(0);\n\t\tX res=X(1);\n\t\twhile(exp>0)\n\t\t{\n\t\t\tif(exp&1)res*=base;\n\t\t\tbase*=base;\n\t\t\texp>>=1;\n\t\t}\n\t\treturn res;\n\t}\n\tauto binpow(X,Y,Z)(X base,Y exp,in Z mm)\n\t\tif(is(typeof(exp&1)))\n\t{\n\t\tif(mm==0) return binpow(base,exp);\n\t\tif(exp<0)return X(0);\n\t\tauto res=X(1)%mm;\n\t\twhile(exp>0)\n\t\t{\n\t\t\tif(exp&1)res=(res*base)%mm;\n\t\t\tbase*=base;\n\t\t\tbase%=mm;\n\t\t\texp>>=1;\n\t\t}\n\t\treturn res;\n\t}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) \n\t\tif(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nstruct pair(X,Y)\n{\n\tX first;\n\tY second;\n\talias first fi;\n\talias second se;\n\tthis(X a_,Y b_){\n\t\tfi=a_;\n\t\tse=b_;\n\t}\n\tthis(this){fi=fi;se=se;}\n\tthis(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))\n\t{\n\t\tfi=p.fi;\n\t\tse=p.se;\n\t}\n\tpair!(X,Y) opAssign(A,B)(pair!(A,B) val)\n\t{\n\t\treturn this=pair!(X,Y)(val);\n\t}\n\tint opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe \n\t\tif(is(typeof(s_.fis_.fi);\n\t\tif(cmp!=0)return cmp;\n\t\telse return (0-(ses_.se));\n\t}\n};\npair!(X,Y) make_pair(X,Y)(in X x_,in Y y_) pure nothrow @safe\n{\n\tpair!(X,Y) pp;\n\tpp.fi=x_;\n\tpp.se=y_;\n\treturn pp;\n}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.split.map!(to!(T)).array;}\nstruct Tree(T,alias arg=\"a+b\")\n{\n\tprivate alias fun=binaryFun!arg;\n\tprivate static const size_t n=size_t.max>>1;\n\tprivate T[size_t] t;\n\tthis(this){t=t.dup;}\n\tthis(R)(R range) if(isInputRange!R && is(ElementType!(R) : T))\n\t{\n\t\tforeach(pos,i;range) upd(pos,i);\n\t}\n\tthis(R,alias f)(Tree!(R,f) q) if(is(R:T))\n\t{\n\t\tforeach(i,j;q.t)\n\t\t{\n\t\t\tt[i]=j;\n\t\t}\n\t}\n\tTree!(T,arg) opAssign(R)(R val)\n\t{\n\t\treturn this=Tree!(T,arg)(val);\n\t}\n\tprivate void upd(in size_t v,in size_t vl,in size_t vr,in size_t i,in T x)\n\t{\n\t\tif (vr - vl == 1){t[v] = x;return;}\n\t\tsize_t vm = (vl + vr) >> 1;\n\t\tif(i < vm) upd(2*v, vl, vm, i, x);\n\t\telse upd(2*v+1, vm, vr, i, x);\n\t\tif(v*2+1 !in t)t[v]=t[v*2];\n\t\telse if (v*2 !in t)t[v]=t[v*2+1];\n\t\telse t[v]=fun(t[v*2],t[v*2+1]);\n\t}\n\tvoid upd(in size_t i,in T x)\n\t{\n\t\tassert(i>=0 && i> 1;\n\t\tif(r<=vm) return cel(2*v,vl,vm,l,r);\n\t\telse if(l>=vm) return cel(2*v+1,vm,vr,l,r);\n\t\telse return fun(cel(2*v,vl,vm,l,vm),cel(2*v+1,vm,vr,vm,r));\n\t}\n\tT cel(in size_t l,in size_t r){\n\t\tassert(l<=r && l>=0 && r0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.variant;\nimport core.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\nimport std.stdio;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;\nalias redBlackTree rbt;\nalias BigInt big;\nalias pair!(int,int) pii;\nalias pair!(long,long) pll;\nalias BoyerMooreFinder strfind;\nalias mp=make_pair;\nalias bins=binary_search;\nalias orsq=orient_square;\nimmutable int mod=1000000007;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b)\n\t{\n\t\treturn a/gcd(a,b)*b;\n\t}\n\tX binpow(X,Y)(X base,Y exp)\n\t\tif(is(typeof(exp&1)))\n\t{\n\t\tif(exp<0)return X(0);\n\t\tX res=X(1);\n\t\twhile(exp>0)\n\t\t{\n\t\t\tif(exp&1)res*=base;\n\t\t\tbase*=base;\n\t\t\texp>>=1;\n\t\t}\n\t\treturn res;\n\t}\n\tauto binpow(X,Y,Z)(X base,Y exp,in Z mm)\n\t\tif(is(typeof(exp&1)))\n\t{\n\t\tif(mm==0) return binpow(base,exp);\n\t\tif(exp<0)return X(0);\n\t\tauto res=X(1)%mm;\n\t\twhile(exp>0)\n\t\t{\n\t\t\tif(exp&1)res=(res*base)%mm;\n\t\t\tbase*=base;\n\t\t\tbase%=mm;\n\t\t\texp>>=1;\n\t\t}\n\t\treturn res;\n\t}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) \n\t\tif(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m]1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nstruct pair(X,Y)\n{\n\tX first;\n\tY second;\n\talias first fi;\n\talias second se;\n\tthis(X a_,Y b_){\n\t\tfi=a_;\n\t\tse=b_;\n\t}\n\tthis(this){fi=fi;se=se;}\n\tthis(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))\n\t{\n\t\tfi=p.fi;\n\t\tse=p.se;\n\t}\n\tpair!(X,Y) opAssign(A,B)(pair!(A,B) val)\n\t{\n\t\treturn this=pair!(X,Y)(val);\n\t}\n\tint opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe \n\t\tif(is(typeof(s_.fis_.fi);\n\t\tif(cmp!=0)return cmp;\n\t\telse return (0-(ses_.se));\n\t}\n};\npair!(X,Y) make_pair(X,Y)(in X x_,in Y y_) pure nothrow @safe\n{\n\tpair!(X,Y) pp;\n\tpp.fi=x_;\n\tpp.se=y_;\n\treturn pp;\n}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.split.map!(to!(T)).array;}\nstruct Tree(T,alias arg=\"a+b\")\n{\n\tprivate alias fun=binaryFun!arg;\n\tprivate static const size_t n=size_t.max>>1;\n\tprivate T[size_t] t;\n\tthis(this){t=t.dup;}\n\tthis(R)(R range) if(isInputRange!R && is(ElementType!(R) : T))\n\t{\n\t\tforeach(pos,i;range) upd(pos,i);\n\t}\n\tthis(R,alias f)(Tree!(R,f) q) if(is(R:T))\n\t{\n\t\tforeach(i,j;q.t)\n\t\t{\n\t\t\tt[i]=j;\n\t\t}\n\t}\n\tTree!(T,arg) opAssign(R)(R val)\n\t{\n\t\treturn this=Tree!(T,arg)(val);\n\t}\n\tprivate void upd(in size_t v,in size_t vl,in size_t vr,in size_t i,in T x)\n\t{\n\t\tif (vr - vl == 1){t[v] = x;return;}\n\t\tsize_t vm = (vl + vr) >> 1;\n\t\tif(i < vm) upd(2*v, vl, vm, i, x);\n\t\telse upd(2*v+1, vm, vr, i, x);\n\t\tif(v*2+1 !in t)t[v]=t[v*2];\n\t\telse if (v*2 !in t)t[v]=t[v*2+1];\n\t\telse t[v]=fun(t[v*2],t[v*2+1]);\n\t}\n\tvoid upd(in size_t i,in T x)\n\t{\n\t\tassert(i>=0 && i> 1;\n\t\tif(r<=vm) return cel(2*v,vl,vm,l,r);\n\t\telse if(l>=vm) return cel(2*v+1,vm,vr,l,r);\n\t\telse return fun(cel(2*v,vl,vm,l,vm),cel(2*v+1,vm,vr,vm,r));\n\t}\n\tT cel(in size_t l,in size_t r){\n\t\tassert(l<=r && l>=0 && r0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}, {"source_code": "import std.stdio,std.range,std.algorithm,std.string,std.regex,std.conv;\nbool input(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\n@property auto arread(T)(){return readln.split.map!(to!(T)).array;}\nvoid main()\n{\n\tint t;\n\twhile(input(&t))\n\t{\n\t\tforeach(gg;0..t)\n\t\t{\n\t\t\tint n;\n\t\t\tinput(&n);\n\t\t\treadln;\n\t\t\tauto aut=arread!string;\n\t\t\tint m;\n\t\t\tinput(&m);\n\t\t\treadln;\n\t\t\tauto a=new int[m], g=new int[][m],mes=new string[m];\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tauto s=readln.strip;\n\t\t\t\tmes[i]=s;\n\t\t\t\tif(s[0]=='?')\n\t\t\t\t{\n\t\t\t\t\ta[i]=-1;\n\t\t\t\t\tauto f=s[2..$].split(regex(\"[ ?.,:!]+\"));\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tauto f=s.split(regex(\"[ ?.,:!]+\"));\n\t\t\t\t\ta[i]=aut.countUntil(f.front);\n\t\t\t\t\tf.popFront;\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint[105][105] dp, table;\n\t\t\tforeach(ref i;table)i[]=-1;\n\t\t\tint go(int p,int prev)\n\t\t\t{\n\t\t\t\tif(p==m)return true;\n\t\t\t\tint ans=table[p][prev];\n\t\t\t\tif(ans!=-1)return ans;\n\t\t\t\tans=0;\n\t\t\t\tforeach(i;0..n)\n\t\t\t\t{\n\t\t\t\t\tif((p!=0 && prev==i) || (a[p]!=-1 && a[p]!=i) || g[p].count(i)>0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}, {"source_code": "import std.stdio,std.range,std.algorithm,std.regex,std.conv,std.string;\nbool input(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\nvoid main()\n{\n\tint t;\n\twhile(input(&t))\n\t{\n\t\tforeach(gg;0..t)\n\t\t{\n\t\t\tint n;\n\t\t\tinput(&n);\n\t\t\tauto aut=arread!string;\n\t\t\tint m;\n\t\t\tinput(&m);\n\t\t\tauto a=new int[m], g=new int[][m],mes=new string[m];\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tauto s=readln.strip;\n\t\t\t\tmes[i]=s;\n\t\t\t\tif(s[0]=='?')\n\t\t\t\t{\n\t\t\t\t\ta[i]=-1;\n\t\t\t\t\tauto f=s[2..$].split(regex(\"[ ?.,:!]+\"));\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tauto f=s.split(regex(\"[ ?.,:!]+\"));;\n\t\t\t\t\ta[i]=aut.countUntil(f.front);\n\t\t\t\t\tf.popFront;\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint[105][105] dp, table;\n\t\t\tforeach(ref i;table)i[]=-1;\n\t\t\tint go(int p,int prev)\n\t\t\t{\n\t\t\t\tif(p==m)return true;\n\t\t\t\tint ans=table[p][prev];\n\t\t\t\tif(ans!=-1)return ans;\n\t\t\t\tans=0;\n\t\t\t\tforeach(i;0..n)\n\t\t\t\t{\n\t\t\t\t\tif((p!=0 && prev==i) || (a[p]!=-1 && a[p]!=i) || g[p].count(i)>0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}], "negative_code": [{"source_code": "import std.stdio,std.range,std.algorithm,std.regex,std.conv;\nbool input(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\nvoid main()\n{\n\tint t;\n\twhile(input(&t))\n\t{\n\t\tforeach(gg;0..t)\n\t\t{\n\t\t\tint n;\n\t\t\tinput(&n);\n\t\t\tauto aut=arread!string;\n\t\t\tint m;\n\t\t\tinput(&m);\n\t\t\tauto a=new int[m], g=new int[][m],mes=new string[m];\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tauto s=readln;\n\t\t\t\tmes[i]=s;\n\t\t\t\tif(s[0]=='?')\n\t\t\t\t{\n\t\t\t\t\ta[i]=-1;\n\t\t\t\t\tauto f=s[2..$].splitter(ctRegex!(\"[ ?.,:!\\n]+\"));\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tauto f=s.splitter(ctRegex!(\"[ ?.,:!\\n]+\"));;\n\t\t\t\t\ta[i]=aut.countUntil(f.front);\n\t\t\t\t\tf.popFront;\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint[105][105] dp, table;\n\t\t\tforeach(ref i;table)i[]=-1;\n\t\t\tint go(int p,int prev)\n\t\t\t{\n\t\t\t\tif(p==m)return true;\n\t\t\t\tint ans=table[p][prev];\n\t\t\t\tif(ans!=-1)return ans;\n\t\t\t\tans=0;\n\t\t\t\tforeach(i;0..n)\n\t\t\t\t{\n\t\t\t\t\tif((p!=0 && prev==i) || (a[p]!=-1 && a[p]!=i) || g[p].count(i)>0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}, {"source_code": "import std.stdio,std.range,std.algorithm,std.regex,std.conv;\nbool input(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~'\\n', ptrs)==ptrs.length;}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\nvoid main()\n{\n\tint t;\n\twhile(input(&t))\n\t{\n\t\tforeach(gg;0..t)\n\t\t{\n\t\t\tint n;\n\t\t\tinput(&n);\n\t\t\tauto aut=arread!string;\n\t\t\tint m;\n\t\t\tinput(&m);\n\t\t\tauto a=new int[m], g=new int[][m],mes=new string[m];\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tauto s=readln;\n\t\t\t\tmes[i]=s;\n\t\t\t\tif(s[0]=='?')\n\t\t\t\t{\n\t\t\t\t\ta[i]=-1;\n\t\t\t\t\tauto f=s[2..$].splitter(ctRegex!(\"[ ?.,:!]+\"));\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tauto f=s.split(regex(\"[ ?.,:!]+\"));\n\t\t\t\t\ta[i]=aut.countUntil(f.front);\n\t\t\t\t\tf.popFront;\n\t\t\t\t\tforeach(j;f)\n\t\t\t\t\t{\n\t\t\t\t\t\tint k=countUntil(aut,j);\n\t\t\t\t\t\tif(k!=-1)g[i]~=k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint[105][105] dp, table;\n\t\t\tforeach(ref i;table)i[]=-1;\n\t\t\tint go(int p,int prev)\n\t\t\t{\n\t\t\t\tif(p==m)return true;\n\t\t\t\tint ans=table[p][prev];\n\t\t\t\tif(ans!=-1)return ans;\n\t\t\t\tans=0;\n\t\t\t\tforeach(i;0..n)\n\t\t\t\t{\n\t\t\t\t\tif((p!=0 && prev==i) || (a[p]!=-1 && a[p]!=i) || g[p].count(i)>0)continue;\n\t\t\t\t\tif(go(p+1,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tans=true;\n\t\t\t\t\t\tdp[p][prev]=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn table[p][prev]=ans;\n\t\t\t}\n\t\t\tauto o=go(0,0);\n\t\t\tif(!o){writeln(\"Impossible\");continue;}\n\t\t\tint cur=0;\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tint x=dp[i][cur];\n\t\t\t\ta[i]=cur=x;\n\t\t\t}\n\t\t\tforeach(i;0..m)\n\t\t\t{\n\t\t\t\tif(mes[i][0]=='?')writeln(aut[a[i]],mes[i][1..$]);\n\t\t\t\telse writeln(mes[i]);\n\t\t\t}\n\t\t}\n\t}\n}"}], "src_uid": "3ac91d8fc508ee7d1afcf22ea4b930e4"} {"nl": {"description": "Riley is a very bad boy, but at the same time, he is a yo-yo master. So, he decided to use his yo-yo skills to annoy his friend Anton.Anton's room can be represented as a grid with $$$n$$$ rows and $$$m$$$ columns. Let $$$(i, j)$$$ denote the cell in row $$$i$$$ and column $$$j$$$. Anton is currently standing at position $$$(i, j)$$$ in his room. To annoy Anton, Riley decided to throw exactly two yo-yos in cells of the room (they can be in the same cell).Because Anton doesn't like yo-yos thrown on the floor, he has to pick up both of them and return back to the initial position. The distance travelled by Anton is the shortest path that goes through the positions of both yo-yos and returns back to $$$(i, j)$$$ by travelling only to adjacent by side cells. That is, if he is in cell $$$(x, y)$$$ then he can travel to the cells $$$(x + 1, y)$$$, $$$(x - 1, y)$$$, $$$(x, y + 1)$$$ and $$$(x, y - 1)$$$ in one step (if a cell with those coordinates exists).Riley is wondering where he should throw these two yo-yos so that the distance travelled by Anton is maximized. But because he is very busy, he asked you to tell him.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of each test case contains four integers $$$n$$$, $$$m$$$, $$$i$$$, $$$j$$$ ($$$1 \\leq n, m \\leq 10^9$$$, $$$1\\le i\\le n$$$, $$$1\\le j\\le m$$$) — the dimensions of the room, and the cell at which Anton is currently standing.", "output_spec": "For each test case, print four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \\leq x_1, x_2 \\leq n$$$, $$$1\\le y_1, y_2\\le m$$$) — the coordinates of where the two yo-yos should be thrown. They will be thrown at coordinates $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$. If there are multiple answers, you may print any.", "sample_inputs": ["7\n2 3 1 1\n4 4 1 2\n3 5 2 2\n5 1 2 1\n3 1 3 1\n1 1 1 1\n1000000000 1000000000 1000000000 50"], "sample_outputs": ["1 2 2 3\n4 1 4 4\n3 1 1 5\n5 1 1 1\n1 1 2 1\n1 1 1 1\n50 1 1 1000000000"], "notes": "NoteHere is a visualization of the first test case. "}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.typecons, std.math;\n\nalias Point = Tuple!(long, long);\n\nlong distance(Point a, Point b)\n{\n return abs(a[0] - b[0]) + abs(a[1] - b[1]);\n}\n\nlong distance3(Point start, Point a, Point b)\n{\n// writeln(start);\n long tmp1 = distance(start, a) + distance(a, b);\n long tmp2 = distance(start, b) + distance(b, a);\n// long tmp3 = distance(start, a) + distance(start, b);\n// writeln(tmp1);\n// writeln(tmp2);\n// writeln(tmp3);\n// return min(tmp1, tmp2, tmp3);\n return min(tmp1, tmp2);\n}\n\nvoid main()\n{\n long t;\n readf!\" %d \"(t);\n\n while (t--) {\n long n, m, i, j;\n readf!\" %d %d %d %d \"(n, m, i, j);\n i -= 1;\n j -= 1;\n auto start = Point(i, j);\n Point c0 = Point(0, 0);\n Point c1 = Point(0, m - 1);\n Point c2 = Point(n - 1, 0);\n Point c3 = Point(n - 1, m - 1);\n\n Point c4 = Point(i, 0);\n Point c5 = Point(i, m - 1);\n\n// auto x = [c0, c1, c2, c3];\n// writeln(x.permutations);\n long max = -1;\n Point p1, p2;\n foreach (x ; [c0, c1, c2, c3].permutations) {\n long dist = distance3(start, x[0], x[1]);\n if (dist > max) {\n max = dist;\n p1 = x[0];\n p2 = x[1];\n }\n }\n writefln(\"%d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1);\n// writefln(\"%d %d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1, max);\n }\n// writeln(distance3(Point(1000000000 - 1, 50 - 1), Point(50 - 1, 1 - 1), Point(1 - 1, 1000000000 - 1)));\n// writeln(distance3(Point(1000000000 - 1, 50 - 1), Point(50 - 1, 1 - 1), Point(1 - 1, 1000000000 - 1)));\n}\n"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1537/problem/B\n// \nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n long t = readln.chomp.to!long;\n\nwhile(t--) {\n long n, m, i, j;\n readf(\"%s %s %s %s\\n\", &n, &m, &i, &j);\n if(m - j > j - 1) {\n writefln(\"%s %s %s %s\", n, 1, 1, m);\n } else {\n writefln(\"%s %s %s %s\", n, m, 1, 1);\n }\n}\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.typecons, std.math;\n\nalias Point = Tuple!(long, long);\n\nlong distance(Point a, Point b)\n{\n return abs(a[0] - b[0]) + abs(a[1] - b[1]);\n}\n\nlong distance3(Point start, Point a, Point b)\n{\n// writeln(start);\n long tmp1 = distance(start, a) + distance(a, b);\n long tmp2 = distance(start, b) + distance(b, a);\n long tmp3 = distance(start, a) + distance(start, b);\n// writeln(tmp1);\n// writeln(tmp2);\n// writeln(tmp3);\n return min(tmp1, tmp2, tmp3);\n}\n\nvoid main()\n{\n long t;\n readf!\" %d \"(t);\n\n while (t--) {\n long n, m, i, j;\n readf!\" %d %d %d %d \"(n, m, i, j);\n i -= 1;\n j -= 1;\n auto start = Point(i, j);\n Point c0 = Point(0, 0);\n Point c1 = Point(0, m - 1);\n Point c2 = Point(n - 1, 0);\n Point c3 = Point(n - 1, m - 1);\n\n Point c4 = Point(i, 0);\n Point c5 = Point(i, m - 1);\n\n// auto x = [c0, c1, c2, c3];\n// writeln(x.permutations);\n long max = -1;\n Point p1, p2;\n foreach (x ; [c0, c1, c2, c3, c0, c1, c2, c3].permutations) {\n long dist = distance3(start, x[0], x[1]);\n if (dist > max) {\n max = dist;\n p1 = x[0];\n p2 = x[1];\n }\n }\n writefln(\"%d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1);\n// writefln(\"%d %d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1, max);\n }\n// writeln(distance3(Point(1000000000 - 1, 50 - 1), Point(50 - 1, 1 - 1), Point(1 - 1, 1000000000 - 1)));\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.typecons, std.math;\n\nalias Point = Tuple!(long, long);\n\nlong distance(Point a, Point b)\n{\n return abs(a[0] - b[0]) + abs(a[1] - b[1]);\n}\n\nlong distance3(Point start, Point a, Point b)\n{\n writeln(start);\n long tmp1 = distance(start, a) + distance(a, b);\n long tmp2 = distance(start, b) + distance(b, a);\n long tmp3 = distance(start, a) + distance(start, b);\n writeln(tmp1);\n writeln(tmp2);\n writeln(tmp3);\n return min(tmp1, tmp2, tmp3);\n}\n\nvoid main()\n{\n long t;\n readf!\" %d \"(t);\n\n while (t--) {\n long n, m, i, j;\n readf!\" %d %d %d %d \"(n, m, i, j);\n i -= 1;\n j -= 1;\n auto start = Point(i, j);\n Point c0 = Point(0, 0);\n Point c1 = Point(0, m - 1);\n Point c2 = Point(n - 1, 0);\n Point c3 = Point(n - 1, m - 1);\n\n Point c4 = Point(i, 0);\n Point c5 = Point(i, m - 1);\n\n// auto x = [c0, c1, c2, c3];\n// writeln(x.permutations);\n long max = -1;\n Point p1, p2;\n foreach (x ; [c0, c1, c2, c3, c0, c1, c2, c3].permutations) {\n long tmp1 = distance(start, x[0]) + distance(x[0], x[1]);\n long tmp2 = distance(start, x[1]) + distance(x[1], x[0]);\n long tmp3 = distance(start, x[0]) + distance(start, x[1]);\n if (min(tmp1, tmp2, tmp3) > max) {\n max = min(tmp1, tmp2, tmp3);\n p1 = x[0];\n p2 = x[1];\n }\n }\n writefln(\"%d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1);\n// writefln(\"%d %d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1, max);\n }\n// writeln(distance3(Point(1000000000 - 1, 50 - 1), Point(50 - 1, 1 - 1), Point(1 - 1, 1000000000 - 1)));\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.typecons, std.math;\n\nalias Point = Tuple!(int, int);\n\nint distance(Point a, Point b)\n{\n return abs(a[0] - b[0]) + abs(a[1] - b[1]);\n}\n\nvoid main()\n{\n int t;\n readf!\" %d \"(t);\n\n while (t--) {\n int n, m, i, j;\n readf!\" %d %d %d %d \"(n, m, i, j);\n i -= 1;\n j -= 1;\n auto start = Point(i, j);\n Point c0 = Point(0, 0);\n Point c1 = Point(0, m - 1);\n Point c2 = Point(n - 1, 0);\n Point c3 = Point(n - 1, m - 1);\n// auto x = [c0, c1, c2, c3];\n// writeln(x.permutations);\n int max = -1;\n Point p1, p2;\n foreach (x ; [c0, c1, c2, c3, c0, c1, c2, c3].permutations) {\n int tmp1 = distance(start, x[0]) + distance(x[0], x[1]);\n int tmp2 = distance(start, x[1]) + distance(x[1], x[0]);\n int tmp3 = distance(start, x[0]) + distance(start, x[1]);\n if (min(tmp1, tmp2, tmp3) > max) {\n max = min(tmp1, tmp2, tmp3);\n p1 = x[0];\n p2 = x[1];\n }\n }\n writefln(\"%d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1);\n }\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.typecons, std.math;\n\nalias Point = Tuple!(int, int);\n\nint distance(Point a, Point b)\n{\n return abs(a[0] - b[0]) + abs(a[1] - b[1]);\n}\n\nvoid main()\n{\n int t;\n readf!\" %d \"(t);\n\n while (t--) {\n int n, m, i, j;\n readf!\" %d %d %d %d \"(n, m, i, j);\n i -= 1;\n j -= 1;\n auto start = Point(i, j);\n Point c0 = Point(0, 0);\n Point c1 = Point(0, m - 1);\n Point c2 = Point(n - 1, 0);\n Point c3 = Point(n - 1, m - 1);\n// auto x = [c0, c1, c2, c3];\n// writeln(x.permutations);\n int max = -1;\n Point p1, p2;\n foreach (x ; [c0, c1, c2, c3, c0, c1, c2, c3].permutations) {\n int tmp1 = distance(start, x[0]) + distance(x[0], x[1]);\n int tmp2 = distance(start, x[1]) + distance(x[0], x[1]);\n if (min(tmp1, tmp2) > max) {\n max = min(tmp1, tmp2);\n p1 = x[0];\n p2 = x[1];\n }\n }\n writefln(\"%d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1);\n }\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.typecons, std.math;\n\nalias Point = Tuple!(int, int);\n\nint distance(Point a, Point b)\n{\n return abs(a[0] - b[0]) + abs(a[1] - b[1]);\n}\n\nvoid main()\n{\n int t;\n readf!\" %d \"(t);\n\n while (t--) {\n int n, m, i, j;\n readf!\" %d %d %d %d \"(n, m, i, j);\n auto start = Point(i, j);\n Point c0 = Point(0, 0);\n Point c1 = Point(0, m - 1);\n Point c2 = Point(n - 1, 0);\n Point c3 = Point(n - 1, m - 1);\n// auto x = [c0, c1, c2, c3];\n// writeln(x.permutations);\n int max = -1;\n Point p1, p2;\n foreach (x ; [c0, c1, c2, c3].permutations) {\n int tmp1 = distance(start, x[0]) + distance(x[0], x[1]);\n int tmp2 = distance(start, x[1]) + distance(x[0], x[1]);\n if (min(tmp1, tmp2) > max) {\n max = min(tmp1, tmp2);\n p1 = x[0];\n p2 = x[1];\n }\n }\n writefln(\"%d %d %d %d\", p1[0] + 1, p1[1] + 1, p2[0] + 1, p2[1] + 1);\n }\n}\n"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1537/problem/B\n// \nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n long t = readln.chomp.to!long;\n\nwhile(t--) {\n long n, m, i, j;\n readf(\"%s %s %s %s\\n\", &n, &m, &i, &j);\n if(n - i < i - 1) {\n writefln(\"%s %s %s %s\", n, n, 1, 1);\n } else {\n writefln(\"%s %s %s %s\", 1, n, n, 1);\n }\n}\n}\n"}], "src_uid": "5aae6b27f35852512a250751ef957ab9"} {"nl": {"description": "You are given the array $$$a$$$ consisting of $$$n$$$ positive (greater than zero) integers.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) such that the absolute difference between $$$a_i$$$ and $$$a_j$$$ is no more than one ($$$|a_i - a_j| \\le 1$$$) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "For each test case, print the answer: \"YES\" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or \"NO\" otherwise.", "sample_inputs": ["5\n3\n1 2 2\n4\n5 5 5 5\n3\n1 2 4\n4\n1 3 4 4\n1\n100"], "sample_outputs": ["YES\nYES\nNO\nNO\nYES"], "notes": "NoteIn the first test case of the example, we can perform the following sequence of moves: choose $$$i=1$$$ and $$$j=3$$$ and remove $$$a_i$$$ (so $$$a$$$ becomes $$$[2; 2]$$$); choose $$$i=1$$$ and $$$j=2$$$ and remove $$$a_j$$$ (so $$$a$$$ becomes $$$[2]$$$). In the second test case of the example, we can choose any possible $$$i$$$ and $$$j$$$ any move and it doesn't matter which element we remove.In the third test case of the example, there is no way to get rid of $$$2$$$ and $$$4$$$."}, "positive_code": [{"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nDList!string _words;\n\nvoid read(T...)(ref T t)\n{\n void singleRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n static foreach(i; 0 .. T.length)\n {\n static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))\n {\n foreach(ref element; t[i])\n read(element);\n }\n else static if (isAggregateType!(T[i]))\n {\n static foreach(fieldName; FieldNameTuple!(T[i]))\n {\n static if (hasUDA!(mixin(text(T[i].stringof, \".\", fieldName)), string))\n {\n with(t[i])\n {\n mixin(q{t[i].}, fieldName, q{.length}) =\n cast(size_t) mixin(getUDAs!(mixin(text(T[i].stringof, \".\", fieldName)), string)[0]);\n }\n }\n read(mixin(text(q{t[i].}, fieldName)));\n }\n }\n else\n {\n singleRead(t[i]);\n }\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\nE[] makeSlice(E, S)(S size, lazy E value)\n{\n auto a = new E[size.ind];\n foreach(ref ai; a)\n ai = value();\n return a;\n}\n\nE[] makeSlice(E, S)(S size = 0)\n{\n return new E[size.ind];\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n testCase:foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nalias type = multi;\nstruct TestCase\n{\n long n;\n @(\"n\") long[] a;\n\n void solve(long i = -1)\n {\n sort(a);\n if (iota(0, n - 1).all!(i => a.at(i + 1) - a.at(i) <= 1))\n writeln(\"YES\");\n else\n writeln(\"NO\");\n }\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\t\ta.sort();\n\n\t\tbool ok = true;\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\tif (a[i] - a[i-1] > 1)\n\t\t\t{\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tans[ti] = ok;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e ? \"YES\" : \"NO\");\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nDList!string _words;\n\nvoid read(T...)(ref T t)\n{\n void singleRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n static foreach(i; 0 .. T.length)\n {\n static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))\n {\n foreach(ref element; t[i])\n read(element);\n }\n else static if (isAggregateType!(T[i]))\n {\n static foreach(fieldName; FieldNameTuple!(T[i]))\n {\n static if (hasUDA!(mixin(text(T[i].stringof, \".\", fieldName)), string))\n {\n with(t[i])\n {\n mixin(q{t[i].}, fieldName, q{.length}) =\n cast(size_t) mixin(getUDAs!(mixin(text(T[i].stringof, \".\", fieldName)), string)[0]);\n }\n }\n read(mixin(text(q{t[i].}, fieldName)));\n }\n }\n else\n {\n singleRead(t[i]);\n }\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\nE[] makeSlice(E, S)(S size, lazy E value)\n{\n auto a = new E[size.ind];\n foreach(ref ai; a)\n ai = value();\n return a;\n}\n\nE[] makeSlice(E, S)(S size = 0)\n{\n return new E[size.ind];\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n testCase:foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nalias type = multi;\nstruct TestCase\n{\n long n;\n @(\"n\") long[] a;\n\n void solve(long i = -1)\n {\n auto m = a.fold!min(long.max);\n if (a.any!(ai => ai != m && ai != m + 1))\n writeln(\"NO\");\n else\n writeln(\"YES\");\n }\n}\n\nvoid main()\n{\n type;\n}\n"}], "src_uid": "ed449ba7c453a43e2ac5904dc0174530"} {"nl": {"description": "You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, . A periodic subarray (l, s) (0 ≤ l < n, 1 ≤ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l.A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top — infinite array a, bottom — its periodic subarray (l, s)): Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays.", "input_spec": "The first line contains number n (1 ≤ n ≤ 2·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≤ ai ≤ 106), separated by a space.", "output_spec": "Print a single integer — the sought number of pairs.", "sample_inputs": ["4\n7 1 2 3", "2\n2 1", "3\n1 1 1"], "sample_outputs": ["2", "1", "6"], "notes": "NoteIn the first sample the superior subarrays are (0, 1) and (3, 2).Subarray (0, 1) is superior, as a0 ≥ a0, a0 ≥ a1, a0 ≥ a2, a0 ≥ a3, a0 ≥ a0, ...Subarray (3, 2) is superior a3 ≥ a3, a0 ≥ a0, a3 ≥ a1, a0 ≥ a2, a3 ≥ a3, ...In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tauto a = readln.split.map !(to !(int)).array;\n\t\tlong res = 0;\n\t\tauto x = new int [n + 1];\n\t\tforeach (i; 0..n + 1)\n\t\t{\n\t\t\tx[i] = gcd (i, n);\n\t\t}\n\n\t\tforeach (d; 1..n)\n\t\t{\n\t\t\tif (n % d != 0)\n\t\t\t{\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tdebug {writeln (\"d = \", d);}\n\t\t\tint j;\n\n\t\t\tauto s = new int [n + 1];\n\t\t\ts[0] = 0;\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\ts[i + 1] = s[i] + (x[i + 1] == d);\n\t\t\t}\n\t\t\tdebug {writeln (s);}\n\n\t\t\tauto m = new int [d];\n\t\t\tj = 0;\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tm[j] = max (m[j], a[i]);\n\t\t\t\tj++;\n\t\t\t\tif (j >= d)\n\t\t\t\t{\n\t\t\t\t\tj -= d;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tauto b = new bool [n];\n\t\t\tj = 0;\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tb[i] = (a[i] == m[j]);\n\t\t\t\tj++;\n\t\t\t\tif (j >= d)\n\t\t\t\t{\n\t\t\t\t\tj -= d;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (b);}\n\t\t\tb ~= b;\n\n\t\t\tint start = 0;\n\t\t\tforeach (int i, cur; b)\n\t\t\t{\n\t\t\t\tif (i + 1 - start >= n)\n\t\t\t\t{\n\t\t\t\t\tstart++;\n\t\t\t\t}\n\t\t\t\tif (!cur)\n\t\t\t\t{\n\t\t\t\t\tstart = i + 1;\n\t\t\t\t}\n\t\t\t\telse if (i >= n)\n\t\t\t\t{\n\t\t\t\t\tres += s[(i + 1 - start)];\n\t\t\t\t\tdebug {writeln (start, \" \", i, \" \",\n\t\t\t\t\t \"+\", s[(i + 1 - start)]);}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tauto a = readln.split.map !(to !(int)).array;\n\t\tlong res = 0;\n\t\tforeach (d; 1..n)\n\t\t{\n\t\t\tif (n % d != 0)\n\t\t\t{\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tdebug {writeln (\"d = \", d);}\n\t\t\tint j;\n\n\t\t\tauto m = new int [d];\n\t\t\tj = 0;\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tm[j] = max (m[j], a[i]);\n\t\t\t\tj++;\n\t\t\t\tif (j >= d)\n\t\t\t\t{\n\t\t\t\t\tj -= d;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tauto b = new bool [n];\n\t\t\tj = 0;\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tb[i] = (a[i] == m[j]);\n\t\t\t\tj++;\n\t\t\t\tif (j >= d)\n\t\t\t\t{\n\t\t\t\t\tj -= d;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug {writeln (b);}\n\t\t\tb ~= b;\n\n\t\t\tint start = 0;\n\t\t\tforeach (i, cur; b)\n\t\t\t{\n\t\t\t\tif (i + 1 - start >= n)\n\t\t\t\t{\n\t\t\t\t\tstart++;\n\t\t\t\t}\n\t\t\t\tif (!cur)\n\t\t\t\t{\n\t\t\t\t\tstart = i + 1;\n\t\t\t\t}\n\t\t\t\telse if (i >= n)\n\t\t\t\t{\n\t\t\t\t\tres += (i + 1 - start) / d;\n\t\t\t\t\tdebug {writeln (start, \" \", i, \" \",\n\t\t\t\t\t \"+\", (i + 1 - start) / d);}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\twriteln (res);\n\t}\n}\n"}], "src_uid": "393ed779ea3ca8fdb63e8de1293eecd3"} {"nl": {"description": "In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.", "input_spec": "The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as \"namei ai\" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.", "output_spec": "If there's no acceptable order of the people in the queue, print the single line containing \"-1\" without the quotes. Otherwise, print in n lines the people as \"namei hi\", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.", "sample_inputs": ["4\na 0\nb 2\nc 0\nd 0", "4\nvasya 0\npetya 1\nmanya 3\ndunay 3"], "sample_outputs": ["a 150\nc 170\nd 180\nb 160", "-1"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int MX = 3000;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto ps = new string[] (n);\n auto bigger = new int[][] (n+1);\n foreach (i; 0 .. n) {\n int a;\n readf(\"%s %s\", &ps[i], &a);\n readln;\n \n bigger[a] ~= i;\n }\n \n debug { bigger[0 .. min(n, 5)].writeln; }\n \n int[] qord;\n auto hord = make!(SList!int);\n \n foreach (i; 0 .. n+1) {\n if (bigger[i].empty()) { continue; }\n \n if (qord.length < i) {\n writeln(-1);\n return;\n }\n \n foreach (e; bigger[i]) { \n qord ~= e;\n if (i == 0) { hord.insertFront(e); }\n else { hord.insertAfter(take(hord[], i), e); }\n }\n }\n \n debug { writeln(qord, ' ', hord[]); }\n \n int ch = MX;\n auto h = new int[] (n+1);\n foreach (e; hord) { \n h[e] = ch; \n ch--;\n }\n \n foreach (e; qord) {\n writeln(ps[e], ' ', h[e]);\n }\n}"}], "negative_code": [], "src_uid": "112d5d664b0183d96e55a3c545d9b7d5"} {"nl": {"description": "You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.", "input_spec": "The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.", "output_spec": "Print \"Yes\" if there is such a point, \"No\" — otherwise. You can print every letter in any case (upper or lower).", "sample_inputs": ["3\n1 1\n-1 -1\n2 -1", "4\n1 1\n2 2\n-1 1\n-2 2", "3\n1 2\n2 1\n4 60"], "sample_outputs": ["Yes", "No", "Yes"], "notes": "NoteIn the first example the second point can be removed.In the second example there is no suitable for the condition point.In the third example any point can be removed."}, "positive_code": [{"source_code": "\nimport std.stdio;\n\nvoid main() {\n // writeln(\"hello world!\");\n // int[3] A;\n // foreach (i; 0 .. 3) {\n // readf(\" %s\", &A[i]);\n // }\n // writeln(A);\n // write(A, \"\\n\");\n // writefln(\"%(%s %)\", A);\n // int t, s, x;\n int n;\n readf(\" %s\\n\", &n);\n int left = 0;\n int right = 0;\n // int middle = 0;\n foreach (idx; 0 .. n) {\n int x, y;\n readf(\" %s %s\\n\", &x, &y);\n if (x > 0) {\n ++right;\n }\n if (x < 0) {\n ++left;\n }\n }\n if (right < 2 || left < 2) {\n writeln(\"Yes\");\n }\n else {\n writeln(\"No\");\n }\n // readf(\" %s %s %s\\n\", &t, &s, &x);\n // writeln(n, x, y);\n\n}\n"}], "negative_code": [{"source_code": "\nimport std.stdio;\n\nvoid main() {\n // writeln(\"hello world!\");\n // int[3] A;\n // foreach (i; 0 .. 3) {\n // readf(\" %s\", &A[i]);\n // }\n // writeln(A);\n // write(A, \"\\n\");\n // writefln(\"%(%s %)\", A);\n // int t, s, x;\n int n;\n readf(\" %s\\n\", &n);\n int left = 0;\n int right = 0;\n // int middle = 0;\n foreach (idx; 0 .. n) {\n int x, y;\n readf(\" %s %s\\n\", &x, &y);\n if (x > 0) {\n ++right;\n }\n if (x < 0) {\n ++left;\n }\n }\n if (right == 1 || left == 1) {\n writeln(\"Yes\");\n }\n else {\n writeln(\"No\");\n }\n // readf(\" %s %s %s\\n\", &t, &s, &x);\n // writeln(n, x, y);\n\n}\n"}], "src_uid": "cf7bf89a6038586b69d3b8021cee0b27"} {"nl": {"description": "It turns out that the meaning of life is a permutation $$$p_1, p_2, \\ldots, p_n$$$ of the integers $$$1, 2, \\ldots, n$$$ ($$$2 \\leq n \\leq 100$$$). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries.A query consists of an array $$$a_1, a_2, \\ldots, a_n$$$ of integers between $$$1$$$ and $$$n$$$. $$$a$$$ is not required to be a permutation. Omkar will first compute the pairwise sum of $$$a$$$ and $$$p$$$, meaning that he will compute an array $$$s$$$ where $$$s_j = p_j + a_j$$$ for all $$$j = 1, 2, \\ldots, n$$$. Then, he will find the smallest index $$$k$$$ such that $$$s_k$$$ occurs more than once in $$$s$$$, and answer with $$$k$$$. If there is no such index $$$k$$$, then he will answer with $$$0$$$.You can perform at most $$$2n$$$ queries. Figure out the meaning of life $$$p$$$.", "input_spec": null, "output_spec": null, "sample_inputs": ["5\n\n2\n\n0\n\n1"], "sample_outputs": ["? 4 4 2 3 2\n\n? 3 5 1 5 5\n\n? 5 2 4 3 1\n\n! 3 2 1 5 4"], "notes": "NoteIn the sample, the hidden permutation $$$p$$$ is $$$[3, 2, 1, 5, 4]$$$. Three queries were made.The first query is $$$a = [4, 4, 2, 3, 2]$$$. This yields $$$s = [3 + 4, 2 + 4, 1 + 2, 5 + 3, 4 + 2] = [7, 6, 3, 8, 6]$$$. $$$6$$$ is the only number that appears more than once, and it appears first at index $$$2$$$, making the answer to the query $$$2$$$.The second query is $$$a = [3, 5, 1, 5, 5]$$$. This yields $$$s = [3 + 3, 2 + 5, 1 + 1, 5 + 5, 4 + 5] = [6, 7, 2, 10, 9]$$$. There are no numbers that appear more than once here, so the answer to the query is $$$0$$$.The third query is $$$a = [5, 2, 4, 3, 1]$$$. This yields $$$s = [3 + 5, 2 + 2, 1 + 4, 5 + 3, 4 + 1] = [8, 4, 5, 8, 5]$$$. $$$5$$$ and $$$8$$$ both occur more than once here. $$$5$$$ first appears at index $$$3$$$, while $$$8$$$ first appears at index $$$1$$$, and $$$1 < 3$$$, making the answer to the query $$$1$$$.Note that the sample is only meant to provide an example of how the interaction works; it is not guaranteed that the above queries represent a correct strategy with which to determine the answer."}, "positive_code": [{"source_code": "import std.stdio, std.string;\r\nimport std.random, std.algorithm;\r\nimport std.numeric, std.math;\r\nimport std.range, std.array;\r\nimport std.typecons, std.conv;\r\n \r\nint main(string[] args)\r\n{\r\n auto n = to!int(readln.strip);\r\n auto p = new int[n];\r\n auto q = new int[n];\r\n auto prev = new int[n];\r\n fill(prev, -1);\r\n foreach (i; 0 .. n)\r\n {\r\n q[i] = 2;\r\n foreach (j; 0 .. n) if (j != i) q[j] = 1;\r\n writeln(\"? \", join(map!(to!string)(q), \" \"));\r\n stdout.flush;\r\n auto ridx = to!int(readln.strip);\r\n if (ridx != 0 && ridx != i + 1) prev[ridx - 1] = i;\r\n q[i] = 1;\r\n foreach (j; 0 .. n) if (j != i) q[j] = 2;\r\n writeln(\"? \", join(map!(to!string)(q), \" \"));\r\n stdout.flush;\r\n ridx = to!int(readln.strip);\r\n if (ridx != 0 && ridx != i + 1) prev[i] = ridx - 1;\r\n }\r\n auto f = new bool[n];\r\n foreach (i; 0 .. n) if (prev[i] != -1) f[prev[i]] = true;\r\n auto idx = -1;\r\n foreach (i; 0 .. n) if (!f[i])\r\n {\r\n idx = i;\r\n break;\r\n }\r\n p[idx] = n;\r\n foreach (i; 0 .. n - 1)\r\n {\r\n p[prev[idx]] = p[idx] - 1;\r\n idx = prev[idx];\r\n }\r\n writeln(\"! \", join(map!(to!string)(p), \" \"));\r\n stdout.flush;\r\n return 0;\r\n}"}, {"source_code": "immutable multi = false;\n\nvoid solve(int tc)\n{\n\tint n = readInt!int;\n\tauto next = new int[](n+1);\n\tauto prev = new int[](n+1);\n\tint zero = 0;\n\tvoid get(int i)\n\t{\n\t\tint answer = void;\n\t\twrite (\"? \");\n\t\tforeach(j; 1 .. n+1)\n\t\t{\n\t\t\twrite (int(i == j)+1, \" \");\n\t\t}\n\t\twriteln;\n\t\tstdout.flush;\n\t\tanswer = readInt!int;\n\t\tif (answer == 0)\n\t\t{\n\t\t\tnext[i] = 0;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (answer < i)\n\t\t\t{\n\t\t\t\tnext[i] = answer;\n\t\t\t\tprev[answer] = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert(answer == i);\n\t\t\t}\n\t\t}\n\t\t\n\t\twrite (\"? \");\n\t\tforeach(j; 1 .. n+1)\n\t\t{\n\t\t\twrite (int(i != j)+1, \" \");\n\t\t}\n\t\twriteln;\n\t\tstdout.flush;\n\t\tanswer = readInt!int;\n\t\tif (answer == 0)\n\t\t{\n\t\t\tprev[i] = 0;\n\t\t\tzero = i;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (answer < i)\n\t\t\t{\n\t\t\t\tprev[i] = answer;\n\t\t\t\tnext[answer] = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert(answer == i);\n\t\t\t}\n\t\t}\n\t}\n\tforeach(i; 1 .. n + 1) get(i);\n\tassert (zero != 0);\n\tauto p = new int[](n+1);\n\tint v = 1;\n\tint i = zero;\n\twhile (i)\n\t{\n\t\tp[i] = v++;\n\t\ti = next[i];\n\t}\n\twrite(\"! \");\n\tforeach(j; 1 .. n + 1) write (p[j], \" \");\n\twriteln;\n\tstdout.flush;\n}\n\n//{{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1;\n\tforeach(tc; 0 .. t) solve(tc);\n}\n//}}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t{\n\t\tpopChar;\n\t}\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}], "negative_code": [{"source_code": "immutable multi = false;\n\nvoid solve(int tc)\n{\n\tint n = readInt!int;\n\tauto next = new int[](n+1);\n\tauto prev = new int[](n+1);\n\tint zero = 0;\n\tvoid get(int i)\n\t{\n\t\tint answer = void;\n\t\twrite (\"? \");\n\t\tforeach(j; 1 .. n+1)\n\t\t{\n\t\t\twrite (int(i == j), \" \");\n\t\t}\n\t\twriteln;\n\t\tstdout.flush;\n\t\tanswer = readInt!int;\n\t\tif (answer == 0)\n\t\t{\n\t\t\tnext[i] = 0;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (answer < i)\n\t\t\t{\n\t\t\t\tnext[i] = answer;\n\t\t\t\tprev[answer] = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert(answer == i);\n\t\t\t}\n\t\t}\n\t\t\n\t\twrite (\"? \");\n\t\tforeach(j; 1 .. n+1)\n\t\t{\n\t\t\twrite (int(i != j), \" \");\n\t\t}\n\t\twriteln;\n\t\tstdout.flush;\n\t\tanswer = readInt!int;\n\t\tif (answer == 0)\n\t\t{\n\t\t\tprev[i] = 0;\n\t\t\tzero = i;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (answer < i)\n\t\t\t{\n\t\t\t\tprev[i] = answer;\n\t\t\t\tnext[answer] = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert(answer == i);\n\t\t\t}\n\t\t}\n\t}\n\tforeach(i; 1 .. n + 1) get(i);\n\tassert (zero != 0);\n\tauto p = new int[](n+1);\n\tint v = 1;\n\tint i = zero;\n\twhile (i)\n\t{\n\t\tp[i] = v++;\n\t\ti = next[i];\n\t}\n\twrite(\"! \");\n\tforeach(j; 1 .. n + 1) write (p[j], \" \");\n\twriteln;\n\tstdout.flush;\n}\n\n//{{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1;\n\tforeach(tc; 0 .. t) solve(tc);\n}\n//}}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t{\n\t\tpopChar;\n\t}\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "immutable multi = false;\n\nvoid solve(int tc)\n{\n\tint n = readInt!int;\n\tauto next = new int[](n+1);\n\tauto prev = new int[](n+1);\n\tint zero = 0;\n\tvoid get(int i)\n\t{\n\t\tint answer = void;\n\t\twrite (\"? \");\n\t\tforeach(j; 1 .. n+1)\n\t\t{\n\t\t\twrite (int(i == j), \" \");\n\t\t}\n\t\twriteln;\n\t\tstdout.flush;\n\t\tanswer = readInt!int;\n\t\tif (answer == 0)\n\t\t{\n\t\t\tnext[i] = 0;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (answer < i)\n\t\t\t{\n\t\t\t\tnext[i] = answer;\n\t\t\t\tprev[answer] = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert(answer == i);\n\t\t\t}\n\t\t}\n\t\t\n\t\twrite (\"? \");\n\t\tforeach(j; 1 .. n+1)\n\t\t{\n\t\t\twrite (int(i != j), \" \");\n\t\t}\n\t\twriteln;\n\t\tstdout.flush;\n\t\tanswer = readInt!int;\n\t\tif (answer == 0)\n\t\t{\n\t\t\tprev[i] = 0;\n\t\t\tzero = i;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (answer < i)\n\t\t\t{\n\t\t\t\tprev[i] = answer;\n\t\t\t\tnext[answer] = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert(answer == i);\n\t\t\t}\n\t\t}\n\t}\n\tforeach(i; 1 .. n + 1) get(i);\n\tassert (zero != 0);\n\tauto p = new int[](n+1);\n\tint v = 1;\n\tint i = zero;\n\twhile (i)\n\t{\n\t\tp[i] = v++;\n\t\ti = next[i];\n\t}\n\tdebug writeln(p);\n}\n\n//{{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1;\n\tforeach(tc; 0 .. t) solve(tc);\n}\n//}}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t{\n\t\tpopChar;\n\t}\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}], "src_uid": "2cb5fe3fdff43e104729866cdfa73102"} {"nl": {"description": "You are given two positive integers $$$n$$$ ($$$1 \\le n \\le 10^9$$$) and $$$k$$$ ($$$1 \\le k \\le 100$$$). Represent the number $$$n$$$ as the sum of $$$k$$$ positive integers of the same parity (have the same remainder when divided by $$$2$$$).In other words, find $$$a_1, a_2, \\ldots, a_k$$$ such that all $$$a_i>0$$$, $$$n = a_1 + a_2 + \\ldots + a_k$$$ and either all $$$a_i$$$ are even or all $$$a_i$$$ are odd at the same time.If such a representation does not exist, then report it.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the input. Next, $$$t$$$ test cases are given, one per line. Each test case is two positive integers $$$n$$$ ($$$1 \\le n \\le 10^9$$$) and $$$k$$$ ($$$1 \\le k \\le 100$$$).", "output_spec": "For each test case print: YES and the required values $$$a_i$$$, if the answer exists (if there are several answers, print any of them); NO if the answer does not exist. The letters in the words YES and NO can be printed in any case.", "sample_inputs": ["8\n10 3\n100 4\n8 7\n97 2\n8 8\n3 10\n5 3\n1000000000 9"], "sample_outputs": ["YES\n4 2 4\nYES\n55 5 5 35\nNO\nNO\nYES\n1 1 1 1 1 1 1 1\nNO\nYES\n3 1 1\nYES\n111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n auto nk = readln.split.to!(long[]);\n auto N = nk[0];\n auto K = nk[1];\n\n auto d = N / K;\n auto r = N % K;\n auto x = d+r;\n\n if (d == 0) {\n writeln(\"NO\");\n continue;\n }\n\n if (d%2 != x%2) {\n if (d <= 1 || K%2 == 0) {\n writeln(\"NO\");\n continue;\n }\n d -= 1;\n x += K-1;\n }\n \n long[] as;\n foreach (_k; 0..K-1) as ~= d;\n as ~= x;\n writeln(\"YES\");\n writeln(as.to!(string[]).join(\" \"));\n }\n}"}, {"source_code": "// Author: Ivan Kazmenko (gassa@mail.ru)\nimport std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nint [] solve (int n, int k)\n{\n\tforeach (v; [1, 2])\n\t{\n\t\tauto last = n - v * (k - 1);\n\t\tif (last > 0 && last % 2 == v % 2)\n\t\t{\n\t\t\treturn v.repeat (k - 1).array ~ last;\n\t\t}\n\t}\n\treturn null;\n}\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\tauto res = solve (n, k);\n\t\tif (res is null)\n\t\t{\n\t\t\twriteln (\"NO\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (\"YES\");\n\t\t\twritefln !(\"%(%s %)\") (res);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid calc(int n, int k) {\n int x = n - (k - 1);\n if (x > 0 && x % 2 == 1) {\n writeln(\"YES\");\n auto ans = chain(repeat(1, k - 1), [n - (k - 1)]);\n writeln(ans.map!text.join(' '));\n return;\n }\n\n int y = n - (k - 1) * 2;\n if (y > 0 && y % 2 == 0) {\n writeln(\"YES\");\n auto ans = chain(repeat(2, k - 1), [n - (k - 1) * 2]);\n writeln(ans.map!text.join(' '));\n return;\n }\n\n writeln(\"NO\");\n}\n\nvoid main() {\n int t; scan(t);\n foreach (_; 0..t) {\n int n, k; scan(n, k);\n calc(n, k);\n }\n}\n\nvoid scan(T...)(ref T a) {\n string[] ss = readln.split;\n foreach (i, t; T) a[i] = ss[i].to!t;\n}\nT read(T=string)() { return readln.chomp.to!T; }\nT[] reads(T)() { return readln.split.to!(T[]); }\nalias readints = reads!int;\n"}, {"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.math;\nimport std.conv;\nimport std.string;\n\nT readNum(T)(){\n return readStr.to!T;\n}\n\nT[] readNums(T)(){\n return readStr.split.to!(T[]);\n}\n\nstring readStr(){\n return readln.chomp;\n}\n\nvoid main(){\n auto t = readNum!int;\n\n foreach(i; 0 .. t){\n auto nk = readNums!long;\n\n long a = nk[0] / nk[1];\n long b = nk[0] % nk[1];\n\n if(b % 2 == 0 && a > 0){\n writeln(\"YES\");\n foreach(j; 0 .. nk[1] - 1){\n write(a, \" \");\n }\n writeln(a+b);\n } else if((b+nk[1]) % 2 == 0 && a-1 > 0){\n writeln(\"YES\");\n foreach(j; 0 .. nk[1] - 1){\n write(a-1, \" \");\n }\n writeln(a-1+b+nk[1]);\n } else {\n writeln(\"NO\");\n }\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD;\n\t\tauto k = RD;\n\n\t\tif (k % 2 == 0)\n\t\t{\n\t\t\tif (n % 2) continue;\n\t\t\tif (k > n) continue;\n\t\t\tforeach (i; 0..k-1)\n\t\t\t{\n\t\t\t\tans[ti] ~= 1;\n\t\t\t}\n\t\t\tans[ti] ~= n - (k-1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (n % 2)\n\t\t\t{\n\t\t\t\tif (k > n) continue;\n\t\t\t\tforeach (i; 0..k-1)\n\t\t\t\t{\n\t\t\t\t\tans[ti] ~= 1;\n\t\t\t\t}\n\t\t\t\tans[ti] ~= n - (k-1);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (k*2 > n) continue;\n\t\t\t\tforeach (i; 0..k-1)\n\t\t\t\t{\n\t\t\t\t\tans[ti] ~= 2;\n\t\t\t\t}\n\t\t\t\tans[ti] ~= n - (k-1)*2;\n\t\t\t}\n\t\t}\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\tif (e.empty)\n\t\t\twriteln(\"NO\");\n\t\telse\n\t\t{\n\t\t\twriteln(\"YES\");\n\t\t\te.map!(to!string).join(\" \").writeln();\n\t\t}\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n auto nk = readln.split.to!(long[]);\n auto N = nk[0];\n auto K = nk[1];\n\n auto d = N / K;\n auto r = N % K;\n auto x = d+r;\n\n if (d%2 != x%2) {\n if (d <= 1 || K%2 == 0) {\n writeln(\"NO\");\n continue;\n }\n d -= 1;\n x += K-1;\n }\n \n long[] as;\n foreach (_k; 0..K-1) as ~= d;\n as ~= x;\n writeln(\"YES\");\n writeln(as.to!(string[]).join(\" \"));\n }\n}"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n auto nk = readln.split.to!(long[]);\n auto N = nk[0];\n auto K = nk[1];\n\n auto d = N / K;\n auto r = N % K;\n auto x = d+r;\n\n if (d%2 != x%2) {\n if (d == 1 || K%2 == 0) {\n writeln(\"NO\");\n continue;\n }\n d -= 1;\n x += K-1;\n }\n \n long[] as;\n foreach (_k; 0..K-1) as ~= d;\n as ~= x;\n writeln(\"YES\");\n writeln(as.to!(string[]).join(\" \"));\n }\n}"}, {"source_code": "// Author: Ivan Kazmenko (gassa@mail.ru)\n// tries to build with 4 instead of 2\nimport std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nint [] solve (int n, int k)\n{\n\tforeach (v; [1, 4])\n\t{\n\t\tauto last = n - v * (k - 1);\n\t\tif (last > 0 && last % 2 == v % 2)\n\t\t{\n\t\t\treturn v.repeat (k - 1).array ~ last;\n\t\t}\n\t}\n\treturn null;\n}\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\tauto res = solve (n, k);\n\t\tif (res is null)\n\t\t{\n\t\t\twriteln (\"NO\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (\"YES\");\n\t\t\twritefln !(\"%(%s %)\") (res);\n\t\t}\n\t}\n}\n"}], "src_uid": "6b94dcd088b0328966b54acefb5c6d22"} {"nl": {"description": "Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs exactly in two bits.Vasya wants to know how many pairs of indexes (i, j) are in his sequence so that i < j and the pair of integers ai and aj is k-interesting. Your task is to help Vasya and determine this number.", "input_spec": "The first line contains two integers n and k (2 ≤ n ≤ 105, 0 ≤ k ≤ 14) — the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ. The second line contains the sequence a1, a2, ..., an (0 ≤ ai ≤ 104), which Vasya has.", "output_spec": "Print the number of pairs (i, j) so that i < j and the pair of integers ai and aj is k-interesting.", "sample_inputs": ["4 1\n0 3 2 1", "6 0\n200 100 100 100 200 200"], "sample_outputs": ["4", "6"], "notes": "NoteIn the first test there are 4 k-interesting pairs: (1, 3), (1, 4), (2, 3), (2, 4). In the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs: (1, 5), (1, 6), (2, 3), (2, 4), (3, 4), (5, 6). "}, "positive_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.format;\nimport std.algorithm;\nimport std.math;\nimport core.bitop;\n\nconst int N = 100004;\n\nlong ans;\nint n, k;\nint[N] a;\nint[] masks;\nint[1 << 14] cnt;\n\nvoid read() {\n\treadf(\" %s %s\", &n, &k);\n\tforeach (i; 0..n) {\n\t\treadf(\" %s\", &a[i]);\n\t}\n}\n\nvoid precalc() {\n\tforeach (mask; 0..1<<14) {\n\t\tif (_popcnt(mask) == k) {\n\t\t\tmasks ~= mask;\n\t\t}\n\t}\n}\n\nvoid compute() {\n\tforeach (i; 0..n) {\n\t\tforeach (m; masks) {\n\t\t\tans += cnt[a[i] ^ m];\n\t\t}\n\t\tcnt[a[i]]++;\n\t}\n}\n\nvoid solve() {\n\tread();\n\tprecalc();\n\tcompute();\n\twriteln(ans);\n}\n\nint main() {\n\tsolve();\n\n return 0;\n}\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int MX = 2 ^^ 14 - 1;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto cnt = new int[] (MX+1);\n \n arr.each!(x => ++cnt[x]);\n \n auto ans = 0L;\n foreach (i; 0 .. MX+1) {\n foreach (j; i .. MX+1) {\n if ((i ^ j).popcnt == k) {\n ans += i == j ? \n cnt[i].to!long * (cnt[i]-1) / 2\n : cnt[i].to!long * cnt[j]; \n }\n }\n }\n \n ans.writeln;\n}"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int MX = 2 ^^ 14 - 1;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto cnt = new int[] (MX+1);\n \n arr.each!(x => ++cnt[x]);\n \n auto ans = 0L;\n foreach (i; 0 .. MX+1) {\n foreach (j; i .. MX+1) {\n if ((i ^ j).popcnt != k) { ans += cnt[i].to!long * cnt[j]; }\n }\n }\n \n ans.writeln;\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int MX = 2 ^^ 14 - 1;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto cnt = new int[] (MX+1);\n \n arr.each!(x => ++cnt[x]);\n \n auto ans = 0L;\n foreach (i; 0 .. MX+1) {\n foreach (j; i .. MX+1) {\n if ((i ^ j).popcnt == k) { ans += cnt[i].to!long * cnt[j]; }\n }\n }\n \n ans.writeln;\n}"}], "src_uid": "7b7623dfde563b383cdc2364c81a7539"} {"nl": {"description": "Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of elements, which work in the following way.Each element has one controller that can be set to any non-negative real value. If a controller is set on some value x, then the controller consumes x2 energy units per second. At the same time, any two elements connected by a wire produce y·z energy units per second, where y and z are the values set on their controllers.Petr has only a limited number of wires, so he has already built some scheme of elements and wires, and is now interested if it's possible to set the controllers in such a way that the system produces at least as much power as it consumes, and at least one controller is set on the value different from 0. Help him check this, and if it's possible, find the required integer values that should be set.It is guaranteed that if there exist controllers' settings satisfying the above conditions, then there exist required integer values not greater than 106.", "input_spec": "There are several (at least one) test cases in the input. The first line contains single integer — the number of test cases. There is an empty line before each test case. The first line of test case contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of elements in the scheme and the number of wires. After that, m lines follow, each of them contains two integers a and b (1 ≤ a, b ≤ n) — two elements connected by a wire. No element is connected with itself, no two elements are connected by more than one wire. It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 105. For hacks you can only use tests with one test case.", "output_spec": "Print answer for each test case. For each test case print \"YES\" if it's possible to set the controllers in such a way that the consumed power is not greater than the power produced, and the required values on the next line. The settings should be integers from 0 to 106, inclusive, and at least one value should be different from 0. If there are multiple answers, print any of them. If it's not possible to set the controllers in the required way, print one line \"NO\".", "sample_inputs": ["4\n \n4 4\n1 2\n2 3\n3 4\n4 2\n \n3 2\n2 3\n3 1\n \n4 6\n1 2\n3 4\n4 2\n1 4\n1 3\n3 2\n \n10 9\n2 1\n3 2\n5 2\n6 2\n2 7\n2 8\n2 9\n2 10\n4 2"], "sample_outputs": ["YES\n1 2 2 1\nNO\nYES\n1 1 1 1\nYES\n1 5 1 1 1 1 1 1 1 1"], "notes": "NoteIn the first example it's possible to set the controllers in the required way, for example, in the following way: set 1 on the first element, set 2 on the second and on the third, set 1 on the fourth. The consumed power is then equal to 12 + 22 + 22 + 12 = 10 energy units per second, the produced power is equal to 1·2 + 2·2 + 2·1 + 2·1 = 10 energy units per second. Thus the answer is \"YES\".In the second test case it's not possible to set the controllers in the required way. For example, if we set all controllers to 0.5, then the consumed powers equals 0.75 energy units per second, while produced power equals 0.5 energy units per second."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\n/*\n x \"--- x t\"\n x (x t) - (x t)^2 = x^2 (t - t^2) <= (1/4) x^2 (max at t = 1/2)\n \n x \"--- x t --- *\"\n x (x t) - (x t)^2 + (1/4) (x t)^2 = x^2 (t - (3/4) t^2) <= (1/3) x^2 (max at t = 2/3)\n \n x \"--- x t --- * --- *\"\n x (x t) - (x t)^2 + (1/3) (x t)^2 = x^2 (t - (2/3) t^2) <= (3/8) x^2 (max at t = 3/4)\n \n x \"--- * --- * ... --- *\"\n (1/2 - 1/(2(k+1))) x^2 at (k/(k+1)) x, ..., (1/(k+1)) x\n \n deg = 3\n -x^2 + \\sum_{j=1}^3 (1/2 - 1/(2(k_j+1))) x^2\n = (1/2) (1 - \\sum_{j=1}^3 1/(k_j+1)) x^2\n \n 1/2 + 1/3 + 1/6\n 1/2 + 1/4 + 1/4\n 1/3 + 1/3 + 1/3\n*/\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto G = new int[][N];\n foreach (i; 0 .. M) {\n G[A[i]] ~= i;\n G[B[i]] ~= i;\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n auto uss = new int[][N];\n foreach (u; 0 .. N) {\n uss[uf.root(u)] ~= u;\n }\n auto iss = new int[][N];\n foreach (i; 0 .. M) {\n iss[uf.root(A[i])] ~= i;\n }\n \n auto ans = new long[N];\n foreach (r; 0 .. N) {\n if (uf[r] < 0) {\n if (cast(int)(iss[r].length) >= -uf[r]) {\n foreach (u; uss[r]) {\n ans[u] = 1;\n }\n } else {\n const us4 = uss[r].filter!(u => (G[u].length >= 4)).array;\n if (us4.length >= 1) {\n foreach (u; uss[r]) {\n ans[u] = 1;\n }\n ans[us4[0]] = 2;\n } else {\n const us3 = uss[r].filter!(u => (G[u].length >= 3)).array;\n if (us3.length >= 2) {\n int dfs(int u, int p) {\n int ret = 1;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n const res = dfs(v, u);\n if (res == 2) {\n ret = 2;\n }\n }\n }\n if (u == us3[1]) {\n ret = 2;\n }\n return ans[u] = ret;\n }\n dfs(us3[0], -1);\n } else if (us3.length >= 1) {\n auto vss = new int[][3];\n foreach (j; 0 .. 3) {\n const i0 = G[us3[0]][j];\n for (int p = us3[0], u = A[i0] ^ B[i0] ^ us3[0]; ; ) {\n vss[j] ~= u;\n if (G[u].length == 1) {\n break;\n }\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n p = u;\n u = v;\n break;\n }\n }\n }\n }\n vss.sort!\"a.length < b.length\";\n debug {\n writeln(\"vss = \", vss);\n }\n foreach (ks; [[1, 2, 5], [1, 3, 3], [2, 2, 2]]) {\n bool ok = true;\n foreach (j; 0 .. 3) {\n ok = ok && (vss[j].length >= ks[j]);\n }\n if (ok) {\n ans[us3[0]] = 12;\n foreach (j; 0 .. 3) {\n foreach (l; 0 .. ks[j]) {\n ans[vss[j][l]] = 12 / (ks[j] + 1) * (ks[j] - l);\n }\n }\n break;\n }\n }\n }\n }\n }\n }\n }\n \n long score;\n foreach (u; 0 .. N) {\n score -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n score += ans[A[i]] * ans[B[i]];\n }\n debug {\n writeln(\"score = \", score);\n }\n if (score >= 0 && ans.sum > 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum L = 10L^^6;\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto deg = new int[N];\n foreach (i; 0 .. M) {\n ++deg[A[i]];\n ++deg[B[i]];\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n \n auto numEdges = new int[N];\n foreach (i; 0 .. M) {\n ++numEdges[uf.root(A[i])];\n }\n auto hasHub = new bool[N];\n foreach (u; 0 .. N) {\n if (deg[u] >= 4) {\n hasHub[uf.root(u)] = true;\n }\n }\n \n auto ans = new long[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (numEdges[r] > -uf[r] - 1) {\n ans[u] = L;\n } else {\n ans[u] = (deg[u] >= 4) ? L : hasHub[r] ? (L / 2) : 1;\n }\n }\n \n long sum;\n foreach (u; 0 .. N) {\n sum -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n sum += ans[A[i]] * ans[B[i]];\n }\n if (sum >= 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum L = 10L^^6;\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto deg = new int[N];\n foreach (i; 0 .. M) {\n ++deg[A[i]];\n ++deg[B[i]];\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n \n auto numEdges = new int[N];\n foreach (i; 0 .. M) {\n const r = uf.root(A[i]);\n ++numEdges[r];\n }\n auto numLeaves = new int[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (deg[u] == 1) {\n ++numLeaves[r];\n }\n }\n \n auto ans = new long[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (numEdges[r] > (-uf[r]) - 1 || numLeaves[r] >= 4) {\n ans[u] = (deg[u] == 1) ? (L / 2) : L;\n } else {\n ans[u] = 0;\n }\n }\n \n long sum;\n foreach (u; 0 .. N) {\n sum -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n sum += ans[A[i]] * ans[B[i]];\n }\n debug {\n writeln(\"sum = \", sum);\n }\n if (sum >= 0 && ans.sum > 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum L = 10L^^6;\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto deg = new int[N];\n foreach (i; 0 .. M) {\n ++deg[A[i]];\n ++deg[B[i]];\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n \n auto numEdges = new int[N];\n foreach (i; 0 .. M) {\n ++numEdges[uf.root(A[i])];\n }\n auto cnt4 = new int[N];\n auto cnt3 = new int[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (deg[u] >= 4) {\n ++cnt4[r];\n } else if (deg[u] >= 3) {\n ++cnt3[r];\n }\n }\n \n auto ans = new long[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (numEdges[r] > (-uf[r]) - 1) {\n ans[u] = L;\n } else if (cnt4[r] >= 1) {\n ans[u] = (deg[u] >= 4) ? L : (L / 2);\n } else if (cnt3[r] >= 2) {\n ans[u] = (deg[u] >= 3) ? L : (L / 2);\n } else {\n ans[u] = 1;\n }\n }\n \n long sum;\n foreach (u; 0 .. N) {\n sum -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n sum += ans[A[i]] * ans[B[i]];\n }\n debug {\n writeln(\"sum = \", sum);\n }\n if (sum >= 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum L = 10L^^6;\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto deg = new int[N];\n foreach (i; 0 .. M) {\n ++deg[A[i]];\n ++deg[B[i]];\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n \n auto numEdges = new int[N];\n foreach (i; 0 .. M) {\n const r = uf.root(A[i]);\n ++numEdges[r];\n }\n auto numLeaves = new int[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (deg[u] == 1) {\n ++numLeaves[r];\n }\n }\n \n auto ans = new long[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (numEdges[r] > (-uf[r]) - 1 || numLeaves[r] >= 4) {\n ans[u] = (deg[u] == 1) ? (L / 2) : L;\n } else {\n ans[u] = 1;\n }\n }\n \n long sum;\n foreach (u; 0 .. N) {\n sum -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n sum += ans[A[i]] * ans[B[i]];\n }\n debug {\n writeln(\"sum = \", sum);\n }\n if (sum >= 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum L = 10L^^6;\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto deg = new int[N];\n foreach (i; 0 .. M) {\n ++deg[A[i]];\n ++deg[B[i]];\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n \n auto numEdges = new int[N];\n foreach (i; 0 .. M) {\n ++numEdges[uf.root(A[i])];\n }\n auto cnt4 = new int[N];\n auto cnt3 = new int[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (deg[u] >= 4) {\n ++cnt4[r];\n } else if (deg[u] >= 3) {\n ++cnt3[r];\n }\n }\n \n auto ans = new long[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (numEdges[r] > (-uf[r]) - 1) {\n ans[u] = L;\n } else if (cnt4[r] >= 1 || cnt3[r] >= 2) {\n ans[u] = (deg[u] == 1) ? (L / 2) : L;\n } else {\n ans[u] = 1;\n }\n }\n \n long sum;\n foreach (u; 0 .. N) {\n sum -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n sum += ans[A[i]] * ans[B[i]];\n }\n debug {\n writeln(\"sum = \", sum);\n }\n if (sum >= 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum L = 10L^^6;\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const M = readInt();\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n }\n \n auto deg = new int[N];\n foreach (i; 0 .. M) {\n ++deg[A[i]];\n ++deg[B[i]];\n }\n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. M) {\n uf.connect(A[i], B[i]);\n }\n \n auto numEdges = new int[N];\n foreach (i; 0 .. M) {\n const r = uf.root(A[i]);\n ++numEdges[r];\n }\n auto numLeaves = new int[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (deg[u] == 1) {\n ++numLeaves[r];\n }\n }\n \n auto ans = new long[N];\n foreach (u; 0 .. N) {\n const r = uf.root(u);\n if (numEdges[r] > (-uf[r]) - 1 || numLeaves[r] >= 4) {\n ans[u] = (deg[u] == 1) ? (L / 2) : L;\n } else {\n ans[u] = 0;\n }\n }\n \n long sum;\n foreach (u; 0 .. N) {\n sum -= ans[u]^^2;\n }\n foreach (i; 0 .. M) {\n sum += ans[A[i]] * ans[B[i]];\n }\n debug {\n writeln(\"sum = \", sum);\n }\n if (sum >= 0) {\n writeln(\"YES\");\n foreach (u; 0 .. N) {\n if (u > 0) write(\" \");\n write(ans[u]);\n }\n writeln;\n } else {\n writeln(\"NO\");\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "d2da1a95ad105dd4dcb9ed2eef445146"} {"nl": {"description": "Polycarp doesn't like integers that are divisible by $$$3$$$ or end with the digit $$$3$$$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.Polycarp starts to write out the positive (greater than $$$0$$$) integers which he likes: $$$1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \\dots$$$. Output the $$$k$$$-th element of this sequence (the elements are numbered from $$$1$$$).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$k$$$ ($$$1 \\le k \\le 1000$$$).", "output_spec": "For each test case, output in a separate line one integer $$$x$$$ — the $$$k$$$-th element of the sequence that was written out by Polycarp.", "sample_inputs": ["10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1000"], "sample_outputs": ["1\n2\n4\n5\n7\n8\n10\n11\n14\n1666"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n long t;\n readf!\" %d \"(t);\n while (t--) {\n long n;\n readf!\" %d \"(n);\n long ans;\n foreach (i ; 1 .. 100000) {\n if (i % 10 == 3 || i % 3 == 0) {\n } else {\n if (--n == 0) {\n writeln(i);\n break;\n }\n }\n }\n }\n}\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n int k = scan!int;\n int j = 0;\n for(int i = 1; i <= 3*k; ++i){\n if(i % 3 == 0 || i % 10 == 3) continue;\n ++j;\n if(j == k){\n writeln(i);\n }\n }\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}], "negative_code": [], "src_uid": "c37604d5d833a567ff284d7ce5eda059"} {"nl": {"description": "Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.", "output_spec": "Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.", "sample_inputs": ["5\nthreehorses\ngoodsubstrings\nsecret\nprimematrix\nbeautifulyear", "4\naa\nbdefghijklmn\nopqrstuvwxyz\nc"], "sample_outputs": ["j", "ab"], "notes": "NoteIn the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.string;\n\nimmutable int LET = 26;\n\nstruct node\n{\n\tint p [LET];\n}\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tnode [] t;\n\t\tt.length++;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tauto s = strip (readln ());\n\t\t\twhile (s.length > 0)\n\t\t\t{\n\t\t\t\tdebug {writeln (s);}\n\t\t\t\tint r = 0;\n\t\t\t\tforeach (c; s)\n\t\t\t\t{\n\t\t\t\t\tint d = c - 'a';\n\t\t\t\t\tif (!t[r].p[d])\n\t\t\t\t\t{\n\t\t\t\t\t\tt[r].p[d] = t.length;\n\t\t\t\t\t\tt.length++;\n\t\t\t\t\t}\n\t\t\t\t\tr = t[r].p[d];\n\t\t\t\t}\n\t\t\t\ts = s[1..$];\n\t\t\t}\n\t\t}\n\t\tstring res = \"zzz\";\n\t\tforeach (d; 0..LET)\n\t\t{\n\t\t\tdebug {writefln (\"%s %s\", d, t[0].p[d]);}\n\t\t\tif (!t[0].p[d])\n\t\t\t{\n\t\t\t\tres = min (res, \"\" ~ cast (char) (d + 'a'));\n\t\t\t}\n\t\t}\n\t\tif (res == \"zzz\")\n\t\t{\n\t\t\tforeach (d; 0..LET)\n\t\t\t{\n\t\t\t\tforeach (e; 0..LET)\n\t\t\t\t{\n\t\t\t\t\tif (!t[t[0].p[d]].p[e])\n\t\t\t\t\t{\n\t\t\t\t\t\tres = min (res, \"\" ~ cast (char) (d + 'a') ~\n\t\t\t\t\t\t cast (char) (e + 'a'));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "58fa5c2f270e2c34e8f9671d5ffdb9c8"} {"nl": {"description": "These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts $$$1$$$ minute.He was asked to insert one takeoff in the schedule. The takeoff takes $$$1$$$ minute itself, but for safety reasons there should be a time space between the takeoff and any landing of at least $$$s$$$ minutes from both sides.Find the earliest time when Arkady can insert the takeoff.", "input_spec": "The first line of input contains two integers $$$n$$$ and $$$s$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le s \\le 60$$$) — the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff. Each of next $$$n$$$ lines contains two integers $$$h$$$ and $$$m$$$ ($$$0 \\le h \\le 23$$$, $$$0 \\le m \\le 59$$$) — the time, in hours and minutes, when a plane will land, starting from current moment (i. e. the current time is $$$0$$$ $$$0$$$). These times are given in increasing order.", "output_spec": "Print two integers $$$h$$$ and $$$m$$$ — the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff.", "sample_inputs": ["6 60\n0 0\n1 20\n3 21\n5 0\n19 30\n23 40", "16 50\n0 30\n1 20\n3 0\n4 30\n6 10\n7 50\n9 30\n11 10\n12 50\n14 30\n16 10\n17 50\n19 30\n21 10\n22 50\n23 59", "3 17\n0 30\n1 0\n12 0"], "sample_outputs": ["6 1", "24 50", "0 0"], "notes": "NoteIn the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute.In the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than $$$24$$$ hours to insert the takeoff.In the third example Arkady can insert the takeoff even between the first landing."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\n\nvoid main() {\n\tauto ns = readln.chomp.split.map!(to!int);\n\tint n = ns[0];\n\tint s = ns[1];\n\tint[] times;\n\ttimes ~= -s - 1;\n\tforeach (i; 0..n) {\n\t\tauto hs = readln.chomp.split.map!(to!int);\n\t\tint h = hs[0];\n\t\tint m = hs[1];\n\t\tint t = h * 60 + m;\n\t\ttimes ~= t;\n\t}\n\ttimes ~= times[$-1] + (1 << 28);\n\tint len = cast(int) times.length;\n\tfor (int i = 0; i < len - 1; ++i) {\n\t\tint t1 = times[i];\n\t\tint t2 = times[i + 1];\n\t\tdebug stderr.writefln(\"[%d] %d %d ... %d\", i, t1, t2, t2 - t1);\n\t\tif (t2 - t1 >= s * 2 + 2) {\n\t\t\tint h = t1 / 60;\n\t\t\tint m = t1 % 60;\n\t\t\tdebug stderr.writefln(\"\\t%d, %d\", h, m);\n\t\t\tm += s + 1;\n\t\t\th += m / 60;\n\t\t\tm %= 60;\n\t\t\tif (h < 0 || m < 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdebug stderr.writefln(\"\\t\\t[%d] %d %d ... %d\", i, t1, t2, t2 - t1);\n\t\t\twritefln(\"%d %d\", h, m);\n\t\t\treturn;\n\t\t}\n\t}\n}\n\n\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nvoid main()\n{\n int n, s; readV(n, s);\n\n auto t = new int[](n);\n foreach (i; 0..n) {\n int h, m; readV(h, m);\n t[i] = h*60+m;\n }\n\n auto put(int ans)\n {\n writeln(ans/60, \" \", ans%60);\n }\n\n if (t[0] >= s+1) {\n put(0);\n return;\n }\n\n foreach (i; 0..n-1) {\n if (t[i+1]-t[i] >= s*2+2) {\n put(t[i]+s+1);\n return;\n }\n }\n\n put(t[$-1]+s+1);\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.typecons;\nimport std.algorithm;\nimport std.container;\nimport std.typecons;\nimport std.random;\nimport std.csv;\nimport std.regex;\nimport std.math;\nimport core.time;\nimport std.ascii;\nimport std.digest.sha;\nimport std.outbuffer;\n\nvoid main() {\n\tauto ns = readln.chomp.split.map!(to!int);\n\tint n = ns[0];\n\tint s = ns[1];\n\tint[] times;\n\ttimes ~= -s - 1;\n\tforeach (i; 0..n) {\n\t\tauto hs = readln.chomp.split.map!(to!int);\n\t\tint h = hs[0];\n\t\tint m = hs[1];\n\t\tint t = h * 60 + m;\n\t\ttimes ~= t;\n\t}\n\ttimes ~= times[$-1] + s;\n\ttimes ~= times[$-1] + 1 << 28;\n\tint len = cast(int) times.length;\n\tfor (int i = 0; i < len - 1; ++i) {\n\t\tint t1 = times[i];\n\t\tint t2 = times[i + 1];\n\t\tif (t2 - t1 >= s * 2 + 2) {\n\t\t\tint h = t1 / 60;\n\t\t\tint m = t1 % 60;\n\t\t\tm += s + 1;\n\t\t\th += m / 60;\n\t\t\tm %= 60;\n\t\t\tdebug stderr.writefln(\"[%d] %d %d ... %d\", i, t1, t2, t2 - t1);\n\t\t\twritefln(\"%d %d\", h, m);\n\t\t\treturn;\n\t\t}\n\t}\n}\n\n\n"}], "src_uid": "dcca7c58ba7111125608f659a577c3a2"} {"nl": {"description": "In this problem, we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a string. We denote the substring of string $$$s$$$ starting from the $$$l$$$-th character and ending with the $$$r$$$-th character as $$$s[l \\dots r]$$$. The characters of each string are numbered from $$$1$$$.We can perform several operations on the strings we consider. Each operation is to choose a substring of our string and replace it with another string. There are two possible types of operations: replace 011 with 110, or replace 110 with 011. For example, if we apply exactly one operation to the string 110011110, it can be transformed into 011011110, 110110110, or 110011011.Binary string $$$a$$$ is considered reachable from binary string $$$b$$$ if there exists a sequence $$$s_1$$$, $$$s_2$$$, ..., $$$s_k$$$ such that $$$s_1 = a$$$, $$$s_k = b$$$, and for every $$$i \\in [1, k - 1]$$$, $$$s_i$$$ can be transformed into $$$s_{i + 1}$$$ using exactly one operation. Note that $$$k$$$ can be equal to $$$1$$$, i. e., every string is reachable from itself.You are given a string $$$t$$$ and $$$q$$$ queries to it. Each query consists of three integers $$$l_1$$$, $$$l_2$$$ and $$$len$$$. To answer each query, you have to determine whether $$$t[l_1 \\dots l_1 + len - 1]$$$ is reachable from $$$t[l_2 \\dots l_2 + len - 1]$$$.", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of string $$$t$$$. The second line contains one string $$$t$$$ ($$$|t| = n$$$). Each character of $$$t$$$ is either 0 or 1. The third line contains one integer $$$q$$$ ($$$1 \\le q \\le 2 \\cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each line represents a query. The $$$i$$$-th line contains three integers $$$l_1$$$, $$$l_2$$$ and $$$len$$$ ($$$1 \\le l_1, l_2 \\le |t|$$$, $$$1 \\le len \\le |t| - \\max(l_1, l_2) + 1$$$) for the $$$i$$$-th query.", "output_spec": "For each query, print either YES if $$$t[l_1 \\dots l_1 + len - 1]$$$ is reachable from $$$t[l_2 \\dots l_2 + len - 1]$$$, or NO otherwise. You may print each letter in any register.", "sample_inputs": ["5\n11011\n3\n1 3 3\n1 4 2\n1 2 3"], "sample_outputs": ["Yes\nYes\nNo"], "notes": null}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nstruct ModInt(int M_) {\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op)() const if (op == \"-\") { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const { return mixin(\"ModInt(this) \" ~ op ~ \"= a\"); }\n ModInt opBinaryRight(string op)(long a) const { return mixin(\"ModInt(a) \" ~ op ~ \"= this\"); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 403;\nalias Mint = ModInt!MO;\nMint R = 2020;\n\n\n// T: monoid\n// op: T * T -> T\n// query(a, b): returns t_a ... t_{b-1}\nclass SegmentTree(T, alias op) {\n import std.functional : binaryFun;\n alias opFun = binaryFun!op;\n const(T) idT;\n\n int n;\n T[] ts;\n this(int n_, const(T) idT) {\n this.idT = idT;\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[] = idT;\n }\n this(inout(T)[] ts_, const(T) idT) {\n this.idT = idT;\n const n_ = cast(int)(ts_.length);\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[0 .. n] = idT;\n ts[n .. n + n_] = ts_[];\n ts[n + n_ .. n << 1] = idT;\n build();\n }\n ref T at(int a) {\n return ts[n + a];\n }\n void build() {\n foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void set(int a, const(T) val) {\n ts[a += n] = val;\n for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void mulL(int a, const(T) val) {\n set(a, opFun(val, ts[a + n]));\n }\n void mulR(int a, const(T) val) {\n set(a, opFun(ts[a + n], val));\n }\n T query(int a, int b) const {\n T prodL = idT, prodR = idT;\n for (a += n, b += n; a < b; a >>= 1, b >>= 1) {\n if (a & 1) prodL = opFun(prodL, ts[a++]);\n if (b & 1) prodR = opFun(ts[--b], prodR);\n }\n return opFun(prodL, prodR);\n }\n}\n\n\nMint[] RR;\n\nstruct Info {\n int num1, len, head;\n Mint val;\n Info opBinary(string op)(const(Info) o) const if (op == \"*\") {\n Info ret;\n ret.num1 = num1 + o.num1;\n if (len == 0) {\n ret.len = o.len;\n ret.head = (num1 & 1) ^ o.head;\n ret.val = o.val;\n } else if (o.len == 0) {\n ret.len = len;\n ret.head = head;\n ret.val = val;\n } else if ((head ^ (len & 1)) == ((num1 & 1) ^ o.head)) {\n // concat\n ret.len = len + o.len;\n ret.head = head;\n ret.val = val + RR[len] * o.val;\n } else {\n // overlap\n ret.len = len + o.len - 1;\n ret.head = head;\n ret.val = val + RR[len - 1] * o.val;\n }\n return ret;\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const T = readToken();\n const Q = readInt();\n auto A = new int[Q];\n auto B = new int[Q];\n auto L = new int[Q];\n foreach (q; 0 .. Q) {\n A[q] = readInt() - 1;\n B[q] = readInt() - 1;\n L[q] = readInt();\n }\n \n foreach (q; 0 .. Q) {\n R.x ^= A[q] ^ B[q] ^ L[q];\n }\n RR = new Mint[N + 1];\n RR[0] = 1;\n foreach (i; 1 .. N + 1) {\n RR[i] = RR[i - 1] * R;\n }\n \n auto seg = new SegmentTree!(Info, \"a * b\")(N, Info(0, 0, 0, Mint(0)));\n foreach (i; 0 .. N) {\n seg.at(i) = (T[i] == '1') ? Info(1, 0, 0, Mint(0)) : Info(0, 1, 0, Mint(1));\n }\n seg.build;\n debug {\n foreach (i; 0 .. N) foreach (j; i + 1 .. N + 1) {\n // writeln(i, \" \", j, \": \", seg.query(i, j));\n }\n }\n \n foreach (q; 0 .. Q) {\n const resA = seg.query(A[q], A[q] + L[q]);\n const resB = seg.query(B[q], B[q] + L[q]);\n debug {\n writeln(true, \" \", resA, \" \", resB);\n }\n const ans = (resA == resB);\n writeln(ans ? \"YES\" : \"NO\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nstruct ModInt(int M_) {\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op)() const if (op == \"-\") { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const { return mixin(\"ModInt(this) \" ~ op ~ \"= a\"); }\n ModInt opBinaryRight(string op)(long a) const { return mixin(\"ModInt(a) \" ~ op ~ \"= this\"); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 403;\nalias Mint = ModInt!MO;\n\n// T: monoid\n// op: T * T -> T\n// query(a, b): returns t_a ... t_{b-1}\nclass SegmentTree(T, alias op) {\n import std.functional : binaryFun;\n alias opFun = binaryFun!op;\n const(T) idT;\n\n int n;\n T[] ts;\n this(int n_, const(T) idT) {\n this.idT = idT;\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[] = idT;\n }\n this(inout(T)[] ts_, const(T) idT) {\n this.idT = idT;\n const n_ = cast(int)(ts_.length);\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[0 .. n] = idT;\n ts[n .. n + n_] = ts_[];\n ts[n + n_ .. n << 1] = idT;\n build();\n }\n ref T at(int a) {\n return ts[n + a];\n }\n void build() {\n foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void set(int a, const(T) val) {\n ts[a += n] = val;\n for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void mulL(int a, const(T) val) {\n set(a, opFun(val, ts[a + n]));\n }\n void mulR(int a, const(T) val) {\n set(a, opFun(ts[a + n], val));\n }\n T query(int a, int b) const {\n T prodL = idT, prodR = idT;\n for (a += n, b += n; a < b; a >>= 1, b >>= 1) {\n if (a & 1) prodL = opFun(prodL, ts[a++]);\n if (b & 1) prodR = opFun(ts[--b], prodR);\n }\n return opFun(prodL, prodR);\n }\n}\n\n\nMint R;\nMint[] RR;\n\nstruct Info {\n int num1, len, head;\n Mint val;\n Info opBinary(string op)(const(Info) o) const if (op == \"*\") {\n Info ret;\n ret.num1 = num1 + o.num1;\n if (len == 0) {\n ret.len = o.len;\n ret.head = (num1 & 1) ^ o.head;\n ret.val = o.val;\n } else if (o.len == 0) {\n ret.len = len;\n ret.head = head;\n ret.val = val;\n } else if ((head ^ (len & 1)) == ((num1 & 1) ^ o.head)) {\n // concat\n ret.len = len + o.len;\n ret.head = head;\n ret.val = val + RR[len] * o.val;\n } else {\n // overlap\n ret.len = len + o.len - 1;\n ret.head = head;\n ret.val = val + RR[len - 1] * o.val;\n }\n return ret;\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const T = readToken();\n const Q = readInt();\n auto A = new int[Q];\n auto B = new int[Q];\n auto L = new int[Q];\n foreach (q; 0 .. Q) {\n A[q] = readInt() - 1;\n B[q] = readInt() - 1;\n L[q] = readInt();\n }\n \n import std.datetime;\n R = 2020 ^ Clock.currStdTime;\n debug {\n writeln(\"R = \", R);\n }\n RR = new Mint[N + 1];\n RR[0] = 1;\n foreach (i; 1 .. N + 1) {\n RR[i] = RR[i - 1] * R;\n }\n \n auto seg = new SegmentTree!(Info, \"a * b\")(N, Info(0, 0, 0, Mint(0)));\n foreach (i; 0 .. N) {\n seg.at(i) = (T[i] == '1') ? Info(1, 0, 0, Mint(0)) : Info(0, 1, 0, Mint(1));\n }\n seg.build;\n debug {\n foreach (i; 0 .. N) foreach (j; i + 1 .. N + 1) {\n // writeln(i, \" \", j, \": \", seg.query(i, j));\n }\n }\n \n foreach (q; 0 .. Q) {\n const resA = seg.query(A[q], A[q] + L[q]);\n const resB = seg.query(B[q], B[q] + L[q]);\n debug {\n writeln(true, \" \", resA, \" \", resB);\n }\n const ans = (resA == resB);\n writeln(ans ? \"YES\" : \"NO\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nstruct SuffixArray(T) {\n import std.algorithm : sort;\n int n;\n T[] ts;\n int[] us, su, lcp;\n this(T)(T[] ts) {\n n = cast(int)(ts.length);\n this.ts = ts;\n us = new int[n + 1];\n su = new int[n + 1];\n foreach (i; 0 .. n + 1) us[i] = i;\n us.sort!((u, v) => (cmp(u, v) < 0));\n auto vals = new int[n + 1], cnt = new int[n + 1], tmp = new int[n + 1];\n foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((cmp(us[i], us[i + 1]) < 0) ? 1 : 0);\n for (int h = 1; ; h <<= 1) {\n int ahead(int i) {\n return (us[i] + h <= n) ? su[us[i] + h] : 0;\n }\n foreach (i; 0 .. n + 1) su[us[i]] = vals[i];\n if (vals[n] == n) break;\n cnt[] = 0;\n foreach (i; 0 .. n + 1) ++cnt[ahead(i)];\n foreach (j; 0 .. n) cnt[j + 1] += cnt[j];\n foreach_reverse (i; 0 .. n + 1) tmp[--cnt[ahead(i)]] = us[i];\n cnt[] = 0;\n foreach (i; 0 .. n + 1) ++cnt[su[tmp[i]]];\n foreach (j; 0 .. n) cnt[j + 1] += cnt[j];\n foreach_reverse (i; 0 .. n + 1) us[--cnt[su[tmp[i]]]] = tmp[i];\n foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((su[us[i]] < su[us[i + 1]] || ahead(i) < ahead(i + 1)) ? 1 : 0);\n }\n lcp = new int[n];\n int h;\n foreach (u; 0 .. n) {\n for (int v = us[su[u] - 1]; cmp(u + h, v + h) == 0; ++h) {}\n lcp[su[u] - 1] = h;\n if (h > 0) --h;\n }\n }\n int cmp(int u, int v) const {\n return (u == n) ? ((v == n) ? 0 : -1) : (v == n) ? +1 : (ts[u] < ts[v]) ? -1 : (ts[u] > ts[v]) ? +1 : 0;\n }\n void print() const {\n import std.math : log10;\n import std.stdio : writefln;\n const numDigits = cast(int)(log10(n)) + 1;\n foreach (i; 0 .. n + 1) {\n writefln(\"%*d %s\", numDigits, us[i], ts[us[i] .. $]);\n }\n }\n}\n\n\n// T: monoid\n// op: T * T -> T\n// query(a, b): returns t_a ... t_{b-1}\nclass SegmentTree(T, alias op) {\n import std.functional : binaryFun;\n alias opFun = binaryFun!op;\n const(T) idT;\n\n int n;\n T[] ts;\n this(int n_, const(T) idT) {\n this.idT = idT;\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[] = idT;\n }\n this(inout(T)[] ts_, const(T) idT) {\n this.idT = idT;\n const n_ = cast(int)(ts_.length);\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[0 .. n] = idT;\n ts[n .. n + n_] = ts_[];\n ts[n + n_ .. n << 1] = idT;\n build();\n }\n ref T at(int a) {\n return ts[n + a];\n }\n void build() {\n foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void set(int a, const(T) val) {\n ts[a += n] = val;\n for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void mulL(int a, const(T) val) {\n set(a, opFun(val, ts[a + n]));\n }\n void mulR(int a, const(T) val) {\n set(a, opFun(ts[a + n], val));\n }\n T query(int a, int b) const {\n T prodL = idT, prodR = idT;\n for (a += n, b += n; a < b; a >>= 1, b >>= 1) {\n if (a & 1) prodL = opFun(prodL, ts[a++]);\n if (b & 1) prodR = opFun(ts[--b], prodR);\n }\n return opFun(prodL, prodR);\n }\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const T = readToken();\n const Q = readInt();\n auto A = new int[Q];\n auto B = new int[Q];\n auto L = new int[Q];\n foreach (q; 0 .. Q) {\n A[q] = readInt() - 1;\n B[q] = readInt() - 1;\n L[q] = readInt();\n }\n \n auto num1 = new int[N + 1];\n foreach (i; 0 .. N) {\n num1[i + 1] = num1[i] + ((T[i] == '1') ? 1 : 0);\n }\n auto num11 = new int[N];\n foreach (i; 0 .. N - 1) {\n num11[i + 1] = num11[i] + ((T[i] == '1' && T[i + 1] == '1') ? 1 : 0);\n }\n \n auto sum = new long[N + 1];\n foreach (i; 0 .. N) {\n sum[i + 1] = sum[i] + ((T[i] == '1') ? i : 0);\n }\n \n auto sa = new SuffixArray!(immutable(char))(T);\n auto seg = new SegmentTree!(int, min)(sa.lcp, N + 1);\n \n foreach (q; 0 .. Q) {\n bool ans;\n if (num11[A[q] + L[q] - 1] - num11[A[q]] > 0 && num11[B[q] + L[q] - 1] - num11[B[q]] > 0) {\n const n1A = num1[A[q] + L[q]] - num1[A[q]];\n const n1B = num1[B[q] + L[q]] - num1[B[q]];\n const sA = (sum[A[q] + L[q]] - sum[A[q]]) - 1L * n1A * A[q];\n const sB = (sum[B[q] + L[q]] - sum[B[q]]) - 1L * n1B * B[q];\n debug {\n writeln(true, \" \", [n1A, n1B], \" \", [sA, sB]);\n }\n ans = (n1A == n1B && (sA - sB) % 2 == 0);\n } else {\n int a = sa.su[A[q]], b = sa.su[B[q]];\n if (a > b) {\n swap(a, b);\n }\n const res = seg.query(a, b);\n debug {\n writeln(false, \" \", A[q], \" \", B[q], \": \", res);\n }\n ans = (res >= L[q]);\n }\n writeln(ans ? \"YES\" : \"NO\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nstruct SuffixArray(T) {\n import std.algorithm : sort;\n int n;\n T[] ts;\n int[] us, su, lcp;\n this(T)(T[] ts) {\n n = cast(int)(ts.length);\n this.ts = ts;\n us = new int[n + 1];\n su = new int[n + 1];\n foreach (i; 0 .. n + 1) us[i] = i;\n us.sort!((u, v) => (cmp(u, v) < 0));\n auto vals = new int[n + 1], cnt = new int[n + 1], tmp = new int[n + 1];\n foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((cmp(us[i], us[i + 1]) < 0) ? 1 : 0);\n for (int h = 1; ; h <<= 1) {\n int ahead(int i) {\n return (us[i] + h <= n) ? su[us[i] + h] : 0;\n }\n foreach (i; 0 .. n + 1) su[us[i]] = vals[i];\n if (vals[n] == n) break;\n cnt[] = 0;\n foreach (i; 0 .. n + 1) ++cnt[ahead(i)];\n foreach (j; 0 .. n) cnt[j + 1] += cnt[j];\n foreach_reverse (i; 0 .. n + 1) tmp[--cnt[ahead(i)]] = us[i];\n cnt[] = 0;\n foreach (i; 0 .. n + 1) ++cnt[su[tmp[i]]];\n foreach (j; 0 .. n) cnt[j + 1] += cnt[j];\n foreach_reverse (i; 0 .. n + 1) us[--cnt[su[tmp[i]]]] = tmp[i];\n foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((su[us[i]] < su[us[i + 1]] || ahead(i) < ahead(i + 1)) ? 1 : 0);\n }\n lcp = new int[n];\n int h;\n foreach (u; 0 .. n) {\n for (int v = us[su[u] - 1]; cmp(u + h, v + h) == 0; ++h) {}\n lcp[su[u] - 1] = h;\n if (h > 0) --h;\n }\n }\n int cmp(int u, int v) const {\n return (u == n) ? ((v == n) ? 0 : -1) : (v == n) ? +1 : (ts[u] < ts[v]) ? -1 : (ts[u] > ts[v]) ? +1 : 0;\n }\n void print() const {\n import std.math : log10;\n import std.stdio : writefln;\n const numDigits = cast(int)(log10(n)) + 1;\n foreach (i; 0 .. n + 1) {\n writefln(\"%*d %s\", numDigits, us[i], ts[us[i] .. $]);\n }\n }\n}\n\n\n// T: monoid\n// op: T * T -> T\n// query(a, b): returns t_a ... t_{b-1}\nclass SegmentTree(T, alias op) {\n import std.functional : binaryFun;\n alias opFun = binaryFun!op;\n const(T) idT;\n\n int n;\n T[] ts;\n this(int n_, const(T) idT) {\n this.idT = idT;\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[] = idT;\n }\n this(inout(T)[] ts_, const(T) idT) {\n this.idT = idT;\n const n_ = cast(int)(ts_.length);\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[0 .. n] = idT;\n ts[n .. n + n_] = ts_[];\n ts[n + n_ .. n << 1] = idT;\n build();\n }\n ref T at(int a) {\n return ts[n + a];\n }\n void build() {\n foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void set(int a, const(T) val) {\n ts[a += n] = val;\n for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void mulL(int a, const(T) val) {\n set(a, opFun(val, ts[a + n]));\n }\n void mulR(int a, const(T) val) {\n set(a, opFun(ts[a + n], val));\n }\n T query(int a, int b) const {\n T prodL = idT, prodR = idT;\n for (a += n, b += n; a < b; a >>= 1, b >>= 1) {\n if (a & 1) prodL = opFun(prodL, ts[a++]);\n if (b & 1) prodR = opFun(ts[--b], prodR);\n }\n return opFun(prodL, prodR);\n }\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const T = readToken();\n const Q = readInt();\n auto A = new int[Q];\n auto B = new int[Q];\n auto L = new int[Q];\n foreach (q; 0 .. Q) {\n A[q] = readInt() - 1;\n B[q] = readInt() - 1;\n L[q] = readInt();\n }\n \n auto num1 = new int[N + 1];\n foreach (i; 0 .. N) {\n num1[i + 1] = num1[i] + ((T[i] == '1') ? 1 : 0);\n }\n auto num11 = new int[N];\n foreach (i; 0 .. N - 1) {\n num11[i + 1] = num11[i] + ((T[i] == '1' && T[i + 1] == '1') ? 1 : 0);\n }\n \n auto sa = new SuffixArray!(immutable(char))(T);\n auto seg = new SegmentTree!(int, min)(sa.lcp, N + 1);\n \n foreach (q; 0 .. Q) {\n bool ans;\n if (num11[A[q] + L[q] - 1] - num11[A[q]] > 0 && num11[B[q] + L[q] - 1] - num11[B[q]] > 0) {\n ans = (num1[A[q] + L[q]] - num1[A[q]] == num1[B[q] + L[q]] - num1[B[q]]);\n } else {\n int a = sa.su[A[q]], b = sa.su[B[q]];\n if (a > b) {\n swap(a, b);\n }\n const res = seg.query(a, b);\n debug {\n writeln(A[q], \" \", B[q], \": \", res);\n }\n ans = (res >= L[q]);\n }\n writeln(ans ? \"YES\" : \"NO\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nstruct SuffixArray(T) {\n import std.algorithm : sort;\n int n;\n T[] ts;\n int[] us, su, lcp;\n this(T)(T[] ts) {\n n = cast(int)(ts.length);\n this.ts = ts;\n us = new int[n + 1];\n su = new int[n + 1];\n foreach (i; 0 .. n + 1) us[i] = i;\n us.sort!((u, v) => (cmp(u, v) < 0));\n auto vals = new int[n + 1], cnt = new int[n + 1], tmp = new int[n + 1];\n foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((cmp(us[i], us[i + 1]) < 0) ? 1 : 0);\n for (int h = 1; ; h <<= 1) {\n int ahead(int i) {\n return (us[i] + h <= n) ? su[us[i] + h] : 0;\n }\n foreach (i; 0 .. n + 1) su[us[i]] = vals[i];\n if (vals[n] == n) break;\n cnt[] = 0;\n foreach (i; 0 .. n + 1) ++cnt[ahead(i)];\n foreach (j; 0 .. n) cnt[j + 1] += cnt[j];\n foreach_reverse (i; 0 .. n + 1) tmp[--cnt[ahead(i)]] = us[i];\n cnt[] = 0;\n foreach (i; 0 .. n + 1) ++cnt[su[tmp[i]]];\n foreach (j; 0 .. n) cnt[j + 1] += cnt[j];\n foreach_reverse (i; 0 .. n + 1) us[--cnt[su[tmp[i]]]] = tmp[i];\n foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((su[us[i]] < su[us[i + 1]] || ahead(i) < ahead(i + 1)) ? 1 : 0);\n }\n lcp = new int[n];\n int h;\n foreach (u; 0 .. n) {\n for (int v = us[su[u] - 1]; cmp(u + h, v + h) == 0; ++h) {}\n lcp[su[u] - 1] = h;\n if (h > 0) --h;\n }\n }\n int cmp(int u, int v) const {\n return (u == n) ? ((v == n) ? 0 : -1) : (v == n) ? +1 : (ts[u] < ts[v]) ? -1 : (ts[u] > ts[v]) ? +1 : 0;\n }\n void print() const {\n import std.math : log10;\n import std.stdio : writefln;\n const numDigits = cast(int)(log10(n)) + 1;\n foreach (i; 0 .. n + 1) {\n writefln(\"%*d %s\", numDigits, us[i], ts[us[i] .. $]);\n }\n }\n}\n\n\n// T: monoid\n// op: T * T -> T\n// query(a, b): returns t_a ... t_{b-1}\nclass SegmentTree(T, alias op) {\n import std.functional : binaryFun;\n alias opFun = binaryFun!op;\n const(T) idT;\n\n int n;\n T[] ts;\n this(int n_, const(T) idT) {\n this.idT = idT;\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[] = idT;\n }\n this(inout(T)[] ts_, const(T) idT) {\n this.idT = idT;\n const n_ = cast(int)(ts_.length);\n for (n = 1; n < n_; n <<= 1) {}\n ts = new T[n << 1];\n ts[0 .. n] = idT;\n ts[n .. n + n_] = ts_[];\n ts[n + n_ .. n << 1] = idT;\n build();\n }\n ref T at(int a) {\n return ts[n + a];\n }\n void build() {\n foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void set(int a, const(T) val) {\n ts[a += n] = val;\n for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);\n }\n void mulL(int a, const(T) val) {\n set(a, opFun(val, ts[a + n]));\n }\n void mulR(int a, const(T) val) {\n set(a, opFun(ts[a + n], val));\n }\n T query(int a, int b) const {\n T prodL = idT, prodR = idT;\n for (a += n, b += n; a < b; a >>= 1, b >>= 1) {\n if (a & 1) prodL = opFun(prodL, ts[a++]);\n if (b & 1) prodR = opFun(ts[--b], prodR);\n }\n return opFun(prodL, prodR);\n }\n}\n\n\nstruct Info {\n int a, b0, b1;\n Info opBinary(string op)(const(Info) o) const if (op == \"*\") {\n return Info(a + o.a, b0 + ((a & 1) ? o.b1 : o.b0), b1 + ((a & 1) ? o.b0 : o.b1));\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const T = readToken();\n const Q = readInt();\n auto A = new int[Q];\n auto B = new int[Q];\n auto L = new int[Q];\n foreach (q; 0 .. Q) {\n A[q] = readInt() - 1;\n B[q] = readInt() - 1;\n L[q] = readInt();\n }\n \n auto num1 = new int[N + 1];\n foreach (i; 0 .. N) {\n num1[i + 1] = num1[i] + ((T[i] == '1') ? 1 : 0);\n }\n auto num11 = new int[N];\n foreach (i; 0 .. N - 1) {\n num11[i + 1] = num11[i] + ((T[i] == '1' && T[i + 1] == '1') ? 1 : 0);\n }\n \n auto segInfo = new SegmentTree!(Info, \"a * b\")(N, Info(0, 0, 0));\n foreach (i; 0 .. N) {\n segInfo.at(i) = (T[i] == '1') ? Info(1, 0, 0) : Info(0, 1, 0);\n }\n segInfo.build;\n debug {\n foreach (i; 0 .. N) foreach (j; i + 1 .. N + 1) {\n // writeln(i, \" \", j, \": \", segInfo.query(i, j));\n }\n }\n \n auto sa = new SuffixArray!(immutable(char))(T);\n auto segLCP = new SegmentTree!(int, min)(sa.lcp, N + 1);\n \n foreach (q; 0 .. Q) {\n bool ans;\n if (num11[A[q] + L[q] - 1] - num11[A[q]] > 0 && num11[B[q] + L[q] - 1] - num11[B[q]] > 0) {\n const resA = segInfo.query(A[q], A[q] + L[q]);\n const resB = segInfo.query(B[q], B[q] + L[q]);\n debug {\n writeln(true, \" \", resA, \" \", resB);\n }\n ans = (resA == resB);\n } else {\n int a = sa.su[A[q]], b = sa.su[B[q]];\n if (a > b) {\n swap(a, b);\n }\n const res = segLCP.query(a, b);\n debug {\n writeln(false, \" \", A[q], \" \", B[q], \": \", res);\n }\n ans = (res >= L[q]);\n }\n writeln(ans ? \"YES\" : \"NO\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "6bd41042c6a442765cd93c73d55f6189"} {"nl": {"description": "Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from $$$1$$$ to $$$n$$$ and then $$$3n$$$ times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements $$$7n+1$$$ times instead of $$$3n$$$ times. Because it is more random, OK?!You somehow get a test from one of these problems and now you want to know from which one.", "input_spec": "In the first line of input there is one integer $$$n$$$ ($$$10^{3} \\le n \\le 10^{6}$$$). In the second line there are $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$ — the permutation of size $$$n$$$ from the test. It is guaranteed that all tests except for sample are generated this way: First we choose $$$n$$$ — the size of the permutation. Then we randomly choose a method to generate a permutation — the one of Petr or the one of Alex. Then we generate a permutation using chosen method.", "output_spec": "If the test is generated via Petr's method print \"Petr\" (without quotes). If the test is generated via Alex's method print \"Um_nik\" (without quotes).", "sample_inputs": ["5\n2 4 5 1 3"], "sample_outputs": ["Petr"], "notes": "NotePlease note that the sample is not a valid test (because of limitations for $$$n$$$) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.Due to randomness of input hacks in this problem are forbidden."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\nimmutable int N = 10 ^^ 6 + 23;\n\nint[N] fw;\n\nvoid fwadd(int p, int x) {\n while (p < N) {\n fw[p] += x;\n p += (1 << bsf(p));\n }\n}\n\nint fwsum(int p) {\n int ans = 0;\n while (p > 0) {\n ans += fw[p];\n p -= (1 << bsf(p));\n }\n \n debug { ans.writeln; }\n \n return ans;\n}\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n bool odd = false;\n foreach_reverse (e; arr) {\n odd ^= fwsum(e) & 1;\n fwadd(e, 1);\n }\n \n debug { odd.writeln; }\n \n bool PetrOdd = (3 * n) & 1;\n \n writeln(odd == PetrOdd ? \"Petr\" : \"Um_nik\");\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\n\nvoid main() {\n int n;\n debug dbg ();\n readf (\" %d\", &n);\n auto a = new int[n];\n foreach (i; 0 .. n) {\n readf (\" %d\", &a[i]);\n }\n auto b = new int[n];\n int merge (int l, int r) {\n if (r - l > 1) {\n immutable m = (l + r) >> 1;\n int res = merge (l, m) ^ merge (m, r);\n int i = l;\n int j = m;\n int k = 0;\n while (i < m && j < r) {\n if (a[i] < a[j]) {\n b[k++] = a[i++];\n } else {\n b[k++] = a[j++];\n res ^= (m - i) & 1;\n }\n }\n while (i < m) b[k++] = a[i++];\n while (j < r) b[k++] = a[j++];\n assert (r - l == k);\n a[l .. r] = b[0 .. k];\n return res;\n }\n return 0;\n }\n int r = merge (0, n);\n assert (r == 0 || r == 1);\n debug stderr.writeln (r);\n debug stderr.writeln (a);\n writeln (r == ((3 * n) & 1) ? \"Petr\" : \"Um_nik\");\n}\n\nvoid dbg () {\n foreach (i; 1000 .. 1000000) {\n assert ( (((3 * i) ^ (7 * i + 1)) & 1) == 1);\n }\n}\n"}], "negative_code": [], "src_uid": "a9cc20ba7d6e31706ab1743bdde97669"} {"nl": {"description": "You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word \"hello\". Determine how long it will take to print the word $$$s$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.", "output_spec": "Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.", "sample_inputs": ["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"], "sample_outputs": ["13\n0\n68\n0\n74"], "notes": null}, "positive_code": [{"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n\tauto alphabet = readString;\n\tauto word = readString;\n\tint[char] position;\n\tforeach(i, ch; alphabet) position[ch] = cast(int)i;\n\tlong sum = 0;\n\tforeach(i; 1 .. word.length)\n\t{\n\t\tsum += abs(position[word[i]] - position[word[i-1]]);\n\t}\n\tsum.writeln;\n}\n\n//{{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1;\n\tforeach(tc; 0 .. t) solve(tc);\n}\n//}}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t{\n\t\tpopChar;\n\t}\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "// Author: Ivan Kazmenko (gassa@mail.ru)\r\nmodule solution;\r\nimport std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto k = readln.strip;\r\n\t\tauto s = readln.strip;\r\n\t\tauto t = s.map !(x => k.countUntil (x)).array;\r\n\t\tzip (t, t.drop (1)).map !(q{abs (a[0] - a[1])}).sum.writeln;\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "7f9853be7ac857bb3c4eb17e554ad3f1"} {"nl": {"description": "The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.In the Pindows operating system a strings are the lexemes of the command line — the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command \" run.exe one, two . \", we give four lexemes to the Pindows command line: \"run.exe\", \"one,\", \"two\", \".\". More formally, if we run a command that can be represented as string s (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string s that contain no spaces.To send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited — that is, for each occurrence of character \"\"\" we should be able to say clearly that the quotes are opening or closing. For example, as we run the command \"\"run.exe o\" \"\" \" ne, \" two . \" \" \", we give six lexemes to the Pindows command line: \"run.exe o\", \"\" (an empty string), \" ne, \", \"two\", \".\", \" \" (a single space).It is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.You have a string that consists of uppercase and lowercase English letters, digits, characters \".,?!\"\" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character \"\"\" to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters.", "input_spec": "The single line contains a non-empty string s. String s consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the \".,?!\"\" signs, or a space. It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme.", "output_spec": "In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the \"<\" (less) character to the left of your lexemes and the \">\" (more) character to the right. Print the lexemes in the order in which they occur in the command. Please, follow the given output format strictly. For more clarifications on the output format see the test samples.", "sample_inputs": ["\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"", "firstarg second \"\""], "sample_outputs": ["<RUn.exe O>\n<>\n< 2ne, >\n<two!>\n<.>\n< >", "<firstarg>\n<second>\n<>"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.regex;\nvoid main()\n{\n string command;\n string[] lexems;\n auto reg = regex(\"[^\\\" ]+|(\\\"[^\\\"]*\\\")\");\n\n command = readln();\n command = strip(command);\n\n while(true)\n {\n auto m = match(command, reg);\n if (m.empty) break;\n lexems ~= chomp(chompPrefix(m.front.hit, `\"`),`\"`);\n command = m.post;\n }\n\n foreach(ref l; lexems)\n writeln(\"<\", l, \">\");\n}"}, {"source_code": "import std.algorithm;\nimport std.container;\nimport std.exception;\nimport std.math;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tstring s;\n\twhile ((s = readln ()) != null)\n\t{\n\t\twhile (1)\n\t\t{\n\t\t\ts = strip (s);\n\t\t\tif (s.length == 0)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tstring t;\n\t\t\tif (s[0] == '\"')\n\t\t\t{\n\t\t\t\tt = find (s[1..$], '\"');\n\t\t\t\twritefln (\"<%s>\", s[1..$ - t.length]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tt = find (s[1..$], ' ');\n\t\t\t\twritefln (\"<%s>\", s[0..$ - t.length]);\n\t\t\t}\n\t\t\ts = t;\n\t\t\tif (s.length > 0)\n\t\t\t{\n\t\t\t\ts = s[1..$];\n\t\t\t}\n\t\t}\n\t}\n}\n"}, {"source_code": "module cf_291B;\n\nimport std.stdio;\n\nvoid main() {\n char symbol;\n bool wasQuote = false;\n string lexem;\n\n while (readf(\"%c\", &symbol)) {\n if (symbol == '\\\"') {\n wasQuote = !wasQuote;\n\n if (!wasQuote) {\n writefln(\"<%s>\", lexem);\n }\n lexem = \"\";\n\n continue;\n }\n\n lexem ~= symbol;\n if (!wasQuote && (symbol == ' ' || symbol == '\\n')) {\n lexem = lexem[0 .. $ - 1];\n if (lexem.length > 0) {\n writefln(\"<%s>\", lexem);\n lexem = \"\";\n }\n }\n\n if (symbol == '\\n') {\n break;\n }\n }\n}"}, {"source_code": "module sigod.codeforces.p291B;\n\nimport std.array;\nimport std.stdio;\nimport std.string;\n\nvoid main()\n{\n\tauto result = solve(stdin.readln());\n\n\tforeach (r; result) {\n\t\tstdout.writeln(r);\n\t}\n}\n\nstring[] solve(string input)\n{\n\tinput = input.strip();\n\n\tauto result = appender!(string[])();\n\n\tbool quote = false;\n\tbool without = false;\n\tint start;\n\n\tforeach (index, c; input) {\n\t\tif (quote) {\n\t\t\tif (c == '\"') {\n\t\t\t\tresult.put('<' ~ input[start + 1 .. index] ~ '>');\n\t\t\t\tquote = false;\n\t\t\t}\n\t\t}\n\t\telse if (without) {\n\t\t\tif (c == ' ') {\n\t\t\t\tresult.put('<' ~ input[start .. index] ~ '>');\n\t\t\t\twithout = false;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (c == '\"') {\n\t\t\t\tquote = true;\n\t\t\t\tstart = index;\n\t\t\t}\n\t\t\telse if (c != ' ') {\n\t\t\t\twithout = true;\n\t\t\t\tstart = index;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (without) {\n\t\tresult.put('<' ~ input[start .. $] ~ '>');\n\t}\n\n\treturn result.data();\n}\n\nunittest {\n\tassert(std.algorithm.equal(solve(`\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"`), [\n\t\t\t\"\",\n\t\t\t\"<>\",\n\t\t\t\"< 2ne, >\",\n\t\t\t\"\",\n\t\t\t\"<.>\",\n\t\t\t\"< >\"\n\t\t]));\n\tassert(std.algorithm.equal(solve(` firstarg second \"\" `), [\n\t\t\t\"\",\n\t\t\t\"\",\n\t\t\t\"<>\"\n\t\t]));\n}"}], "negative_code": [{"source_code": "module cf_291B;\n\nimport std.stdio;\n\nvoid main() {\n char symbol;\n bool wasQuote = false;\n string lexem;\n\n while (readf(\"%c\", &symbol), symbol != '\\n') {\n if (symbol == '\\\"') {\n wasQuote = !wasQuote;\n\n if (!wasQuote) {\n writefln(\"<%s>\", lexem);\n }\n lexem = \"\";\n\n continue;\n }\n\n lexem ~= symbol;\n if (!wasQuote && symbol == ' ') {\n lexem = lexem[0 .. $ - 1];\n if (lexem.length > 0) {\n writefln(\"<%s>\", lexem);\n lexem = \"\";\n }\n }\n }\n}"}], "src_uid": "6c7858731c57e1b24c7a299a8eeab373"} {"nl": {"description": "Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).You may perform the following operations until both a and s are empty: Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty). You can perform these operations in arbitrary order.If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations: Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b. After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k qi = pi, and qk > pk). You may not swap or change any of first k elements of the permutation.Print the lexicographically maximal permutation p you can obtain.If there exists no answer then output -1.", "input_spec": "The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively. The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the first k elements of p. These integers are pairwise distinct.", "output_spec": "If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation. Otherwise print -1.", "sample_inputs": ["5 3\n3 2 1", "5 3\n2 3 1", "5 1\n3", "5 2\n3 4"], "sample_outputs": ["3 2 1 5 4", "-1", "3 2 1 5 4", "-1"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nconst int INF = 1 << 29;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1];\n auto A = readln.split.map!(to!int).array;\n\n auto ans = new int[](N);\n int p = 0;\n auto used = new bool[](N+1);\n foreach (a; A) used[a] = true;\n int[] stack;\n int back = 0;\n auto rbt = new RedBlackTree!(int, \"a < b\")();\n foreach (i; 1..N+1) if (!used[i]) rbt.insert(i);\n\n foreach (i; 0..K) {\n while (!stack.empty && stack.back == back + 1) {\n stack.popBack;\n back += 1;\n }\n if (A[i] == back + 1) {\n back = A[i];\n } else if (stack.empty || stack.back > A[i]) {\n stack ~= A[i];\n } else {\n writeln(-1);\n return;\n }\n ans[p++] = A[i];\n }\n\n while (!rbt.empty) {\n while (!stack.empty && stack.back == back + 1) {\n stack.popBack;\n back += 1;\n }\n\n int tar = stack.empty ? INF : stack.back;\n auto lb = rbt.lowerBound(tar);\n if (lb.empty) {\n writeln(-1);\n return;\n }\n auto x = lb.back;\n\n rbt.removeKey(x);\n if (x == back + 1) {\n back += 1;\n } else {\n stack ~= x;\n }\n ans[p++] = x;\n }\n\n ans.map!(to!string).join(\" \").writeln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nconst int INF = 1 << 29;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1];\n auto A = readln.split.map!(to!int).array;\n\n auto ans = new int[](N);\n int p = 0;\n auto used = new bool[](N+1);\n foreach (a; A) used[a] = true;\n int[] stack;\n int back = 0;\n auto rbt = new RedBlackTree!(int, \"a < b\")();\n foreach (i; 1..N+1) if (!used[i]) rbt.insert(i);\n\n foreach (i; 0..K) {\n if (A[i] == back + 1) {\n back = A[i];\n } else if (stack.empty || stack.back > A[i]) {\n stack ~= A[i];\n } else {\n writeln(-1);\n return;\n }\n ans[p++] = A[i];\n }\n\n while (!rbt.empty) {\n while (!stack.empty && stack.back == back + 1) {\n stack.popBack;\n back += 1;\n }\n\n int tar = stack.empty ? INF : stack.back;\n auto lb = rbt.lowerBound(tar);\n if (lb.empty) {\n writeln(-1);\n return;\n }\n auto x = lb.back;\n\n rbt.removeKey(x);\n if (x == back + 1) {\n back += 1;\n } else {\n stack ~= x;\n }\n ans[p++] = x;\n }\n\n ans.map!(to!string).join(\" \").writeln;\n}\n"}], "src_uid": "848e81bc0aeaec7dbe3ec8e68d7b07ef"} {"nl": {"description": "It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array $$$a$$$. Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices $$$x, y, z, w$$$ such that $$$a_x + a_y = a_z + a_w$$$.Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery?", "input_spec": "The first line contains the single integer $$$n$$$ ($$$4 \\leq n \\leq 200\\,000$$$) — the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 2.5 \\cdot 10^6$$$).", "output_spec": "Print \"YES\" if there are such four indices, and \"NO\" otherwise. If such indices exist, print these indices $$$x$$$, $$$y$$$, $$$z$$$ and $$$w$$$ ($$$1 \\le x, y, z, w \\le n$$$). If there are multiple answers, print any of them.", "sample_inputs": ["6\n2 1 5 2 7 4", "5\n1 3 1 9 20"], "sample_outputs": ["YES\n2 3 1 6", "NO"], "notes": "NoteIn the first example $$$a_2 + a_3 = 1 + 5 = 2 + 4 = a_1 + a_6$$$. Note that there are other answer, for example, 2 3 4 6.In the second example, we can't choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that $$$a_1 + a_2 = 1 + 3 = 3 + 1 = a_2 + a_3$$$"}, "positive_code": [{"source_code": "import std;\r\n\r\nalias P = Tuple!(int, int);\r\n\r\nvoid main() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").to!(int[]);\r\n {\r\n int[][int] M;\r\n foreach (i, a; A) {\r\n M[a] ~= i;\r\n if (M[a].length >= 4) {\r\n writeln(\"YES\");\r\n writefln(\"%(%s %)\", M[a][0 .. 4].map!(i => i + 1));\r\n return;\r\n }\r\n }\r\n }\r\n Tuple!(int,int)[][int] C;\r\n for (int i = 0; i < N; i++) {\r\n for (int j = i+1; j < N; j++) {\r\n int s = A[i] + A[j];\r\n C[s] ~= tuple(i, j);\r\n C[s] ~= tuple(j, i);\r\n if (C[s].length >= 4) {\r\n auto xs = C[s];\r\n foreach (x; xs) {\r\n foreach (y; xs) {\r\n if (x[0] == y[0] || x[0] == y[1] || x[1] == y[0] || x[1] == y[1]) continue;\r\n writeln(\"YES\");\r\n writefln(\"%(%s %)\", [x[0] + 1, x[1] + 1, y[0] + 1, y[1] + 1]);\r\n return;\r\n }\r\n }\r\n }\r\n }\r\n }\r\n writeln(\"NO\");\r\n}\r\n"}], "negative_code": [{"source_code": "import std;\r\n\r\nalias P = Tuple!(int, int);\r\n\r\nvoid main() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").to!(int[]);\r\n\r\n int[][int] M;\r\n foreach (i, a; A) {\r\n M[a] ~= i;\r\n }\r\n auto X = M.byPair.array.sort!((a, b) => a.value.length > b.value.length).array;\r\n if (X.front.value.length >= 4) {\r\n int[] xs = X[0].value;\r\n writeln(\"YES\");\r\n writefln(\"%(%s %)\", xs[0 .. 4].map!(i => i + 1));\r\n } else if (X.length >= 2 && X[0].value.length >= 2 && X[1].value.length >= 2) {\r\n int[] xs = X[0].value;\r\n int[] ys = X[1].value;\r\n writeln(\"YES\");\r\n writefln(\"%(%s %)\", [xs[0], ys[0], xs[1], ys[1]].map!(i => i + 1));\r\n } else {\r\n int n = X.length;\r\n X.sort!\"a.key < b.key\";\r\n Tuple!(int,int)[][int] C;\r\n for (int i = 0; i < n; i++) {\r\n for (int j = i+1; j < n; j++) {\r\n int s = X[i].key + X[j].key;\r\n C[s] ~= tuple(X[i].value[0], X[j].value[0]);\r\n if (C[s].length == 2) {\r\n writeln(\"YES\");\r\n writefln(\"%(%s %)\", [C[s][0][0], C[s][0][1], C[s][1][0], C[s][1][1]].map!(i => i + 1));\r\n return;\r\n }\r\n }\r\n }\r\n writeln(\"NO\");\r\n }\r\n}\r\n"}], "src_uid": "704297bc97528ec27cce5f9388019e29"} {"nl": {"description": "Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos.In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word \"helllo\" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words \"helloo\" and \"wwaatt\" contain typos).Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word.", "input_spec": "The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters.", "output_spec": "Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them.", "sample_inputs": ["helloo", "woooooow"], "sample_outputs": ["hello", "woow"], "notes": "NoteThe second valid answer to the test from the statement is \"heloo\"."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array, std.container;\nimport std.typecons;\n\nalias Tuple!(char, int) P;\n\nvoid main() {\n string s = stdin.readln.chomp;\n Array!P oc;\n\n int count = 1;\n char ch = s[0];\n foreach (i; 1 .. s.length) {\n if (s[i] == ch) {\n count++;\n } else {\n oc.insert(P(ch, count));\n count = 1;\n ch = s[i];\n }\n }\n oc.insert(P(ch, count));\n\n foreach (i; 0 .. oc.length) {\n auto o = oc[i];\n if (o[1] >= 2) { \n o[1] = 2;\n if (i + 1 < oc.length) {\n auto p = oc[i + 1];\n if (p[1] >= 2) {\n p[1] = 1;\n oc[i + 1] = p;\n }\n }\n }\n oc[i] = o;\n }\n\n foreach (o; oc) {\n foreach (_; 0 .. o[1]) {\n o[0].write;\n }\n }\n \"\".writeln;\n}\n"}], "negative_code": [], "src_uid": "31d803d886c47fe681bdcfbe6c74f090"} {"nl": {"description": "This is an interactive problemYou are given a grid $$$n\\times n$$$, where $$$n$$$ is odd. Rows are enumerated from $$$1$$$ to $$$n$$$ from up to down, columns are enumerated from $$$1$$$ to $$$n$$$ from left to right. Cell, standing on the intersection of row $$$x$$$ and column $$$y$$$, is denoted by $$$(x, y)$$$.Every cell contains $$$0$$$ or $$$1$$$. It is known that the top-left cell contains $$$1$$$, and the bottom-right cell contains $$$0$$$.We want to know numbers in all cells of the grid. To do so we can ask the following questions: \"$$$?$$$ $$$x_1$$$ $$$y_1$$$ $$$x_2$$$ $$$y_2$$$\", where $$$1 \\le x_1 \\le x_2 \\le n$$$, $$$1 \\le y_1 \\le y_2 \\le n$$$, and $$$x_1 + y_1 + 2 \\le x_2 + y_2$$$. In other words, we output two different cells $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$ of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent.As a response to such question you will be told if there exists a path between $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$, going only to the right or down, numbers in cells of which form a palindrome.For example, paths, shown in green, are palindromic, so answer for \"$$$?$$$ $$$1$$$ $$$1$$$ $$$2$$$ $$$3$$$\" and \"$$$?$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$3$$$\" would be that there exists such path. However, there is no palindromic path between $$$(1, 1)$$$ and $$$(3, 1)$$$. Determine all cells of the grid by asking not more than $$$n^2$$$ questions. It can be shown that the answer always exists.", "input_spec": "The first line contains odd integer ($$$3 \\le n < 50$$$) — the side of the grid.", "output_spec": null, "sample_inputs": ["3\n0\n1\n0\n1\n1\n1\n1"], "sample_outputs": ["? 1 1 1 3\n? 1 1 2 3\n? 2 1 2 3\n? 3 1 3 3\n? 2 2 3 3\n? 1 2 3 2\n? 1 2 3 3\n!\n100\n001\n000"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\t\n\tint[][] xf = new int[][](n, n);\n\t\n\txf[0][0] = 1;\n\txf[n - 1][n - 1] = 0;\n\t\n\tbool ask(int ai, int aj, int bi, int bj){\n\t\tint[] q;\n\t\tif(ai < bi || ai == bi && aj < bj) q = [ai + 1, aj + 1, bi + 1, bj + 1];\n\t\telse q = [bi + 1, bj + 1, ai + 1, aj + 1];\n\t\t(\"? \" ~ q.map!(to!string).array.join(\" \")).writeln;\n\t\tstdout.flush;\n\t\treturn (readln.chomp == \"1\");\n\t}\n\t\n\tvoid set(int i, int j, int bi, int bj){\n\t\tbool b = ask(bi, bj, i, j);\n\t\tif(b) xf[i][j] = xf[bi][bj];\n\t\telse xf[i][j] = 1 - xf[bi][bj];\n\t}\n\t\t\t\t\n\tforeach(i; 0 .. n){\n\t\tforeach(j; 0 .. n){\n\t\t\tif(i == 0 && j == 1) xf[i][j] = 3; // 3 or -2\n\t\t\tif(i == 1 && j == 1) set(i, j, 0, 0);\n\t\t\tif(i == 1 && j == 2){\n\t\t\t\tset(i, j, 0, 1);\n\t\t\t\tset(1, 0, 1, 2);\n\t\t\t}\n\t\t\telse if(i >= 2) set(i, j, i - 2, j);\n\t\t\telse if(j >= 2) set(i, j, i, j - 2); \n\t\t}\n\t}\n\t\n\tvoid equalize(int ai, int aj, int bi, int bj){\n\t\tbool b = ask(ai, aj, bi, bj);\n\t\tint x = xf[ai][aj], y = xf[bi][bj];\n\t\tint[int] m;\n\t\tm[0] = 0, m[1] = 1;\n\t\tif(x == 0 || x == 1){\n\t\t\tif(b) m[y] = x, m[1 - y] = 1 - x;\n\t\t\telse m[y] = 1 - x, m[1 - y] = x;\n\t\t}\n\t\telse{\n\t\t\tif(b) m[x] = y, m[1 - x] = 1 - y;\n\t\t\telse m[x] = 1 - y, m[1 - x] = y;\n\t\t}\n\t\tforeach(i; 0 .. n) foreach(j; 0 .. n){\n\t\t\txf[i][j] = m[xf[i][j]];\n\t\t}\n\t}\n\t\n\tforeach(i; 0 .. n){\n\t\tforeach(j; 0 .. n){\n\t\t\tif(i > 0 && j < n - 1){\n\t\t\t\tif(xf[i][j] != xf[i - 1][j + 1]){\n\t\t\t\t\tif(i >= 2) equalize(i - 2, j, i, j + 1);\n\t\t\t\t\telse equalize(i - 1, j, i + 1, j + 1);\n\t\t\t\t\tgoto A;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tforeach(i; 0 .. n - 2){\n\t\tif(xf[i][i] == xf[i + 1][i + 1] && xf[i][i + 1] == xf[i + 1][i + 2]\n\t\t\t\t|| xf[i][i] != xf[i + 1][i + 1] && xf[i][i + 1] != xf[i + 1][i + 2]){\n\t\t\tequalize(i, i, i + 1, i + 2);\n\t\t\tgoto A;\n\t\t}\n\t\tif(xf[i][i + 1] == xf[i + 1][i + 2] && xf[i + 1][i + 1] == xf[i + 2][i + 2]\n\t\t\t\t|| xf[i][i + 1] != xf[i + 1][i + 2] && xf[i + 1][i + 1] != xf[i + 2][i + 2]){\n\t\t\tequalize(i, i + 1, i + 2, i + 2);\n\t\t\tgoto A;\n\t\t}\n\t}\n\t\n\tA:\n\t\"!\".writeln;\n\tforeach(i; 0 .. n){\n\t\txf[i].map!(x => x.to!string).array.join(\"\").writeln;\n\t}\n\t\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\t\n\tint[][] xf = new int[][](n, n);\n\t\n\txf[0][0] = 1;\n\txf[n - 1][n - 1] = 0;\n\t\n\tbool ask(int ai, int aj, int bi, int bj){\n\t\t(\"? \" ~ [ai + 1, aj + 1, bi + 1, bj + 1].map!(to!string).array.join(\" \")).writeln;\n\t\tstdout.flush;\n\t\treturn (readln.chomp == \"1\");\n\t}\n\t\n\tvoid set(int i, int j, int bi, int bj){\n\t\tbool b = ask(bi, bj, i, j);\n\t\tif(b) xf[i][j] = xf[bi][bj];\n\t\telse xf[i][j] = 1 - xf[bi][bj];\n\t}\n\t\t\t\t\n\tforeach(i; 0 .. n){\n\t\tforeach(j; 0 .. n){\n\t\t\tif(i == 0 && j == 1) xf[i][j] = 3; // 3 or -2\n\t\t\tif(i == 1 && j == 0) set(i, j, 0, 1);\n\t\t\tif(i == 1 && j == 1) set(i, j, 0, 0);\n\t\t\tif(i >= 2) set(i, j, i - 2, j);\n\t\t\telse if(j >= 2) set(i, j, i, j - 2); \n\t\t}\n\t}\n\t\n\tvoid equalize(int ai, int aj, int bi, int bj){\n\t\tbool b = ask(ai, aj, bi, bj);\n\t\tint x = xf[ai][aj], y = xf[bi][bj];\n\t\tint[int] m;\n\t\tm[0] = 0, m[1] = 1;\n\t\tif(x == 0 || x == 1){\n\t\t\tif(b) m[y] = x, m[1 - y] = 1 - x;\n\t\t\telse m[y] = 1 - x, m[1 - y] = x;\n\t\t}\n\t\telse{\n\t\t\tif(b) m[x] = y, m[1 - x] = 1 - y;\n\t\t\telse m[x] = 1 - y, m[1 - x] = y;\n\t\t}\n\t\tforeach(i; 0 .. n) foreach(j; 0 .. n){\n\t\t\txf[i][j] = m[xf[i][j]];\n\t\t}\n\t}\n\t\n\tforeach(i; 0 .. n){\n\t\tforeach(j; 0 .. n){\n\t\t\tif(i > 0 && j < n - 1){\n\t\t\t\tif(xf[i][j] != xf[i - 1][j + 1]){\n\t\t\t\t\tif(i >= 2) equalize(i - 2, j, i, j + 1);\n\t\t\t\t\telse equalize(i - 1, j, i + 1, j + 1);\n\t\t\t\t\tgoto A;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tforeach(i; 0 .. n - 2){\n\t\tif(xf[i][i] == xf[i + 1][i + 1] && xf[i][i + 1] == xf[i + 1][i + 2]\n\t\t\t\t|| xf[i][i] != xf[i + 1][i + 1] && xf[i][i + 1] != xf[i + 1][i + 2]){\n\t\t\tequalize(i, i, i + 1, i + 2);\n\t\t\tgoto A;\n\t\t}\n\t\tif(xf[i][i + 1] == xf[i + 1][i + 2] && xf[i + 1][i + 1] == xf[i + 2][i + 2]\n\t\t\t\t|| xf[i][i + 1] != xf[i + 1][i + 2] && xf[i + 1][i + 1] != xf[i + 2][i + 2]){\n\t\t\tequalize(i, i + 1, i + 2, i + 2);\n\t\t\tgoto A;\n\t\t}\n\t}\n\t\n\tA:\n\t\"!\".writeln;\n\tforeach(i; 0 .. n){\n\t\txf[i].map!(x => x.to!string).array.join(\"\").writeln;\n\t}\n\t\n}\n"}], "src_uid": "0d37339ba85957f9b6cde4f254457196"} {"nl": {"description": "You have $$$r$$$ red and $$$b$$$ blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans $$$r_i \\ge 1$$$); has at least one blue bean (or the number of blue beans $$$b_i \\ge 1$$$); the number of red and blue beans should differ in no more than $$$d$$$ (or $$$|r_i - b_i| \\le d$$$) Can you distribute all beans?", "input_spec": "The first line contains the single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first and only line of each test case contains three integers $$$r$$$, $$$b$$$, and $$$d$$$ ($$$1 \\le r, b \\le 10^9$$$; $$$0 \\le d \\le 10^9$$$) — the number of red and blue beans and the maximum absolute difference in each packet.", "output_spec": "For each test case, if you can distribute all beans, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["4\n1 1 0\n2 7 3\n6 1 4\n5 4 0"], "sample_outputs": ["YES\nYES\nNO\nNO"], "notes": "NoteIn the first test case, you can form one packet with $$$1$$$ red and $$$1$$$ blue bean. The absolute difference $$$|1 - 1| = 0 \\le d$$$.In the second test case, you can form two packets: $$$1$$$ red and $$$4$$$ blue beans in the first packet and $$$1$$$ red and $$$3$$$ blue beans in the second one.In the third test case, since $$$b = 1$$$, you can form only one packet with $$$6$$$ red and $$$1$$$ blue beans. The absolute difference $$$|6 - 1| = 5 > d$$$.In the fourth test case, since $$$d = 0$$$ so each packet should contain the same number of red and blue beans, but $$$r \\neq b$$$."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(params[0] == 0 || params[1] == 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n long res = (max / min) + (max % min != 0 ? 1 : 0) - 1;\r\n writeln(res <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(abs(params[0] - params[1]) <= d)\r\n {\r\n writeln(\"YES\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n long res = (max / min) + (max % min != 0 ? 1 : 0) - 1;\r\n writeln(res <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1519/problem/A\n// greedy\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n\nwhile(t--) {\n long r, b, d;\n readf(\"%s %s %s\\n\", &r, &b, &d);\n if(r > b)\n swap(r,b);\n if(b <= r*(d + 1))\n \"YES\".writeln;\n else\n \"NO\".writeln;\n}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad) * vec[0] - sin(rad) * vec[1], sin(rad) * vec[0] + cos(rad) * vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new bool[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto r = RD;\r\n\t\tauto b = RD;\r\n\t\tauto d = RD;\r\n\r\n\t\tauto x = min(b, r);\r\n\t\tauto y = max(b, r);\r\n\t\tans[ti] = y-x <= d * x;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e ? \"YES\" : \"NO\");\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(params[0] == 0 || params[1] == 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else if(abs(params[0] - params[1]) <= d)\r\n {\r\n writeln(\"YES\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n long res = (max / min) + (max % min) - 1;\r\n writeln(res <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(params[0] <= 0 || params[1] <= 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n long res = (max / min) + (max % min) - 1;\r\n writeln(res <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(params[0] <= 0 || params[1] <= 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n long res = (max / min) - 1;\r\n writeln(max % min <= d && res <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(params[0] <= 0 || params[1] <= 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n long res = (max / min) - min;\r\n writeln(res >= 0 && res <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!long).array;\r\n long d = params[2];\r\n \r\n if(params[0] <= 0 || params[1] <= 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else\r\n {\r\n long min = params[0] > params[1] ? params[1] : params[0];\r\n long max = params[0] > params[1] ? params[0] : params[1];\r\n \r\n writeln(abs((max / min) - min) <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!int).array;\r\n int d = params[2];\r\n \r\n if(params[0] == 0 || params[1] == 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n if(params[0] == 1 || params[1] == 1)\r\n {\r\n writeln(d == 0 ? \"YES\" : \"NO\");\r\n }\r\n else\r\n {\r\n int min = params[0] > params[1] ? params[1] : params[0];\r\n int max = params[0] > params[1] ? params[0] : params[1];\r\n \r\n writeln(abs((max / min) - min) <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.algorithm;\r\nimport std.array;\r\nimport std.string;\r\nimport std.uni;\r\nimport std.math;\r\nimport std.container.rbtree;\r\nimport std.range;\r\n\r\nvoid main()\r\n{\r\n auto n= to!long(readln().chomp);\r\n foreach(_; 0..n)\r\n {\r\n auto params = readln().chomp.splitter(\" \").map!(to!int).array;\r\n int d = params[2];\r\n \r\n if(params[0] == 0 || params[1] == 0)\r\n {\r\n writeln(\"NO\");\r\n }\r\n else\r\n {\r\n int min = params[0] > params[1] ? params[1] : params[0];\r\n int max = params[0] > params[1] ? params[0] : params[1];\r\n \r\n writeln(abs((max / min) - min) <= d ? \"YES\" : \"NO\");\r\n }\r\n }\r\n}"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1519/problem/A\n// greedy\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n\nwhile(t--) {\n int r, b, d;\n readf(\"%s %s %s\\n\", &r, &b, &d);\n if(r > b)\n swap(r,b);\n if(b <= r*(d + 1))\n \"YES\".writeln;\n else\n \"NO\".writeln;\n}\n}\n"}], "src_uid": "c0ad2a6d14b0c9e09af2221d88a34d52"} {"nl": {"description": "There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have $$$b$$$ buns, $$$p$$$ beef patties and $$$f$$$ chicken cutlets in your restaurant. You can sell one hamburger for $$$h$$$ dollars and one chicken burger for $$$c$$$ dollars. Calculate the maximum profit you can achieve.You have to answer $$$t$$$ independent queries.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) – the number of queries. The first line of each query contains three integers $$$b$$$, $$$p$$$ and $$$f$$$ ($$$1 \\le b, ~p, ~f \\le 100$$$) — the number of buns, beef patties and chicken cutlets in your restaurant. The second line of each query contains two integers $$$h$$$ and $$$c$$$ ($$$1 \\le h, ~c \\le 100$$$) — the hamburger and chicken burger prices in your restaurant.", "output_spec": "For each query print one integer — the maximum profit you can achieve.", "sample_inputs": ["3\n15 2 3\n5 10\n7 5 2\n10 12\n1 100 100\n100 100"], "sample_outputs": ["40\n34\n0"], "notes": "NoteIn first query you have to sell two hamburgers and three chicken burgers. Your income is $$$2 \\cdot 5 + 3 \\cdot 10 = 40$$$.In second query you have to ell one hamburgers and two chicken burgers. Your income is $$$1 \\cdot 10 + 2 \\cdot 12 = 34$$$.In third query you can not create any type of burgers because because you have only one bun. So your income is zero."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nint main()\n{\n\tint t=0;\n\treadf(\" %d\", &t);\n\tfor (int i=0; ie)\n\t\t\t{\n\t\t\t\tsum=sum+min(r, b)*d+min(r-min(r,b),c)*e;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tsum=sum+min(r, c)*e+min(r-min(r,c),b)*d;\n\t\t\t}\n\t\t}\n\t\twriteln(sum);\n\t}\n\treturn 0;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (i; 0..t)\n\t{\n\t\tauto bpf = RDA;\n\t\tauto b = bpf[0];\n\t\tauto hc = RDA;\n\t\tlong cnt1, cnt2;\n\t\tif (hc[0] > hc[1])\n\t\t{\n\t\t\tcnt1 = bpf[1];\n\t\t\tcnt2 = bpf[2];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcnt1 = bpf[2];\n\t\t\tcnt2 = bpf[1];\n\t\t}\n\t\tauto c1 = min(cnt1, b/2);\n\t\tb -= c1 * 2;\n\t\tans[i] += c1 * max(hc[0], hc[1]);\n\t\tauto c2 = min(cnt2, b/2);\n\t\tans[i] += c2 * min(hc[0], hc[1]);\n\t}\n\t\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "92bf30e66f4d5ddebb697d2fa4fa0689"} {"nl": {"description": " William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands.A valid nested list is any list which can be created from a list with one item \"1\" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $$$a_1 \\,.\\, a_2 \\,.\\, a_3 \\,.\\, \\,\\cdots\\, \\,.\\,a_k$$$ and can be one of two types: Add an item $$$a_1 \\,.\\, a_2 \\,.\\, a_3 \\,.\\, \\cdots \\,.\\, a_k \\,.\\, 1$$$ (starting a list of a deeper level), or Add an item $$$a_1 \\,.\\, a_2 \\,.\\, a_3 \\,.\\, \\cdots \\,.\\, (a_k + 1)$$$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the \"Notes\" section.When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the \"Ctrl-S\" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number.William wants you to help him restore a fitting original nested list.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^3$$$), which is the number of lines in the list. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$1 \\le a_i \\le n$$$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $$$n$$$ across all test cases does not exceed $$$10^3$$$.", "output_spec": "For each test case output $$$n$$$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any.", "sample_inputs": ["2\n4\n1\n1\n2\n3\n9\n1\n1\n1\n2\n2\n1\n2\n1\n2"], "sample_outputs": ["1\n1.1\n1.2\n1.3\n1\n1.1\n1.1.1\n1.1.2\n1.2\n1.2.1\n2\n2.1\n2.2"], "notes": "NoteIn the second example test case one example of a fitting list is:11.1 1.1.11.1.21.21.2.122.12.2This list can be produced by using the sequence of operations shown below: Original list with a single item $$$1$$$. Insert item $$$2$$$ by using the insertion operation of the second type after item $$$1$$$. Insert item $$$1.1$$$ by using the insertion operation of the first type after item $$$1$$$. Insert item $$$1.2$$$ by using the insertion operation of the second type after item $$$1.1$$$. Insert item $$$1.1.1$$$ by using the insertion operation of the first type after item $$$1.1$$$. Insert item $$$1.1.2$$$ by using the insertion operation of the second type after item $$$1.1.1$$$. Insert item $$$1.2.1$$$ by using the insertion operation of the first type after item $$$1.2$$$. Insert item $$$2.1$$$ by using the insertion operation of the first type after item $$$2$$$. Insert item $$$2.2$$$ by using the insertion operation of the second type after item $$$2.1$$$. "}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tint [] c;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tc ~= readln.strip.to !(int);\r\n\t\t}\r\n\r\n\t\tint [] stack;\r\n\t\tforeach (ref x; c)\r\n\t\t{\r\n\t\t\tif (x == 1)\r\n\t\t\t{\r\n\t\t\t\tstack ~= x;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\twhile (stack.back != x - 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tstack.popBack ();\r\n\t\t\t\t}\r\n\t\t\t\tstack.assumeSafeAppend ();\r\n\t\t\t\tstack.back += 1;\r\n\t\t\t}\r\n\t\t\twritefln !(\"%(%s.%)\") (stack);\r\n\t\t}\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "c1bf6c8a9a20f377cf2a5dbea2267c88"} {"nl": {"description": "\"The Chamber of Secrets has been opened again\" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.The Chamber of Secrets is an n × m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below. The left light ray passes through a regular column, and the right ray — through the magic column. The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position. This figure illustrates the first sample test. Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.", "input_spec": "The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either \".\" or \"#\" and represents one cell of the Chamber grid. It's \".\" if the corresponding cell is empty and \"#\" if it's a regular column.", "output_spec": "Print the minimum number of columns to make magic or -1 if it's impossible to do.", "sample_inputs": ["3 3\n.#.\n...\n.#.", "4 3\n##.\n...\n.#.\n.#."], "sample_outputs": ["2", "2"], "notes": "NoteThe figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n string[] arr;\n foreach (i; 0 .. n) { arr ~= readln.chomp; }\n\n debug { arr.writefln!\"%(%s %)\"; }\n \n auto rows = new bool[] (n);\n auto cols = new bool[] (m);\n \n auto rq = make!(DList!int), cq = make!(DList!int);\n rq ~= n-1;\n \n int step = 0;\n bool reach = false;\n outer: while (!rq.empty()) {\n step += 2;\n while (!rq.empty()) {\n int x = rq.front();\n rq.removeFront();\n \n if (rows[x]) { continue; }\n \n rows[x] = true;\n foreach (i; 0 .. m) {\n if (arr[x][i] == '.') { continue; }\n if (cols[i]) { continue; }\n \n cq ~= i;\n }\n }\n \n while (!cq.empty()) {\n int x = cq.front();\n cq.removeFront();\n \n if (cols[x]) { continue; }\n \n cols[x] = true;\n foreach (i; 0 .. n) {\n if (arr[i][x] == '.') { continue; }\n if (rows[i]) { continue; }\n \n if (i == 0) { \n reach = true;\n break outer;\n }\n \n rq ~= i;\n }\n }\n }\n \n if (!reach) {\n writeln(-1);\n return;\n }\n \n step.writeln;\n}"}], "negative_code": [], "src_uid": "8b6f93cf2a41445649e0cbfc471541b6"} {"nl": {"description": "The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, \"101101\" is a palindrome, while \"0101\" is not.Alice and Bob are playing a game on a string $$$s$$$ (which is initially a palindrome in this version) of length $$$n$$$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.In each turn, the player can perform one of the following operations: Choose any $$$i$$$ ($$$1 \\le i \\le n$$$), where $$$s[i] =$$$ '0' and change $$$s[i]$$$ to '1'. Pay 1 dollar. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, \"01001\" becomes \"10010\" after reversing.The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^3$$$). The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting of the characters '0' and '1'. It is guaranteed that the string $$$s$$$ is a palindrome and contains at least one '0'. Note that there is no limit on the sum of $$$n$$$ over test cases.", "output_spec": "For each test case print a single word in a new line: \"ALICE\", if Alice will win the game, \"BOB\", if Bob will win the game, \"DRAW\", if the game ends in a draw. ", "sample_inputs": ["2\n4\n1001\n1\n0"], "sample_outputs": ["BOB\nBOB"], "notes": "NoteIn the first test case of the example, in the $$$1$$$-st move Alice has to perform the $$$1$$$-st operation, since the string is currently a palindrome. in the $$$2$$$-nd move Bob reverses the string. in the $$$3$$$-rd move Alice again has to perform the $$$1$$$-st operation. All characters of the string are '1', game over. Alice spends $$$2$$$ dollars while Bob spends $$$0$$$ dollars. Hence, Bob always wins."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto s = RD!string;\r\n\r\n\t\tlong cnt;\r\n\t\tforeach (c; s)\r\n\t\t{\r\n\t\t\tif (c == '0')\r\n\t\t\t\t++cnt;\r\n\t\t}\r\n\r\n\t\tif (cnt % 2)\r\n\t\t\tans[ti] = cnt == 1 ? -1 : 1;\r\n\t\telse\r\n\t\t\tans[ti] = -1;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.range, std.stdio, std.string, std.typecons;\r\n\r\nvoid main() {\r\n foreach(t; 0 .. to!int(readln.chomp)) {\r\n int n = to!int(readln.chomp);\r\n string s = readln.chomp;\r\n bool pal = true;\r\n int open = 0;\r\n foreach(i; 0 .. s.length/2) {\r\n if(s[i] == s[$-(1+i)]) {\r\n if(s[i] == '0') open += 2;\r\n } else {\r\n open++;\r\n pal = false;\r\n }\r\n }\r\n if(pal) {\r\n if(n == 1) {\r\n writeln(\"BOB\");\r\n } else if(n%2) {\r\n if(s[$/2] == '0' && open) writeln(\"ALICE\");\r\n else writeln(\"BOB\");\r\n } else {\r\n writeln(\"BOB\");\r\n }\r\n\r\n } else {\r\n }\r\n }\r\n}\r\n"}, {"source_code": "import std.stdio, std.string;\r\nvoid main()\r\n{\r\n int q;\r\n readf!\"%d\\n\"(q);\r\n foreach (_; 0 .. q)\r\n {\r\n readln;\r\n ulong cnt = readln.chomp.count('0');\r\n if (cnt == 1)\r\n writeln(\"BOB\");\r\n else if (cnt % 2)\r\n writeln(\"ALICE\");\r\n else\r\n writeln(\"BOB\");\r\n }\r\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto s = RD!string;\r\n\r\n\t\tlong cnt;\r\n\t\tforeach (c; s)\r\n\t\t{\r\n\t\t\tif (c == '0')\r\n\t\t\t\t++cnt;\r\n\t\t}\r\n\r\n\t\tif (cnt % 2)\r\n\t\t\tans[ti] = cnt <= 3 ? -1 : 1;\r\n\t\telse\r\n\t\t\tans[ti] = -1;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto s = RD!string;\r\n\r\n\t\tlong cnt;\r\n\t\tforeach (c; s)\r\n\t\t{\r\n\t\t\tif (c == '0')\r\n\t\t\t\t++cnt;\r\n\t\t}\r\n\r\n\t\tif (cnt % 2)\r\n\t\t\tans[ti] = ((cnt/2) % 2) ? 1 : -1;\r\n\t\telse\r\n\t\t\tans[ti] = -1;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto s = RD!string;\n\n\t\tlong cnt;\n\t\tforeach (c; s)\n\t\t{\n\t\t\tif (c == '0')\n\t\t\t\t++cnt;\n\t\t}\n\n\t\tif (cnt % 2)\n\t\t\tans[ti] = ((cnt/2) % 2) ? 1 : -1;\n\t\telse\n\t\t\tans[ti] = ((cnt/2) % 2) ? -1 : 0;\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto s = RD!string;\n\n\t\tlong cnt;\n\t\tforeach (c; s)\n\t\t{\n\t\t\tif (c == '0')\n\t\t\t\t++cnt;\n\t\t}\n\t\tif (cnt == 0) continue;\n\n\t\tif (cnt % 2)\n\t\t\tans[ti] = (cnt/2) % 2 ? 1 : -1;\n\t\telse\n\t\t\tans[ti] = cnt % 4 ? -1 : 0;\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto s = RD!string;\n\n\t\tlong cnt;\n\t\tforeach (c; s)\n\t\t{\n\t\t\tif (c == '0')\n\t\t\t\t++cnt;\n\t\t}\n\t\tif (cnt == 0) continue;\n\n\t\tif (n % 2 && s[n/2] == '0')\n\t\t{\n\t\t\tif (cnt % 2 == 0)\n\t\t\t\tans[ti] = 0;\n\t\t\telse\n\t\t\t\tans[ti] = (cnt/2) % 2 ? 1 : -1;\n\t\t}\n\t\telse\n\t\t\tans[ti] = cnt % 4 ? -1 : 0;\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD;\n\t\tauto s = RD!string;\n\n\t\tlong cnt;\n\t\tforeach (c; s)\n\t\t{\n\t\t\tif (c == '0')\n\t\t\t\t++cnt;\n\t\t}\n\t\tif (cnt == 0) continue;\n\t\tans[ti] = cnt % 4 ? -1 : 0;\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD;\n\t\tauto s = RD!string;\n\n\t\tlong cnt;\n\t\tforeach (c; s)\n\t\t{\n\t\t\tif (c == '0')\n\t\t\t\t++cnt;\n\t\t}\n\t\tans[ti] = cnt % 4 ? -1 : 0;\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e == 1 ? \"ALICE\" : e == -1 ? \"BOB\" : \"DRAW\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.range, std.stdio, std.string, std.typecons;\r\n\r\nvoid main() {\r\n foreach(t; 0 .. to!int(readln.chomp)) {\r\n int n = to!int(readln.chomp);\r\n string s = readln.chomp;\r\n bool pal = true;\r\n int open = 0;\r\n foreach(i; 0 .. s.length/2) {\r\n if(s[i] == s[$-(1+i)]) {\r\n if(s[i] == '0') open += 2;\r\n } else {\r\n open++;\r\n pal = false;\r\n }\r\n }\r\n if(pal) {\r\n if(n == 1) {\r\n writeln(\"BOB\");\r\n } else if(n%2) {\r\n if(s[$/2] == '0') writeln(\"ALICE\");\r\n else writeln(\"BOB\");\r\n } else {\r\n writeln(\"BOB\");\r\n }\r\n\r\n } else {\r\n }\r\n }\r\n}\r\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.range, std.stdio, std.string, std.typecons;\r\n\r\nvoid main() {\r\n foreach(t; 0 .. to!int(readln.chomp)) {\r\n int n = to!int(readln.chomp);\r\n string s = readln.chomp;\r\n bool pal = true;\r\n int open = 0;\r\n foreach(i; 0 .. s.length/2) {\r\n if(s[i] == s[$-(1+i)]) {\r\n if(s[i] == '0') open += 2;\r\n } else {\r\n open++;\r\n pal = false;\r\n }\r\n }\r\n if(pal) {\r\n if(n == 1) {\r\n writeln(\"BOB\");\r\n } else if(n%2) {\r\n if(s[$/2+1] == '0') writeln(\"ALICE\");\r\n else writeln(\"BOB\");\r\n } else {\r\n writeln(\"BOB\");\r\n }\r\n\r\n } else {\r\n }\r\n }\r\n}\r\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.range, std.stdio, std.string, std.typecons;\r\n\r\nvoid main() {\r\n foreach(t; 0 .. to!int(readln.chomp)) {\r\n int n = to!int(readln.chomp);\r\n string s = readln.chomp;\r\n bool pal = true;\r\n int open = 0;\r\n foreach(i; 0 .. s.length/2) {\r\n if(s[i] == s[$-(1+i)]) {\r\n if(s[i] == '0') open += 2;\r\n } else {\r\n open++;\r\n pal = false;\r\n }\r\n }\r\n if(pal) {\r\n if(n == 1) {\r\n writeln(\"BOB\");\r\n } else if(n%2) {\r\n writeln(\"ALICE\");\r\n } else {\r\n writeln(\"BOB\");\r\n }\r\n\r\n } else {\r\n }\r\n }\r\n}\r\n"}], "src_uid": "42b425305ccc28b0d081b4c417fe77a1"} {"nl": {"description": "Even polar bears feel cold when lying on the ice. Therefore, a polar bear Alice is going to make a carpet. The carpet can be viewed as a grid with height h and width w. Then the grid is divided into h × w squares. Alice is going to assign one of k different colors to each square. The colors are numbered from 1 to k. She may choose not to use all of the colors.However, there are some restrictions. For every two adjacent squares (squares that shares an edge) x and y, there is a color constraint in one of the forms: color(x) = color(y), or color(x) ≠ color(y). Example of the color constraints: Ideally, Alice wants to satisfy all color constraints. But again, life in the Arctic is hard. It is not always possible to satisfy all color constraints. Fortunately, she will still be happy if at least of the color constraints are satisfied. If she has 4 colors she can color the carpet in the following way: And she is happy because of the color constraints are satisfied, and . Your task is to help her color the carpet.", "input_spec": "The first line contains three integers h, w, k (2 ≤ h, w ≤ 1000, 1 ≤ k ≤ w·h). The next 2h - 1 lines describe the color constraints from top to bottom, left to right. They contain w - 1, w, w - 1, w, ..., w - 1 characters respectively. Each color constraint is represented by a character \"E\" or \"N\", where \"E\" means \" = \" and \"N\" means \" ≠ \". The color constraints listed in the order they are depicted on the picture.", "output_spec": "If there is a coloring that satisfies at least of the color constraints, print \"YES\" (without quotes) in the first line. In each of the next h lines, print w integers describing the coloring. Otherwise, print \"NO\" (without quotes).", "sample_inputs": ["3 4 4\nENE\nNNEE\nNEE\nENEN\nENN"], "sample_outputs": ["YES\n1 1 2 2\n3 4 1 1\n3 3 2 4"], "notes": null}, "positive_code": [{"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint M, N, K;\nchar[][] A;\n\nint[][] solve() {\n\tif (K == 1) {\n\t\tint nmr, dnm;\n\t\tforeach (x; 0 .. M) foreach (y; 0 .. N) {\n\t\t\tif (x > 0) {\n\t\t\t\tif (A[(x - 1) + x][y + y] == 'E') {\n\t\t\t\t\t++nmr;\n\t\t\t\t}\n\t\t\t\t++dnm;\n\t\t\t}\n\t\t\tif (y > 0) {\n\t\t\t\tif (A[x + x][(y - 1) + y] == 'E') {\n\t\t\t\t\t++nmr;\n\t\t\t\t}\n\t\t\t\t++dnm;\n\t\t\t}\n\t\t}\n\t\tif (nmr * 4 >= dnm * 3) {\n\t\t\treturn new int[][](M, N);\n\t\t} else {\n\t\t\treturn null;\n\t\t}\n\t} else {\n\t\tint m, n;\n\t\tchar[][] a;\n\t\tif ((M - 1) * N <= M * (N - 1)) {\n\t\t\tm = M;\n\t\t\tn = N;\n\t\t\ta = A;\n\t\t} else {\n\t\t\tm = N;\n\t\t\tn = M;\n\t\t\ta = new char[][](m * 2, n * 2);\n\t\t\tforeach (x; 0 .. m * 2) foreach (y; 0 .. n * 2) {\n\t\t\t\ta[x][y] = A[y][x];\n\t\t\t}\n\t\t}\n\t\tint[][] ret = new int[][](m, n);\n\t\tforeach (x; 0 .. m) {\n\t\t\tforeach (src; 0 .. 2) {\n\t\t\t\tret[x][0] = src;\n\t\t\t\tforeach (y; 1 .. n) {\n\t\t\t\t\tret[x][y] = ret[x][y - 1] ^ ((a[x + x][(y - 1) + y] != 'E') ? 1 : 0);\n\t\t\t\t}\n\t\t\t\tif (x > 0) {\n\t\t\t\t\tint cnt;\n\t\t\t\t\tforeach (y; 0 .. n) {\n\t\t\t\t\t\tif ((a[(x - 1) + x][y + y] == 'E') == (ret[x - 1][y] == ret[x][y])) {\n\t\t\t\t\t\t\t++cnt;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (cnt * 2 >= n) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint[][] RET;\n\t\tif ((M - 1) * N <= M * (N - 1)) {\n\t\t\tRET = ret;\n\t\t} else {\n\t\t\tRET = new int[][](M, N);\n\t\t\tforeach (x; 0 .. M) foreach (y; 0 .. N) {\n\t\t\t\tRET[x][y] = ret[y][x];\n\t\t\t}\n\t\t}\n\t\treturn RET;\n\t}\n}\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tM = readInt;\n\t\tN = readInt;\n\t\tK = readInt;\n\t\tA = new char[][](M * 2, N * 2);\n\t\tforeach (x; 0 .. M) {\n\t\t\tif (x > 0) {\n\t\t\t\tauto str = readToken;\n\t\t\t\tforeach (y; 0 .. N) {\n\t\t\t\t\tA[(x - 1) + x][y + y] = str[y];\n\t\t\t\t}\n\t\t\t}\n\t\t\t{\n\t\t\t\tauto str = readToken;\n\t\t\t\tforeach (y; 1 .. N) {\n\t\t\t\t\tA[x + x][(y - 1) + y] = str[y - 1];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tif (auto res = solve) {\n\t\t\twriteln(\"YES\");\n\t\t\tforeach (line; res) {\n\t\t\t\tline[] += 1;\n\t\t\t\twriteln(line.to!string.removechars(\"[],\"));\n\t\t\t}\n\t\t} else {\n\t\t\twriteln(\"NO\");\n\t\t}\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [{"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint M, N, K;\nchar[][] A;\n\nint[][] solve() {\n\tif (K == 1) {\n\t\tint nmr, dnm;\n\t\tforeach (x; 0 .. M) foreach (y; 0 .. N) {\n\t\t\tif (x > 0) {\n\t\t\t\tif (A[(x - 1) + x][y + y] == 'E') {\n\t\t\t\t\t++nmr;\n\t\t\t\t}\n\t\t\t\t++dnm;\n\t\t\t}\n\t\t\tif (y > 0) {\n\t\t\t\tif (A[x + x][(y - 1) + y] == 'E') {\n\t\t\t\t\t++nmr;\n\t\t\t\t}\n\t\t\t\t++dnm;\n\t\t\t}\n\t\t}\n\t\tif (nmr * 2 >= dnm) {\n\t\t\treturn new int[][](M, N);\n\t\t} else {\n\t\t\treturn null;\n\t\t}\n\t} else {\n\t\tint m, n;\n\t\tchar[][] a;\n\t\tif ((M - 1) * N <= M * (N - 1)) {\n\t\t\tm = M;\n\t\t\tn = N;\n\t\t\ta = A;\n\t\t} else {\n\t\t\tm = N;\n\t\t\tn = M;\n\t\t\ta = new char[][](m * 2, n * 2);\n\t\t\tforeach (x; 0 .. m * 2) foreach (y; 0 .. n * 2) {\n\t\t\t\ta[x][y] = A[y][x];\n\t\t\t}\n\t\t}\n\t\tint[][] ret = new int[][](m, n);\n\t\tforeach (x; 0 .. m) {\n\t\t\tforeach (src; 0 .. 2) {\n\t\t\t\tret[x][0] = src;\n\t\t\t\tforeach (y; 1 .. n) {\n\t\t\t\t\tret[x][y] = ret[x][y - 1] ^ ((a[x + x][(y - 1) + y] != 'E') ? 1 : 0);\n\t\t\t\t}\n\t\t\t\tif (x > 0) {\n\t\t\t\t\tint cnt;\n\t\t\t\t\tforeach (y; 0 .. n) {\n\t\t\t\t\t\tif ((a[(x - 1) + x][y + y] == 'E') == (ret[x - 1][y] == ret[x][y])) {\n\t\t\t\t\t\t\t++cnt;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (cnt * 2 >= n) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint[][] RET;\n\t\tif ((M - 1) * N <= M * (N - 1)) {\n\t\t\tRET = ret;\n\t\t} else {\n\t\t\tRET = new int[][](M, N);\n\t\t\tforeach (x; 0 .. M) foreach (y; 0 .. N) {\n\t\t\t\tRET[x][y] = ret[y][x];\n\t\t\t}\n\t\t}\n\t\treturn RET;\n\t}\n}\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tM = readInt;\n\t\tN = readInt;\n\t\tK = readInt;\n\t\tA = new char[][](M * 2, N * 2);\n\t\tforeach (x; 0 .. M) {\n\t\t\tif (x > 0) {\n\t\t\t\tauto str = readToken;\n\t\t\t\tforeach (y; 0 .. N) {\n\t\t\t\t\tA[(x - 1) + x][y + y] = str[y];\n\t\t\t\t}\n\t\t\t}\n\t\t\t{\n\t\t\t\tauto str = readToken;\n\t\t\t\tforeach (y; 1 .. N) {\n\t\t\t\t\tA[x + x][(y - 1) + y] = str[y - 1];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tif (auto res = solve) {\n\t\t\twriteln(\"YES\");\n\t\t\tforeach (line; res) {\n\t\t\t\tline[] += 1;\n\t\t\t\twriteln(line.to!string.removechars(\"[],\"));\n\t\t\t}\n\t\t} else {\n\t\t\twriteln(\"NO\");\n\t\t}\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "src_uid": "a5f650b6f5737f654eb30f1e57ce03d6"} {"nl": {"description": "You are given array $$$a_1, a_2, \\dots, a_n$$$. Find the subsegment $$$a_l, a_{l+1}, \\dots, a_r$$$ ($$$1 \\le l \\le r \\le n$$$) with maximum arithmetic mean $$$\\frac{1}{r - l + 1}\\sum\\limits_{i=l}^{r}{a_i}$$$ (in floating-point numbers, i.e. without any rounding).If there are many such subsegments find the longest one.", "input_spec": "The first line contains single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the array $$$a$$$.", "output_spec": "Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.", "sample_inputs": ["5\n6 1 6 6 0"], "sample_outputs": ["2"], "notes": "NoteThe subsegment $$$[3, 4]$$$ is the longest among all subsegments with maximum arithmetic mean."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n auto a = readln.chomp.split.map!(to!int).array;\n \n auto mx = a.maxElement;\n \n int ans = 1;\n for (int i = 0; i < n; ) {\n if (a[i] != mx) {\n i += 1;\n continue;\n }\n \n int j = i+1;\n while (j < n && a[j] == mx) ++j;\n \n ans = max(ans, j - i);\n \n i = j;\n }\n \n ans.writeln;\n}"}], "negative_code": [], "src_uid": "5db2ae5b2c91b29e4fe4a45ab864e3f1"} {"nl": {"description": "Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \\ne j$$$; $$$1 \\leq i,j \\leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.", "input_spec": "The first line contains a single positive integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 100$$$) — the sequence $$$a$$$.", "output_spec": "For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.", "sample_inputs": ["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"], "sample_outputs": ["4\n3\n2"], "notes": "NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 < a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$."}, "positive_code": [{"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N);\r\n\r\n long ans;\r\n if (!A.canFind(0)) {\r\n if (A.sort.uniq.array.length == N) ans++;\r\n ans += N;\r\n } else {\r\n ans = A.count!\"a != 0\";\r\n }\r\n \r\n return ans;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve().writeln;\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}], "negative_code": [], "src_uid": "ad242f98f1c8eb8d30789ec672fc95a0"} {"nl": {"description": "You are given $$$n$$$ elements numbered from $$$1$$$ to $$$n$$$, the element $$$i$$$ has value $$$a_i$$$ and color $$$c_i$$$, initially, $$$c_i = 0$$$ for all $$$i$$$.The following operation can be applied: Select three elements $$$i$$$, $$$j$$$ and $$$k$$$ ($$$1 \\leq i < j < k \\leq n$$$), such that $$$c_i$$$, $$$c_j$$$ and $$$c_k$$$ are all equal to $$$0$$$ and $$$a_i = a_k$$$, then set $$$c_j = 1$$$. Find the maximum value of $$$\\sum\\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.", "input_spec": "The first line contains an integer $$$n$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of elements. The second line consists of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element.", "output_spec": "Print a single integer in a line — the maximum value of $$$\\sum\\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.", "sample_inputs": ["7\n1 2 1 2 7 4 7", "13\n1 2 3 2 1 3 3 4 5 5 5 4 7"], "sample_outputs": ["2", "7"], "notes": "NoteIn the first test, it is possible to apply the following operations in order: "}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto hi = new int [n + 1];\r\n\t\thi[] = int.min;\r\n\t\tforeach (int i, ref c; a)\r\n\t\t{\r\n\t\t\thi[c] = max (hi[c], i);\r\n\t\t}\r\n\r\n\t\tint best = -1;\r\n\t\tint next = -1;\r\n\t\tint res = 0;\r\n\t\tforeach (int i, ref c; a)\r\n\t\t{\r\n\t\t\tnext = max (next, hi[c]);\r\n\t\t\tif (i < best)\r\n\t\t\t{\r\n\t\t\t\tres += 1;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tbest = next;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto hi = new int [n + 1];\r\n\t\thi[] = int.min;\r\n\t\tforeach (int i, ref c; a)\r\n\t\t{\r\n\t\t\thi[c] = max (hi[c], i);\r\n\t\t}\r\n\r\n\t\tint best = -1;\r\n\t\tint lo = -1;\r\n\t\tint res = 0;\r\n\t\tforeach (int i, ref c; a)\r\n\t\t{\r\n\t\t\tif (best < hi[c])\r\n\t\t\t{\r\n\t\t\t\tbest = hi[c];\r\n\t\t\t\tif (lo < i)\r\n\t\t\t\t{\r\n\t\t\t\t\tlo = best;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tres += 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\telse if (i < best)\r\n\t\t\t{\r\n\t\t\t\tres += 1;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto lo = new int [n + 1];\r\n\t\tlo[] = int.max;\r\n\t\tauto hi = new int [n + 1];\r\n\t\thi[] = int.min;\r\n\t\tforeach (int i, ref c; a)\r\n\t\t{\r\n\t\t\tlo[c] = min (lo[c], i);\r\n\t\t\thi[c] = max (hi[c], i);\r\n\t\t}\r\n\r\n\t\tint best = 0;\r\n\t\tint res = 0;\r\n\t\tforeach (int i, ref c; a)\r\n\t\t{\r\n\t\t\tif (best < hi[c])\r\n\t\t\t{\r\n\t\t\t\tbest = hi[c];\r\n\t\t\t}\r\n\t\t\telse if (best > i)\r\n\t\t\t{\r\n\t\t\t\tres += 1;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "src_uid": "daf675dadc8edcd6734edbf3548eb6bf"} {"nl": {"description": "Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n.Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city x to city y cannot be used to travel from city y to city x. The transportation within each city is extremely developed, therefore if a pipe from city x to city y and a pipe from city y to city z are both constructed, people will be able to travel from city x to city z instantly.Mr. Kitayuta is also involved in national politics. He considers that the transportation between the m pairs of city (ai, bi) (1 ≤ i ≤ m) is important. He is planning to construct teleportation pipes so that for each important pair (ai, bi), it will be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.", "input_spec": "The first line contains two space-separated integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively. The following m lines describe the important pairs. The i-th of them (1 ≤ i ≤ m) contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), denoting that it must be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). It is guaranteed that all pairs (ai, bi) are distinct.", "output_spec": "Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.", "sample_inputs": ["4 5\n1 2\n1 3\n1 4\n2 3\n2 4", "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4"], "sample_outputs": ["3", "4"], "notes": "NoteFor the first sample, one of the optimal ways to construct pipes is shown in the image below: For the second sample, one of the optimal ways is shown below: "}, "positive_code": [{"source_code": "import std.stdio, std.array, std.range, std.string, std.typecons;\nimport std.algorithm, std.container, std.math, std.numeric, std.random;\nvoid scan(T...)(ref T args) { foreach (ref arg; args) readf(\" %s\", &arg); }\nvoid minify(T)(ref T a, in T b) { if (a > b) a = b; }\nvoid maxify(T)(ref T a, in T b) { if (a < b) a = b; }\nvoid ewriteln(T...)(T args) { stderr.writeln(\"\\033[35m\", args, \"\\033[0m\"); }\nint ilen(T)(const ref T a) { return cast(int)(a.length); }\n\nenum int N = 10 ^^ 5 + 8;\nint n, m;\nArray!int[N] graph;\nArray!int[N] antigraph;\nint[N] outdeg;\nbool[N] vis;\nArray!int curv;\nint cost = 0;\n\nvoid dfs(int v) {\n\tif (vis[v]) return;\n\tcurv ~= v;\n\tvis[v] = true;\n\tforeach (w; graph[v]) { dfs(w); }\n\tforeach (w; antigraph[v]) { dfs(w); }\n}\n\nSList!int out0;\nvoid solveFrom(int v0) {\n\tif (!vis[v0]) {\n\t\tcurv.length = 0;\n\t\tdfs(v0);\n\t\tout0.clear();\n\t\tdebug {\n\t\t\tewriteln(\"solving from: \", v0);\n\t\t\tewriteln(\" curv cap \", curv.capacity);\n\t\t\tewriteln(\" out0 cap \", out0.capacity);\n\t\t}\n\t\tint ovs = 0;\n\t\tforeach (v; curv) {\n\t\t\toutdeg[v] = graph[v].ilen;\n\t\t\tif (outdeg[v] == 0) {\n\t\t\t\tout0.insertFront(v);\n\t\t\t\tovs++;\n\t\t\t}\n\t\t}\n\t\twhile (!out0.empty) {\n\t\t\tint v = out0.front; out0.removeFront();\n\t\t\tforeach (w; antigraph[v]) {\n\t\t\t\tassert(outdeg[w] >= 1);\n\t\t\t\toutdeg[w]--;\n\t\t\t\tif (outdeg[w] == 0) {\n\t\t\t\t\tout0.insertFront(w);\n\t\t\t\t\tovs++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcost += ovs == curv.ilen ? curv.ilen - 1 : curv.ilen;\n\t}\n}\n\nint solve() {\n\tforeach (i; 1..n+1) { solveFrom(i); }\n\treturn cost;\n}\n\nvoid main() {\n\tscan(n, m);\n\tforeach(_; 0..m) {\n\t\tint a, b;\n\t\tscan(a, b);\n\t\tgraph[a] ~= b;\n\t\tantigraph[b] ~= a;\n\t}\n\twriteln(solve());\n}\n"}, {"source_code": "import std.stdio, std.array, std.range, std.string, std.typecons;\nimport std.algorithm, std.container, std.math, std.numeric, std.random;\nvoid scan(T...)(ref T args) { foreach (ref arg; args) readf(\" %s\", &arg); }\nvoid minify(T)(ref T a, in T b) { if (a > b) a = b; }\nvoid maxify(T)(ref T a, in T b) { if (a < b) a = b; }\nvoid ewriteln(T...)(T args) { stderr.writeln(\"\\033[35m\", args, \"\\033[0m\"); }\nint ilen(T)(const ref T a) { return cast(int)(a.length); }\nvoid push(T)(ref Array!T a, in T b) {\n\tif (a.length == a.capacity) a.reserve(2*a.capacity + 1);\n\ta ~= b;\n}\n\nenum int N = 10 ^^ 5 + 8;\nint n, m;\nArray!int[N] graph;\nArray!int[N] antigraph;\nint[N] outdeg;\nbool[N] vis;\nArray!int curv;\nint cost = 0;\n\nvoid dfs(int v) {\n\tif (vis[v]) return;\n\tcurv.push(v);\n\tvis[v] = true;\n\tforeach (w; graph[v]) { dfs(w); }\n\tforeach (w; antigraph[v]) { dfs(w); }\n}\n\nArray!int out0;\nvoid solveFrom(int v0) {\n\tif (!vis[v0]) {\n\t\tcurv.length = 0;\n\t\tdfs(v0);\n\t\tout0.length = 0;\n\t\tdebug {\n\t\t\tewriteln(\"solving from: \", v0);\n\t\t\tewriteln(\" curv cap \", curv.capacity);\n\t\t\tewriteln(\" out0 cap \", out0.capacity);\n\t\t}\n\t\tint ovs = 0;\n\t\tforeach (v; curv) {\n\t\t\toutdeg[v] = graph[v].ilen;\n\t\t\tif (outdeg[v] == 0) {\n\t\t\t\tout0.push(v);\n\t\t\t\tovs++;\n\t\t\t}\n\t\t}\n\t\twhile (!out0.empty) {\n\t\t\tint v = out0.back; out0.removeBack();\n\t\t\tforeach (w; antigraph[v]) {\n\t\t\t\tassert(outdeg[w] >= 1);\n\t\t\t\toutdeg[w]--;\n\t\t\t\tif (outdeg[w] == 0) {\n\t\t\t\t\tout0.push(w);\n\t\t\t\t\tovs++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcost += ovs == curv.ilen ? curv.ilen - 1 : curv.ilen;\n\t}\n}\n\nint solve() {\n\tforeach (i; 1..n+1) { solveFrom(i); }\n\treturn cost;\n}\n\nvoid main() {\n\tscan(n, m);\n\tforeach(_; 0..m) {\n\t\tint a, b;\n\t\tscan(a, b);\n\t\tgraph[a].push(b);\n\t\tantigraph[b].push(a);\n\t}\n\twriteln(solve());\n}\n"}, {"source_code": "import std.stdio, std.array, std.range, std.string, std.typecons;\nimport std.algorithm, std.container, std.math, std.numeric, std.random;\nvoid scan(T...)(ref T args) { foreach (ref arg; args) scanf(\"%d\", &arg); }\nvoid minify(T)(ref T a, in T b) { if (a > b) a = b; }\nvoid maxify(T)(ref T a, in T b) { if (a < b) a = b; }\nvoid ewriteln(T...)(T args) { stderr.writeln(\"\\033[35m\", args, \"\\033[0m\"); }\nint ilen(T)(const ref T a) { return cast(int)(a.length); }\nvoid push(T)(ref Array!T a, in T b) {\n\t// if (a.length == a.capacity) a.reserve(2*a.capacity + 1);\n\ta ~= b;\n}\n\nenum int N = 10 ^^ 5 + 8;\nint n, m;\nArray!int[N] graph;\nArray!int[N] antigraph;\nint[N] outdeg;\nbool[N] vis;\nArray!int curv;\nint cost = 0;\n\nvoid dfs(int v) {\n\tif (vis[v]) return;\n\tcurv.push(v);\n\tvis[v] = true;\n\tforeach (w; graph[v]) { dfs(w); }\n\tforeach (w; antigraph[v]) { dfs(w); }\n}\n\nArray!int out0;\nvoid solveFrom(int v0) {\n\tif (!vis[v0]) {\n\t\tcurv.length = 0;\n\t\tdfs(v0);\n\t\tout0.length = 0;\n\t\tdebug {\n\t\t\tewriteln(\"solving from: \", v0);\n\t\t\tewriteln(\" curv cap \", curv.capacity);\n\t\t\tewriteln(\" out0 cap \", out0.capacity);\n\t\t}\n\t\tint ovs = 0;\n\t\tforeach (v; curv) {\n\t\t\toutdeg[v] = graph[v].ilen;\n\t\t\tif (outdeg[v] == 0) {\n\t\t\t\tout0.push(v);\n\t\t\t\tovs++;\n\t\t\t}\n\t\t}\n\t\twhile (!out0.empty) {\n\t\t\tint v = out0.back; out0.removeBack();\n\t\t\tforeach (w; antigraph[v]) {\n\t\t\t\tassert(outdeg[w] >= 1);\n\t\t\t\toutdeg[w]--;\n\t\t\t\tif (outdeg[w] == 0) {\n\t\t\t\t\tout0.push(w);\n\t\t\t\t\tovs++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcost += ovs == curv.ilen ? curv.ilen - 1 : curv.ilen;\n\t}\n}\n\nint solve() {\n\tforeach (i; 1..n+1) { solveFrom(i); }\n\treturn cost;\n}\n\nvoid main() {\n\tscan(n, m);\n\tforeach(_; 0..m) {\n\t\tint a, b;\n\t\tscan(a, b);\n\t\tgraph[a].push(b);\n\t\tantigraph[b].push(a);\n\t}\n\twriteln(solve());\n}\n"}, {"source_code": "import std.stdio, std.array, std.range, std.string, std.typecons;\nimport std.algorithm, std.container, std.math, std.numeric, std.random;\nvoid scan(T...)(ref T args) { foreach (ref arg; args) readf(\" %s\", &arg); }\nvoid minify(T)(ref T a, in T b) { if (a > b) a = b; }\nvoid maxify(T)(ref T a, in T b) { if (a < b) a = b; }\nvoid ewriteln(T...)(T args) { stderr.writeln(\"\\033[35m\", args, \"\\033[0m\"); }\nint ilen(T)(const ref T a) { return cast(int)(a.length); }\n\nenum int N = 10 ^^ 5 + 8;\nint n, m;\nArray!int[N] graph;\nArray!int[N] antigraph;\nint[N] outdeg;\nbool[N] vis;\nArray!int curv;\nint cost = 0;\n\nvoid dfs(int v) {\n\tif (vis[v]) return;\n\tcurv ~= v;\n\tvis[v] = true;\n\tforeach (w; graph[v]) { dfs(w); }\n\tforeach (w; antigraph[v]) { dfs(w); }\n}\n\nArray!int out0;\nvoid solveFrom(int v0) {\n\tif (!vis[v0]) {\n\t\tcurv.length = 0;\n\t\tdfs(v0);\n\t\tout0.length = 0;\n\t\tdebug {\n\t\t\tewriteln(\"solving from: \", v0);\n\t\t\tewriteln(\" curv cap \", curv.capacity);\n\t\t\tewriteln(\" out0 cap \", out0.capacity);\n\t\t}\n\t\tint ovs = 0;\n\t\tforeach (v; curv) {\n\t\t\toutdeg[v] = graph[v].ilen;\n\t\t\tif (outdeg[v] == 0) {\n\t\t\t\tout0 ~= v;\n\t\t\t\tovs++;\n\t\t\t}\n\t\t}\n\t\twhile (!out0.empty) {\n\t\t\tint v = out0.back; out0.removeBack();\n\t\t\tforeach (w; antigraph[v]) {\n\t\t\t\tassert(outdeg[w] >= 1);\n\t\t\t\toutdeg[w]--;\n\t\t\t\tif (outdeg[w] == 0) {\n\t\t\t\t\tout0 ~= w;\n\t\t\t\t\tovs++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcost += ovs == curv.ilen ? curv.ilen - 1 : curv.ilen;\n\t}\n}\n\nint solve() {\n\tforeach (i; 1..n+1) { solveFrom(i); }\n\treturn cost;\n}\n\nvoid main() {\n\tscan(n, m);\n\tforeach(_; 0..m) {\n\t\tint a, b;\n\t\tscan(a, b);\n\t\tgraph[a] ~= b;\n\t\tantigraph[b] ~= a;\n\t}\n\twriteln(solve());\n}\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\nauto pair(S, T)(inout(S) x, T y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint root(int[] uf, int u) {\n\treturn (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool conn(int[] uf, int u, int v) {\n\tu = uf.root(u);\n\tv = uf.root(v);\n\tif (u == v) return false;\n\tif (uf[u] > uf[v]) swap(u, v);\n\tuf[u] += uf[v];\n\tuf[v] = u;\n\treturn true;\n}\n\nPair!(int, int[]) scc(int[][] g0, int[][] g1) {\n\tint n = g0.length;\n\tint compN;\n\tint[] compIds = new int[n];\n\tvoid dfs(int[][] g, int u, int a, int b, ref int[] st) {\n\t\tif (compIds[u] == a) {\n\t\t\tcompIds[u] = b;\n\t\t\tforeach (v; g[u]) dfs(g, v, a, b, st);\n\t\t\tst ~= u;\n\t\t}\n\t}\n\tint[] stack, dump;\n\tforeach (u; 0 .. n) {\n\t\tdfs(g0, u, 0, -1, stack);\n\t}\n\tfor (; !stack.empty; ) {\n\t\tint u = stack[$ - 1];\n\t\tif (compIds[u] == -1) {\n\t\t\tdfs(g1, u, -1, compN, dump);\n\t\t\t++compN;\n\t\t}\n\t\tstack.popBack;\n\t}\n\treturn pair(compN, compIds);\n}\n\nint N, M;\nint[] A, B;\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tN = readInt;\n\t\tM = readInt;\n\t\tA = new int[M];\n\t\tB = new int[M];\n\t\tforeach (i; 0 .. M) {\n\t\t\tA[i] = readInt - 1;\n\t\t\tB[i] = readInt - 1;\n\t\t}\n\t\t\n\t\tint[] uf = new int[N];\n\t\tfill(uf, -1);\n\t\tforeach (i; 0 .. M) {\n\t\t\tuf.conn(A[i], B[i]);\n\t\t}\n\t\tint[][] uComps = new int[][N];\n\t\tforeach (u; 0 .. N) {\n\t\t\tuComps[uf.root(u)] ~= u;\n\t\t}\n\t\t\n\t\tint[][] g0 = new int[][N];\n\t\tint[][] g1 = new int[][N];\n\t\tforeach (i; 0 .. M) {\n\t\t\tg0[A[i]] ~= B[i];\n\t\t\tg1[B[i]] ~= A[i];\n\t\t}\n\t\tint[] compIds = scc(g0, g1).y;\n\t\t\n\t\tint ans;\n\t\tforeach (uComp; uComps) if (!uComp.empty) {\n\t\t\tconst int sz = uComp.length;\n\t\t\tint[] ids = new int[sz];\n\t\t\tforeach (j; 0 .. sz) {\n\t\t\t\tids[j] = compIds[uComp[j]];\n\t\t\t}\n\t\t\tids.sort();\n\t\t\tbool ok = true;\n\t\t\tforeach (j; 1 .. sz) {\n\t\t\t\tok = ok && (ids[j - 1] != ids[j]);\n\t\t\t}\n\t\t\tans += ok ? (sz - 1) : sz;\n\t\t}\n\t\twriteln(ans);\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [], "src_uid": "586204a0e1ba55208fd92ca61bd4d9b5"} {"nl": {"description": "You are given $$$k$$$ sequences of integers. The length of the $$$i$$$-th sequence equals to $$$n_i$$$.You have to choose exactly two sequences $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $$$i$$$ (its length will be equal to $$$n_i - 1$$$) equals to the sum of the changed sequence $$$j$$$ (its length will be equal to $$$n_j - 1$$$).Note that it's required to remove exactly one element in each of the two chosen sequences.Assume that the sum of the empty (of the length equals $$$0$$$) sequence is $$$0$$$.", "input_spec": "The first line contains an integer $$$k$$$ ($$$2 \\le k \\le 2 \\cdot 10^5$$$) — the number of sequences. Then $$$k$$$ pairs of lines follow, each pair containing a sequence. The first line in the $$$i$$$-th pair contains one integer $$$n_i$$$ ($$$1 \\le n_i < 2 \\cdot 10^5$$$) — the length of the $$$i$$$-th sequence. The second line of the $$$i$$$-th pair contains a sequence of $$$n_i$$$ integers $$$a_{i, 1}, a_{i, 2}, \\dots, a_{i, n_i}$$$. The elements of sequences are integer numbers from $$$-10^4$$$ to $$$10^4$$$. The sum of lengths of all given sequences don't exceed $$$2 \\cdot 10^5$$$, i.e. $$$n_1 + n_2 + \\dots + n_k \\le 2 \\cdot 10^5$$$.", "output_spec": "If it is impossible to choose two sequences such that they satisfy given conditions, print \"NO\" (without quotes). Otherwise in the first line print \"YES\" (without quotes), in the second line — two integers $$$i$$$, $$$x$$$ ($$$1 \\le i \\le k, 1 \\le x \\le n_i$$$), in the third line — two integers $$$j$$$, $$$y$$$ ($$$1 \\le j \\le k, 1 \\le y \\le n_j$$$). It means that the sum of the elements of the $$$i$$$-th sequence without the element with index $$$x$$$ equals to the sum of the elements of the $$$j$$$-th sequence without the element with index $$$y$$$. Two chosen sequences must be distinct, i.e. $$$i \\ne j$$$. You can print them in any order. If there are multiple possible answers, print any of them.", "sample_inputs": ["2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1", "3\n1\n5\n5\n1 1 1 1 1\n2\n2 3", "4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2"], "sample_outputs": ["YES\n2 6\n1 2", "NO", "YES\n2 2\n4 1"], "notes": "NoteIn the first example there are two sequences $$$[2, 3, 1, 3, 2]$$$ and $$$[1, 1, 2, 2, 2, 1]$$$. You can remove the second element from the first sequence to get $$$[2, 1, 3, 2]$$$ and you can remove the sixth element from the second sequence to get $$$[1, 1, 2, 2, 2]$$$. The sums of the both resulting sequences equal to $$$8$$$, i.e. the sums are equal."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n Tuple!(int, int)[long] D;\n\n foreach (i; 0..N) {\n auto M = readln.chomp.to!int;\n auto A = readln.split.map!(to!long).array;\n auto S = A.sum;\n foreach (j; 0..M) {\n auto T = S - A[j];\n if (T in D) {\n if (D[T][0] != i + 1) {\n writeln(\"YES\");\n writeln(D[T][0], \" \", D[T][1]);\n writeln(i+1, \" \", j+1);\n return;\n }\n } else {\n D[T] = tuple(i+1, j+1);\n }\n }\n }\n\n writeln(\"NO\");\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n int[][] a;\n foreach (_; 0..n) {\n readln;\n a ~= readln.chomp.split.map!(to!int).array;\n }\n \n debug { a.writeln; }\n \n Tuple!(int, int) [int] v;\n \n foreach (i, s; a.enumerate(1)) {\n int sm = s.sum;\n foreach (j, e; s.enumerate(1)) {\n int cur = sm - e;\n if (cur in v) {\n writeln(\"YES\");\n writeln(v[cur][0], ' ', v[cur][1]);\n writeln(i, ' ', j);\n return;\n }\n }\n \n foreach (j, e; s.enumerate(1)) {\n int cur = sm - e;\n v[cur] = tuple(i, j);\n }\n }\n \n writeln(\"NO\");\n}"}, {"source_code": "void main(){\n import std.stdio, std.algorithm, std.string, std.conv;\n\n struct P{\n int idx1, idx2;\n }\n int K; rd(K);\n P[int] set;\n for(int k=1; k<=K; k++){\n int n; rd(n);\n auto as=readln.split.to!(int[]);\n int s=reduce!\"a+b\"(as);\n P[] cand;\n foreach(int i, a; as){\n int t=s-a;\n if(t in set){\n if(set[t].idx1 lim) && !prevLess)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tint nextLess = prevLess ||\n\t\t\t\t\t\t (cur < lim);\n\t\t\t\t\t\tadd (f[i + 1][j + cur]\n\t\t\t\t\t\t [nextLess], f[i][j]\n\t\t\t\t\t\t [prevLess]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tadd (f[n][1][1], mod - 1);\n\n\t\tint res = 0;\n\t\tforeach (j; 1..n + 1)\n\t\t{\n\t\t\tif (h[j] == k - 1)\n\t\t\t{\n\t\t\t\tadd (res, f[n][j][0]);\n\t\t\t\tadd (res, f[n][j][1]);\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n import std.conv : to;\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n if (a < 0) return (this = inv()^^(-a));\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e > 0; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op: \"-\")() const { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const {\n return mixin(\"ModInt(this) \" ~ op ~ \"= a\");\n }\n ModInt opBinaryRight(string op)(long a) const {\n return mixin(\"ModInt(a) \" ~ op ~ \"= this\");\n }\n bool opCast(T: bool)() const { return (x != 0); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 7;\nalias Mint = ModInt!MO;\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readToken();\n const K = readInt();\n const L = cast(int)(N.length);\n \n auto numSteps = new int[L + 1];\n foreach (i; 2 .. L + 1) {\n numSteps[i] = 1 + numSteps[popcnt(i)];\n }\n debug {\n writeln(\"numSteps = \", numSteps);\n }\n \n auto dp = new Mint[][][](L + 1, 2, L + 1);\n dp[0][0][0] = 1;\n foreach (i; 0 .. L) {\n foreach (s; 0 .. 2) foreach (j; 0 .. i + 1) {\n foreach (x; 0 .. 2) {\n if (s || x <= N[i] - '0') {\n dp[i + 1][(s || x < N[i] - '0') ? 1 : 0][j + x] += dp[i][s][j];\n }\n }\n }\n }\n Mint ans;\n if (K == 0) {\n // 1 <= N\n ans += 1;\n } else {\n foreach (s; 0 .. 2) foreach (j; 1 .. L + 1) {\n if (K == 1 + numSteps[j]) {\n ans += dp[L][s][j];\n }\n }\n if (K == 1) {\n // 1 <= N\n ans -= 1;\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int mod = 10 ^^ 9 + 7;\nimmutable int limit = 1003;\n\nvoid add (ref int a, int b)\n{\n\ta = (a + b) % mod;\n}\n\nvoid main ()\n{\n\tauto h = new int [limit];\n\th[1] = 0;\n\tforeach (i; 2..limit)\n\t{\n\t\th[i] = h[popcnt (i)] + 1;\n\t}\n\n\tstring s;\n\twhile ((s = readln.strip) != \"\")\n\t{\n\t\tauto k = readln.strip.to !(int);\n\t\tif ((k == 0) || (s == \"1\"))\n\t\t{\n\t\t\tint res = (k == 0) && (s == \"1\");\n\t\t\twriteln (res);\n\t\t\tcontinue;\n\t\t}\n\n\t\tauto n = s.length.to !(int);\n\t\tauto f = new int [2] [] [] (n + 1, n + 1);\n\t\tf[0][0][0] = 1;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint lim = (s[i] == '1');\n\t\t\tforeach (j; 0..i + 1)\n\t\t\t{\n\t\t\t\tforeach (prevLess; 0..2)\n\t\t\t\t{\n\t\t\t\t\tforeach (cur; 0..2)\n\t\t\t\t\t{\n\t\t\t\t\t\tif ((cur > lim) && !prevLess)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tint nextLess = prevLess ||\n\t\t\t\t\t\t (cur < lim);\n\t\t\t\t\t\tadd (f[i + 1][j + cur]\n\t\t\t\t\t\t [nextLess], f[i][j]\n\t\t\t\t\t\t [prevLess]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tadd (f[n][1][0], mod - 1);\n\n\t\tint res = 0;\n\t\tforeach (j; 1..n + 1)\n\t\t{\n\t\t\tif (h[j] == k - 1)\n\t\t\t{\n\t\t\t\tadd (res, f[n][j][0]);\n\t\t\t\tadd (res, f[n][j][1]);\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n import std.conv : to;\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n if (a < 0) return (this = inv()^^(-a));\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e > 0; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op: \"-\")() const { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const {\n return mixin(\"ModInt(this) \" ~ op ~ \"= a\");\n }\n ModInt opBinaryRight(string op)(long a) const {\n return mixin(\"ModInt(a) \" ~ op ~ \"= this\");\n }\n bool opCast(T: bool)() const { return (x != 0); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 7;\nalias Mint = ModInt!MO;\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readToken();\n const K = readInt();\n const L = cast(int)(N.length);\n \n auto numSteps = new int[L + 1];\n foreach (i; 2 .. L + 1) {\n numSteps[i] = 1 + numSteps[popcnt(i)];\n }\n debug {\n writeln(\"numSteps = \", numSteps);\n }\n \n auto dp = new Mint[][][](L + 1, 2, L + 1);\n dp[0][0][0] = 1;\n foreach (i; 0 .. L) {\n foreach (s; 0 .. 2) foreach (j; 0 .. i + 1) {\n foreach (x; 0 .. 2) {\n if (s || x <= N[i] - '0') {\n dp[i + 1][(s || x < N[i] - '0') ? 1 : 0][j + x] += dp[i][s][j];\n }\n }\n }\n }\n Mint ans;\n if (K == 0) {\n // 1 <= N\n ans = 1;\n } else {\n foreach (s; 0 .. 2) foreach (j; 1 .. L + 1) {\n if (K == 1 + numSteps[j]) {\n ans += dp[L][s][j];\n }\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "e367f5d18b08882ec518b2d4188ccdea"} {"nl": {"description": "Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site.He has suggestions to work on n distinct projects. Mikhail has already evaluated that the participation in the i-th project will increase his experience by ai per day and bring bi dollars per day. As freelance work implies flexible working hours, Mikhail is free to stop working on one project at any time and start working on another project. Doing so, he receives the respective share of experience and money. Mikhail is only trying to become a cool programmer, so he is able to work only on one project at any moment of time.Find the real value, equal to the minimum number of days Mikhail needs to make his dream come true.For example, suppose Mikhail is suggested to work on three projects and a1 = 6, b1 = 2, a2 = 1, b2 = 3, a3 = 2, b3 = 6. Also, p = 20 and q = 20. In order to achieve his aims Mikhail has to work for 2.5 days on both first and third projects. Indeed, a1·2.5 + a2·0 + a3·2.5 = 6·2.5 + 1·0 + 2·2.5 = 20 and b1·2.5 + b2·0 + b3·2.5 = 2·2.5 + 3·0 + 6·2.5 = 20.", "input_spec": "The first line of the input contains three integers n, p and q (1 ≤ n ≤ 100 000, 1 ≤ p, q ≤ 1 000 000) — the number of projects and the required number of experience and money. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 1 000 000) — the daily increase in experience and daily income for working on the i-th project.", "output_spec": "Print a real value — the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .", "sample_inputs": ["3 20 20\n6 2\n1 3\n2 6", "4 1 1\n2 3\n3 2\n2 3\n3 2"], "sample_outputs": ["5.000000000000000", "0.400000000000000"], "notes": "NoteFirst sample corresponds to the example in the problem statement."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nreal vp (real x0, real y0, real x1, real y1, real x2, real y2)\n{\n\treturn (x2 - x0) * (y1 - y0) - (x1 - x0) * (y2 - y0);\n}\n\nvoid main ()\n{\n\tint n;\n\treal p, q;\n\twhile (readf (\" %s %s %s\", &n, &p, &q) > 0)\n\t{\n\t\talias Point = Tuple !(real, q{x}, real, q{y});\n\t\tauto a = new Point [n];\n\t\tforeach (ref z; a)\n\t\t{\n\t\t\treadf (\" %s %s\", &z.x, &z.y);\n\t\t}\n\t\tsort !((a, b) => (vp (0, 0, a.x, a.y, b.x, b.y) < 0) ||\n\t\t (vp (0, 0, a.x, a.y, b.x, b.y) == 0 &&\n\t\t hypot (a.y, a.x) < hypot (b.y, b.x))) (a);\n\t\ta = a.uniq.array;\n/*\n\t\tPoint [] c;\n\t\tforeach (ref z; a)\n\t\t{\n\t\t\tif (!c.empty &&\n\t\t\t (vp (0, 0, z.x, z.y, c[0].x, c[0].y) == 0 &&\n\t\t\t hypot (z.y, z.x) >= hypot (c[0].y, c[0].x)))\n\t\t\t{\n\t\t\t\tc.popBack ();\n\t\t\t\tc.assumeSafeAppend ();\n\t\t\t}\n\t\t\tc ~= z;\n\t\t}\n\t\tdebug {writeln (c);}\n\t\ta = c;\n*/\n n = a.length;\n\t\tPoint [] b;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\twhile (b.length >= 2 &&\n\t\t\t vp (b[$ - 2].x, b[$ - 2].y,\n\t\t\t b[$ - 1].x, b[$ - 1].y, a[i].x, a[i].y) > 0)\n\t\t\t{\n\t\t\t\tb.popBack ();\n\t\t\t\tb.assumeSafeAppend ();\n\t\t\t}\n\t\t\tb ~= a[i];\n\t\t}\n\t\tdebug {writeln (b);}\n\n\t\treal res = 1E100;\n\t\tforeach (i; 0..b.length)\n\t\t{\n\t\t\tres = min (res, max (p / b[i].x, q / b[i].y));\n\t\t\tif (i > 0 && vp (0, 0, b[i - 1].x, b[i - 1].y, p, q) *\n\t\t\t vp (0, 0, b[i].x, b[i].y, p, q) < 0)\n\t\t\t{\n\t\t\t\treal lo = 0;\n\t\t\t\treal hi = 1;\n\t\t\t\tforeach (j; 0..100)\n\t\t\t\t{\n\t\t\t\t\treal me = (lo + hi) * 0.5;\n\t\t\t\t\treal ppart1 = me * p;\n\t\t\t\t\treal ppart2 = p - ppart1;\n\t\t\t\t\treal t1 = ppart1 / b[i - 1].x;\n\t\t\t\t\treal t2 = ppart2 / b[i].x;\n\t\t\t\t\treal qpart1 = t1 * b[i - 1].y;\n\t\t\t\t\treal qpart2 = t2 * b[i].y;\n\t\t\t\t\tif (q < qpart1 + qpart2)\n\t\t\t\t\t{\n\t\t\t\t\t\tlo = me;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\thi = me;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdebug {writeln (lo);}\n\t\t\t\treal ppart1 = lo * p;\n\t\t\t\treal ppart2 = p - ppart1;\n\t\t\t\treal t1 = ppart1 / b[i - 1].x;\n\t\t\t\treal t2 = ppart2 / b[i].x;\n\t\t\t\tres = min (res, t1 + t2);\n\t\t\t}\n\t\t}\n\n\t\twritefln (\"%.10f\", res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nreal vp (real x0, real y0, real x1, real y1, real x2, real y2)\n{\n\treturn (x2 - x0) * (y1 - y0) - (x1 - x0) * (y2 - y0);\n}\n\nvoid main ()\n{\n\tint n;\n\treal p, q;\n\twhile (readf (\" %s %s %s\", &n, &p, &q) > 0)\n\t{\n\t\talias Point = Tuple !(real, q{x}, real, q{y});\n\t\tauto a = new Point [n];\n\t\tforeach (ref z; a)\n\t\t{\n\t\t\treadf (\" %s %s\", &z.x, &z.y);\n\t\t}\n\t\tsort !((a, b) => (vp (0, 0, a.x, a.y, b.x, b.y) < 0) ||\n\t\t (vp (0, 0, a.x, a.y, b.x, b.y) == 0 &&\n\t\t hypot (a.y, a.x) < hypot (b.y, b.x))) (a);\n\t\ta = a.uniq.array;\n/*\n\t\tPoint [] c;\n\t\tforeach (ref z; a)\n\t\t{\n\t\t\tif (!c.empty &&\n\t\t\t (vp (0, 0, z.x, z.y, c[0].x, c[0].y) == 0 &&\n\t\t\t hypot (z.y, z.x) >= hypot (c[0].y, c[0].x)))\n\t\t\t{\n\t\t\t\tc.popBack ();\n\t\t\t\tc.assumeSafeAppend ();\n\t\t\t}\n\t\t\tc ~= z;\n\t\t}\n\t\tdebug {writeln (c);}\n\t\ta = c;\n*/\n n = a.length;\n\t\tPoint [] b;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\twhile (b.length >= 2 &&\n\t\t\t vp (b[$ - 2].x, b[$ - 2].y,\n\t\t\t b[$ - 1].x, b[$ - 1].y, a[i].x, a[i].y) > 0)\n\t\t\t{\n\t\t\t\tb.popBack ();\n\t\t\t\tb.assumeSafeAppend ();\n\t\t\t}\n\t\t\tb ~= a[i];\n\t\t}\n\t\tdebug {writeln (b);}\n\n\t\treal res = 1E100;\n\t\tforeach (i; 0..b.length)\n\t\t{\n\t\t\tres = min (res, max (p / b[i].x, q / b[i].y));\n\t\t\tif (i > 0 && vp (0, 0, b[i - 1].x, b[i - 1].y, p, q) *\n\t\t\t vp (0, 0, b[i].x, b[i].y, p, q) < 0)\n\t\t\t{\n\t\t\t\treal lo = 0;\n\t\t\t\treal hi = 1;\n\t\t\t\tforeach (j; 0..100)\n\t\t\t\t{\n\t\t\t\t\treal me = (lo + hi) * 0.5;\n\t\t\t\t\treal ppart1 = me * p;\n\t\t\t\t\treal ppart2 = p - ppart1;\n\t\t\t\t\treal t1 = ppart1 / b[i - 1].x;\n\t\t\t\t\treal t2 = ppart2 / b[i].x;\n\t\t\t\t\treal qpart1 = t1 * b[i - 1].y;\n\t\t\t\t\treal qpart2 = t2 * b[i].y;\n\t\t\t\t\tif (q < qpart1 + qpart2)\n\t\t\t\t\t{\n\t\t\t\t\t\tlo = me;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\thi = me;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdebug {writeln (lo);}\n\t\t\t\treal ppart1 = lo * p;\n\t\t\t\treal ppart2 = p - ppart1;\n\t\t\t\treal t1 = ppart1 / b[i - 1].x;\n\t\t\t\treal t2 = ppart2 / b[i].x;\n\t\t\t\tres = min (res, t1 + t2);\n\t\t\t}\n\t\t}\n\n\t\twritefln (\"%.10f\", res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "131db180c7afad3e5a3342407408fded"} {"nl": {"description": "Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.", "input_spec": "The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array. Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105). Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m). The numbers in the lines are separated by single spaces.", "output_spec": "On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.", "sample_inputs": ["3 3 3\n1 2 3\n1 2 1\n1 3 2\n2 3 4\n1 2\n1 3\n2 3", "1 1 1\n1\n1 1 1\n1 1", "4 3 6\n1 2 3 4\n1 2 1\n2 3 2\n3 4 4\n1 2\n1 3\n2 3\n1 2\n1 3\n2 3"], "sample_outputs": ["9 18 17", "2", "5 18 31 20"], "notes": null}, "positive_code": [{"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n int n,m,k;\n readf!\"%d %d %d\"(n,m,k);\n readln;\n long[] a=readln.splitter.map!(to!long).array;\n //if(n==40) writeln(a);\n Tuple!(int,int,ulong)[] q;\n int l,r;\n ulong v;\n foreach(i;0..m){\n readf!\"%d %d %d\"(l,r,v);\n readln;\n q~=tuple(l,r,v);\n } \n long[] dk=new long[m];\n foreach(i;0..k){\n readf!\"%d %d\"(l,r);\n readln;\n dk[l-1]++;\n if(r 0)\n\t{\n\t\tauto a = new long [n];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %s\", &a[i]);\n\t\t}\n\t\tauto l = new int [m];\n\t\tauto r = new int [m];\n\t\tauto d = new long [m];\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\treadf (\" %s %s %s\", &l[j], &r[j], &d[j]);\n\t\t\tl[j]--;\n\t\t\tr[j]--;\n\t\t}\n\t\tauto x = new int [k];\n\t\tauto y = new int [k];\n\t\tforeach (p; 0..k)\n\t\t{\n\t\t\treadf (\" %s %s\", &x[p], &y[p]);\n\t\t\tx[p]--;\n\t\t\ty[p]--;\n\t\t}\n\t\tauto v = new long [m + 1];\n\t\tforeach (p; 0..k)\n\t\t{\n\t\t\tv[x[p]]++;\n\t\t\tv[y[p] + 1]--;\n\t\t}\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\tv[j + 1] += v[j];\n\t\t}\n\t\tauto t = new long [n + 1];\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\tt[l[j]] += v[j] * d[j];\n\t\t\tt[r[j] + 1] -= v[j] * d[j];\n\t\t}\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tt[i + 1] += t[i];\n\t\t}\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\ta[i] += t[i];\n\t\t}\n\t\twritefln (\"%(%s %)\", a);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;\nimport std.uni, std.math, std.container, std.typecons, std.typetuple;\nimport core.bitop, std.datetime;\n\nimmutable int mod = 10^^9 + 7;\n\nvoid main() {\n int n, m, k;\n readVars(n, m, k);\n\n auto a = readln.split.to!(long[]);\n\n auto l = new int[](m);\n auto r = new int[](m);\n auto d = new int[](m);\n\n foreach (i ; 0 .. m) {\n readVars(l[i], r[i], d[i]);\n l[i]--;\n }\n\n auto ims = new int[](m + 1);\n int xi, yi;\n\n foreach (i ; 0 .. k) {\n readVars(xi, yi);\n ims[xi - 1]++;\n ims[yi]--;\n }\n\n iota(m).each!(i => ims[i + 1] += ims[i]);\n\n auto dif = new long[](n + 1);\n\n foreach (i ; 0 .. m) {\n dif[l[i]] += d[i].to!long * ims[i];\n dif[r[i]] -= d[i].to!long * ims[i];\n }\n\n iota(n).each!(i => dif[i + 1] += dif[i]);\n\n debug {\n stderr.writeln(\"ims:\", ims);\n stderr.writeln(\"dif:\", dif);\n }\n\n a[] += dif[0 .. $ - 1];\n\n writefln(\"%(%s %)\", a);\n}\n\nint dfs(int n, int[][] child, int u) {\n if (child[u].empty) {\n return 0;\n }\n\n int res;\n\n foreach(v ; child[u]) {\n res = max(res, dfs(n, child, v));\n }\n\n return res + 1;\n}\n\nvoid readVars(T...)(auto ref T args)\n{\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m, k;\n readf(\"%s %s %s\", &n, &m, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!long).array;\n \n struct op { int le, r, d; }\n op[] ops;\n foreach (_; 0 .. m) {\n int le, r, d;\n readf(\"%s %s %s\", &le, &r, &d);\n readln;\n --le, --r;\n \n ops ~= op(le, r, d);\n }\n \n debug { ops.writeln; }\n \n auto opscnt = new int[] (m+1);\n foreach (_; 0 .. k) {\n int x, y;\n readf(\"%s %s\", &x, &y);\n readln;\n --x, --y;\n \n opscnt[x] += 1;\n opscnt[y+1] -= 1;\n }\n \n auto addcnt = new long[] (n+1);\n foreach (i; 0 .. m) {\n if (i > 0) opscnt[i] += opscnt[i-1];\n auto val = cast(long)ops[i].d * opscnt[i];\n addcnt[ops[i].le] += val;\n addcnt[ops[i].r+1] -= val; \n }\n \n foreach (i; 0 .. n) {\n if (i > 0) addcnt[i] += addcnt[i-1];\n arr[i] += addcnt[i];\n }\n \n arr.writefln!(\"%(%s %)\");\n}"}, {"source_code": "module sigod.codeforces.p296C;\n\nimport std.stdio;\n\nvoid main()\n{\n\tint n, m, k;\n\tstdin.readf(\" %s %s %s\", &n, &m, &k);\n\n\tlong[] a = new long[n + 2];\n\tforeach (i; 1 .. n + 1) {\n\t\tstdin.readf(\" %s\", &a[i]);\n\t}\n\n\tint[] l = new int[m + 1];\n\tint[] r = new int[m + 1];\n\tlong[] d = new long[m + 2];\n\n\tforeach (i; 1 .. m + 1) {\n\t\tstdin.readf(\" %s %s %s\", &l[i], &r[i], &d[i]);\n\t}\n\n\t// process requests\n\n\tint[] multipl = new int[m + 2];\n\n\tforeach (i; 0 .. k) {\n\t\tint x, y;\n\t\tstdin.readf(\" %s %s\", &x, &y);\n\n\t\t++multipl[x];\n\t\t--multipl[y + 1];\n\t}\n\n\tforeach (i; 1 .. m + 1) {\n\t\tmultipl[i] += multipl[i - 1];\n\n\t\td[i] *= multipl[i];\n\t}\n\n\t// process operations\n\n\tlong[] sum = new long[n + 2];\n\n\tforeach (i; 1 .. m + 1) {\n\t\tsum[l[i]] += d[i];\n\t\tsum[r[i] + 1] -= d[i];\n\t}\n\n\tforeach (i; 1 .. n + 1) {\n\t\tsum[i] += sum[i - 1];\n\n\t\ta[i] += sum[i];\n\t}\n\n\t// output\n\n\tforeach (i; 1 .. n + 1) {\n\t\tstdout.write(a[i], \" \");\n\t}\n}"}], "negative_code": [{"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n int n,m,k;\n readf!\"%d %d %d\"(n,m,k);\n readln;\n ulong[] a=readln.splitter.map!(to!ulong).array;\n Tuple!(int,int,long)[] q;\n int l,r;\n long v;\n foreach(i;0..m){\n readf!\"%d %d %d\"(l,r,v);\n readln;\n q~=tuple(l,r,v);\n } \n long[] dk=new long[m];\n while(k--){\n readf!\"%d %d\"(l,r);\n readln;\n dk[l-1]++;\n if(r ims[i + 1] += ims[i]);\n\n auto dif = new long[](n + 1);\n\n foreach (i ; 0 .. m) {\n dif[l[i]] += d[i] * ims[i];\n dif[r[i]] -= d[i] * ims[i];\n }\n\n iota(n).each!(i => dif[i + 1] += dif[i]);\n\n debug {\n stderr.writeln(\"ims:\", ims);\n stderr.writeln(\"dif:\", dif);\n }\n\n a[] += dif[0 .. $ - 1];\n\n writefln(\"%(%s %)\", a);\n}\n\nint dfs(int n, int[][] child, int u) {\n if (child[u].empty) {\n return 0;\n }\n\n int res;\n\n foreach(v ; child[u]) {\n res = max(res, dfs(n, child, v));\n }\n\n return res + 1;\n}\n\nvoid readVars(T...)(auto ref T args)\n{\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m, k;\n readf(\"%s %s %s\", &n, &m, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!long).array;\n \n struct op { int le, r, d; }\n op[] ops;\n foreach (_; 0 .. m) {\n int le, r, d;\n readf(\"%s %s %s\", &le, &r, &d);\n readln;\n --le, --r;\n \n ops ~= op(le, r, d);\n }\n \n debug { ops.writeln; }\n \n auto opscnt = new int[] (m+1);\n foreach (_; 0 .. k) {\n int x, y;\n readf(\"%s %s\", &x, &y);\n readln;\n --x, --y;\n \n opscnt[x] += 1;\n opscnt[y+1] -= 1;\n }\n \n auto addcnt = new long[] (n+1);\n foreach (i; 0 .. m) {\n if (i > 0) opscnt[i] += opscnt[i-1];\n addcnt[ops[i].le] += ops[i].d * opscnt[i];\n addcnt[ops[i].r+1] -= ops[i].d * opscnt[i]; \n }\n \n foreach (i; 0 .. n) {\n if (i > 0) addcnt[i] += addcnt[i-1];\n arr[i] += addcnt[i];\n }\n \n arr.writefln!(\"%(%s %)\");\n}"}, {"source_code": "module sigod.codeforces.p296C;\n\nimport std.stdio;\n\nstruct FenwickTree\n{\n\tprivate {\n\t\tint _size;\n\t\tlong[] _array;\n\t}\n\n\tthis(int size)\n\t{\n\t\tthis._size = size;\n\t\tthis._array = new long[size];\n\t}\n\n\tthis(long[] array)\n\t{\n\t\t//this._size = array.length;\n\t\t//this._array = array.dup;\n\n\t\tthis(array.length);\n\n\t\tfor (int i = 0; i < this._size; ++i) {\n\t\t\tthis.inc(i, array[i]);\n\t\t}\n\t}\n\n\tvoid inc(int i, long delta)\n\t{\n\t\tfor (; i < this._size; i = (i | (i + 1))) {\n\t\t\tthis._array[i] += delta;\n\t\t}\n\t}\n\n\tlong sum(int r)\n\t{\n\t\tlong result = 0;\n\n\t\tfor (; r >= 0; r = (r & (r + 1)) - 1) {\n\t\t\tresult += this._array[r];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tlong sum(int l, int r)\n\t{\n\t\treturn sum(r) - sum(l);\n\t}\n\n\tlong[] toArray()\n\t{\n\t\tlong[] result = new long[this._size];\n\n\t\tforeach (i; 0 .. this._size) {\n\t\t\tresult[i] = this.sum(i);\n\t\t}\n\n\t\tfor (int i = this._size - 1; i > 0; --i) {\n\t\t\tresult[i] -= result[i - 1];\n\t\t}\n\n\t\treturn result;\n\t}\n}\n\nvoid main()\n{\n\tint array_size = read_value!int();\n\tint operations_count = read_value!int();\n\tint requests_count = read_value!int();\n\n\tlong[] array = read_array!long(array_size);\n\n\tint[] left_operations = new int[operations_count];\n\tint[] right_operations = new int[operations_count];\n\tint[] delta = new int[operations_count];\n\n\tforeach (i; 0 .. operations_count) {\n\t\tleft_operations[i] = read_value!int() - 1;\n\t\tright_operations[i] = read_value!int() - 1;\n\t\tdelta[i] = read_value!int();\n\t}\n\n\tint[] x = new int[requests_count];\n\tint[] y = new int[requests_count];\n\n\tforeach (i; 0 .. requests_count) {\n\t\tx[i] = read_value!int() - 1;\n\t\ty[i] = read_value!int() - 1;\n\t}\n\n\tFenwickTree array_tree = FenwickTree(array);\n\tFenwickTree operations_tree = FenwickTree(operations_count);\n\n\tforeach (i; 0 .. requests_count) {\n\t\tforeach (j; x[i] .. y[i] + 1) {\n\t\t\toperations_tree.inc(j, 1);\n\t\t}\n\t}\n\n\tstdout.writeln(\"operations_counts: \", operations_tree.toArray());\n\n\tstdout.writeln(\"array: \", array_tree.toArray());\n\n\tforeach (index, count; operations_tree.toArray()) {\n\t\tif (count == 0) continue;\n\n\t\tforeach (i; left_operations[index] .. right_operations[index] + 1) {\n\t\t\tarray_tree.inc(i, delta[index] * count);\n\t\t}\n\n\t\tstdout.writeln(\"left: \", left_operations[index], \"; right: \", right_operations[index]);\n\t\tstdout.writeln(\"delta: \", delta[index], \"; count: \", count);\n\t\tstdout.writeln(\"array: \", array_tree.toArray());\n\t}\n\n\tforeach (element; array_tree.toArray()) {\n\t\tstdout.write(element, \" \");\n\t}\n}\n\nprivate\nT read_value(T)()\n{\n\tT value;\n\tstdin.readf(\" %s\", &value);\n\n\treturn value;\n}\n\nprivate\nT[] read_array(T)(int size)\n{\n\tT[] array = new T[size];\n\n\tforeach (ref element; array) {\n\t\tstdin.readf(\" %s\", &element);\n\t}\n\n\treturn array;\n}"}, {"source_code": "module sigod.codeforces.p296C;\n\nimport std.stdio;\n\nvoid main()\n{\n\tint n, m, k;\n\tstdin.readf(\" %s %s %s\", &n, &m, &k);\n\n\tlong[] a = new long[n + 2];\n\tforeach (i; 1 .. n + 1) {\n\t\tstdin.readf(\" %s\", &a[i]);\n\t}\n\n\tint[] l = new int[m + 1];\n\tint[] r = new int[m + 1];\n\tint[] d = new int[m + 2];\n\n\tforeach (i; 1 .. m + 1) {\n\t\tstdin.readf(\" %s %s %s\", &l[i], &r[i], &d[i]);\n\t}\n\n\t// process requests\n\n\tint[] multipl = new int[m + 2];\n\n\tforeach (i; 0 .. k) {\n\t\tint x, y;\n\t\tstdin.readf(\" %s %s\", &x, &y);\n\n\t\t++multipl[x];\n\t\t--multipl[y + 1];\n\t}\n\n\tforeach (i; 1 .. m + 2) {\n\t\tmultipl[i] += multipl[i - 1];\n\n\t\td[i] *= multipl[i];\n\t}\n\n\t// process operations\n\n\tlong[] sum = new long[n + 2];\n\n\tforeach (i; 1 .. m + 1) {\n\t\tsum[l[i]] += d[i];\n\t\tsum[r[i] + 1] -= d[i];\n\t}\n\n\tforeach (i; 1 .. n + 1) {\n\t\tsum[i] += sum[i - 1];\n\n\t\ta[i] += sum[i];\n\t}\n\n\t// output\n\n\tforeach (i; 1 .. n + 1) {\n\t\tstdout.write(a[i], \" \");\n\t}\n}\n\nprivate\nT max(T)(T a, T b)\n{\n\tif (a > b) return a;\n\telse return b;\n}"}], "src_uid": "c3120f96894c17957bd8acb968bf37cd"} {"nl": {"description": "Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 \\le i \\le n$$$) and for every $$$j \\in [1, n]$$$ adds $$$x + d \\cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = |i - j|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$).For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 \\cdot 2,~1 - 1 + 2 \\cdot 1,~2 - 1 + 2 \\cdot 0,~2 - 1 + 2 \\cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$).Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.What is the maximum arithmetic mean value Bob can achieve?", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^5$$$) — the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 \\le x_i, d_i \\le 10^3$$$) — the parameters for the $$$i$$$-th change.", "output_spec": "Print the maximal average arithmetic mean of the elements Bob can achieve. Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.", "sample_inputs": ["2 3\n-1 3\n0 0\n-1 -4", "3 2\n0 2\n5 0"], "sample_outputs": ["-2.500000000000000", "7.000000000000000"], "notes": null}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto totx = 0, posd = 0, negd = 0;\n while (m--) {\n int x, d;\n readf(\"%s %s\", &x, &d);\n readln;\n \n totx += x;\n if (d > 0) posd += d;\n else negd += d;\n }\n \n debug { writeln(totx, ' ', negd, ' ', posd); }\n \n auto bigd = (cast(long)n).iota.sum;\n auto smalld = n % 2 == 1 ? 2L * (cast(long)n/2 + 1).iota.sum \n : (cast(long)n/2 + 1).iota.sum + (cast(long)n/2).iota.sum;\n \n debug { writeln(smalld, ' ', bigd); }\n \n auto ans = totx + cast(double)(posd * bigd + negd * smalld) / n;\n \n ans.writefln!(\"%.18f\");\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto totx = 0, posd = 0, negd = 0;\n while (m--) {\n int x, d;\n readf(\"%s %s\", &x, &d);\n readln;\n \n totx += x;\n if (d > 0) posd += d;\n else negd += d;\n }\n \n debug { writeln(totx, ' ', negd, ' ', posd); }\n \n auto bigd = (cast(long)n).iota.sum;\n auto smalld = n % 2 == 1 ? 2L * (cast(long)n/2 + 1).iota.sum : (cast(long)n/2 + 1).iota.sum + (cast(long)n/2).iota.sum;\n \n debug { writeln(smalld, ' ', bigd); }\n \n auto ans = totx + cast(double)(posd * bigd + negd * smalld) / n;\n \n ans.writefln!(\"%.18f\");\n}"}, {"source_code": "void main(){\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int n, m; rd(n, m);\n long sum=0;\n while(m--){\n long x, d; rd(x, d);\n sum+=x*n;\n if(d>0){// 0+d+d*2+...+d*(n-1)\n sum+=d*n*(n-1)/2;\n }else{\n auto k=n/2;\n sum+=d*k*(k+1)/2*2;\n if(n%2==0) sum-=d*k;\n }\n }\n\n writefln(\"%.18f\", 1.0*sum/n);\n}\n\nvoid rd(T...)(ref T x){\n import std.stdio, std.string, std.conv;\n auto l=readln.split;\n assert(l.length==x.length);\n foreach(i, ref e; x) e=l[i].to!(typeof(e));\n}\n\n/*\n\n 0 0\n -1 2\n -1 2\n -2 -3\n\n 0 0 0\n 0 2 4\n 5 7 9\n\n*/"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto totx = 0, posd = 0, negd = 0;\n while (m--) {\n int x, d;\n readf(\"%s %s\", &x, &d);\n readln;\n \n totx += x;\n if (d > 0) posd += d;\n else negd += d;\n }\n \n debug { writeln(totx, ' ', negd, ' ', posd); }\n \n auto bigd = (cast(long)n).iota.sum;\n auto smalld = n % 2 == 1 ? 2L * (cast(long)n/2 + 1).iota.sum : (cast(long)n/2 + 1).iota.sum + (cast(long)n/2).iota.sum;\n \n debug { writeln(smalld, ' ', bigd); }\n \n auto ans = totx + cast(double)(posd * bigd + negd * smalld) / n;\n \n ans.writeln;\n}"}], "src_uid": "1c8423407ea7a0b2647e41392670d6b7"} {"nl": {"description": " Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of $$$7$$$ segments, which can be turned on or off to display different numbers. The picture shows how all $$$10$$$ decimal digits are displayed: After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly $$$k$$$ segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly $$$k$$$ sticks (which are off now)? It is allowed that the number includes leading zeros.", "input_spec": "The first line contains integer $$$n$$$ $$$(1 \\leq n \\leq 2000)$$$  — the number of digits on scoreboard and $$$k$$$ $$$(0 \\leq k \\leq 2000)$$$  — the number of segments that stopped working. The next $$$n$$$ lines contain one binary string of length $$$7$$$, the $$$i$$$-th of which encodes the $$$i$$$-th digit of the scoreboard. Each digit on the scoreboard consists of $$$7$$$ segments. We number them, as in the picture below, and let the $$$i$$$-th place of the binary string be $$$0$$$ if the $$$i$$$-th stick is not glowing and $$$1$$$ if it is glowing. Then a binary string of length $$$7$$$ will specify which segments are glowing now. Thus, the sequences \"1110111\", \"0010010\", \"1011101\", \"1011011\", \"0111010\", \"1101011\", \"1101111\", \"1010010\", \"1111111\", \"1111011\" encode in sequence all digits from $$$0$$$ to $$$9$$$ inclusive.", "output_spec": "Output a single number consisting of $$$n$$$ digits  — the maximum number that can be obtained if you turn on exactly $$$k$$$ sticks or $$$-1$$$, if it is impossible to turn on exactly $$$k$$$ sticks so that a correct number appears on the scoreboard digits.", "sample_inputs": ["1 7\n0000000", "2 5\n0010010\n0010010", "3 5\n0100001\n1001001\n1010011"], "sample_outputs": ["8", "97", "-1"], "notes": "NoteIn the first test, we are obliged to include all $$$7$$$ sticks and get one $$$8$$$ digit on the scoreboard.In the second test, we have sticks turned on so that units are formed. For $$$5$$$ of additionally included sticks, you can get the numbers $$$07$$$, $$$18$$$, $$$34$$$, $$$43$$$, $$$70$$$, $$$79$$$, $$$81$$$ and $$$97$$$, of which we choose the maximum  — $$$97$$$.In the third test, it is impossible to turn on exactly $$$5$$$ sticks so that a sequence of numbers appears on the scoreboard."}, "positive_code": [{"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\nimport std.format;\nimport std.exception;\nimport std.bitmanip;\n\nimmutable int[][] digits = [\n [1,2,3,5,6,7],\n [3,6],\n [1,3,4,5,7],\n [1,3,4,6,7],\n [2,3,4,6],\n [1,2,4,6,7],\n [1,2,4,5,6,7],\n [1,3,6],\n [1,2,3,4,5,6,7],\n [1,2,3,4,6,7]\n];\n\nint cvt (in int[] a) {\n return a.map!(i => 1 << (i - 1)).sum;\n}\n\nimmutable int[] mask = digits.map!(cvt).array.idup;\n\nfinal class Solve {\n immutable int n, maxk;\n immutable int[] c;\n BitArray dp;\n int[] pc;\n bool f (int i, int t) {\n int idx = (i * (maxk + 1) + t) * 2;\n if (dp[idx]) return dp[idx+1];\n dp[idx] = true;\n if (i == n) {\n if (t == 0) {\n dp[idx+1] = true;\n return true;\n }\n return false;\n }\n const int ci = c[i];\n foreach_reverse (j; mask) {\n const int w = ci & j;\n if (w != ci) continue;\n const int bits = pc[j ^ ci];\n if (bits > t) continue;\n if (f (i + 1, t - bits)) {\n dp[idx+1] = true;\n return true;\n }\n }\n return false;\n }\n void restore () {\n int i, t = maxk;\n while (i < n) {\n const int ci = c[i];\n foreach_reverse (pos, j; mask) {\n const int w = ci & j;\n if (w != ci) continue;\n const int bits = pc[j ^ ci];\n if (bits > t) continue;\n if (f (i + 1, t - bits)) {\n write(pos);\n t -= bits;\n break;\n }\n }\n ++i;\n }\n }\n this (int n, int maxk, int[] c) {\n this.n = n;\n this.maxk = maxk;\n this.c = c.idup;\n dp.length = 2 * (n + 1) * (maxk + 1);\n pc = new int[128];\n foreach (i; 1 .. 128) {\n pc[i] = pc[i & (i - 1)] + 1;\n }\n }\n}\n\nvoid main() {\n auto s = readln;\n int n, k;\n formattedRead(s, \" %d %d\", n, k);\n auto c = new int[n];\n foreach (i; 0 .. n) {\n auto t = readln;\n foreach (j; 0 .. 7) if (t[j] == '1') {\n c[i] += 1 << j;\n }\n }\n auto solve = new Solve (n, k, c);\n if (solve.f (0, k)) {\n solve.restore ();\n writeln;\n } else {\n writeln (-1);\n }\n\n}\n\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n int[] arr;\n foreach (_; 0 .. n) {\n string s = readln.chomp;\n arr ~= to!int(s, 2);\n }\n \n int[] nums;\n nums ~= to!int(\"1110111\", 2);\n nums ~= to!int(\"0010010\", 2);\n nums ~= to!int(\"1011101\", 2);\n nums ~= to!int(\"1011011\", 2);\n nums ~= to!int(\"0111010\", 2);\n nums ~= to!int(\"1101011\", 2);\n nums ~= to!int(\"1101111\", 2);\n nums ~= to!int(\"1010010\", 2);\n nums ~= to!int(\"1111111\", 2);\n nums ~= to!int(\"1111011\", 2);\n \n auto best = new Tuple!(int, int)[][] (1 << 7);\n foreach (x; 0 .. (1 << 7)) {\n outer: foreach (val, target; nums.enumerate(0)) {\n int need = 0;\n foreach (bit; 0 .. 8) {\n int xb = x & (1 << bit);\n int tb = target & (1 << bit);\n if (tb < xb) { continue outer; }\n if (tb > xb) { ++need; }\n }\n best[x] ~= tuple(val, need);\n }\n }\n \n auto dp = new int[][] (n+1, k+1);\n foreach (i; 0 .. n+1) { dp[i][] = -2; }\n \n int go(int pos, int left) {\n if (pos == n) { return left == 0 ? 0 : -1; }\n if (dp[pos][left] > -2) { return dp[pos][left]; }\n \n foreach (i, pr; best[arr[pos]]) {\n auto target = pr[0], need = pr[1];\n if (need > left) { continue; }\n \n auto rest = go(pos+1, left - need);\n if (rest >= 0) { dp[pos][left] = i.to!int; }\n }\n \n if (dp[pos][left] == -2) { dp[pos][left] = -1; }\n \n return dp[pos][left];\n }\n \n auto score = go(0, k);\n \n if (score < 0) {\n writeln(-1);\n return;\n }\n \n int[] ans;\n int left = k;\n foreach (i; 0 .. n) {\n auto pr = best[arr[i]][dp[i][left]];\n ans ~= pr[0];\n left -= pr[1];\n }\n \n ans.map!(to!string).join.writeln;\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.stdio;\n\nimmutable int digits = 10;\nint [digits] digit;\n\nvoid prepare ()\n{\n\tdigit[0] = 0b1110111;\n\tdigit[1] = 0b0010010;\n\tdigit[2] = 0b1011101;\n\tdigit[3] = 0b1011011;\n\tdigit[4] = 0b0111010;\n\tdigit[5] = 0b1101011;\n\tdigit[6] = 0b1101111;\n\tdigit[7] = 0b1010010;\n\tdigit[8] = 0b1111111;\n\tdigit[9] = 0b1111011;\n}\n\nvoid main ()\n{\n\tprepare ();\n\n\tint n, k;\n\twhile (readf !(\" %s %s\") (n, k) > 0)\n\t{\n\t\tauto a = new int [n];\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\treadf !(\" %b\") (c);\n\t\t}\n\t\tauto f = new byte [] [] (n + 1, k + 10);\n\t\tforeach (ref g; f)\n\t\t{\n\t\t\tg[] = -1;\n\t\t}\n\t\tf[n][0] = digits;\n\t\tforeach_reverse (i; 0..n)\n\t\t{\n\t\t\tforeach (d, v; digit)\n\t\t\t{\n\t\t\t\tif ((a[i] | v) != v)\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tauto add = popcnt (a[i] ^ v);\n\t\t\t\tforeach (j; add..k + 1)\n\t\t\t\t{\n\t\t\t\t\tif (f[i + 1][j - add] >= 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tf[i][j] = cast (byte)\n\t\t\t\t\t\t ((add << 4) | d);\n\t\t\t\t\t}\n\t\t\t\t}\n\t \t}\n\t\t}\n\n\t\tif (f[0][k] >= 0)\n\t\t{\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\twrite (f[i][k] & 15);\n\t\t\t\tk -= f[i][k] >> 4;\n\t\t\t}\n\t\t\twriteln;\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (-1);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum A = 10;\nenum DIGITS = [\"1110111\", \"0010010\", \"1011101\", \"1011011\", \"0111010\", \"1101011\", \"1101111\", \"1010010\", \"1111111\", \"1111011\"];\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const K = readInt();\n auto S = new string[N];\n foreach (i; 0 .. N) {\n S[i] = readToken();\n }\n \n auto dks = new int[][](N, A);\n foreach (i; 0 .. N) {\n foreach (a; 0 .. A) {\n foreach (pos; 0 .. 7) {\n if (S[i][pos] > DIGITS[a][pos]) {\n dks[i][a] += K + 1;\n } else if (S[i][pos] < DIGITS[a][pos]) {\n dks[i][a] += 1;\n }\n }\n }\n }\n auto dp = new bool[][](N + 1, K + 1);\n dp[N][0] = true;\n foreach_reverse (i; 0 .. N) {\n foreach (a; 0 .. A) {\n const dk = dks[i][a];\n foreach (k; dk .. K + 1) {\n if (dp[i + 1][k - dk]) {\n dp[i][k] = true;\n }\n }\n }\n }\n \n if (dp[0][K]) {\n string ans;\n int k = K;\n foreach (i; 0 .. N) {\n foreach_reverse (a; 0 .. A) {\n if (k >= dks[i][a] && dp[i + 1][k - dks[i][a]]) {\n ans ~= cast(char)('0' + a);\n k -= dks[i][a];\n goto found;\n }\n }\n assert(false);\n found:\n }\n writeln(ans);\n } else {\n writeln(-1);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum A = 10;\nenum DIGITS = [\"1110111\", \"0010010\", \"1011101\", \"1011011\", \"0111010\", \"1101011\", \"1101111\", \"1010010\", \"1111111\", \"1111011\"];\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const K = readInt();\n auto S = new string[N];\n foreach (i; 0 .. N) {\n S[i] = readToken();\n }\n \n auto dks = new int[][](N, A);\n foreach (i; 0 .. N) {\n foreach (a; 0 .. A) {\n foreach (pos; 0 .. 7) {\n if (DIGITS[a][pos] != S[i][pos]) {\n ++dks[i][a];\n }\n }\n }\n }\n auto dp = new bool[][](N + 1, K + 1);\n dp[N][0] = true;\n foreach_reverse (i; 0 .. N) {\n foreach (a; 0 .. A) {\n const dk = dks[i][a];\n foreach (k; dk .. K + 1) {\n if (dp[i + 1][k - dk]) {\n dp[i][k] = true;\n }\n }\n }\n }\n \n if (dp[0][K]) {\n string ans;\n int k = K;\n foreach (i; 0 .. N) {\n foreach_reverse (a; 0 .. A) {\n if (k >= dks[i][a] && dp[i + 1][k - dks[i][a]]) {\n ans ~= cast(char)('0' + a);\n k -= dks[i][a];\n goto found;\n }\n }\n assert(false);\n found:\n }\n writeln(ans);\n } else {\n writeln(-1);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "d7f73762ff7a01c33280257e556a9b36"} {"nl": {"description": "In the Bus of Characters there are $$$n$$$ rows of seat, each having $$$2$$$ seats. The width of both seats in the $$$i$$$-th row is $$$w_i$$$ centimeters. All integers $$$w_i$$$ are distinct.Initially the bus is empty. On each of $$$2n$$$ stops one passenger enters the bus. There are two types of passengers: an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$) — the number of rows in the bus. The second line contains the sequence of integers $$$w_1, w_2, \\dots, w_n$$$ ($$$1 \\le w_i \\le 10^{9}$$$), where $$$w_i$$$ is the width of each of the seats in the $$$i$$$-th row. It is guaranteed that all $$$w_i$$$ are distinct. The third line contains a string of length $$$2n$$$, consisting of digits '0' and '1' — the description of the order the passengers enter the bus. If the $$$j$$$-th character is '0', then the passenger that enters the bus on the $$$j$$$-th stop is an introvert. If the $$$j$$$-th character is '1', the the passenger that enters the bus on the $$$j$$$-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal $$$n$$$), and for each extrovert there always is a suitable row.", "output_spec": "Print $$$2n$$$ integers — the rows the passengers will take. The order of passengers should be the same as in input.", "sample_inputs": ["2\n3 1\n0011", "6\n10 8 9 11 13 5\n010010011101"], "sample_outputs": ["2 1 1 2", "6 6 2 3 3 1 4 4 1 2 5 5"], "notes": "NoteIn the first example the first passenger (introvert) chooses the row $$$2$$$, because it has the seats with smallest width. The second passenger (introvert) chooses the row $$$1$$$, because it is the only empty row now. The third passenger (extrovert) chooses the row $$$1$$$, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row $$$2$$$, because it is the only row with an empty place."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto W = readln.split.map!(to!int).array;\n auto A = N.iota.map!(i => tuple(W[i], i+1)).array;\n auto P = readln.chomp.map!(a => a == '1').array;\n auto ans = new int[](2*N);\n A.sort();\n auto stack = new int[](N);\n int a = 0, sp = -1;\n\n foreach (i; 0..2*N) {\n if (P[i]) {\n ans[i] = stack[sp];\n sp--;\n } else {\n ans[i] = A[a][1];\n sp++;\n stack[sp] = A[a][1];\n a++;\n }\n }\n\n ans.map!(to!string).join(\" \").writeln;\n}\n"}], "negative_code": [], "src_uid": "161009edb8e3d438cdd8c0d1e202f783"} {"nl": {"description": "Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.Omkar currently has $$$n$$$ supports arranged in a line, the $$$i$$$-th of which has height $$$a_i$$$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $$$1$$$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $$$1$$$ to each of their heights. Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!An array $$$b$$$ is a subsegment of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.An array $$$b_1, b_2, \\dots, b_n$$$ is called nondecreasing if $$$b_i\\le b_{i+1}$$$ for every $$$i$$$ from $$$1$$$ to $$$n-1$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\leq t \\leq 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of supports Omkar has. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(0 \\leq a_{i} \\leq 10^9)$$$ — the heights of the supports. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.", "sample_inputs": ["3\n4\n5 3 2 5\n5\n1 2 3 5 3\n3\n1 1 1"], "sample_outputs": ["3\n2\n0"], "notes": "NoteThe subarray with which Omkar performs the operation is bolded.In the first test case:First operation:$$$[5, 3, \\textbf{2}, 5] \\to [5, 3, \\textbf{3}, 5]$$$Second operation:$$$[5, \\textbf{3}, \\textbf{3}, 5] \\to [5, \\textbf{4}, \\textbf{4}, 5]$$$Third operation:$$$[5, \\textbf{4}, \\textbf{4}, 5] \\to [5, \\textbf{5}, \\textbf{5}, 5]$$$In the third test case, the array is already nondecreasing, so Omkar does $$$0$$$ operations."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tlong res = 0;\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\tres += max (0, a[i - 1] - a[i]);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n \n long ans;\n foreach (i; 0 .. N - 1) {\n ans += max(A[i] - A[i + 1], 0);\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nalias type = multi;\nstruct TestCase\n{\n mixin prettyPrinting;\n\n long n;\n @Dim(\"n\") long[] a;\n\n void solve(long tc = -1)\n {\n a ~= long.max;\n long ops = 0;\n foreach_reverse(i; 0 .. cast(size_t)n)\n {\n if (a[i] > a[i + 1])\n {\n ops += a[i] - a[i + 1];\n }\n }\n writeln(ops);\n }\n}\n\nstruct Interval\n{\n long l, h;\n\n bool empty()\n {\n return l > h;\n }\n}\n\nInterval intersect(Interval a, Interval b)\n{\n return Interval(max(a.l, b.l), min(a.h, b.h));\n}\n\nstruct If\n{\n string condition;\n}\nenum ignore = If(\"false\");\nstruct Dim\n{\n string dimensions;\n int levels;\n this(string dimensions)\n {\n this.dimensions = dimensions;\n levels = cast(int)dimensions.count(\",\") + 1;\n }\n}\n\ntemplate getArrayLevel(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));\n else\n enum getArrayLevel = 0;\n}\n\nstatic foreach(fieldName; 'a' .. cast(char)('z' + 1))\n {\n static if (fieldName != 'o')\n mixin(\"enum d\", fieldName, \" = Dim(\\\"\", fieldName, \"\\\");\");\n }\n\n\nmixin template prettyPrinting()\n{\n string toString()\n {\n alias structType = typeof(this);\n auto res = appender!(string)();\n res.put(structType.stringof);\n res.put(\"(\");\n static foreach(fieldName; FieldNameTuple!structType)\n {\n res.put(text(\" \", fieldName, \": \", mixin(fieldName)));\n }\n res.put(\" )\");\n res.put(\"\\n\");\n return res[];\n }\n}\n\nstruct Graph(N)\n{\n size_t size;\n this(S)(S size)\n {\n adjacencyList.length = cast(size_t) size;\n this.size = cast(size_t) size;\n this.visited = makeSlice!bool(size);\n }\n N[][] adjacencyList;\n alias ne = neighbours;\n N[] neighbours(N n)\n {\n return adjacencyList.at(n);\n }\n alias bcon = biconnect;\n void biconnect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n adjacencyList.at(b) ~= a;\n }\n alias con = connect;\n void connect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n }\n void connect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n connect(edge[0], edge[1]);\n }\n }\n void biconnect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n biconnect(edge[0], edge[1]);\n }\n }\n alias deg = degree!long;\n auto degree(T)(N a)\n {\n return cast(T) adjacencyList.at(a).length;\n }\n\n auto visited = new bool[0];\n void dfs(Visitor)(long startNode)\n {\n auto visitor = Visitor.init;\n void internalDfs(long node)\n {\n visited.set(node, true);\n static if (is(typeof({visitor.n = node;})))\n visitor.n = node;\n static if (is(typeof({visitor.pre;})))\n visitor.pre;\n foreach(w; ne(node))\n if (!visited.at(w))\n {\n static if (is(typeof({visitor.w = w;})))\n visitor.w = w;\n static if (is(typeof(visitor.pren)))\n visitor.pren;\n internalDfs(w);\n static if (is(typeof(visitor.postn)))\n visitor.postn;\n }\n static if (is(typeof(visitor.post)))\n visitor.post;\n }\n internalDfs(startNode);\n }\n void dfs(long startNode)\n {\n struct DefVisitor {}\n dfs!DefVisitor(startNode);\n }\n}\n\nDList!string _words;\n\ntemplate removeArray(W, int times)\n{\n static if (times == 0)\n alias removeArray = W;\n else static if (times == 1)\n alias removeArray = typeof(function(){W w; return w[0];}());\n else\n {\n static assert(isDynamicArray!W);\n alias removeArray = removeArray!(removeArray!(W, 1), times - 1);\n }\n}\n\ntemplate baseType(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));\n else\n alias baseType = W;\n}\n\nauto makeSlice(E, W, T...)(W size, T otherSizes)\n{\n static if (T.length == 0)\n {\n E[] res;\n res.length = cast(size_t) size;\n return res;\n }\n else\n {\n typeof(makeSlice!E(otherSizes))[] res;\n res.length = cast(size_t) size;\n foreach(ref row; res)\n row = makeSlice!E(otherSizes);\n return res;\n }\n}\n\nvoid read(T...)(ref T t)\n{\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\n\t\tforeach (i; 0..n-1)\n\t\t{\n\t\t\tauto x = a[i] - a[i+1];\n\t\t\tif (x > 0)\n\t\t\t{\n\t\t\t\tans[ti] += x;\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA!int;\n\t\n\t\tforeach (i; 0..n-1)\n\t\t{\n\t\t\tauto x = a[i] - a[i+1];\n\t\t\tif (x > 0)\n\t\t\t{\n\t\t\t\tans[ti] += x;\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "src_uid": "0bd06900e42db9f83cdd43c24da6686e"} {"nl": {"description": "Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?Take a look at the notes if you think you haven't understood the problem completely.", "input_spec": "The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends. The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor. Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.", "output_spec": "Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.", "sample_inputs": ["5 2\n2 5 3 4 8\n1 4\n4 5", "10 0\n1 2 3 4 5 6 7 8 9 10", "10 5\n1 6 2 7 3 8 4 9 5 10\n1 2\n3 4\n5 6\n7 8\n9 10"], "sample_outputs": ["10", "55", "15"], "notes": "NoteIn the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.In the second example Vova has to bribe everyone.In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters."}, "positive_code": [{"source_code": "#!/usr/bin/env dub\n/+ dub.sdl:\n name \"C\"\n+/\n\nimport std.array;\nimport std.algorithm;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.stdio;\nimport std.string;\n\nvoid main()\n{\n int N, M;\n readf(\" %s %s \", N, M);\n \n long[] C = new long[N];\n foreach (i; 0..N) {\n readf(\" %s \", C[i]);\n } \n\n int[][] adj = new int[][N];\n foreach (i; 0..M) {\n int x, y;\n readf(\" %s %s \", x, y);\n x--;\n y--;\n\n adj[x] ~= y;\n adj[y] ~= x;\n }\n\n debug(2) { writeln(adj);}\n\n int[] comps = replicate([-1], N);\n int ncomps = 0;\n\n foreach (v; 0..N) {\n if (comps[v] == -1) {\n auto bfs = DList!int(v);\n comps[v] = ncomps;\n\n while (!bfs.empty) {\n auto cur = bfs.front();\n bfs.removeFront();\n\n foreach (nbr; adj[cur]) {\n if (comps[nbr] == -1) {\n bfs.insertBack(nbr);\n comps[nbr] = ncomps;\n }\n }\n }\n\n ncomps++;\n }\n }\n\n long[] mins = replicate([long.max], ncomps);\n\n foreach (v; 0..N) {\n mins[comps[v]] = min(mins[comps[v]], C[v]);\n }\n\n long ans = sum(mins);\n writeln(ans);\n}"}, {"source_code": "#!/usr/bin/env dub\n/+ dub.sdl:\n name \"C\"\n+/\n\nimport std.array;\nimport std.algorithm;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.stdio;\nimport std.string;\n\nclass Graph {\n int N;\n int[][] adj;\n\n private int[] comps;\n private int ncomps;\n\n bool flag_connected_components = false;\n\n this(int N) {\n this.N = N;\n adj = new int[][N];\n }\n\n void addEdge(int u, int v) {\n adj[u] ~= v;\n adj[v] ~= u;\n }\n\n void do_connected_components() {\n comps = replicate([-1], N);\n\n foreach (v; 0..N) {\n if (comps[v] == -1) {\n auto bfs = DList!int(v);\n comps[v] = ncomps;\n\n while (!bfs.empty) {\n auto cur = bfs.front();\n bfs.removeFront();\n\n foreach (nbr; adj[cur]) {\n if (comps[nbr] == -1) {\n bfs.insertBack(nbr);\n comps[nbr] = ncomps;\n }\n }\n }\n\n ncomps++;\n }\n }\n\n debug {\n flag_connected_components = true;\n }\n }\n\n int component(int v) {\n debug {\n assert(flag_connected_components);\n }\n return comps[v];\n }\n\n}\n\nvoid main()\n{\n int N, M;\n readf(\" %s %s \", N, M);\n \n long[] C = new long[N];\n foreach (i; 0..N) {\n readf(\" %s \", C[i]);\n } \n\n Graph G = new Graph(N);\n foreach (i; 0..M) {\n int x, y;\n readf(\" %s %s \", x, y);\n x--;\n y--;\n\n G.addEdge(x, y);\n }\n G.do_connected_components();\n\n long[] mins = replicate([long.max], G.ncomps);\n\n foreach (v; 0..N) {\n mins[G.component(v)] = \n min(mins[G.component(v)], C[v]);\n }\n\n long ans = sum(mins);\n writeln(ans);\n}"}, {"source_code": "// https://codeforces.com/problemset/problem/893/C\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\nimport std.range.primitives;\n\nint[][] graph;\nint[] visited;\nlong[] c;\n\nlong dfs(int u) {\n visited[u] = 1;\n long minima = c[u];\n for(int i = 0; i < graph[u].length; i++) {\n int v = graph[u][i];\n if(visited[v] == 0) {\n minima = min(minima,dfs(v));\n }\n }\n return minima;\n}\n\nvoid main() {\n int n, m;\n readf(\"%s %s\\n\", &n, &m);\n c = readln.split.map!(to!long).array;\n // TODO how to initialize this?\n visited = new int[n];\n foreach(_; 0..n) {\n int[] vertex;\n graph ~= vertex;\n }\n foreach(_; 0..m) {\n int x,y;\n readf(\"%s %s\\n\", &x, &y);\n x -= 1;\n y -= 1;\n graph[x] ~= y;\n graph[y] ~= x;\n }\n long[] cost;\n foreach(vertex; 0..n) {\n if(visited[vertex] == 0) {\n cost ~= dfs(vertex);\n }\n }\n long total;\n foreach(item; cost)\n total += item;\n total.writeln;\n}\n\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm, std.numeric;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\n\nimmutable inf = 10^^9 + 7;\n\nvoid main() {\n int n, m;\n scan(n, m);\n auto c = readln.split.to!(int[]);\n\n auto uf = UnionFind(n);\n\n foreach (i ; 0 .. m) {\n int xi, yi;\n scan(xi, yi);\n xi--, yi--;\n uf.unite(xi, yi);\n }\n\n auto d = new int[](n);\n d[] = inf;\n\n foreach (i ; 0 .. n) {\n int r = uf.find_root(i);\n d[r] = min(d[r], c[i]);\n }\n\n long ans;\n\n foreach (i ; 0 .. n) {\n if (d[i] < inf) ans += d[i];\n }\n\n writeln(ans);\n}\n\n\nstruct UnionFind {\n private {\n int N;\n int[] p;\n int[] rank;\n }\n\n this (int n) {\n N = n;\n p = iota(N).array;\n rank = new int[](N);\n }\n\n int find_root(int x) {\n if (p[x] != x) {\n p[x] = find_root(p[x]);\n }\n\n return p[x];\n }\n\n bool same(int x, int y) {\n return find_root(x) == find_root(y);\n }\n\n void unite(int x, int y) {\n int u = find_root(x), v = find_root(y);\n\n if (u == v) return;\n\n if (rank[u] < rank[v]) {\n p[u] = v;\n }\n else {\n p[v] = u;\n\n if (rank[u] == rank[v]) {\n rank[u]++;\n }\n }\n }\n}\n\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}\n\n\n\nstruct Queue(T) {\n private {\n int N, head, tail;\n T[] data;\n }\n\n this(int n) {\n N = n + 1;\n data = new T[](N);\n }\n\n bool empty() {\n return head == tail;\n }\n\n bool full() {\n return (tail + 1) % N == head;\n }\n\n T front() {\n return data[head];\n }\n\n void push(T x) {\n assert(!full);\n data[tail++] = x;\n tail %= N;\n }\n\n void pop() {\n assert(!empty);\n head = (head + 1) % N;\n }\n\n void clear() {\n head = tail = 0;\n }\n}"}], "negative_code": [{"source_code": "// https://codeforces.com/problemset/problem/893/C\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\nimport std.range.primitives;\n\nint[][] graph;\nint[] visited;\nint[] c;\n\nint dfs(int u) {\n visited[u] = 1;\n int minima = c[u];\n for(int i = 0; i < graph[u].length; i++) {\n int v = graph[u][i];\n minima = min(minima, c[v]);\n if(visited[v] == 0) {\n dfs(v);\n }\n }\n return minima;\n}\n\nvoid main() {\n int n, m;\n readf(\"%s %s\\n\", &n, &m);\n c = readln.split.map!(to!int).array;\n // TODO how to initialize this?\n visited = new int[n];\n foreach(_; 0..n) {\n int[] vertex;\n graph ~= vertex;\n }\n foreach(_; 0..m) {\n int x,y;\n readf(\"%s %s\\n\", &x, &y);\n x -= 1;\n y -= 1;\n graph[x] ~= y;\n graph[y] ~= x;\n }\n int[] cost;\n foreach(vertex; 0..n) {\n if(visited[vertex] == 0) {\n cost ~= dfs(vertex);\n }\n }\n sum(cost).writeln;\n}\n\n"}, {"source_code": "// https://codeforces.com/problemset/problem/893/C\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\nimport std.range.primitives;\n\nint[][] graph;\nint[] visited;\nlong[] c;\n\nlong dfs(int u) {\n visited[u] = 1;\n long minima = c[u];\n for(int i = 0; i < graph[u].length; i++) {\n int v = graph[u][i];\n minima = min(minima, c[v]);\n if(visited[v] == 0) {\n dfs(v);\n }\n }\n return minima;\n}\n\nvoid main() {\n int n, m;\n readf(\"%s %s\\n\", &n, &m);\n c = readln.split.map!(to!long).array;\n // TODO how to initialize this?\n visited = new int[n];\n foreach(_; 0..n) {\n int[] vertex;\n graph ~= vertex;\n }\n foreach(_; 0..m) {\n int x,y;\n readf(\"%s %s\\n\", &x, &y);\n x -= 1;\n y -= 1;\n graph[x] ~= y;\n graph[y] ~= x;\n }\n long[] cost;\n foreach(vertex; 0..n) {\n if(visited[vertex] == 0) {\n cost ~= dfs(vertex);\n }\n }\n long total;\n foreach(item; cost)\n total += item;\n total.writeln;\n}\n\n"}], "src_uid": "9329cb499f003aa71c6f51556bcc7b05"} {"nl": {"description": "You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$.There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$.You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room.The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).You can't rotate any of the tables, but you can move the first table inside the room. Example of how you may move the first table. What is the minimum distance you should move the first table to free enough space for the second one?", "input_spec": "The first line contains the single integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of the test cases. The first line of each test case contains two integers $$$W$$$ and $$$H$$$ ($$$1 \\le W, H \\le 10^8$$$) — the width and the height of the room. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$0 \\le x_1 < x_2 \\le W$$$; $$$0 \\le y_1 < y_2 \\le H$$$) — the coordinates of the corners of the first table. The third line contains two integers $$$w$$$ and $$$h$$$ ($$$1 \\le w \\le W$$$; $$$1 \\le h \\le H$$$) — the width and the height of the second table.", "output_spec": "For each test case, print the minimum distance you should move the first table, or $$$-1$$$ if there is no way to free enough space for the second table. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.", "sample_inputs": ["5\n8 5\n2 1 7 4\n4 2\n5 4\n2 2 5 4\n3 3\n1 8\n0 3 1 6\n1 5\n8 1\n3 0 6 1\n5 1\n8 10\n4 5 7 8\n8 5"], "sample_outputs": ["1.000000000\n-1\n2.000000000\n2.000000000\n0.000000000"], "notes": "NoteThe configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by $$$(0, -1)$$$, so the lower left corner will move from $$$(2, 1)$$$ to $$$(2, 0)$$$. Then you can place the second table at $$$(0, 3)-(4, 5)$$$.In the second test case, there is no way to fit both tables in the room without intersecting.In the third test case, you can move the first table by $$$(0, 2)$$$, so the lower left corner will move from $$$(0, 3)$$$ to $$$(0, 5)$$$."}, "positive_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n ll W = scan; ll H = scan;\n ll x1 = scan; ll y1 = scan;\n ll x2 = scan; ll y2 = scan;\n ll w = scan; ll h = scan;\n // Height\n ll wdis = max(W - x2, x1);\n ll hdis = max(H - y2, y1);\n ll wdiff = max(w - wdis, 0);\n ll hdiff = max(h - hdis, 0);\n /* show(wdis, hdis); */\n /* show(wdiff, hdiff); */\n ll nx, ny;\n bool f = 1;\n if(W - x2 > x1){\n nx = x1 - wdiff;\n if(nx < 0){\n f = 0;\n }\n }else{\n nx = x1 + wdiff;\n if(x2 + wdiff > W){\n f = 0;\n }\n }\n bool f2 = 1;\n if(H - y2 > y1){\n ny = y1 - hdiff;\n if(ny < 0){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }else{\n ny = y1 + hdiff;\n if(y2 + hdiff > H){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }\n show(nx, ny);\n ll dist = -1;\n if(f && f2){\n dist = min(abs(nx - x1), abs(ny - y1));\n }else if(f2){\n dist = abs(ny - y1);\n }else if(f){\n dist = abs(nx - x1);\n }\n if(dist == -1){\n writeln(dist);\n return;\n }\n double dis = dist.to!double;\n writefln(\"%.10f\", dis);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport std.range;\nimport core.stdc.stdio;\n\nvoid main()\n{\n\tint t = readInt!int; while (t--) solve;\n}\n\nvoid solve()\n{\n\tlong w = readInt!long;\n\tlong h = readInt!long;\n\tlong[2] xb, yb;\n\tlong x1 = readInt!long;\n\tlong y1 = readInt!long;\n\tlong x2 = readInt!long;\n\tlong y2 = readInt!long;\n\txb = [x1, x2];\n\tyb = [y1, y2];\n\tlong rw = readInt!long;\n\tlong rh = readInt!long; \n\tlong wb = x2 - x1;\n\tlong hb = y2 - y1;\n\tlong minDist = long.max;\n\tvoid tryPoint(long x, long y)\n\t{\n\t\tif (rw <= x || rh <= y) { minDist = 0; return; }\n\t\tif (rw > x) \n\t\t{\n\t\t\tlong hx = rw;\n\t\t\tif (hx + wb <= w) \n\t\t\t{\n\t\t\t\tminDist = min(minDist, rw - x);\n\t\t\t}\n\t\t}\n\t\tif (rh > y)\n\t\t{\n\t\t\tlong hy = rh;\n\t\t\tif (hy + hb <= h)\n\t\t\t{\n\t\t\t\tminDist = min(minDist, rh - y);\n\t\t\t}\n\t\t}\n\t}\n\ttryPoint(xb[0], yb[0]);\n\ttryPoint(xb[0], h - yb[1]);\n\ttryPoint(w - xb[1], yb[0]);\n\ttryPoint(w - xb[1], h - yb[1]);\n\tif (minDist != long.max) minDist.writeln; else (-1).writeln;\n}\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n\tcurrChar = getchar();\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n int t;\n readf!\" %d \"(t);\n while (t--) {\n int W, H;\n readf!\" %d %d \"(W, H);\n int x1, y1, x2, y2;\n readf!\" %d %d %d %d \"(x1, y1, x2, y2);\n\n int X1 = max(x1, W - x2);\n int Y1 = max(y1, H - y2);\n\n int w, h;\n readf!\" %d %d \"(w, h);\n\n int movex, movey;\n if (w > X1) {\n movex = w - X1;\n if (w + abs(x2 - x1) > W)\n movex = int.max;\n }\n if (h > Y1) {\n movey = h - Y1;\n if (h + abs(y2 - y1) > H)\n movey = int.max;\n }\n\n int ans = min(movex, movey);\n if (ans == int.max) {\n writeln(-1);\n } else {\n writefln(\"%.9f\", cast(double)ans);\n }\n }\n}\n"}], "negative_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n ll W = scan; ll H = scan;\n ll x1 = scan; ll y1 = scan;\n ll x2 = scan; ll y2 = scan;\n ll w = scan; ll h = scan;\n // Height\n ll wdis = max(W - x2, x1);\n ll hdis = max(H - y2, y1);\n ll wdiff = max(w - wdis, 0);\n ll hdiff = max(h - hdis, 0);\n /* show(wdis, hdis); */\n /* show(wdiff, hdiff); */\n ll nx, ny;\n bool f = 1;\n if(W - x2 > x1){\n nx = x1 - wdiff;\n if(x1 < 0){\n f = 0;\n }\n }else{\n nx = x1 + wdiff;\n if(x2 + wdiff > W){\n f = 0;\n }\n }\n bool f2 = 1;\n if(H - y2 > y1){\n ny = y1 - hdiff;\n if(y1 < 0){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }else{\n ny = y1 + hdiff;\n if(y2 + hdiff > H){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }\n show(nx, ny);\n ll dist = -1;\n if(f && f2){\n dist = min(abs(nx - x1), abs(ny - y1));\n }else if(f2){\n dist = abs(ny - y1);\n }else if(f){\n dist = abs(nx - x1);\n }\n if(dist == -1){\n writeln(dist);\n return;\n }\n double dis = dist.to!double;\n writefln(\"%.10f\", dis);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n ll W = scan; ll H = scan;\n ll x1 = scan; ll y1 = scan;\n ll x2 = scan; ll y2 = scan;\n ll w = scan; ll h = scan;\n // Height\n ll wdis = max(W - x2, x1);\n ll hdis = max(H - y2, y1);\n ll wdiff = max(w - wdis, 0);\n ll hdiff = max(h - hdis, 0);\n /* show(wdis, hdis); */\n /* show(wdiff, hdiff); */\n ll nx, ny;\n bool f = 1;\n if(W - x2 > x1){\n nx = x1 - wdiff;\n if(x1 < 0){\n f = 0;\n }\n }else{\n nx = x1 + wdiff;\n if(x2 + wdiff > W){\n f = 0;\n }\n }\n bool f2 = 1;\n if(H - y2 > y1){\n ny = y1 - hdiff;\n if(y1 < 0){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }else{\n ny = y1 + hdiff;\n if(y2 + hdiff > H){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }\n show(nx, ny);\n ll dist = -1;\n if(f && f2){\n dist = min(abs(nx - x1), abs(ny - y1));\n }else if(f2){\n dist = abs(ny - y1);\n }else if(f){\n dist = abs(nx - x1);\n }\n double dis = dist.to!double;\n writefln(\"%.10f\", dis);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n ll W = scan; ll H = scan;\n ll x1 = scan; ll y1 = scan;\n ll x2 = scan; ll y2 = scan;\n ll w = scan; ll h = scan;\n // Height\n ll wdis = max(W - x2, x1);\n ll hdis = max(H - y2, y1);\n ll wdiff = max(w - wdis, 0);\n ll hdiff = max(h - hdis, 0);\n show(wdis, hdis);\n show(wdiff, hdiff);\n ll nx, ny;\n bool f = 1;\n if(W - x2 > x1){\n nx = x1 - wdiff;\n if(x1 < 0){\n f = 0;\n }\n }else{\n nx = x1 + wdiff;\n if(x2 + wdiff > W){\n f = 0;\n }\n }\n bool f2 = 1;\n if(H - y2 > y1){\n ny = y1 - hdiff;\n if(y1 < 0){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }else{\n ny = y1 + hdiff;\n if(y2 + hdiff > H && !f){\n if(!f){\n writeln(-1);\n return;\n }else{\n f2 = 0;\n }\n }\n }\n ll dist = -1;\n if(f && f2){\n dist = min(abs(nx - x1), abs(ny - y1));\n }else if(f2){\n dist = abs(ny - y1);\n }else if(f){\n dist = abs(nx - x1);\n }\n double dis = dist.to!double;\n writefln(\"%.8f\", dis);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}], "src_uid": "29fd4c77a2f28478ebce98dfc6496aac"} {"nl": {"description": "Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $$$k$$$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.Now each knight ponders: how many coins he can have if only he kills other knights?You should answer this question for each knight.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ $$$(1 \\le n \\le 10^5, 0 \\le k \\le \\min(n-1,10))$$$ — the number of knights and the number $$$k$$$ from the statement. The second line contains $$$n$$$ integers $$$p_1, p_2 ,\\ldots,p_n$$$ $$$(1 \\le p_i \\le 10^9)$$$ — powers of the knights. All $$$p_i$$$ are distinct. The third line contains $$$n$$$ integers $$$c_1, c_2 ,\\ldots,c_n$$$ $$$(0 \\le c_i \\le 10^9)$$$ — the number of coins each knight has.", "output_spec": "Print $$$n$$$ integers — the maximum number of coins each knight can have it only he kills other knights.", "sample_inputs": ["4 2\n4 5 9 7\n1 2 11 33", "5 1\n1 2 3 4 5\n1 2 3 4 5", "1 0\n2\n3"], "sample_outputs": ["1 3 46 36", "1 3 5 7 9", "3"], "notes": "NoteConsider the first example. The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. The second knight can kill the first knight and add his coin to his own two. The third knight is the strongest, but he can't kill more than $$$k = 2$$$ other knights. It is optimal to kill the second and the fourth knights: $$$2+11+33 = 46$$$. The fourth knight should kill the first and the second knights: $$$33+1+2 = 36$$$. In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.In the third example there is only one knight, so he can't kill anyone."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto p = readln.chomp.split.map!(to!int).array;\n auto c = readln.chomp.split.map!(to!int).array;\n \n auto idx = n.iota.array;\n makeIndex(p, idx);\n \n debug { idx.writeln; }\n \n auto ans = new long[] (n);\n \n auto mxs = make!(RedBlackTree!(int, \"a < b\", true));\n long sum = 0L;\n foreach (i; idx) {\n ans[i] = sum + c[i];\n \n sum += c[i];\n mxs.insert(c[i]);\n \n if (mxs.length > k) {\n sum -= mxs.front;\n mxs.removeFront();\n }\n \n debug { mxs.writeln; }\n }\n \n ans.writefln!(\"%(%s %)\");\n}"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto p = readln.chomp.split.map!(to!int).array;\n auto c = readln.chomp.split.map!(to!int).array;\n \n auto idx = n.iota.array;\n makeIndex(p, idx);\n \n debug { idx.writeln; }\n \n auto ans = new long[] (n);\n \n auto mxs = make!(RedBlackTree!int);\n long sum = 0L;\n foreach (i; idx) {\n ans[i] = sum + c[i];\n \n sum += c[i];\n mxs.insert(c[i]);\n \n if (mxs.length > k) {\n sum -= mxs.front;\n mxs.removeFront();\n }\n \n debug { mxs.writeln; }\n }\n \n ans.writefln!(\"%(%s %)\");\n}"}], "src_uid": "fa55be247a70fb509182f2fe203f6024"} {"nl": {"description": "We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$. If you sort the array in increasing order (such that $$$a_1 < a_2 < \\ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \\ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\\max(a_1, a_2, \\dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \\le n \\le 50$$$; $$$1 \\le x < y \\le 50$$$) — the length of the array and two elements that are present in the array, respectively.", "output_spec": "For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.", "sample_inputs": ["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"], "sample_outputs": ["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"], "notes": null}, "positive_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\nimport std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.conv;\nimport std.range;\n\nalias ll = long;\n\nvoid play(){\n ll n, x, y;\n readf(\" %s %s %s \", &n, &x, &y);\n ll diff = y - x;\n ll minmax = long.max / 10;\n ll mind = -1, mina = -1;\n foreach(ll d; 1 .. diff+1){\n if(diff % d == 0){\n ll minn = y - (n - 1) * d;\n while(minn <= 0){\n minn += d;\n }\n if(minn > x) continue;\n ll curmax = minn + (n - 1) * d;\n if(curmax < minmax){\n minmax = curmax;\n mind = d;\n mina = minn;\n }\n }\n }\n foreach(ll i; 0 .. n){\n writef(\"%s \", mina + i * mind);\n }\n writeln;\n}\n\nint main(){\n ll t = 1;\n readf(\" %s \", &t); // Toggle!\n while(t--) play(); // Let's play!\n return 0;\n}\n\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\n\nvoid main(string[] args)\n{\n inputFile = stdin;\n debug inputFile = File(args[1]);\n auto nt = next!int;\n foreach(t; 0 .. nt)\n {\n auto n = next!int;\n auto x = next!int;\n auto y = next!int;\n auto diff = y - x;\n int bestjump = -1;\n int bestjumpmax = int.max;\n foreach(jump; 1 .. diff + 1)\n {\n if (diff % jump == 0)\n {\n if (diff / jump + 1 > n)\n continue;\n int rem = n - (diff / jump + 1);\n int lessthan = cast(int) iota(1, x).count!(num => num % jump == x % jump);\n rem -= min(rem, lessthan);\n int jumpmax = y + rem * jump;\n if (jumpmax < bestjumpmax)\n {\n bestjump = jump;\n bestjumpmax = jumpmax;\n }\n }\n }\n foreach(i; 0 .. n)\n {\n write(bestjumpmax - bestjump * i, \" \");\n }\n writeln;\n }\n}\n\n// INPUT\n\nenum InputStyle\n {\n byChunk, byLine\n };\nenum inputStyle = InputStyle.byLine;\n\nFile inputFile;\nstring popWord();\nstatic if (inputStyle == InputStyle.byChunk)\n {\n const chunkSize = 1024 * 1024 * 8;\n const wordSize = 4096;\n char[chunkSize] chunkBuff;\n char[] currChunk;\n void renewChunk()\n {\n if (inputFile.eof)\n currChunk = null;\n else\n currChunk = inputFile.rawRead(chunkBuff);\n }\n char[wordSize] wordBuff;\n char[] word;\n string popWord()\n {\n if (currChunk.length == 0)\n renewChunk;\n assert(currChunk.length > 0);\n while (currChunk[0].isWhite)\n {\n\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n renewChunk;\n }\n word = wordBuff[0 .. 0];\n while (!currChunk[0].isWhite)\n {\n word.length++;\n word[$ - 1] = currChunk[0];\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n {\n renewChunk;\n if (currChunk.length == 0)\n return cast(string) word;\n }\n }\n return cast(string) word;\n }\n }\n else\n {\n DList!string _words;\n string popWord()\n {\n while (_words.empty)\n {\n foreach(w; inputFile.readln.split)\n {\n _words.insertBack(w);\n }\n }\n auto word = _words.front;\n _words.removeFront;\n return word;\n }\n }\nT next(T)()\n{\n return to!T(popWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD;\n\t\tauto x = RD;\n\t\tauto y = RD;\n\t\t\n\t\tauto d = y-x;\n\t\tforeach_reverse (i; 1..n)\n\t\t{\n\t\t\tif (d % i) continue;\n\t\t\tauto cnt = d / i;\n\t\t\tauto begin = x;\n\t\t\twhile (begin <= y)\n\t\t\t{\n\t\t\t\tans[ti] ~= begin;\n\t\t\t\tbegin += cnt;\n\t\t\t}\n\t\t\t\n\t\t\tbegin = x - cnt;\n\t\t\twhile (begin >= 1 && ans[ti].length < n)\n\t\t\t{\n\t\t\t\tans[ti] ~= begin;\n\t\t\t\tbegin -= cnt;\n\t\t\t}\n\t\t\tbegin = y + cnt;\n\t\t\twhile (ans[ti].length < n)\n\t\t\t{\n\t\t\t\tans[ti] ~= begin;\n\t\t\t\tbegin += cnt;\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\te.map!(to!string).join(\" \").writeln;\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "ca9d97e731e86cf8223520f39ef5d945"} {"nl": {"description": "Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $$$e_i$$$ — his inexperience. Russell decided that an explorer with inexperience $$$e$$$ can only join the group of $$$e$$$ or more people.Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.", "input_spec": "The first line contains the number of independent test cases $$$T$$$($$$1 \\leq T \\leq 2 \\cdot 10^5$$$). Next $$$2T$$$ lines contain description of test cases. The first line of description of each test case contains the number of young explorers $$$N$$$ ($$$1 \\leq N \\leq 2 \\cdot 10^5$$$). The second line contains $$$N$$$ integers $$$e_1, e_2, \\ldots, e_N$$$ ($$$1 \\leq e_i \\leq N$$$), where $$$e_i$$$ is the inexperience of the $$$i$$$-th explorer. It's guaranteed that sum of all $$$N$$$ doesn't exceed $$$3 \\cdot 10^5$$$.", "output_spec": "Print $$$T$$$ numbers, each number on a separate line. In $$$i$$$-th line print the maximum number of groups Russell can form in $$$i$$$-th test case.", "sample_inputs": ["2\n3\n1 1 1\n5\n2 3 1 2 2"], "sample_outputs": ["3\n2"], "notes": "NoteIn the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $$$1$$$, so it's not less than the size of his group.In the second example we can organize two groups. Explorers with inexperience $$$1$$$, $$$2$$$ and $$$3$$$ will form the first group, and the other two explorers with inexperience equal to $$$2$$$ will form the second group.This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $$$2$$$, and the second group using only one explorer with inexperience equal to $$$1$$$. In this case the young explorer with inexperience equal to $$$3$$$ will not be included in any group."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto e = RDA!int;\n\t\te.sort();\n\t\t\n\t\tint cnt;\n\t\twhile (!e.empty)\n\t\t{\n\t\t\tauto x = e.front; e.popFront;\n\t\t\t++cnt;\n\t\t\tif (x <= cnt)\n\t\t\t{\n\t\t\t\t++ans[ti];\n\t\t\t\tcnt = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto e = RDA!int;\n\t\te.sort!\"a > b\"();\n\t\t\n\t\twhile (!e.empty)\n\t\t{\n\t\t\tauto x = e.front;\n\t\t\tif (e.length >= x)\n\t\t\t{\n\t\t\t\t++ans[ti];\n\t\t\t}\n\t\t\te = e[min(x, e.length)..$];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\n\tstdout.flush;\n\tdebug readln;\n}"}], "src_uid": "8e766dea94dc2033ba2d5759d7e5cd80"} {"nl": {"description": "Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given in two lines. The first line contains two integers $$$a_1$$$ and $$$b_1$$$ ($$$1 \\le a_1, b_1 \\le 100$$$) — the dimensions of the first one obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length). The second line contains two integers $$$a_2$$$ and $$$b_2$$$ ($$$1 \\le a_2, b_2 \\le 100$$$) — the dimensions of the second obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length).", "output_spec": "Print $$$t$$$ answers, each of which is a string \"YES\" (in the case of a positive answer) or \"NO\" (in the case of a negative answer). The letters in words can be printed in any case (upper or lower).", "sample_inputs": ["3\n2 3\n3 1\n3 2\n1 3\n3 3\n1 3"], "sample_outputs": ["Yes\nYes\nNo"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto p = readln.splitter.map !(to !(int)).array;\n\t\tsort (p);\n\t\tauto q = readln.splitter.map !(to !(int)).array;\n\t\tsort (q);\n\t\tbool ok = false;\n\t\tdo\n\t\t{\n\t\t\tdo\n\t\t\t{\n\t\t\t\tif (p[0] + q[0] == p[1] &&\n\t\t\t\t p[0] + q[0] == q[1])\n\t\t\t\t{\n\t\t\t\t\tok = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile (nextPermutation (q));\n\t\t}\n\t\twhile (nextPermutation (p));\n\t\twriteln (ok ? \"Yes\" : \"No\");\n\t}\n}\n"}], "negative_code": [], "src_uid": "a375dd323b7adbfa9f1cad9aa48f7247"} {"nl": {"description": "You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.You are a coach at a very large university and you know that $$$c$$$ of your students are coders, $$$m$$$ are mathematicians and $$$x$$$ have no specialization.What is the maximum number of full perfect teams you can distribute them into? Note that some students can be left without a team and each student can be a part of no more than one team.You are also asked to answer $$$q$$$ independent queries.", "input_spec": "The first line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of queries. Each of the next $$$q$$$ lines contains three integers $$$c$$$, $$$m$$$ and $$$x$$$ ($$$0 \\le c, m, x \\le 10^8$$$) — the number of coders, mathematicians and students without any specialization in the university, respectively. Note that the no student is both coder and mathematician at the same time. ", "output_spec": "Print $$$q$$$ integers — the $$$i$$$-th of them should be the answer to the $$$i$$$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into. ", "sample_inputs": ["6\n1 1 1\n3 6 0\n0 0 0\n0 1 1\n10 1 10\n4 4 1"], "sample_outputs": ["1\n3\n0\n0\n1\n3"], "notes": "NoteIn the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians. "}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return read.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint q = rint;\n\tforeach(_; 0 .. q){\n\t\tlong c = rlong, m = rlong, x = rlong;\n\t\tlong total = c + m + x;\n\t\tlong ans = min(c, m, total / 3);\n\t\tans.writeln;\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto q = RD!int;\n\tauto ans = new long[](q);\n\tforeach (i; 0..q)\n\t{\n\t\tauto c = RD;\n\t\tauto m = RD;\n\t\tauto x = RD;\n\t\tauto a = c + m + x;\n\t\tans[i] = min(min(c, m), a/3);\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "b18dac401b655c06bee331e71eb3e4de"} {"nl": {"description": "Recall that a permutation of length $$$n$$$ is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once.For a fixed positive integer $$$d$$$, let's define the cost of the permutation $$$p$$$ of length $$$n$$$ as the number of indices $$$i$$$ $$$(1 \\le i < n)$$$ such that $$$p_i \\cdot d = p_{i + 1}$$$.For example, if $$$d = 3$$$ and $$$p = [5, 2, 6, 7, 1, 3, 4]$$$, then the cost of such a permutation is $$$2$$$, because $$$p_2 \\cdot 3 = p_3$$$ and $$$p_5 \\cdot 3 = p_6$$$.Your task is the following one: for a given value $$$n$$$, find the permutation of length $$$n$$$ and the value $$$d$$$ with maximum possible cost (over all ways to choose the permutation and $$$d$$$). If there are multiple answers, then print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 500$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the value $$$d$$$ in the first line, and $$$n$$$ integers in the second line — the permutation itself. If there are multiple answers, then print any of them.", "sample_inputs": ["2\n\n2\n\n3"], "sample_outputs": ["2\n1 2\n3\n2 1 3"], "notes": null}, "positive_code": [{"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n foreach(_; 0..t)\r\n {\r\n int n;\r\n scanf(\"%d\", &n);\r\n int[] arr = [1];\r\n bool[int] set;\r\n set[1] = true;\r\n for (int i = 2; i <= n; i++) {\r\n for (int j = 0; i * 2 ^^ j <= n; j++)\r\n if ((i * 2 ^^ j in set) == null) {\r\n arr ~= i * 2 ^^ j;\r\n set[i * 2 ^^ j] = true;\r\n }\r\n }\r\n writeln(2);\r\n writefln(\"%(%s %)\", arr);\r\n }\r\n}\r\n"}, {"source_code": "// cheese-cracker [2022-07-08]\n\nvoid solve(){\n int n = scan!int;\n auto seen = new bool[](n+1);\n int d = 2;\n writeln(d);\n for(int i = 1; i <= n; ++i){\n if(!seen[i]){\n int num = i;\n while(num <= n){\n seen[num] = 1;\n write(num, \" \");\n num *= 2;\n }\n }\n }\n writeln;\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) solve;\n}\n\n/*_________________________*That's All Folks!*__________________________*/\nimport std.stdio, std.range, std.conv, std.typecons, std.algorithm;\nimport std.container, std.math, std.numeric, std.functional;\nstring[] tk; alias tup = Tuple!(long, long);\nT scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\n"}], "negative_code": [], "src_uid": "3b9380ca571dbf3e24fc1b1c8b91790b"} {"nl": {"description": "Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least , where ε < 10 - 7.To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help.", "input_spec": "First line consists of two space separated integers k, q (1 ≤ k, q ≤ 1000) — number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≤ pi ≤ 1000) — i-th query.", "output_spec": "Output q lines. On i-th of them output single integer — answer for i-th query.", "sample_inputs": ["1 1\n1", "2 2\n1\n2"], "sample_outputs": ["1", "2\n2"], "notes": null}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 2000;\nimmutable int half = limit / 2;\nimmutable int NA = -1;\n// immutable real eps = 1E-7 - 1E-17;\nimmutable real eps = 0;\n\nvoid main ()\n{\n\tint k, q;\n\twhile (readf (\" %s %s\", &k, &q) > 0)\n\t{\n\t\tauto t = new int [limit + 1];\n\t\tt[1..$] = NA;\n\t\tt[0] = 0;\n\n\t\tauto f = new real [] [] (2, k + 1);\n\t\tint b = 0;\n\t\tf[b][1..$] = 0.0;\n\t\tf[b][0] = 1.0;\n\t\treal invK = 1.0 / k;\n\t\tfor (int step = 1; t[half] == NA; step++)\n\t\t{\n\t\t\tb ^= 1;\n\t\t\tf[b][] = 0.0;\n\t\t\tforeach (i; 0..k + 1)\n\t\t\t{\n\t\t\t\tf[b][i] += f[!b][i] * i * invK;\n\t\t\t}\n\t\t\tforeach (i; 0..k)\n\t\t\t{\n\t\t\t\tf[b][i + 1] += f[!b][i] * (k - i) * invK;\n\t\t\t}\n\t\t\tint cur = cast (int) ((f[b][k] + eps) * limit);\n\t\t\tdebug {writeln (step, \": \", f[b][k]);}\n\t\t\twhile (t[cur] == NA)\n\t\t\t{\n\t\t\t\tt[cur] = step;\n\t\t\t\tcur -= 1;\n\t\t\t}\n\t\t}\n\n\t\tforeach (j; 0..q)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\twriteln (t[x]);\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 2000;\nimmutable int half = limit / 2;\nimmutable int NA = -1;\nimmutable real eps = 1E-7 - 1E-17;\n\nvoid main ()\n{\n\tint k, q;\n\twhile (readf (\" %s %s\", &k, &q) > 0)\n\t{\n\t\tauto t = new int [limit + 1];\n\t\tt[1..$] = NA;\n\t\tt[0] = 0;\n\n\t\tauto f = new real [] [] (2, k + 1);\n\t\tint b = 0;\n\t\tf[b][1..$] = 0.0;\n\t\tf[b][0] = 1.0;\n\t\treal invK = 1.0 / k;\n\t\tfor (int step = 1; t[half] == NA; step++)\n\t\t{\n\t\t\tb ^= 1;\n\t\t\tf[b][] = 0.0;\n\t\t\tforeach (i; 0..k + 1)\n\t\t\t{\n\t\t\t\tf[b][i] += f[!b][i] * i * invK;\n\t\t\t}\n\t\t\tforeach (i; 0..k)\n\t\t\t{\n\t\t\t\tf[b][i + 1] += f[!b][i] * (k - i) * invK;\n\t\t\t}\n\t\t\tint cur = cast (int) ((f[b][k] + eps) * limit);\n\t\t\tdebug {writeln (step, \": \", f[b][k]);}\n\t\t\twhile (t[cur] == NA)\n\t\t\t{\n\t\t\t\tt[cur] = step;\n\t\t\t\tcur -= 1;\n\t\t\t}\n\t\t}\n\n\t\tforeach (j; 0..q)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\twriteln (t[x]);\n\t\t}\n\t}\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int limit = 2000;\nimmutable int half = limit / 2;\nimmutable int NA = -1;\nimmutable real eps = 1E-7 - 1E-17;\n\nvoid main ()\n{\n\tint k, q;\n\twhile (readf (\" %s %s\", &k, &q) > 0)\n\t{\n\t\tauto t = new int [limit + 1];\n\t\tt[1..$] = NA;\n\t\tt[0] = 0;\n\n\t\tauto f = new real [] [] (2, k + 1);\n\t\tint b = 0;\n\t\tf[b][1..$] = 0.0;\n\t\tf[b][0] = 1.0;\n\t\treal invK = 1.0 / k;\n\t\tfor (int step = 1; t[half] == NA; step++)\n\t\t{\n\t\t\tb ^= 1;\n\t\t\tf[b][] = 0.0;\n\t\t\tforeach (i; 0..k + 1)\n\t\t\t{\n\t\t\t\tf[b][i] += f[!b][i] * i * invK;\n\t\t\t}\n\t\t\tforeach (i; 0..k)\n\t\t\t{\n\t\t\t\tf[b][i + 1] += f[!b][i] * (k - i) * invK;\n\t\t\t}\n\t\t\tint cur = cast (int) ((f[b][k] - eps) * limit);\n\t\t\tdebug {writeln (step, \": \", f[b][k]);}\n\t\t\twhile (t[cur] == NA)\n\t\t\t{\n\t\t\t\tt[cur] = step;\n\t\t\t\tcur -= 1;\n\t\t\t}\n\t\t}\n\n\t\tforeach (j; 0..q)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\twriteln (t[x]);\n\t\t}\n\t}\n}\n"}], "src_uid": "a2b71d66ea1fdc3249e37be3ab0e67ef"} {"nl": {"description": "Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty — a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). Makes has chosen n problems to solve on Decoforces with difficulties a1, a2, ..., an. He can solve these problems in arbitrary order. Though he can solve problem i with difficulty ai only if he had already solved some problem with difficulty (no matter on what online judge was it).Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty k.With given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list. For every positive integer y there exist some problem with difficulty y on at least one judge besides Decoforces.Makes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another.Makes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.", "input_spec": "The first line contains two integer numbers n, k (1 ≤ n ≤ 103, 1 ≤ k ≤ 109). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.", "sample_inputs": ["3 3\n2 1 9", "4 20\n10 3 6 3"], "sample_outputs": ["1", "0"], "notes": "NoteIn the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3.In the second example he can solve every problem right from the start."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1].to!long * 2;\n auto A = readln.split.map!(to!long).array;\n A.sort();\n\n long ans = 0;\n foreach (i; 0..N) {\n while (K < A[i]) K *= 2, ans += 1;\n K = max(K, A[i] * 2);\n }\n\n ans.writeln;\n}\n"}, {"source_code": "import std.stdio, std.string, std.algorithm, std.conv, std.array;\n\nvoid main(){\n\tauto nk=readln.strip.split.map!(to!int).array;\n\tint n=nk[0],k=nk[1];\n\tauto a=readln.strip.split.map!(to!int).array;\n\tsort(a);\n\tint cur=k,r=0;\n\tforeach(x;a){\n\t\twhile(cur<(x+1)/2){\n\t\t\tr++;\n\t\t\tcur*=2;\n\t\t}\n\t\tcur=max(cur,x);\n\t}\n\twriteln(r);\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1].to!long * 2;\n auto A = readln.split.map!(to!long).array;\n A.sort();\n auto X = A[$-1];\n\n foreach (i; 0..N) {\n if (A[i] <= K) K = max(K, A[i] * 2);\n }\n\n long ans = 0;\n while (K <= X) K *= 4, ans += 1;\n\n ans.writeln;\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1].to!long * 2;\n auto A = readln.split.map!(to!long).array;\n A.sort();\n auto X = A[$-1];\n\n foreach (i; 0..N) {\n if (A[i] <= K) K = max(K, A[i] * 2);\n }\n\n long ans = 0;\n while (K < X) K *= 2, ans += 1;\n\n ans.writeln;\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1].to!long * 2;\n auto A = readln.split.map!(to!long).array;\n A.sort();\n auto X = A[$-1];\n\n foreach (i; 0..N) {\n if (A[i] <= K) K = max(K, A[i] * 2);\n }\n\n long ans = 0;\n while (K < X) K *= 4, ans += 1;\n\n ans.writeln;\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1].to!long * 2;\n auto A = readln.split.map!(to!long).array;\n A.sort();\n auto X = A[$-1];\n\n foreach (i; 0..N) {\n if (A[i] <= K) K = max(K, A[i] * 2);\n }\n\n long ans = 0;\n while (K <= X) K *= 2, ans += 1;\n\n ans.writeln;\n}\n"}, {"source_code": "import std.stdio, std.string, std.algorithm, std.conv, std.array;\n\nvoid main(){\n\tauto nk=readln.strip.split.map!(to!int).array;\n\tint n=nk[0],k=nk[1];\n\tauto a=readln.strip.split.map!(to!int).array;\n\tsort(a);\n\tint cur=k,r=0;\n\tforeach(x;a){\n\t\twhile(cur<(x+1)/2){\n\t\t\tr++;\n\t\t\tcur=cur*2+1;\n\t\t}\n\t\tcur=max(cur,x);\n\t}\n\twriteln(r);\n}\n"}], "src_uid": "adc5a98cef418d9bbf40e5772c02ef43"} {"nl": {"description": "Let's call left cyclic shift of some string $$$t_1 t_2 t_3 \\dots t_{n - 1} t_n$$$ as string $$$t_2 t_3 \\dots t_{n - 1} t_n t_1$$$.Analogically, let's call right cyclic shift of string $$$t$$$ as string $$$t_n t_1 t_2 t_3 \\dots t_{n - 1}$$$.Let's say string $$$t$$$ is good if its left cyclic shift is equal to its right cyclic shift.You are given string $$$s$$$ which consists of digits 0–9.What is the minimum number of characters you need to erase from $$$s$$$ to make it good?", "input_spec": "The first line contains single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test cases — one per line. The first and only line of each test case contains string $$$s$$$ ($$$2 \\le |s| \\le 2 \\cdot 10^5$$$). Each character $$$s_i$$$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the minimum number of characters you need to erase from $$$s$$$ to make it good.", "sample_inputs": ["3\n95831\n100120013\n252525252525"], "sample_outputs": ["3\n5\n0"], "notes": "NoteIn the first test case, you can erase any $$$3$$$ characters, for example, the $$$1$$$-st, the $$$3$$$-rd, and the $$$4$$$-th. You'll get string 51 and it is good.In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.In the third test case, the given string $$$s$$$ is already good."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable char [] digits = \"0123456789\";\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto s = readln.strip;\n\t\tint res = 0;\n\t\tforeach (char c; digits)\n\t\t{\n\t\t\tres = max (res, s.count (c).to !(int));\n\t\t}\n\t\tforeach (char c; digits)\n\t\t{\n\t\t\tforeach (char d; digits)\n\t\t\t{\n\t\t\t\tint pos = 0;\n\t\t\t\tint cur = 0;\n\t\t\t\twhile (true)\n\t\t\t\t{\n\t\t\t\t\twhile (pos < s.length && s[pos] != c)\n\t\t\t\t\t{\n\t\t\t\t\t\tpos += 1;\n\t\t\t\t\t}\n\t\t\t\t\tpos += 1;\n\t\t\t\t\twhile (pos < s.length && s[pos] != d)\n\t\t\t\t\t{\n\t\t\t\t\t\tpos += 1;\n\t\t\t\t\t}\n\t\t\t\t\tif (pos >= s.length)\n\t\t\t\t\t{\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tpos += 1;\n\t\t\t\t\tcur += 2;\n\t\t\t\t}\n\t\t\t\tres = max (res, cur);\n\t\t\t}\n\t\t}\n\t\twriteln (s.length - res);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto s = RD!string;\n\t\tint n = cast(int)s.length;\n\t\tauto a = new int[](s.length);\n\t\tauto cnt = new int[](10);\n\t\tforeach (i; 0..s.length)\n\t\t{\n\t\t\ta[i] = s[i] - '0';\n\t\t\t++cnt[a[i]];\n\t\t}\n\t\tint best;\n\t\tforeach (i; 0..10)\n\t\t\tbest.chmax(cnt[i]);\n\t\tans[ti] = n - best;\n\t\t\n\t\tforeach (i; 0..10)\n\t\t{\n\t\t\tforeach (j; i+1..10)\n\t\t\t{\n\t\t\t\tint last = -1;\n\t\t\t\tint c;\n\t\t\t\tforeach (e; a)\n\t\t\t\t{\n\t\t\t\t\tif (last != e && (e == i || e == j))\n\t\t\t\t\t{\n\t\t\t\t\t\t++c;\n\t\t\t\t\t\tlast = e;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (c % 2)\n\t\t\t\t\t--c;\n\t\t\t\tans[ti].chmin(n - c);\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "977db81b5c1d3725e384e8f093655172"} {"nl": {"description": "This is an interactive problem.Note: the XOR-sum of an array $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) is defined as $$$a_1 \\oplus a_2 \\oplus \\ldots \\oplus a_n$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation.Little Dormi received an array of $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ for Christmas. However, while playing with it over the winter break, he accidentally dropped it into his XOR machine, and the array got lost.The XOR machine is currently configured with a query size of $$$k$$$ (which you cannot change), and allows you to perform the following type of query: by giving the machine $$$k$$$ distinct indices $$$x_1, x_2, \\ldots, x_k$$$, it will output $$$a_{x_1} \\oplus a_{x_2} \\oplus \\ldots \\oplus a_{x_k}$$$.As Little Dormi's older brother, you would like to help him recover the XOR-sum of his array $$$a_1, a_2, \\ldots, a_n$$$ by querying the XOR machine.Little Dormi isn't very patient, so to be as fast as possible, you must query the XOR machine the minimum number of times to find the XOR-sum of his array. Formally, let $$$d$$$ be the minimum number of queries needed to find the XOR-sum of any array of length $$$n$$$ with a query size of $$$k$$$. Your program will be accepted if you find the correct XOR-sum in at most $$$d$$$ queries.Lastly, you also noticed that with certain configurations of the machine $$$k$$$ and values of $$$n$$$, it may not be possible to recover the XOR-sum of Little Dormi's lost array. If that is the case, you should report it as well.The array $$$a_1, a_2, \\ldots, a_n$$$ is fixed before you start querying the XOR machine and does not change with the queries.", "input_spec": "The only line of input contains the integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 500$$$, $$$1 \\le k \\le n$$$), the length of the lost array and the configured query size of the XOR machine. Elements of the original array satisfy $$$1 \\le a_i \\le 10^9$$$. It can be proven that that if it is possible to recover the XOR sum under the given constraints, it can be done in at most $$$500$$$ queries. That is, $$$d \\le 500$$$. After taking $$$n$$$ and $$$k$$$, begin interaction.", "output_spec": "If it is impossible to recover the XOR-sum of the array, output $$$-1$$$ immediately after taking $$$n$$$ and $$$k$$$. Do not begin interaction. Otherwise, when your program finds the XOR-sum of the lost array $$$a_1, a_2, \\ldots, a_n$$$, report the answer in the following format: \"! x\", where $$$x$$$ is the XOR sum of the array $$$a_1, a_2, \\ldots, a_n$$$, and terminate your program normally immediately after flushing the output stream. Note that answering does not count as a query.", "sample_inputs": ["5 3\n\n4\n\n0\n\n1", "3 2"], "sample_outputs": ["? 1 2 3\n\n? 2 3 5\n\n? 4 1 5\n\n! 7", "-1"], "notes": "NoteIn the first example interaction, the array $$$a_1, a_2, \\ldots, a_n$$$ is $$$2, 1, 7, 5, 6$$$ and its XOR-sum is $$$7$$$. The first query made asks for indices $$$1,2,3$$$, so the response is $$$a_1 \\oplus a_2 \\oplus a_3 = 2 \\oplus 1 \\oplus 7 = 4$$$.The second query made asks for indices $$$2,3,5$$$, so the response is $$$a_2 \\oplus a_3 \\oplus a_5 = 1 \\oplus 7 \\oplus 6 = 0$$$.The third query made asks for indices $$$4,1,5$$$, so the response is $$$a_4 \\oplus a_1 \\oplus a_5 = 5 \\oplus 2 \\oplus 6 = 1$$$. Note that the indices may be output in any order.Additionally, even though three queries were made in the example interaction, it is just meant to demonstrate the interaction format and does not necessarily represent an optimal strategy.In the second example interaction, there is no way to recover the XOR-sum of Little Dormi's array no matter what is queried, so the program immediately outputs $$$-1$$$ and exits."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nimmutable int limit = 500;\r\n\r\nalias Plan = Tuple !(int, q{x}, int, q{y}, int, q{p}, int, q{q}, int, q{r});\r\n\r\nint [] [] go (int n, int k, Plan plan)\r\n{\r\n\tauto nv = plan.r + n + 2;\r\n\tauto start = nv - 2;\r\n\tauto finish = nv - 1;\r\n\r\n\tauto c = new int [] [] (nv, nv);\r\n\tforeach (i; 0..plan.r)\r\n\t{\r\n\t\tforeach (j; 0..n)\r\n\t\t{\r\n\t\t\tc[i + n][j] = 1;\r\n\t\t}\r\n\t}\r\n\tforeach (i; 0..plan.r)\r\n\t{\r\n\t\tc[start][i + n] = k;\r\n\t}\r\n\tforeach (j; 0..n)\r\n\t{\r\n\t\tc[j][finish] = (j < plan.x) ? plan.p : plan.q;\r\n\t}\r\n\r\n\tauto vis = new bool [nv];\r\n\r\n\tbool flow (int v)\r\n\t{\r\n\t\tif (v == finish)\r\n\t\t{\r\n\t\t\treturn true;\r\n\t\t}\r\n\t\tif (vis[v])\r\n\t\t{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\tvis[v] = true;\r\n\t\tforeach_reverse (i; 0..nv)\r\n\t\t{\r\n\t\t\tif (c[v][i] > 0 && flow (i))\r\n\t\t\t{\r\n\t\t\t\tc[v][i] -= 1;\r\n\t\t\t\tc[i][v] += 1;\r\n\t\t\t\treturn true;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn false;\r\n\t}\r\n\r\n\tforeach (step; 0..plan.r * k)\r\n\t{\r\n\t\tvis[start] = false;\r\n\t\tvis[finish] = false;\r\n\t\tif (!flow (start))\r\n\t\t{\r\n\t\t\tvis[] = false;\r\n\t\t\tif (!flow (start))\r\n\t\t\t{\r\n\t\t\t\tassert (false);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tint [] [] res;\r\n\tforeach (i; 0..plan.r)\r\n\t{\r\n\t\tres ~= n.iota.filter !(j => c[j][i + n] != 0).array;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tint n, k;\r\n\twhile (readf !(\" %s %s\") (n, k) > 0)\r\n\t{\r\n\t\treadln;\r\n\r\n\t\tPlan res;\r\n\t\tres.r = int.max;\r\n\t\tfor (int y = 1; y <= n; y += 1)\r\n\t\t{\r\n\t\t\tint x = n - y;\r\n\t\t\tfor (int p = 1; p <= limit; p += 2)\r\n\t\t\t{\r\n\t\t\t\tfor (int q = p; q <= limit; q += 2)\r\n\t\t\t\t{\r\n\t\t\t\t\tint value = x * p + y * q;\r\n\t\t\t\t\tif (value % k != 0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tint r = value / k;\r\n\t\t\t\t\tif (r < p || r < q)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tassert (x * p + y * q == r * k);\r\n\t\t\t\t\tif (res.r > r)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tres = Plan (x, y, p, q, r);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (res.r == int.max)\r\n\t\t{\r\n\t\t\twriteln (-1);\r\n\t\t\tstdout.flush ();\r\n\t\t\tcontinue;\r\n\t\t}\r\n\r\n\t\tdebug {writeln (res);}\r\n\r\n\t\tint ask (int [] a)\r\n\t\t{\r\n\t\t\twritefln !(\"? %(%s %)\") (a.map !(q{a + 1}));\r\n\t\t\tstdout.flush ();\r\n\r\n\t\t\tauto temp = readln.strip.to !(int);\r\n\t\t\treturn temp;\r\n\t\t}\r\n\r\n\t\tauto answer = go (n, k, res);\r\n\t\tint total = 0;\r\n\t\tforeach (ref line; answer)\r\n\t\t{\r\n\t\t\ttotal ^= ask (line);\r\n\t\t}\r\n\r\n\t\twriteln (\"! \", total);\r\n\t\tstdout.flush ();\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "5de2777a63c0c889da36c32df91744cf"} {"nl": {"description": "Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. Recently Xenia has bought $$$n_r$$$ red gems, $$$n_g$$$ green gems and $$$n_b$$$ blue gems. Each of the gems has a weight.Now, she is going to pick three gems.Xenia loves colorful things, so she will pick exactly one gem of each color.Xenia loves balance, so she will try to pick gems with little difference in weight.Specifically, supposing the weights of the picked gems are $$$x$$$, $$$y$$$ and $$$z$$$, Xenia wants to find the minimum value of $$$(x-y)^2+(y-z)^2+(z-x)^2$$$. As her dear friend, can you help her?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t \\le 100$$$)  — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$n_r,n_g,n_b$$$ ($$$1\\le n_r,n_g,n_b\\le 10^5$$$)  — the number of red gems, green gems and blue gems respectively. The second line of each test case contains $$$n_r$$$ integers $$$r_1,r_2,\\ldots,r_{n_r}$$$ ($$$1\\le r_i \\le 10^9$$$)  — $$$r_i$$$ is the weight of the $$$i$$$-th red gem. The third line of each test case contains $$$n_g$$$ integers $$$g_1,g_2,\\ldots,g_{n_g}$$$ ($$$1\\le g_i \\le 10^9$$$)  — $$$g_i$$$ is the weight of the $$$i$$$-th green gem. The fourth line of each test case contains $$$n_b$$$ integers $$$b_1,b_2,\\ldots,b_{n_b}$$$ ($$$1\\le b_i \\le 10^9$$$)  — $$$b_i$$$ is the weight of the $$$i$$$-th blue gem. It is guaranteed that $$$\\sum n_r \\le 10^5$$$, $$$\\sum n_g \\le 10^5$$$, $$$\\sum n_b \\le 10^5$$$ (the sum for all test cases).", "output_spec": "For each test case, print a line contains one integer  — the minimum value which Xenia wants to find. ", "sample_inputs": ["5\n2 2 3\n7 8\n6 3\n3 1 4\n1 1 1\n1\n1\n1000000000\n2 2 2\n1 2\n5 4\n6 7\n2 2 2\n1 2\n3 4\n6 7\n3 4 1\n3 2 1\n7 3 3 4\n6"], "sample_outputs": ["14\n1999999996000000002\n24\n24\n14"], "notes": "NoteIn the first test case, Xenia has the following gems:If she picks the red gem with weight $$$7$$$, the green gem with weight $$$6$$$, and the blue gem with weight $$$4$$$, she will achieve the most balanced selection with $$$(x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14$$$."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int t;\n readf(\"%s\", &t);\n readln;\n\n while (t--) {\n auto lens = readln.chomp.split.map!(to!int).array;\n \n int[][] arr;\n foreach (i; 0 .. 3) {\n arr ~= readln.chomp.split.map!(to!int).array;\n arr[i].sort();\n }\n \n ulong ans = ulong.max;\n foreach (ps; iota(3).permutations) {\n int smallidx = 0, bigidx = 0;\n foreach (e; arr[ps[0]]) {\n while (smallidx + 1 < lens[ps[1]] && arr[ps[1]][smallidx + 1] <= e) { ++smallidx; }\n while (bigidx < lens[ps[2]] && arr[ps[2]][bigidx] < e) { ++bigidx; }\n\n if (bigidx == lens[ps[2]]) { continue; }\n \n auto sm = arr[ps[1]][smallidx];\n auto bg = arr[ps[2]][bigidx];\n auto cur = (e.to!ulong - sm)^^2 + (sm.to!ulong - bg)^^2 + (bg.to!ulong - e)^^2;\n \n ans = min(ans, cur);\n }\n }\n \n ans.writeln;\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int total = 3;\n\nvoid main ()\n{\n\tint tests;\n\treadf !(\" %s\") (tests);\n\tforeach (test; 0..tests)\n\t{\n\t\tint [total] n;\n\t\tforeach (ref c; n)\n\t\t{\n\t\t\treadf !(\" %s\") (c);\n\t\t}\n\t\treadln;\n\t\tint [] [total] a;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tc = readln.splitter.map !(to !(int)).array;\n\t\t\tsort (c);\n\t\t}\n\t\tlong res = long.max;\n\t\tforeach (i; 0..total)\n\t\t{\n\t\t\tforeach (j; 0..total)\n\t\t\t{\n\t\t\t\tif (j != i)\n\t\t\t\t{\n\t\t\t\t\tauto k = total - i - j;\n\t\t\t\t\tint v = 0;\n\t\t\t\t\tint w = 0;\n\t\t\t\t\tforeach (u; 0..n[i])\n\t\t\t\t\t{\n\t\t\t\t\t\twhile (v < n[j] &&\n\t\t\t\t\t\t a[j][v] <= a[i][u])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tv += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (v > 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tv -= 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\twhile (w + 1 < n[k] &&\n\t\t\t\t\t\t a[k][w] < a[j][v])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tw += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tres = min (res,\n\t\t\t\t\t\t (cast (long)\n\t\t\t\t\t\t (a[i][u] - a[j][v])) ^^ 2 +\n\t\t\t\t\t\t (cast (long)\n\t\t\t\t\t\t (a[j][v] - a[k][w])) ^^ 2 +\n\t\t\t\t\t\t (cast (long)\n\t\t\t\t\t\t (a[k][w] - a[i][u])) ^^ 2);\n\t \t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n auto N = new int[3];\n foreach (h; 0 .. 3) {\n N[h] = readInt();\n }\n auto A = new long[][3];\n foreach (h; 0 .. 3) {\n A[h] = new long[N[h]];\n foreach (i; 0 .. N[h]) {\n A[h][i] = readLong();\n }\n A[h].sort;\n }\n \n long ans = long.max;\n \n void check(long x, long y, long z) {\n debug {\n writeln(\"check \", x, \" \", y, \" \", z);\n }\n const score = (y - z)^^2 + (z - x)^^2 + (x - y)^^2;\n chmin(ans, score);\n }\n void checkH(int h, long y, long z) {\n const pos = A[h].upperBound((y + z) / 2);\n if (pos < N[h]) {\n check(A[h][pos], y, z);\n }\n if (pos - 1 >= 0) {\n check(A[h][pos - 1], y, z);\n }\n }\n \n foreach (h; 0 .. 3) {\n const h1 = (h + 1) % 3;\n const h2 = (h + 2) % 3;\n for (int i1 = 0, i2 = 0; i1 < N[h1] && i2 < N[h2]; ) {\n debug {\n writeln(h1, \" \", h2, \"; \", i1, \" \", i2);\n }\n checkH(h, A[h1][i1], A[h2][i2]);\n if (A[h1][i1] < A[h2][i2]) {\n ++i1;\n } else {\n ++i2;\n }\n }\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\n\nfinal class InputReader {\n private:\n ubyte[] p, buffer;\n bool eof;\n bool rawRead () {\n if (eof) {\n return false;\n }\n p = stdin.rawRead (buffer);\n if (p.empty) {\n eof = true;\n return false;\n }\n return true;\n }\n ubyte nextByte(bool check) () {\n static if (check) {\n if (p.empty) {\n if (!rawRead ()) {\n return 0;\n }\n }\n }\n auto r = p.front;\n p.popFront ();\n return r;\n }\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n }\n bool seekByte (in ubyte lo) {\n while (true) {\n p = p.find! (c => c >= lo);\n if (!p.empty) {\n return false;\n }\n if (!rawRead ()) {\n return true;\n }\n }\n }\n template next(T) if (isSigned!T) {\n T next () {\n if (seekByte (45)) {\n return 0;\n }\n T res;\n ubyte b = nextByte!false ();\n if (b == 45) {\n while (true) {\n b = nextByte!true ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte!true ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n T next () {\n if (seekByte (48)) {\n return 0;\n }\n T res = nextByte!false () - 48;\n while (true) {\n ubyte b = nextByte!true ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n T[] nextA(T) (in int n) {\n auto a = uninitializedArray!(T[]) (n);\n foreach (i; 0 .. n) {\n a[i] = next!T;\n }\n return a;\n }\n}\n\nvoid main() {\n auto r = new InputReader ();\n immutable nt = r.next!uint ();\n int[] ra (int n) {\n auto a = r.nextA!int (n);\n sort (a);\n return a;\n }\n foreach (tid; 0 .. nt) {\n long res = long.max;\n auto a = r.nextA!uint (3);\n auto t = new int[][3];\n foreach (k; 0 .. 3) t[k] = ra (a[k]);\n void check (long u, long v, long w) {\n long q = (u - v) * (u - v);\n q += (u - w) * (u - w);\n q += (v - w) * (v - w);\n if (res > q) res = q;\n }\n void f (in int[] x, in int[] y, in int[] z) {\n size_t j, k;\n foreach (i; 0 .. x.length) {\n int xv = x[i];\n while (j < y.length && y[j] < xv) ++j; \n while (k < z.length && z[k] < xv) ++k; \n void go (size_t u, size_t v) {\n if (u < y.length && v < z.length)\n check (xv, y[u], z[v]);\n }\n go (j, k);\n if (k > 0) {\n go (j, k - 1);\n }\n if (j > 0) {\n go (j - 1, k);\n if (k > 0) {\n go (j - 1, k - 1);\n }\n }\n }\n }\n f (t[0], t[1], t[2]);\n f (t[1], t[0], t[2]);\n f (t[2], t[0], t[1]);\n writeln (res);\n }\n}\n\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nT binarySearch(alias pred, T)(T ok, T ng)\n{ \n\twhile (abs(ok-ng) > 1)\n\t{\n\t\tauto mid = (ok+ng)/2;\n\t\tif (unaryFun!pred(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\treturn ok;\n}\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto nr = RD!int;\n\t\tauto ng = RD!int;\n\t\tauto nb = RD!int;\n\t\tauto r = RDA;\n\t\tauto g = RDA;\n\t\tauto b = RDA;\n\t\tr.sort();\n\t\tg.sort();\n\t\tb.sort();\n\t\tauto arr = new long[][](nr+ng+nb);\n\t\tforeach (i; 0..nr)\n\t\t{\n\t\t\tarr[i] = [r[i], 0];\n\t\t}\n\t\tforeach (i; 0..ng)\n\t\t{\n\t\t\tarr[nr+i] = [g[i], 1];\n\t\t}\n\t\tforeach (i; 0..nb)\n\t\t{\n\t\t\tarr[nr+ng+i] = [b[i], 2];\n\t\t}\n\t\tarr.sort!\"a[0] < b[0]\"();\n\n\t\tans[ti] = long.max;\n\t\tlong lr, lg, lb;\n\t\tlong search(in long[] _arr, long d)\n\t\t{\n\t\t\tauto p = binarySearch!((int a) => _arr[a] >= d)(cast(int)_arr.length-1, -1);\n\t\t\tlong res = _arr[p];\n\t\t\tif (p != 0)\n\t\t\t{\n\t\t\t\tif (abs(d - _arr[p-1]) < abs(res - d))\n\t\t\t\t\tres = _arr[p-1];\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\t\tlong eval(long rr, long gg, long bb)\n\t\t{\n\t\t\treturn (rr-gg)^^2 + (gg-bb)^^2 + (bb-rr)^^2;\n\t\t}\n\t\tforeach (i; 0..arr.length)\n\t\t{\n\t\t\tif (arr[i][1] == 0)\n\t\t\t{\n\t\t\t\tlr = arr[i][0];\n\t\t\t\tif (lg != 0 && lb != 0)\n\t\t\t\t{\n\t\t\t\t\tlong x;\n\t\t\t\t\tif (lg <= lb)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto d = (lr+lg+1) / 2;\n\t\t\t\t\t\tx = search(b, d);\n\t\t\t\t\t\tans[ti].chmin(eval(lr, lg, x));\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tauto d = (lr+lb+1) / 2;\n\t\t\t\t\t\tx = search(g, d);\n\t\t\t\t\t\tans[ti].chmin(eval(lr, x, lb));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (arr[i][1] == 1)\n\t\t\t{\n\t\t\t\tlg = arr[i][0];\n\t\t\t\tif (lr != 0 && lb != 0)\n\t\t\t\t{\n\t\t\t\t\tlong x;\n\t\t\t\t\tif (lr <= lb)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto d = (lr+lg+1) / 2;\n\t\t\t\t\t\tx = search(b, d);\n\t\t\t\t\t\tans[ti].chmin(eval(lr, lg, x));\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tauto d = (lg+lb+1) / 2;\n\t\t\t\t\t\tx = search(r, d);\n\t\t\t\t\t\tans[ti].chmin(eval(x, lg, lb));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlb = arr[i][0];\n\t\t\t\tif (lr != 0 && lg != 0)\n\t\t\t\t{\n\t\t\t\t\tlong x;\n\t\t\t\t\tif (lr <= lg)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto d = (lr+lb+1) / 2;\n\t\t\t\t\t\tx = search(g, d);\n\t\t\t\t\t\tans[ti].chmin(eval(lr, x, lb));\n\t\t\t\t\t\tdebug writeln(arr[i], \" d:\", d, \" x:\", x);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tauto d = (lg+lb+1) / 2;\n\t\t\t\t\t\tx = search(r, d);\n\t\t\t\t\t\tans[ti].chmin(eval(x, lg, lb));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int total = 3;\n\nvoid main ()\n{\n\tint tests;\n\treadf !(\" %s\") (tests);\n\tforeach (test; 0..tests)\n\t{\n\t\tint [total] n;\n\t\tforeach (ref c; n)\n\t\t{\n\t\t\treadf !(\" %s\") (c);\n\t\t}\n\t\treadln;\n\t\tint [] [total] a;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tc = readln.splitter.map !(to !(int)).array;\n\t\t\tsort (c);\n\t\t}\n\t\tlong res = long.max;\n\t\tforeach (i; 0..total)\n\t\t{\n\t\t\tforeach (j; 0..total) if (j != i)\n\t\t\t{\n\t\t\t\tauto k = total - i - j;\n\t\t\t\tint v = 0;\n\t\t\t\tint w = 0;\n\t\t\t\tforeach (u; 0..n[i])\n\t\t\t\t{\n\t\t\t\t\twhile (v + 1 < n[j] &&\n\t\t\t\t\t a[j][v] < a[i][u])\n\t\t\t\t\t{\n\t\t\t\t\t\tv += 1;\n\t\t\t\t\t}\n\t\t\t\t\twhile (w + 1 < n[k] &&\n\t\t\t\t\t a[k][w] < a[j][v])\n\t\t\t\t\t{\n\t\t\t\t\t\tw += 1;\n\t\t\t\t\t}\n\t\t\t\t\tres = min (res,\n\t\t\t\t\t (cast (long)\n\t\t\t\t\t (a[i][u] - a[j][v])) ^^ 2 +\n\t\t\t\t\t (cast (long)\n\t\t\t\t\t (a[j][v] - a[k][w])) ^^ 2 +\n\t\t\t\t\t (cast (long)\n\t\t\t\t\t (a[k][w] - a[i][u])) ^^ 2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "src_uid": "79b58eb781cd73ccf7994866b9a8b695"} {"nl": {"description": "Lee was planning to get closer to Mashtali's heart to proceed with his evil plan(which we're not aware of, yet), so he decided to beautify Mashtali's graph. But he made several rules for himself. And also he was too busy with his plans that he didn't have time for such minor tasks, so he asked you for help.Mashtali's graph is an undirected weighted graph with $$$n$$$ vertices and $$$m$$$ edges with weights equal to either $$$1$$$ or $$$2$$$. Lee wants to direct the edges of Mashtali's graph so that it will be as beautiful as possible.Lee thinks that the beauty of a directed weighted graph is equal to the number of its Oddysey vertices. A vertex $$$v$$$ is an Oddysey vertex if $$$|d^+(v) - d^-(v)| = 1$$$, where $$$d^+(v)$$$ is the sum of weights of the outgoing from $$$v$$$ edges, and $$$d^-(v)$$$ is the sum of the weights of the incoming to $$$v$$$ edges.Find the largest possible beauty of a graph that Lee can achieve by directing the edges of Mashtali's graph. In addition, find any way to achieve it.Note that you have to orient each edge.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 \\le n \\le 10^5;\\; 1 \\le m \\le 10^5)$$$ — the numbers of vertices and edges in the graph. The $$$i$$$-th line of the following $$$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;\\; \\bf{w_i \\in \\{1, 2\\}} )$$$ — the endpoints of the $$$i$$$-th edge and its weight. Note that the graph doesn't have to be connected, and it might contain multiple edges.", "output_spec": "In the first line print a single integer — the maximum beauty of the graph Lee can achieve. In the second line print a string of length $$$m$$$ consisting of $$$1$$$s and $$$2$$$s — directions of the edges. If you decide to direct the $$$i$$$-th edge from vertex $$$u_i$$$ to vertex $$$v_i$$$, $$$i$$$-th character of the string should be $$$1$$$. Otherwise, it should be $$$2$$$.", "sample_inputs": ["6 7\n1 2 1\n1 3 2\n2 3 2\n1 4 1\n4 5 1\n2 5 2\n2 6 2", "6 7\n1 2 2\n1 3 2\n2 3 2\n1 4 2\n4 5 2\n2 5 2\n2 6 2", "6 7\n1 2 1\n1 3 1\n2 3 1\n1 4 1\n4 5 1\n2 5 1\n2 6 1"], "sample_outputs": ["2\n1212212", "0\n1212212", "2\n1212212"], "notes": "NoteExplanation for the first sample: vertices $$$2$$$ and $$$5$$$ are Oddyseys. Explanation for the third sample: vertices $$$1$$$ and $$$6$$$ are Oddyseys. "}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nuint xrand() {\n static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;\n uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;\n}\n\n\nint N, M;\nint[] A, B, W;\n\nalias Edge = Tuple!(int, \"i\", int, \"v\");\nEdge[][][] G;\n\nint[][] uss, iss;\nint[] see;\nbool[] used;\nvoid dfs(int w, int u) {\n for (; see[u] < G[w][u].length; ) {\n const i = G[w][u][see[u]].i;\n const v = G[w][u][see[u]].v;\n ++see[u];\n if (!used[i]) {\n used[i] = true;\n debug {\n // writefln(\"euler(%s); %s -> %s\", w, u, v);\n }\n dfs(w, v);\n iss[w] ~= i;\n }\n }\n uss[w] ~= u;\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n W = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n W[i] = readInt();\n }\n \n auto deg = new int[][](3, N);\n foreach (i; 0 .. M) {\n ++deg[W[i]][A[i]];\n ++deg[W[i]][B[i]];\n }\n \n G = new Edge[][][](3, N + 1);\n foreach (i; 0 .. M) {\n G[W[i]][A[i]] ~= Edge(i, B[i]);\n G[W[i]][B[i]] ~= Edge(i, A[i]);\n }\n foreach (w; [1, 2]) {\n foreach (u; 0 .. N) {\n if (deg[w][u] & 1) {\n G[w][u] ~= Edge(M + u, N);\n G[w][N] ~= Edge(M + u, u);\n }\n }\n }\n \n uss = new int[][](3);\n iss = new int[][](3);\n foreach (w; [1, 2]) {\n see = new int[N + 1];\n used = new bool[M + N];\n foreach_reverse (u; 0 .. N + 1) {\n dfs(w, u);\n iss[w] ~= -1;\n }\n debug {\n writefln(\"uss[%s] = %s\", w, uss[w]);\n writefln(\"iss[%s] = %s\", w, iss[w]);\n }\n }\n \n alias Entry = Tuple!(int, \"to\", int, \"j\", int, \"k\");\n auto ess = new Entry[][](3, N);\n foreach (w; [1, 2]) {\n ess[w][] = Entry(-1, -1, -1);\n const len = cast(int)(iss[w].length);\n for (int j = 0, k; j < len; j = k) {\n for (k = j + 1; k < len && uss[w][k] != N; ++k) {}\n if (k == len) {\n break;\n }\n const u = uss[w][j + 1];\n const v = uss[w][k - 1];\n debug {\n writefln(\"waf %s; %s -> %s\", w, u, v);\n }\n ess[w][u] = Entry(v, j + 1, k - 1);\n ess[w][v] = Entry(u, k - 1, j + 1);\n }\n }\n \n auto ans = new char[M];\n ans[] = '?';\n foreach (w; [1, 2]) {\n const len = cast(int)(iss[w].length);\n foreach (j; 0 .. len - 1) {\n const i = iss[w][j];\n const u = uss[w][j];\n const v = uss[w][j + 1];\n if (0 <= i && i < M) {\n if (u == A[i] && v == B[i]) {\n ans[i] = '1';\n } else if (u == B[i] && v == A[i]) {\n ans[i] = '2';\n } else {\n assert(false);\n }\n }\n }\n }\n void flip(int w, int j, int k) {\n debug {\n writefln(\"flip %s [%s, %s]\", w, j, k);\n }\n if (j > k) {\n swap(j, k);\n }\n foreach (l; j .. k) {\n const i = iss[w][l];\n if (0 <= i && i < M) {\n ans[i] ^= '1' ^ '2';\n }\n }\n }\n \n auto vis = new bool[N];\n foreach (r; 0 .. N) if (!vis[r]) {\n foreach (w0; [1, 2]) {\n for (int w = w0, u = r; ; ) {\n vis[u] = true;\n const v = ess[w][u].to;\n const j = ess[w][u].j;\n const k = ess[w][u].k;\n if (!~v) {\n break;\n }\n ess[w][u] = ess[w][v] = Entry(-1, -1, -1);\n debug {\n writefln(\"%s %s; %s; %s -> %s [%s, %s]\", r, w0, w, u, v, j, k);\n }\n if ((w0 == 1) != (j < k)) {\n flip(w, j, k);\n }\n if (v == r) {\n break;\n }\n w ^= 1 ^ 2;\n u = v;\n }\n }\n }\n \n const score = deg[1].count!((a) => (a % 2 != 0));\n writeln(score);\n writeln(ans);\n \n auto vals = new int[N];\n foreach (i; 0 .. M) {\n final switch (ans[i]) {\n case '1': vals[A[i]] += W[i]; vals[B[i]] -= W[i]; break;\n case '2': vals[B[i]] += W[i]; vals[A[i]] -= W[i]; break;\n }\n }\n debug {\n writeln(\"vals = \", vals);\n }\n assert(score == vals.count!((a) => abs(a) == 1));\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nuint xrand() {\n static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;\n uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;\n}\n\n\nint N, M;\nint[] A, B, W;\n\nalias Edge = Tuple!(int, \"i\", int, \"v\");\nEdge[][][] G;\n\nint[][] uss, iss;\nint[] see;\nbool[] used;\nvoid dfs(int w, int u) {\n for (; see[u] < G[w][u].length; ) {\n const i = G[w][u][see[u]].i;\n const v = G[w][u][see[u]].v;\n ++see[u];\n if (!used[i]) {\n used[i] = true;\n debug {\n writefln(\"euler(%s); %s -> %s\", w, u, v);\n }\n dfs(w, v);\n iss[w] ~= i;\n }\n }\n uss[w] ~= u;\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n W = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n W[i] = readInt();\n }\n \n auto deg = new int[][](3, N);\n foreach (i; 0 .. M) {\n ++deg[W[i]][A[i]];\n ++deg[W[i]][B[i]];\n }\n \n G = new Edge[][][](3, N + 1);\n foreach (i; 0 .. M) {\n G[W[i]][A[i]] ~= Edge(i, B[i]);\n G[W[i]][B[i]] ~= Edge(i, A[i]);\n }\n foreach (w; [1, 2]) {\n foreach (u; 0 .. N) {\n if (deg[w][u] & 1) {\n G[w][u] ~= Edge(M + u, N);\n G[w][N] ~= Edge(M + u, u);\n }\n }\n }\n \n uss = new int[][](3);\n iss = new int[][](3);\n foreach (w; [1, 2]) {\n see = new int[N + 1];\n used = new bool[M + N];\n dfs(w, N);\n foreach (u; 0 .. N) {\n dfs(w, u);\n }\n debug {\n writefln(\"uss[%s] = %s\", w, uss[w]);\n writefln(\"iss[%s] = %s\", w, iss[w]);\n }\n }\n \n alias Entry = Tuple!(int, \"to\", int, \"j\", int, \"k\");\n auto ess = new Entry[][](3, N);\n foreach (w; [1, 2]) {\n ess[w][] = Entry(-1, -1, -1);\n const len = cast(int)(iss[w].length);\n for (int j = 0, k; j < len; j = k) {\n for (k = j + 1; k < len && uss[w][k] != N; ++k) {}\n if (uss[w][k] != N) {\n break;\n }\n const u = uss[w][j + 1];\n const v = uss[w][k - 1];\n debug {\n writefln(\"waf %s; %s -> %s\", w, u, v);\n }\n ess[w][u] = Entry(v, j + 1, k - 1);\n ess[w][v] = Entry(u, k - 1, j + 1);\n }\n }\n \n auto ans = new char[M];\n ans[] = '?';\n foreach (w; [1, 2]) {\n const len = cast(int)(iss[w].length);\n foreach (j; 0 .. len) {\n const i = iss[w][j];\n const u = uss[w][j];\n const v = uss[w][j + 1];\n if (i < M) {\n if (u == A[i] && v == B[i]) {\n ans[i] = '1';\n } else if (u == B[i] && v == A[i]) {\n ans[i] = '2';\n } else {\n assert(false);\n }\n }\n }\n }\n void flip(int w, int j, int k) {\n debug {\n writefln(\"flip %s [%s, %s]\", w, j, k);\n }\n if (j > k) {\n swap(j, k);\n }\n foreach (l; j .. k) {\n const i = iss[w][l];\n if (i < M) {\n ans[i] ^= '1' ^ '2';\n }\n }\n }\n \n auto vis = new bool[N];\n foreach (r; 0 .. N) if (!vis[r]) {\n foreach (w0; [1, 2]) {\n for (int w = w0, u = r; ; ) {\n vis[u] = true;\n const v = ess[w][u].to;\n const j = ess[w][u].j;\n const k = ess[w][u].k;\n debug {\n writefln(\"%s %s; %s -> %s [%s, %s]\", r, w0, u, v, j, k);\n }\n if (!~v) {\n break;\n }\n if ((w0 == 1) != (j < k)) {\n flip(w, j, k);\n }\n if (v == r) {\n break;\n }\n w ^= 1 ^ 2;\n u = v;\n }\n }\n }\n \n const score = deg[1].count!((a) => (a % 2 != 0));\n writeln(score);\n writeln(ans);\n \n auto vals = new int[N];\n foreach (i; 0 .. M) {\n final switch (ans[i]) {\n case '1': vals[A[i]] += W[i]; vals[B[i]] -= W[i]; break;\n case '2': vals[B[i]] += W[i]; vals[A[i]] -= W[i]; break;\n }\n }\n debug {\n writeln(\"vals = \", vals);\n }\n assert(score == vals.count!((a) => abs(a) == 1));\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N, M;\nint[] A, B, W;\n\nchar[] ans;\n\nalias Edge = Tuple!(int, \"i\", int, \"v\");\nEdge[][][] G;\n\nint[] poss;\nbool[] used;\nvoid euler(int w) {\n void dfs(int u) {\n for (; poss[u] < G[w][u].length; ) {\n const i = G[w][u][poss[u]].i;\n const v = G[w][u][poss[u]].v;\n ++poss[u];\n if (!used[i]) {\n used[i] = true;\n debug {\n writefln(\"euler(%s); %s -> %s\", w, u, v);\n }\n // u -> v\n if (i < M) {\n if (u == A[i] && v == B[i]) {\n ans[i] = '1';\n } else if (v == A[i] && u == B[i]) {\n ans[i] = '2';\n } else {\n assert(false);\n }\n }\n dfs(v);\n }\n }\n }\n poss = new int[N + 1];\n used = new bool[M + N];\n dfs(N);\n foreach (u; 0 .. N) {\n dfs(u);\n }\n debug {\n writefln(\"euler(%s); used = %s\", w, used);\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n W = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n W[i] = readInt();\n }\n \n auto deg = new int[][](3, N);\n foreach (i; 0 .. M) {\n ++deg[W[i]][A[i]];\n ++deg[W[i]][B[i]];\n }\n auto uss = new int[][4];\n foreach (u; 0 .. N) {\n int p;\n if (deg[1][u] & 1) p |= 1;\n if (deg[2][u] & 1) p |= 2;\n uss[p] ~= u;\n }\n debug {\n writeln(\"deg = \", deg);\n writeln(\"uss = \", uss);\n }\n G = new Edge[][][](3, N + 1);\n foreach (i; 0 .. M) {\n G[W[i]][A[i]] ~= Edge(i, B[i]);\n G[W[i]][B[i]] ~= Edge(i, A[i]);\n }\n foreach (w; [1, 2]) {\n bool s = (w == 2);\n foreach (u; uss[3] ~ uss[w]) {\n if (s) {\n G[w][N] ~= Edge(M + u, u);\n } else {\n G[w][u] ~= Edge(M + u, N);\n }\n s = !s;\n }\n }\n ans = new char[M];\n ans[] = '?';\n foreach (w; [1, 2]) {\n debug {\n writefln(\"G[%s] = %s\", w, G[w]);\n }\n euler(w);\n }\n \n writeln(uss[1].length + uss[3].length);\n writeln(ans);\n \n auto vals = new int[N];\n foreach (i; 0 .. M) {\n final switch (ans[i]) {\n case '1': vals[A[i]] += W[i]; vals[B[i]] -= W[i]; break;\n case '2': vals[B[i]] += W[i]; vals[A[i]] -= W[i]; break;\n }\n }\n debug {\n writeln(\"vals = \", vals);\n }\n assert(uss[1].length + uss[3].length == vals.count!((a) => abs(a) == 1));\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N, M;\nint[] A, B, W;\n\nchar[] ans;\n\nalias Edge = Tuple!(int, \"i\", int, \"v\");\nEdge[][][] G;\n\nint[] poss;\nbool[] used;\nvoid euler(int w) {\n void dfs(int u) {\n for (; poss[u] < G[w][u].length; ) {\n const i = G[w][u][poss[u]].i;\n const v = G[w][u][poss[u]].v;\n ++poss[u];\n if (!used[i]) {\n used[i] = true;\n dfs(v);\n // u -> v\n if (i < M) {\n if (u == A[i] && v == B[i]) {\n ans[i] = '1';\n } else if (v == A[i] && u == B[i]) {\n ans[i] = '2';\n } else {\n assert(false);\n }\n }\n }\n }\n }\n poss = new int[N + 1];\n used = new bool[M + N];\n foreach (u; 0 .. N + 1) {\n dfs(u);\n }\n debug {\n writefln(\"euler(%s); used = %s\", w, used);\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n W = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n W[i] = readInt();\n }\n \n auto deg = new int[][](3, N);\n foreach (i; 0 .. M) {\n ++deg[W[i]][A[i]];\n ++deg[W[i]][B[i]];\n }\n auto uss = new int[][4];\n foreach (u; 0 .. N) {\n int p;\n if (deg[1][u] & 1) p |= 1;\n if (deg[2][u] & 1) p |= 2;\n uss[p] ~= u;\n }\n debug {\n writeln(\"deg = \", deg);\n writeln(\"uss = \", uss);\n }\n G = new Edge[][][](3, N + 1);\n foreach (i; 0 .. M) {\n G[W[i]][A[i]] ~= Edge(i, B[i]);\n G[W[i]][B[i]] ~= Edge(i, A[i]);\n }\n foreach (w; [1, 2]) {\n bool s = (w == 2);\n foreach (u; uss[3] ~ uss[w]) {\n if (s) {\n G[w][N] ~= Edge(M + u, u);\n } else {\n G[w][u] ~= Edge(M + u, N);\n }\n s = !s;\n }\n }\n ans = new char[M];\n ans[] = '?';\n foreach (w; [1, 2]) {\n debug {\n writefln(\"G[%s] = %s\", w, G[w]);\n }\n euler(w);\n }\n \n writeln(uss[1].length + uss[3].length);\n writeln(ans);\n \n auto vals = new int[N];\n foreach (i; 0 .. M) {\n final switch (ans[i]) {\n case '1': vals[A[i]] += W[i]; vals[B[i]] -= W[i]; break;\n case '2': vals[B[i]] += W[i]; vals[A[i]] -= W[i]; break;\n }\n }\n debug {\n writeln(\"vals = \", vals);\n }\n assert(uss[1].length + uss[3].length == vals.count!((a) => abs(a) == 1));\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "ad3f798f00cc9f3e3c9df932c2459ca3"} {"nl": {"description": "Diamond Miner is a game that is similar to Gold Miner, but there are $$$n$$$ miners instead of $$$1$$$ in this game.The mining area can be described as a plane. The $$$n$$$ miners can be regarded as $$$n$$$ points on the y-axis. There are $$$n$$$ diamond mines in the mining area. We can regard them as $$$n$$$ points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point $$$(0, 0)$$$). Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point $$$(a,b)$$$ uses his hook to mine a diamond mine at the point $$$(c,d)$$$, he will spend $$$\\sqrt{(a-c)^2+(b-d)^2}$$$ energy to mine it (the distance between these points). The miners can't move or help each other.The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 10$$$) — 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 10^5$$$) — the number of miners and mines. Each of the next $$$2n$$$ lines contains two space-separated integers $$$x$$$ ($$$-10^8 \\le x \\le 10^8$$$) and $$$y$$$ ($$$-10^8 \\le y \\le 10^8$$$), which represent the point $$$(x,y)$$$ to describe a miner's or a diamond mine's position. Either $$$x = 0$$$, meaning there is a miner at the point $$$(0, y)$$$, or $$$y = 0$$$, meaning there is a diamond mine at the point $$$(x, 0)$$$. There can be multiple miners or diamond mines at the same point. It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to $$$n$$$ and the number of points on the y-axis is equal to $$$n$$$. It's guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print a single real number — the minimal sum of energy that should be spent. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-9}$$$.", "sample_inputs": ["3\n2\n0 1\n1 0\n0 -1\n-2 0\n4\n1 0\n3 0\n-5 0\n6 0\n0 3\n0 1\n0 2\n0 4\n5\n3 0\n0 4\n0 -3\n4 0\n2 0\n1 0\n-3 0\n0 -10\n0 -2\n0 -10"], "sample_outputs": ["3.650281539872885\n18.061819283610362\n32.052255376143336"], "notes": "NoteIn the first test case, the miners are at $$$(0,1)$$$ and $$$(0,-1)$$$, while the diamond mines are at $$$(1,0)$$$ and $$$(-2,0)$$$. If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy $$$\\sqrt2 + \\sqrt5$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new double[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\r\n\t\tlong[] a, b;\r\n\t\tforeach (i; 0..n*2)\r\n\t\t{\r\n\t\t\tauto x = RD;\r\n\t\t\tauto y = RD;\r\n\t\t\tif (x == 0)\r\n\t\t\t\ta ~= y.abs;\r\n\t\t\telse\r\n\t\t\t\tb ~= x.abs;\r\n\t\t}\r\n\t\ta.sort();\r\n\t\tb.sort();\r\n\r\n\t\tans[ti] = 0.0;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tans[ti] += sqrt(cast(double)a[i]^^2 + b[i]^^2);\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twritefln(\"%.15f\", e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n int n;\n n = scan!int;\n ll[] miner;\n ll[] diam;\n ll x, y;\n for(int i = 0; i < 2*n; ++i) {\n x = scan; y = scan;\n if(x == 0){\n miner ~= abs(y);\n }else{\n diam ~= abs(x);\n }\n }\n miner.sort;\n diam.sort;\n double res = 0;\n for(int i = 0; i < n; ++i){\n double curres = miner[i]*miner[i] + diam[i]*diam[i];\n curres = sqrt(curres);\n res += curres;\n }\n writefln(\"%.12f\", res);\n}\n\nvoid main(){\n long tests = 1;\n tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new double[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\r\n\t\tint[] a, b;\r\n\t\tforeach (i; 0..n*2)\r\n\t\t{\r\n\t\t\tauto x = RD!int;\r\n\t\t\tauto y = RD!int;\r\n\t\t\tif (x == 0)\r\n\t\t\t\ta ~= y.abs;\r\n\t\t\telse\r\n\t\t\t\tb ~= x.abs;\r\n\t\t}\r\n\t\ta.sort();\r\n\t\tb.sort();\r\n\r\n\t\tans[ti] = 0.0;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tans[ti] += sqrt(cast(double)a[i]^^2 + b[i]^^2);\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twritefln(\"%.15f\", e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new double[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\r\n\t\tint[] a, b;\r\n\t\tforeach (i; 0..n*2)\r\n\t\t{\r\n\t\t\tauto x = RD!int;\r\n\t\t\tauto y = RD!int;\r\n\t\t\tif (x == 0)\r\n\t\t\t\ta ~= y.abs;\r\n\t\t\telse\r\n\t\t\t\tb ~= x.abs;\r\n\t\t}\r\n\t\ta.sort();\r\n\t\tb.sort();\r\n\r\n\t\tans[ti] = 0.0;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tans[ti] += sqrt(cast(double)a[i]^^2 + b[i]^^2);\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twritefln(FMT_F, e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "src_uid": "ba27ac62b84705d80fa580567ab64c3b"} {"nl": {"description": "There are $$$n$$$ piranhas with sizes $$$a_1, a_2, \\ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely: The piranha $$$i$$$ can eat the piranha $$$i-1$$$ if the piranha $$$i-1$$$ exists and $$$a_{i - 1} < a_i$$$. The piranha $$$i$$$ can eat the piranha $$$i+1$$$ if the piranha $$$i+1$$$ exists and $$$a_{i + 1} < a_i$$$. When the piranha $$$i$$$ eats some piranha, its size increases by one ($$$a_i$$$ becomes $$$a_i + 1$$$).Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.For example, if $$$a = [5, 3, 4, 4, 5]$$$, then the third piranha can be dominant. Consider the sequence of its moves: The piranha eats the second piranha and $$$a$$$ becomes $$$[5, \\underline{5}, 4, 5]$$$ (the underlined piranha is our candidate). The piranha eats the third piranha and $$$a$$$ becomes $$$[5, \\underline{6}, 5]$$$. The piranha eats the first piranha and $$$a$$$ becomes $$$[\\underline{7}, 5]$$$. The piranha eats the second piranha and $$$a$$$ becomes $$$[\\underline{8}]$$$. You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$) — the number of piranhas in the aquarium. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the size of the $$$i$$$-th piranha. It is guaranteed that the sum of $$$n$$$ does not exceed $$$3 \\cdot 10^5$$$ ($$$\\sum n \\le 3 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.", "sample_inputs": ["6\n5\n5 3 4 4 5\n3\n1 1 1\n5\n4 4 3 4 4\n5\n5 5 4 3 2\n3\n1 1 2\n5\n5 4 3 5 5"], "sample_outputs": ["3\n-1\n4\n3\n3\n1"], "notes": "NoteThe first test case of the example is described in the problem statement.In the second test case of the example, there are no dominant piranhas in the aquarium.In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes $$$[4, 4, 5, 4]$$$, then it can eat any other piranha in the aquarium."}, "positive_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid play(){\n int n;\n n = rd!int;\n ll[] arr = rdarr;\n ll maxx = arr.maxElement;\n ll maxi = -1;\n foreach(i; 0..n){\n if(arr[i] == maxx){\n if(i > 0 && arr[i-1] < maxx){\n maxi = i + 1;\n break;\n }else if(i < n - 1 && arr[i+1] < maxx){\n maxi = i + 1;\n break;\n }\n }\n }\n writeln(maxi);\n}\n\nint main(){\n long t = 1;\n t = rd; // Toggle!\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }\nvoid show(A...)(A a) { debug{ a.each!(w => write(w, \"| \")); writeln; } }\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, long);\nT max(T = long)(T a, T b){ return (a > b) ? a : b; }\nT min(T = long)(T a, T b){ return (a < b) ? a : b; }\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\nmultitest_loop:\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto m = a.maxElement;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] == m && ((i > 0 && a[i - 1] < a[i]) ||\n\t\t\t (i + 1 < n && a[i + 1] < a[i])))\n\t\t\t{\n\t\t\t\twriteln (i + 1);\n\t\t\t\tcontinue multitest_loop;\n\t\t\t}\n\t\t}\n\t\twriteln (-1);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\n\t\tlong x;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tx.chmax(a[i]);\n\t\t}\n\n\t\tans[ti] = -1;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] != x) continue;\n\t\t\tif (i != 0)\n\t\t\t{\n\t\t\t\tif (a[i] > a[i-1])\n\t\t\t\t{\n\t\t\t\t\tans[ti] = i+1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (i != n-1)\n\t\t\t{\n\t\t\t\tif (a[i] > a[i+1])\n\t\t\t\t{\n\t\t\t\t\tans[ti] = i+1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "5598d5954fa3e3cecedb413033259760"} {"nl": {"description": "As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A. The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.", "output_spec": "Print a single number — the maximum size of a clique in a divisibility graph for set A.", "sample_inputs": ["8\n3 4 6 8 10 18 21 24"], "sample_outputs": ["3"], "notes": "NoteIn the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MAX_N = 1_000_006;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s \", &n) > 0)\n\t{\n\t\tauto b = new bool [MAX_N];\n\t\tforeach (x; readln.split.map !(to !(int)))\n\t\t{\n\t\t\tb[x] = true;\n\t\t}\n\n\t\tauto f = new int [MAX_N];\n\t\tforeach (i; 0..MAX_N)\n\t\t{\n\t\t\tf[i] = b[i];\n\t\t}\n\n\t\tint res = 0;\n\t\tfor (int x = 1; x < MAX_N; x++)\n\t\t{\n\t\t\tif (!b[x])\n\t\t\t{\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (int y = x * 2; y < MAX_N; y += x)\n\t\t\t{\n\t\t\t\tif (!b[y])\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tf[y] = max (f[y], f[x] + 1);\n\t\t\t}\n\t\t\tres = max (res, f[x]);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.conv, std.stdio, std.range;\nimmutable int MAX_N = 1_000_006;\nvoid main ()\n{\n int n;\n while (readf (\" %s \", &n) > 0)\n {\n auto f = new int [MAX_N];\n foreach (x; readln.split.map !(to !(int)))\n f[x] = 1;\n for (int x = 1; x < MAX_N; x++) if (f[x])\n for (int y = x * 2; y < MAX_N; y += x) if (f[y])\n f[y] = max (f[y], f[x] + 1);\n f.minPos !(q{a > b}).front.writeln;\n }\n}\n"}], "negative_code": [{"source_code": "import std.algorithm, std.conv, std.stdio, std.range;\nimmutable int MAX_N = 1_000_006;\nvoid main ()\n{\n\tint n;\n\treadf (\" %s \", &n);\n\tauto f = new int [MAX_N];\n\tforeach (x; readln.split.map!(to!int))\n\t\tfor (int y = x * 2; y < MAX_N; y += x)\n\t\t\tf[y] = max (f[y], f[x] + 1);\n\tf.minPos!q{a > b}.front.writeln;\n}\n"}], "src_uid": "f33991da3b4a57dd6535af86edeeddc0"} {"nl": {"description": "Yurii is sure he can do everything. Can he solve this task, though?He has an array $$$a$$$ consisting of $$$n$$$ positive integers. Let's call a subarray $$$a[l...r]$$$ good if the following conditions are simultaneously satisfied: $$$l+1 \\leq r-1$$$, i. e. the subarray has length at least $$$3$$$; $$$(a_l \\oplus a_r) = (a_{l+1}+a_{l+2}+\\ldots+a_{r-2}+a_{r-1})$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation. In other words, a subarray is good if the bitwise XOR of the two border elements is equal to the sum of the rest of the elements. Yurii wants to calculate the total number of good subarrays. What is it equal to?An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$3 \\leq n \\leq 2\\cdot 10^5$$$) — the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$1 \\leq a_i \\lt 2^{30}$$$) — elements of $$$a$$$. ", "output_spec": "Output a single integer — the number of good subarrays. ", "sample_inputs": ["8\n3 1 2 3 1 2 3 15", "10\n997230370 58052053 240970544 715275815 250707702 156801523 44100666 64791577 43523002 480196854"], "sample_outputs": ["6", "2"], "notes": "NoteThere are $$$6$$$ good subarrays in the example: $$$[3,1,2]$$$ (twice) because $$$(3 \\oplus 2) = 1$$$; $$$[1,2,3]$$$ (twice) because $$$(1 \\oplus 3) = 2$$$; $$$[2,3,1]$$$ because $$$(2 \\oplus 1) = 3$$$; $$$[3,1,2,3,1,2,3,15]$$$ because $$$(3 \\oplus 15) = (1+2+3+1+2+3)$$$. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\n\t\tauto s = [0L];\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\ts ~= s.back + c;\n\t\t}\n\n\t\tlong r (int lo, int hi)\n\t\t{\n\t\t\treturn s[hi] - s[lo];\n\t\t}\n\n\t\tlong res = 0;\n\t\tforeach (i; 2..n)\n\t\t{\n\t\t\tint pos = i - 1;\n\t\t\tlong cur = a[pos];\n\t\t\tpos -= 1;\n\t\t\twhile (pos >= 0 && cur < a[i])\n\t\t\t{\n\t\t\t\tif ((a[pos] ^ a[i]) == cur)\n\t\t\t\t{\n\t\t\t\t\tres += 1;\n\t\t\t\t}\n\t\t\t\tcur += a[pos];\n\t\t\t\tpos -= 1;\n\t\t\t}\n\t\t\twhile (pos >= 0)\n\t\t\t{\n\t\t\t\tlong ask = cur - a[i];\n\t\t\t\tint lo = 0;\n\t\t\t\tint hi = pos;\n\t\t\t\tdebug {writeln (ask, \" \", lo, \" \", hi);}\n\t\t\t\twhile (lo < hi)\n\t\t\t\t{\n\t\t\t\t\tint me = (lo + hi + 1) / 2;\n\t\t\t\t\tif (s[pos + 1] - s[me] < ask)\n\t\t\t\t\t{\n\t\t\t\t\t\thi = me - 1;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tlo = me;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcur += s[pos + 1] - s[lo + 1];\n\t\t\t\tpos = lo;\n\t\t\t\tif ((a[pos] ^ a[i]) == cur)\n\t\t\t\t{\n\t\t\t\t\tres += 1;\n\t\t\t\t}\n\t\t\t\tcur += a[pos];\n\t\t\t\tpos -= 1;\n\t\t\t}\n\t\t}\n\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "a4b98242919d7eb520743212efc806a8"} {"nl": {"description": "A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex.The gardener Vitaly grew a tree from $$$n$$$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation \"remove all leaves\" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $$$0$$$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $$$k$$$ operations sequentially to the tree. How many vertices remain?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 4 \\cdot 10^5$$$, $$$1 \\le k \\le 2 \\cdot 10^5$$$) — the number of vertices in the tree and the number of operations, respectively. Then $$$n - 1$$$ lines follow, each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $$$n$$$ from all test cases does not exceed $$$4 \\cdot 10^5$$$.", "output_spec": "For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $$$k$$$ operations.", "sample_inputs": ["6\n\n14 1\n1 2\n2 3\n2 4\n4 5\n4 6\n2 7\n7 8\n8 9\n8 10\n3 11\n3 12\n1 13\n13 14\n\n2 200000\n1 2\n\n3 2\n1 2\n2 3\n\n5 1\n5 1\n3 2\n2 1\n5 4\n\n6 2\n5 1\n2 5\n5 6\n4 2\n3 4\n\n7 1\n4 3\n5 1\n1 3\n6 1\n1 7\n2 1"], "sample_outputs": ["7\n0\n0\n3\n1\n2"], "notes": "NoteThe first test case is considered in the statement.The second test case contains a tree of two vertices. $$$200000$$$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree.In the third test case, a tree of three vertices is given. As a result of the first operation, only $$$1$$$ vertex remains in it (with the index $$$2$$$), the second operation makes the tree empty."}, "positive_code": [{"source_code": "// Not my code! Taken from https://codeforces.com/contest/1755/submission/179873022\n// and converted from Kotlin to D\n\nimport std;\n\nprivate void go()\n{\n readln;\n int N, K; readln.formattedRead!\" %d %d \"(N, K);\n auto adj = new int[][N];\n auto deg = new int[N];\n foreach (_ ; 0 .. N - 1) {\n int i, j; readln.formattedRead!\" %d %d \"(i, j); i--; j--;\n adj[i] ~= j;\n adj[j] ~= i;\n deg[i]++;\n deg[j]++;\n }\n\n int[] q;\n foreach (i ; 0 .. N) {\n if (deg[i] <= 1) {\n q ~= i;\n }\n }\n size_t idx = 0;\n foreach (_ ; 0 .. K) {\n auto en = q.length;\n foreach (i ; idx .. en) {\n auto cur = q[i];\n foreach (nxt ; adj[cur]) {\n deg[nxt]--;\n if (deg[nxt] == 1) {\n q ~= nxt;\n }\n }\n }\n idx = en;\n if (idx == N) {\n break;\n }\n }\n writeln(N - idx);\n}\n\nvoid main()\n{\n foreach (_ ; 0 .. readln.strip.to!int) { go(); }\n}\n"}], "negative_code": [], "src_uid": "07ac198c1086323517540ecd0669eb4c"} {"nl": {"description": "Sam lives in Awesomeburg, its downtown has a triangular shape. Also, the following is true about the triangle: its vertices have integer coordinates, the coordinates of vertices are non-negative, and its vertices are not on a single line. He calls a point on the downtown's border (that is the border of the triangle) safe if he can reach this point from at least one point of the line $$$y = 0$$$ walking along some straight line, without crossing the interior of the triangle. In the picture the downtown is marked with grey color. The first path is invalid because it does not go along a straight line. The second path is invalid because it intersects with the interior of the downtown. The third and fourth paths are correct. Find the total length of the unsafe parts of the downtown border. It can be proven that these parts are segments and their number is finite.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Description of the test cases follows. Each test case contains three lines, each of them contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 \\le x_i, y_i \\le 10^9$$$) — coordinates of the vertices of the downtown's border.", "output_spec": "For each test case print a single number — the answer to the problem. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally let your answer be $$$a$$$, jury answer be $$$b$$$. Your answer will be considered correct if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-9}$$$.", "sample_inputs": ["5\n8 10\n10 4\n6 2\n4 6\n0 1\n4 2\n14 1\n11 2\n13 2\n0 0\n4 0\n2 4\n0 1\n1 1\n0 0"], "sample_outputs": ["0.0000000\n0\n2.0000\n0.00\n1"], "notes": "NoteIn the picture, the downtowns of the first three test cases are illustrated. Triangles are enumerated according to the indices of test cases they belong to. In the first two test cases, all points on the borders of the downtowns are safe, thus the answers are $$$0$$$.In the following picture unsafe points for the third test case are marked with black color: In the fourth test case, all points on the border of the downtown are safe."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.string;\r\nimport std.math;\r\n\r\nauto line_to_array(ref File input)\r\n{\r\n import std.algorithm.sorting;\r\n import std.conv;\r\n import std.string;\r\n return input.readln.chomp.split;\r\n}\r\n\r\nauto line_to_int(ref File input)\r\n{\r\n import std.string;\r\n import std.conv;\r\n return input.readln.chomp.to!int;\r\n}\r\n\r\nvoid solution(ref File input, ref File output)\r\n{\r\n auto fd = input.getFP;\r\n\r\n double answer = 0;\r\n long[3] x, y;\r\n\r\n for (int i = 0; i < 3; i++) input.readf!\"%d %d\\n\"(x[i], y[i]);\r\n\r\n for (int i = 0; i < 3; i++)\r\n for (int j = 0; j < 3; j++)\r\n for (int c = 0; c < 3; c++)\r\n if (i != j && j != c && i != c && y[i] == y[j] && y[i] > y[c])\r\n {\r\n answer += sqrt(cast(double) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2)));\r\n break;\r\n }\r\n output.writefln(\"%f\", answer / 2);\r\n}\r\n\r\nvoid loop(ref File input, ref File output)\r\n{\r\n auto t = input.readln.chomp.to!int;\r\n while (t-- > 0) solution(input, output);\r\n}\r\n\r\nint main()\r\n{\r\n File input, output;\r\n debug(1)\r\n {\r\n input = File(\"input.txt\", \"r\");\r\n output = File(\"output.txt\", \"w\");\r\n } else\r\n {\r\n input = stdin;\r\n output = stdout;\r\n }\r\n loop(input, output);\r\n return 0;\r\n}"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto P = 3.iota.map!(_ => Point(scan!long, scan!long)).array;\r\n\r\n auto maxY = P.map!\"a.y\".maxElement;\r\n auto mys = P.filter!(a => a.y == maxY).array;\r\n\r\n if (mys.length == 2) {\r\n return (mys[0].x - mys[1].x).abs;\r\n }\r\n\r\n return 0;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve().writeln;\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new double[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto x = new long[](3);\r\n\t\tauto y = new long[](3);\r\n\t\tforeach (i; 0..3)\r\n\t\t{\r\n\t\t\tx[i] = RD;\r\n\t\t\ty[i] = RD;\r\n\t\t}\r\n\r\n\t\tauto index = y.MAKE_IDX;\r\n\t\tif (y[index[1]] == y[index[2]])\r\n\t\t\tans[ti] = abs(x[index[1]] - x[index[2]]);\r\n\t\telse\r\n\t\t\tans[ti] = 0.0;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twritefln(FMT_F, e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.string;\r\nimport std.math;\r\n\r\nauto line_to_array(ref File input)\r\n{\r\n import std.algorithm.sorting;\r\n import std.conv;\r\n import std.string;\r\n return input.readln.chomp.split;\r\n}\r\n\r\nauto line_to_int(ref File input)\r\n{\r\n import std.string;\r\n import std.conv;\r\n return input.readln.chomp.to!int;\r\n}\r\n\r\nvoid solution(ref File input, ref File output)\r\n{\r\n auto fd = input.getFP;\r\n\r\n double answer = 0;\r\n long[3] x, y;\r\n\r\n for (int i = 0; i < 3; i++) input.readf!\"%d %d\\n\"(x[i], y[i]);\r\n\r\n for (int i = 0; i < 3; i++)\r\n for (int j = 0; j < 3; j++)\r\n for (int c = 0; c < 3; c++)\r\n if (i != j && j != c && i != c && y[i] == y[j] && y[i] > y[c])\r\n {\r\n answer += sqrt(cast(double) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2)));\r\n break;\r\n }\r\n output.writeln(answer / 2);\r\n}\r\n\r\nvoid loop(ref File input, ref File output)\r\n{\r\n auto t = input.readln.chomp.to!int;\r\n while (t-- > 0) solution(input, output);\r\n}\r\n\r\nint main()\r\n{\r\n File input, output;\r\n debug(1)\r\n {\r\n input = File(\"input.txt\", \"r\");\r\n output = File(\"output.txt\", \"w\");\r\n } else\r\n {\r\n input = stdin;\r\n output = stdout;\r\n }\r\n loop(input, output);\r\n return 0;\r\n}"}, {"source_code": "import std.stdio;\r\nimport std.conv;\r\nimport std.string;\r\nimport std.math;\r\n\r\nauto line_to_array(ref File input)\r\n{\r\n import std.algorithm.sorting;\r\n import std.conv;\r\n import std.string;\r\n return input.readln.chomp.split;\r\n}\r\n\r\nauto line_to_int(ref File input)\r\n{\r\n import std.string;\r\n import std.conv;\r\n return input.readln.chomp.to!int;\r\n}\r\n\r\nvoid solution(ref File input, ref File output)\r\n{\r\n auto fd = input.getFP;\r\n\r\n float answer = 0;\r\n int[3] x, y;\r\n\r\n core.stdc.stdio.fscanf(fd, \"%d %d\\n\", &x[0], &y[0]);\r\n core.stdc.stdio.fscanf(fd, \"%d %d\\n\", &x[1], &y[1]);\r\n core.stdc.stdio.fscanf(fd, \"%d %d\\n\", &x[2], &y[2]);\r\n\r\n for (int i = 0; i < 3; i++)\r\n for (int j = 0; j < 3; j++)\r\n for (int c = 0; c < 3; c++)\r\n if (i != j && j != c && i != c && y[i] == y[j] && y[i] > y[c])\r\n {\r\n //output.writefln(\"i: %d x: %d y: %d\", i, x[i], y[i]);\r\n //output.writefln(\"j: %d x: %d y: %d\", j, x[j], y[j]);\r\n //output.writefln(\"c: %d x: %d y: %d\", c, x[c], y[c]);\r\n answer += sqrt(cast(float) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2)));\r\n //answer += cast(float) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2));\r\n break;\r\n }\r\n output.writeln(answer / 2);\r\n}\r\n\r\nvoid loop(ref File input, ref File output)\r\n{\r\n auto t = input.readln.chomp.to!int;\r\n while (t-- > 0) solution(input, output);\r\n}\r\n\r\nint main()\r\n{\r\n File input, output;\r\n debug(1)\r\n {\r\n input = File(\"input.txt\", \"r\");\r\n output = File(\"output.txt\", \"w\");\r\n } else\r\n {\r\n input = stdin;\r\n output = stdout;\r\n }\r\n loop(input, output);\r\n return 0;\r\n}"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto P = 3.iota.map!(_ => Point(scan!long, scan!long)).array;\r\n\r\n auto maxY = P.map!\"a.y\".maxElement;\r\n auto mys = P.filter!(a => a.y == maxY).array;\r\n\r\n if (mys.length == 2) {\r\n return ((mys[0].x - mys[1].x).to!real.pow(2) + (mys[0].y - mys[1].y).to!real.pow(2)).sqrt;\r\n }\r\n\r\n return 0.0;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve().writeln;\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}], "src_uid": "9f019c3898f27d687c5b3498586644e8"} {"nl": {"description": "Circular land is an $$$2n \\times 2n$$$ grid. Rows of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from top to bottom and columns of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and column $$$y$$$ for $$$1 \\leq x \\leq 2n$$$ and $$$1 \\leq y \\leq 2n$$$.There are $$$n^2$$$ of your friends in the top left corner of the grid. That is, in each cell $$$(x, y)$$$ with $$$1 \\leq x, y \\leq n$$$ there is exactly one friend. Some of the other cells are covered with snow.Your friends want to get to the bottom right corner of the grid. For this in each cell $$$(x, y)$$$ with $$$n+1 \\leq x, y \\leq 2n$$$ there should be exactly one friend. It doesn't matter in what cell each of friends will be.You have decided to help your friends to get to the bottom right corner of the grid.For this, you can give instructions of the following types: You select a row $$$x$$$. All friends in this row should move to the next cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 \\leq y < 2n$$$ will move to the cell $$$(x, y + 1)$$$ and friend from the cell $$$(x, 2n)$$$ will move to the cell $$$(x, 1)$$$. You select a row $$$x$$$. All friends in this row should move to the previous cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 < y \\leq 2n$$$ will move to the cell $$$(x, y - 1)$$$ and friend from the cell $$$(x, 1)$$$ will move to the cell $$$(x, 2n)$$$. You select a column $$$y$$$. All friends in this column should move to the next cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 \\leq x < 2n$$$ will move to the cell $$$(x + 1, y)$$$ and friend from the cell $$$(2n, y)$$$ will move to the cell $$$(1, y)$$$. You select a column $$$y$$$. All friends in this column should move to the previous cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 < x \\leq 2n$$$ will move to the cell $$$(x - 1, y)$$$ and friend from the cell $$$(1, y)$$$ will move to the cell $$$(2n, y)$$$. Note how friends on the grid border behave in these instructions. Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow. You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill.In order to save your friends you can remove snow from some cells before giving the first instruction: You can select the cell $$$(x, y)$$$ that is covered with snow now and remove snow from this cell for $$$c_{x, y}$$$ coins. You can do this operation any number of times.You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill.Please, find how many coins you will spend.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\leq n \\leq 250$$$). Each of the next $$$2n$$$ lines contains $$$2n$$$ integers $$$c_{i, 1}, c_{i, 2}, \\ldots, c_{i, 2n}$$$ ($$$0 \\leq c_{i, j} \\leq 10^9$$$) — costs of removing snow from cells. If $$$c_{i, j} = 0$$$ for some $$$i, j$$$ than there is no snow in cell $$$(i, j)$$$. Otherwise, cell $$$(i, j)$$$ is covered with snow. It is guaranteed that $$$c_{i, j} = 0$$$ for $$$1 \\leq i, j \\leq n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$250$$$.", "output_spec": "For each test case output one integer — the minimal number of coins you should spend.", "sample_inputs": ["4\n\n1\n\n0 8\n\n1 99\n\n2\n\n0 0 0 0\n\n0 0 0 0\n\n9 9 2 2\n\n9 9 9 9\n\n2\n\n0 0 4 2\n\n0 0 2 4\n\n4 2 4 2\n\n2 4 2 4\n\n4\n\n0 0 0 0 0 0 0 2\n\n0 0 0 0 0 0 2 0\n\n0 0 0 0 0 2 0 0\n\n0 0 0 0 2 0 0 0\n\n0 0 0 2 2 0 2 2\n\n0 0 2 0 1 6 2 1\n\n0 2 0 0 2 4 7 4\n\n2 0 0 0 2 0 1 6"], "sample_outputs": ["100\n22\n14\n42"], "notes": "NoteIn the first test case you can remove snow from the cells $$$(2, 1)$$$ and $$$(2, 2)$$$ for $$$100$$$ coins. Then you can give instructions All friends in the first collum should move to the previous cell. After this, your friend will be in the cell $$$(2, 1)$$$. All friends in the second row should move to the next cell. After this, your friend will be in the cell $$$(2, 2)$$$. In the second test case you can remove all snow from the columns $$$3$$$ and $$$4$$$ for $$$22$$$ coins. Then you can give instructions All friends in the first row should move to the next cell. All friends in the first row should move to the next cell. All friends in the second row should move to the next cell. All friends in the second row should move to the next cell. All friends in the third column should move to the next cell. All friends in the third column should move to the next cell. All friends in the fourth column should move to the next cell. All friends in the fourth column should move to the next cell. It can be shown that none of the friends will become ill and that it is impossible to spend less coins."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tint [] [] board;\r\n\t\tforeach (row; 0..n * 2)\r\n\t\t{\r\n\t\t\tboard ~= readln.splitter.map !(to !(int)).array;\r\n\t\t}\r\n\t\tauto res = 0L;\r\n\t\tforeach (row; n..n * 2)\r\n\t\t{\r\n\t\t\tforeach (col; n..n * 2)\r\n\t\t\t{\r\n\t\t\t\tres += board[row][col];\r\n\t\t\t}\r\n\t\t}\r\n\t\tres += min (board[n][0], board[$ - 1][0],\r\n\t\t board[n][n - 1], board[$ - 1][n - 1],\r\n\t\t board[0][n], board[n - 1][n],\r\n\t\t board[0][$ - 1], board[n - 1][$ - 1]);\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "acec4108e865c3f894de14248156abfd"} {"nl": {"description": "You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, \"defor\" is a substring of \"codeforces\" and \"fors\" is not. The length of the substring is the number of letters in it.Let's call some string of length $$$n$$$ diverse if and only if there is no letter to appear strictly more than $$$\\frac n 2$$$ times. For example, strings \"abc\" and \"iltlml\" are diverse and strings \"aab\" and \"zz\" are not.Your task is to find any diverse substring of string $$$s$$$ or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the length of string $$$s$$$. The second line is the string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters.", "output_spec": "Print \"NO\" if there is no diverse substring in the string $$$s$$$. Otherwise the first line should contain \"YES\". The second line should contain any diverse substring of string $$$s$$$.", "sample_inputs": ["10\ncodeforces", "5\naaaaa"], "sample_outputs": ["YES\ncode", "NO"], "notes": "NoteThe first example has lots of correct answers. Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to \"No comments\" answer."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto S = readln.chomp;\n auto cnt = new int[](26);\n\n bool ok(int l, int r) {\n cnt[] = 0;\n foreach (i; l..r+1) cnt[S[i]-'a'] += 1;\n return cnt.reduce!max * 2 <= r - l + 1;\n }\n\n foreach (i; 0..N) {\n foreach (j; i..N) {\n if (ok(i, j)) {\n writeln(\"YES\");\n writeln(S[i..j+1]);\n return;\n }\n }\n }\n\n writeln(\"NO\");\n}\n"}, {"source_code": "//ahmat\nimport core.stdc.stdio:printf,scanf;\nimport std.stdio;\nimport std.string;\nimport std.container;\nimport std.array;\nimport std.range;\nimport std.algorithm;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.bitmanip;\nimport core.bitop;\nint sz(Range)(in Range r){return cast(int)r.length;}\nalias rbt=RedBlackTree!int;\n\n\nvoid main(){\n\tint n;\n\tscanf(\"%d\",&n);\n\treadln;\n\tstring s=readln.strip;\n\tbool found=false;\n\tint i=0;\n\tfor(;i1){\n\t\tfor(;i 100 * (j - i + 1)) {\n m = max(m, j - i + 1);\n }\n }\n }\n\n printf(\"%d\\n\", m);\n}\n\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n int mxlen = 0;\n foreach (le; 0 .. n) {\n int sm = 0, len = 0;\n foreach (r; le .. n) {\n sm += arr[r];\n len += 1;\n \n if (sm > 100 * len) { mxlen = max(mxlen, len); }\n }\n }\n \n mxlen.writeln;\n}"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto arr = readln.chomp.split.map!(to!int).array;\n \n int mxlen = 0;\n foreach (le; 0 .. n) {\n int sm = 0, len = 0;\n foreach (r; le .. n) {\n sm += arr[r];\n len += 1;\n \n if (sm >= 100 * len) { mxlen = max(mxlen, len); }\n }\n }\n \n if (mxlen == n) { mxlen -= 1; }\n \n mxlen.writeln;\n}"}], "src_uid": "ae531bc4b47e5d31fe71b6de1398b95e"} {"nl": {"description": "Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses (\"+\") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him!", "input_spec": "The first line contains two integers, n and k (0 ≤ k < n ≤ 105). The second line contains a string consisting of n digits.", "output_spec": "Print the answer to the problem modulo 109 + 7.", "sample_inputs": ["3 1\n108", "3 2\n108"], "sample_outputs": ["27", "9"], "notes": "NoteIn the first sample the result equals (1 + 08) + (10 + 8) = 27.In the second sample the result equals 1 + 0 + 8 = 9."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MOD = 1_000_000_007;\nimmutable int BASE = 10;\n\nint powmod (int a, int p, int m)\n{\n\tint res = 1;\n\twhile (p)\n\t{\n\t\tif (p & 1)\n\t\t{\n\t\t\tres = (cast (long) res * a) % m;\n\t\t}\n\t\ta = (cast (long) a * a) % m;\n\t\tp >>= 1;\n\t}\n\treturn res;\n}\n\nint inv (int a)\n{\n\treturn powmod (a, MOD - 2, MOD);\n}\n\nvoid main ()\n{\n\tint n, k;\n\twhile (readf (\" %s %s \", &n, &k) > 0)\n\t{\n\t\tauto a = readln ().strip ().map !(q{a - '0'}) ().array ();\n\t\tdebug {writeln (a);}\n\n\t\tlong [] p = [1];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tp ~= (p[$ - 1] * BASE) % MOD;\n\t\t}\n\t\tdebug {writeln (p);}\n\n\t\tlong [] c = [1];\n\t\tforeach (i; k..n - 1)\n\t\t{\n\t\t\tc ~= (((c[$ - 1] * i) % MOD) * inv (i - k + 1)) % MOD;\n\t\t}\n\t\treverse (c);\n\t\tdebug {writeln (c);}\n\n\t\tlong s = 0;\n\t\tforeach (i; 0..c.length)\n\t\t{\n\t\t\ts = (s + c[i] * p[i]) % MOD;\n\t\t}\n\t\tdebug {writeln (s);}\n\n\t\tlong res = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tres = (res + s * a[i]) % MOD;\n\t\t\tint j = n - i - 1;\n\t\t\tif (0 < j && j < c.length)\n\t\t\t{\n\t\t\t\ts = (s - c[j] * p[j]) % MOD;\n\t\t\t\ts = (s + c[j] * p[j - 1]) % MOD;\n\t\t\t\tc[j - 1] = (c[j - 1] + c[j]) % MOD;\n\t\t\t\tj--;\n\t\t\t\ts = (s + MOD) % MOD;\n\t\t\t\tdebug {writeln (c, ' ', s);}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nimmutable long MO = 10^^9 + 7;\nimmutable LIM = 2 * 10^^5 + 5;\n\nlong[] inv, fac, facInv;\n\nvoid prepare() {\n\tinv = new long[LIM];\n\tinv[1] = 1;\n\tforeach (i; 2 .. LIM) {\n\t\tinv[i] = MO - MO / i * inv[cast(size_t)(MO % i)] % MO;\n\t}\n\tfac = new long[LIM];\n\tfacInv = new long[LIM];\n\tfac[0] = 1;\n\tfacInv[0] = 1;\n\tforeach (i; 1 .. LIM) {\n\t\tfac[i] = fac[i - 1] * i % MO;\n\t\tfacInv[i] = facInv[i - 1] * inv[i] % MO;\n\t}\n}\n\nlong binom(int n, int k) {\n\tif (!(0 <= k && k <= n)) {\n\t\treturn 0;\n\t}\n\treturn fac[n] * facInv[k] % MO * facInv[n - k] % MO;\n}\n\nint N, K;\nstring S;\n\nvoid main(string[] args) {\n\tprepare;\n\t\n\ttry {\n\tfor (; ; ) {\n\t\tN = readInt;\n\t\tK = readInt;\n\t\tS = readToken;\n\t\t\ndebug{\nlong brt;\nforeach(h;0..1<<(N-1)){\n int cnt;\n foreach(i;0..N-1)if(h&1<> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n \r\nvoid solve() {\r\n int n = read!int;\r\n auto a = readarray!long;\r\n auto result = a[0];\r\n foreach(i; 1 .. n) {\r\n result = gcd(result, a[i]);\r\n }\r\n if (result == 1) {\r\n writeln(0);\r\n return;\r\n }\r\n if (n == 1)\r\n writeln(1);\r\n else if (gcd(result, gcd(n,a[n-1])) == 1)\r\n writeln(1);\r\n else if (gcd(result, gcd(n-1,a[n-2])) == 1)\r\n writeln(2);\r\n else\r\n writeln(3);\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\nimport std.functional;\r\n \r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n \r\nvoid solve() {\r\n int n = read!int;\r\n auto a = readarray!long;\r\n auto result = a[0];\r\n if (result == 1) {\r\n writeln(0);\r\n return;\r\n }\r\n foreach(i; 1 .. n) {\r\n if (result == 1) {\r\n writeln(0);\r\n return;\r\n }\r\n result = gcd(result, a[i]);\r\n }\r\n if (n == 1)\r\n writeln(1);\r\n else if (gcd(result, gcd(n,a[n-1])) == 1)\r\n writeln(1);\r\n else if (gcd(gcd(n-1,a[n-2]), result) == 1 )\r\n writeln(2);\r\n else\r\n writeln(min(n,3));\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "src_uid": "ff3216bcb009cb963d7e734ceb0e9722"} {"nl": {"description": "The only difference between easy and hard versions is constraints.Nauuo is a girl who loves random picture websites.One day she made a random picture website by herself which includes $$$n$$$ pictures.When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The $$$i$$$-th picture has a non-negative weight $$$w_i$$$, and the probability of the $$$i$$$-th picture being displayed is $$$\\frac{w_i}{\\sum_{j=1}^nw_j}$$$. That is to say, the probability of a picture to be displayed is proportional to its weight.However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add $$$1$$$ to its weight; otherwise, she would subtract $$$1$$$ from its weight.Nauuo will visit the website $$$m$$$ times. She wants to know the expected weight of each picture after all the $$$m$$$ visits modulo $$$998244353$$$. Can you help her?The expected weight of the $$$i$$$-th picture can be denoted by $$$\\frac {q_i} {p_i}$$$ where $$$\\gcd(p_i,q_i)=1$$$, you need to print an integer $$$r_i$$$ satisfying $$$0\\le r_i<998244353$$$ and $$$r_i\\cdot p_i\\equiv q_i\\pmod{998244353}$$$. It can be proved that such $$$r_i$$$ exists and is unique.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\\le n\\le 2\\cdot 10^5$$$, $$$1\\le m\\le 3000$$$) — the number of pictures and the number of visits to the website. The second line contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$a_i$$$ is either $$$0$$$ or $$$1$$$) — if $$$a_i=0$$$ , Nauuo does not like the $$$i$$$-th picture; otherwise Nauuo likes the $$$i$$$-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains $$$n$$$ positive integers $$$w_1,w_2,\\ldots,w_n$$$ ($$$w_i \\geq 1$$$) — the initial weights of the pictures. It is guaranteed that the sum of all the initial weights does not exceed $$$998244352-m$$$.", "output_spec": "The output contains $$$n$$$ integers $$$r_1,r_2,\\ldots,r_n$$$ — the expected weights modulo $$$998244353$$$.", "sample_inputs": ["2 1\n0 1\n2 1", "1 2\n1\n1", "3 3\n0 1 1\n4 3 5"], "sample_outputs": ["332748119\n332748119", "3", "160955686\n185138929\n974061117"], "notes": "NoteIn the first example, if the only visit shows the first picture with a probability of $$$\\frac 2 3$$$, the final weights are $$$(1,1)$$$; if the only visit shows the second picture with a probability of $$$\\frac1 3$$$, the final weights are $$$(2,2)$$$.So, both expected weights are $$$\\frac2 3\\cdot 1+\\frac 1 3\\cdot 2=\\frac4 3$$$ .Because $$$332748119\\cdot 3\\equiv 4\\pmod{998244353}$$$, you need to print $$$332748119$$$ instead of $$$\\frac4 3$$$ or $$$1.3333333333$$$.In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, $$$w_1$$$ will be increased by $$$1$$$.So, the expected weight is $$$1+2=3$$$.Nauuo is very naughty so she didn't give you any hint of the third example."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n// a^-1 (mod m)\nlong modInv(long a, long m)\nin {\n assert(m > 0, \"modInv: m > 0 must hold\");\n}\ndo {\n long b = m, x = 1, y = 0, t;\n for (; ; ) {\n t = a / b;\n a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1, \"modInv: gcd(a, m) != 1\");\n if (b == -1) {\n y = -y;\n }\n return (y < 0) ? (y + m) : y;\n }\n x -= t * y;\n t = b / a;\n b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1, \"modInv: gcd(a, m) != 1\");\n if (a == -1) {\n x = -x;\n }\n return (x < 0) ? (x + m) : x;\n }\n y -= t * x;\n }\n}\n\n\nenum long MO = 998244353;\n\nint N, M;\nint[] A;\nlong[] W;\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n W = new long[N];\n foreach (i; 0 .. N) {\n W[i] = readLong();\n }\n \n long R, S;\n foreach (i; 0 .. N) {\n if (A[i]) {\n R += W[i];\n }\n S += W[i];\n }\n \n auto invs = new long[2 * M + 1];\n foreach (x; -M .. +M + 1) {\n if (S + x > 0) {\n invs[M + x] = modInv(S + x, MO);\n }\n }\n \n auto dp = new long[][](M + 1, M + 1);\n dp[0][0] = 1;\n foreach (j; 0 .. M) {\n foreach (x; 0 .. j + 1) {\n if (S + x + (j - x) > 0) {\n const prob = ((R + x) * invs[M + x - (j - x)]) % MO;\n (dp[j + 1][x + 0] += dp[j][x] * (1 - prob)) %= MO;\n (dp[j + 1][x + 1] += dp[j][x] * prob) %= MO;\n }\n }\n }\n debug {\n writeln(\"dp = \", dp);\n }\n \n long eP, eN;\n foreach (x; 0 .. M + 1) {\n (eP += dp[M][x] * (R + x)) %= MO;\n (eN += dp[M][x] * (S - R - (M - x))) %= MO;\n }\n foreach (i; 0 .. N) {\n long ans;\n if (A[i]) {\n ans = (((eP * W[i]) % MO) * modInv(R, MO)) % MO;\n } else {\n ans = (((eN * W[i]) % MO) * modInv(S - R, MO)) % MO;\n }\n ans = (ans % MO + MO) % MO;\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n// a^-1 (mod m)\nlong modInv(long a, long m)\nin {\n assert(m > 0, \"modInv: m > 0 must hold\");\n}\ndo {\n long b = m, x = 1, y = 0, t;\n for (; ; ) {\n t = a / b;\n a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1, \"modInv: gcd(a, m) != 1\");\n if (b == -1) {\n y = -y;\n }\n return (y < 0) ? (y + m) : y;\n }\n x -= t * y;\n t = b / a;\n b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1, \"modInv: gcd(a, m) != 1\");\n if (a == -1) {\n x = -x;\n }\n return (x < 0) ? (x + m) : x;\n }\n y -= t * x;\n }\n}\n\n\nenum long MO = 998244353;\n\nint N, M;\nint[] A;\nlong[] W;\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n W = new long[N];\n foreach (i; 0 .. N) {\n W[i] = readLong();\n }\n \n long R, S;\n foreach (i; 0 .. N) {\n if (A[i]) {\n R += W[i];\n }\n S += W[i];\n }\n \n auto invs = new long[2 * M + 1];\n foreach (x; -M .. +M + 1) {\n if (S + x > 0) {\n invs[M + x] = modInv(S + x, MO);\n }\n }\n \n auto dp = new long[][](M + 1, M + 1);\n dp[0][0] = 1;\n foreach (j; 0 .. M) {\n foreach (x; 0 .. j + 1) {\n if (S + x + (j - x) > 0) {\n const prob = ((R + x) * invs[M + x - (j - x)]) % MO;\n (dp[j + 1][x + 0] += dp[j][x] * (1 - prob)) %= MO;\n (dp[j + 1][x + 1] += dp[j][x] * prob) %= MO;\n }\n }\n }\n debug {\n writeln(\"dp = \", dp);\n }\n \n long eP, eN;\n foreach (x; 0 .. M + 1) {\n (eP += dp[M][x] * (R + x)) %= MO;\n (eN += dp[M][x] * (S - R - (M - x))) %= MO;\n }\n foreach (i; 0 .. N) {\n long ans;\n if (A[i]) {\n ans = (((eP * W[i]) % MO) * modInv(R, MO)) % MO;\n } else {\n ans = (((eN * W[i]) % MO) * modInv(S - R, MO)) % MO;\n }\n ans = (ans % MO + MO) % MO;\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "ba9c136f84375cd317f0f8b53e3939c7"} {"nl": {"description": "There is a deck of $$$n$$$ cards. The $$$i$$$-th card has a number $$$a_i$$$ on the front and a number $$$b_i$$$ on the back. Every integer between $$$1$$$ and $$$2n$$$ appears exactly once on the cards.A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $$$a_i< a_{i+1}$$$ and $$$b_i> b_{i+1}$$$ for all $$$1\\le i<n$$$.To flip a card $$$i$$$ means swapping the values of $$$a_i$$$ and $$$b_i$$$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1\\le n\\le 2\\cdot 10^5$$$) — the number of cards. The next $$$n$$$ lines describe the cards. The $$$i$$$-th of these lines contains two integers $$$a_i, b_i$$$ ($$$1\\le a_i, b_i\\le 2n$$$). Every integer between $$$1$$$ and $$$2n$$$ appears exactly once.", "output_spec": "If it is impossible to sort the deck, output \"-1\". Otherwise, output the minimum number of flips required to sort the deck.", "sample_inputs": ["5\n3 10\n6 4\n1 9\n5 8\n2 7", "2\n1 2\n3 4", "3\n1 2\n3 6\n4 5"], "sample_outputs": ["2", "-1", "-1"], "notes": "NoteIn the first test case, we flip the cards $$$(1, 9)$$$ and $$$(2, 7)$$$. The deck is then ordered $$$(3,10), (5,8), (6,4), (7,2), (9,1)$$$. It is sorted because $$$3<5<6<7<9$$$ and $$$10>8>4>2>1$$$.In the second test case, it is impossible to sort the deck."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nalias Value = int;\r\nint root(int[] ps, Value[] qs, int u) {\r\n if (ps[u] < 0) {\r\n return u;\r\n } else {\r\n int r = root(ps, qs, ps[u]);\r\n // qs[u] += qs[ps[u]];\r\n qs[u] ^= qs[ps[u]];\r\n return (ps[u] = r);\r\n }\r\n}\r\nbool connect(int[] ps, Value[] qs, int u, int v, Value c) {\r\n int ru = root(ps, qs, u);\r\n int rv = root(ps, qs, v);\r\n // const Value cc = c + qs[u] - qs[v];\r\n const Value cc = c ^ qs[u] ^ qs[v];\r\n if (ru == rv) return (cc == 0);\r\n // if (ps[ru] > ps[rv]) { swap(ru, rv); cc *= -1; }\r\n if (ps[ru] > ps[rv]) { swap(ru, rv); }\r\n ps[ru] += ps[rv]; ps[rv] = ru; qs[rv] = cc;\r\n return true;\r\n}\r\n\r\n\r\nalias Card = Tuple!(int, \"a\", int, \"b\");\r\n\r\nint N;\r\nCard[] C;\r\n\r\nint brute() {\r\n int ans = N + 1;\r\n foreach (p; 0 .. 1 << N) {\r\n foreach (u; 0 .. N) if (p & 1 << u) swap(C[u].a, C[u].b);\r\n bool ok = true;\r\n foreach (u; 0 .. N) foreach (v; 0 .. N) {\r\n if (C[u].a < C[v].a) {\r\n ok = ok && (C[u].b > C[v].b);\r\n }\r\n }\r\n if (ok) {\r\n chmin(ans, popcnt(p));\r\n }\r\n foreach (u; 0 .. N) if (p & 1 << u) swap(C[u].a, C[u].b);\r\n }\r\n return (ans > N) ? -1 : ans;\r\n}\r\n\r\nint solve() {\r\n auto ps = new int[2 * N];\r\n auto qs = new int[2 * N];\r\n ps[] = -1;\r\n \r\n C.sort!((c0, c1) => (min(c0.a, c0.b) < min(c1.a, c1.b)));\r\n foreach (u; 0 .. N) {\r\n if (C[u].a < C[u].b) {\r\n connect(ps, qs, u, N + u, 0);\r\n } else {\r\n swap(C[u].a, C[u].b);\r\n connect(ps, qs, u, N + u, 1);\r\n }\r\n }\r\n \r\n int minB = 2 * N + 1, maxA = 0;\r\n foreach (u; 0 .. N) {\r\n chmin(minB, C[u].b);\r\n chmax(maxA, C[u].a);\r\n }\r\n if (minB < maxA) {\r\n return -1;\r\n }\r\n \r\n int[] us, vs;\r\n foreach (u; 0 .. N) {\r\n if (us.empty || C[us[$ - 1]].b > C[u].b) {\r\n us ~= u;\r\n } else if (vs.empty || C[vs[$ - 1]].b > C[u].b) {\r\n vs ~= u;\r\n } else {\r\n return -1;\r\n }\r\n }\r\n const usLen = cast(int)(us.length);\r\n const vsLen = cast(int)(vs.length);\r\n debug {\r\n // writeln(\"C = \", C);\r\n // writeln(\"us = \", us);\r\n // writeln(\"vs = \", vs);\r\n }\r\n \r\n auto vals = new int[vsLen + 1];\r\n {\r\n int f;\r\n foreach (e; 0 .. usLen) {\r\n for (; f < vsLen && vs[f] < us[e]; ++f) {}\r\n int lo = f - 1, hi = vsLen;\r\n for (; lo + 1 < hi; ) {\r\n const mid = (lo + hi) / 2;\r\n ((C[us[e]].b < C[vs[mid]].b) ? lo : hi) = mid;\r\n }\r\n // e - [f, lo]\r\n if (f <= lo) {\r\n debug {\r\n // writefln(\"%s: [%s, %s]\", e, f, lo);\r\n }\r\n if (!connect(ps, qs, us[e], vs[f], 1)) {\r\n return false;\r\n }\r\n ++vals[f];\r\n --vals[lo];\r\n }\r\n }\r\n }\r\n foreach (f; 0 .. vsLen) {\r\n vals[f + 1] += vals[f];\r\n }\r\n debug {\r\n // writeln(\"vals = \", vals);\r\n }\r\n foreach (f; 0 .. vsLen - 1) {\r\n if (vals[f] > 0) {\r\n debug {\r\n // writefln(\"connect0 %s %s\", vs[f], vs[f + 1]);\r\n }\r\n if (!connect(ps, qs, vs[f], vs[f + 1], 0)) {\r\n return false;\r\n }\r\n }\r\n }\r\n \r\n auto cnt = new int[][](2 * N, 2);\r\n foreach (u; 0 .. N) {\r\n const r = root(ps, qs, N + u);\r\n ++cnt[r][qs[N + u]];\r\n }\r\n debug {\r\n // writeln(\"cnt = \", cnt);\r\n // writeln(\"ps = \", ps);\r\n // writeln(\"qs = \", qs);\r\n }\r\n int ans;\r\n foreach (r; 0 .. 2 * N) {\r\n ans += min(cnt[r][0], cnt[r][1]);\r\n }\r\n return ans;\r\n}\r\n\r\nvoid main() {\r\n try {\r\n for (; ; ) {\r\n N = readInt();\r\n C = new Card[N];\r\n foreach (u; 0 .. N) {\r\n C[u].a = readInt();\r\n C[u].b = readInt();\r\n }\r\n \r\n debug {\r\n const CSave = C.dup;\r\n const brt = brute();\r\n }\r\n \r\n const ans = solve();\r\n writeln(ans);\r\n \r\n debug {\r\n assert(brt == ans, format(\"%s: %s %s\", CSave, brt, ans));\r\n }\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nalias Value = int;\r\nint root(int[] ps, Value[] qs, int u) {\r\n if (ps[u] < 0) {\r\n return u;\r\n } else {\r\n int r = root(ps, qs, ps[u]);\r\n // qs[u] += qs[ps[u]];\r\n qs[u] ^= qs[ps[u]];\r\n return (ps[u] = r);\r\n }\r\n}\r\nbool connect(int[] ps, Value[] qs, int u, int v, Value c) {\r\n int ru = root(ps, qs, u);\r\n int rv = root(ps, qs, v);\r\n // const Value cc = c + qs[u] - qs[v];\r\n const Value cc = c ^ qs[u] ^ qs[v];\r\n if (ru == rv) return (cc == 0);\r\n // if (ps[ru] > ps[rv]) { swap(ru, rv); cc *= -1; }\r\n if (ps[ru] > ps[rv]) { swap(ru, rv); }\r\n ps[ru] += ps[rv]; ps[rv] = ru; qs[rv] = cc;\r\n return true;\r\n}\r\n\r\n\r\nalias Card = Tuple!(int, \"a\", int, \"b\");\r\n\r\nint N;\r\nCard[] C;\r\n\r\nint brute() {\r\n auto ps = new int[N];\r\n auto qs = new int[N];\r\n ps[] = -1;\r\n foreach (u; 0 .. N) foreach (v; u + 1 .. N) {\r\n int au = C[u].a, bu = C[u].b;\r\n int av = C[v].a, bv = C[v].b;\r\n if (au > av) {\r\n swap(au, av);\r\n swap(bu, bv);\r\n }\r\n if (bu < av) {\r\n return -1;\r\n }\r\n if (bu < bv) {\r\n if (!connect(ps, qs, u, v, 1)) {\r\n return -1;\r\n }\r\n }\r\n }\r\n auto cnt = new int[][](N, 2);\r\n foreach (u; 0 .. N) {\r\n const r = root(ps, qs, u);\r\n ++cnt[r][qs[u]];\r\n }\r\n int ans;\r\n foreach (r; 0 .. N) {\r\n ans += min(cnt[r][0], cnt[r][1]);\r\n }\r\n return ans;\r\n}\r\n\r\nint solve() {\r\n foreach (u; 0 .. N) {\r\n if (C[u].a > C[u].b) {\r\n swap(C[u].a, C[u].b);\r\n }\r\n }\r\n C.sort;\r\n debug {\r\n // writeln(\"C = \", C);\r\n }\r\n \r\n int minB = 2 * N + 1, maxA = 0;\r\n foreach (u; 0 .. N) {\r\n chmin(minB, C[u].b);\r\n chmax(maxA, C[u].a);\r\n }\r\n if (minB < maxA) {\r\n return -1;\r\n }\r\n \r\n int[] us, vs;\r\n foreach (u; 0 .. N) {\r\n if (us.empty || C[us[$ - 1]].b > C[u].b) {\r\n us ~= u;\r\n } else if (vs.empty || C[vs[$ - 1]].b > C[u].b) {\r\n vs ~= u;\r\n } else {\r\n return -1;\r\n }\r\n }\r\n const usLen = cast(int)(us.length);\r\n const vsLen = cast(int)(vs.length);\r\n debug {\r\n // writeln(\"us = \", us);\r\n // writeln(\"vs = \", vs);\r\n }\r\n \r\n auto ps = new int[N];\r\n auto qs = new int[N];\r\n ps[] = -1;\r\n auto vals = new int[vsLen + 1];\r\n {\r\n int f;\r\n foreach (e; 0 .. usLen) {\r\n for (; f < vsLen && vs[f] < us[e]; ++f) {}\r\n int lo = f - 1, hi = vsLen;\r\n for (; lo + 1 < hi; ) {\r\n const mid = (lo + hi) / 2;\r\n ((C[us[e]].b < C[vs[mid]].b) ? lo : hi) = mid;\r\n }\r\n // e - [f, lo]\r\n if (f <= lo) {\r\n debug {\r\n // writefln(\"%s: [%s, %s]\", e, f, lo);\r\n }\r\n if (!connect(ps, qs, us[e], vs[f], 1)) {\r\n return false;\r\n }\r\n ++vals[f];\r\n --vals[lo];\r\n }\r\n }\r\n }\r\n foreach (f; 0 .. vsLen) {\r\n vals[f + 1] += vals[f];\r\n }\r\n debug {\r\n // writeln(\"vals = \", vals);\r\n }\r\n foreach (f; 0 .. vsLen - 1) {\r\n if (vals[f] > 0) {\r\n debug {\r\n // writefln(\"connect0 %s %s\", vs[f], vs[f + 1]);\r\n }\r\n if (!connect(ps, qs, vs[f], vs[f + 1], 0)) {\r\n return false;\r\n }\r\n }\r\n }\r\n \r\n auto cnt = new int[][](N, 2);\r\n foreach (u; 0 .. N) {\r\n const r = root(ps, qs, u);\r\n ++cnt[r][qs[u]];\r\n }\r\n int ans;\r\n foreach (r; 0 .. N) {\r\n ans += min(cnt[r][0], cnt[r][1]);\r\n }\r\n return ans;\r\n}\r\n\r\nvoid main() {\r\n try {\r\n for (; ; ) {\r\n N = readInt();\r\n C = new Card[N];\r\n foreach (u; 0 .. N) {\r\n C[u].a = readInt();\r\n C[u].b = readInt();\r\n }\r\n \r\n const ans = solve();\r\n writeln(ans);\r\n \r\n debug {\r\n const brt = brute();\r\n assert(brt == ans, format(\"%s: %s %s\", C, brt, ans));\r\n }\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}], "src_uid": "b1a59d0c59f3e8fd3bf76de6463c6847"} {"nl": {"description": "Luntik came out for a morning stroll and found an array $$$a$$$ of length $$$n$$$. He calculated the sum $$$s$$$ of the elements of the array ($$$s= \\sum_{i=1}^{n} a_i$$$). Luntik calls a subsequence of the array $$$a$$$ nearly full if the sum of the numbers in that subsequence is equal to $$$s-1$$$.Luntik really wants to know the number of nearly full subsequences of the array $$$a$$$. But he needs to come home so he asks you to solve that problem!A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The next $$$2 \\cdot t$$$ lines contain descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 60$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the elements of the array $$$a$$$.", "output_spec": "For each test case print the number of nearly full subsequences of the array.", "sample_inputs": ["5\n5\n1 2 3 4 5\n2\n1000 1000\n2\n1 0\n5\n3 0 2 1 1\n5\n2 1 0 3 0"], "sample_outputs": ["1\n0\n2\n4\n4"], "notes": "NoteIn the first test case, $$$s=1+2+3+4+5=15$$$, only $$$(2,3,4,5)$$$ is a nearly full subsequence among all subsequences, the sum in it is equal to $$$2+3+4+5=14=15-1$$$.In the second test case, there are no nearly full subsequences.In the third test case, $$$s=1+0=1$$$, the nearly full subsequences are $$$(0)$$$ and $$$()$$$ (the sum of an empty subsequence is $$$0$$$)."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\n\t\tlong cnt0, cnt1;\n\t\tforeach (e; a)\n\t\t{\n\t\t\tif (e == 0)\n\t\t\t\t++cnt0;\n\t\t\telse if (e == 1)\n\t\t\t\t++cnt1;\n\t\t}\n\t\tans[ti] = cnt1 * (2L^^cnt0);\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "d786585fee251ebfd5c3e8d8b0425791"} {"nl": {"description": "There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that starts in the upper left cell of the matrix; each following cell is to the right or down from the current cell; the way ends in the bottom right cell. Moreover, if we multiply together all the numbers along the way, the result should be the least \"round\". In other words, it should end in the least possible number of zeros.", "input_spec": "The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).", "output_spec": "In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.", "sample_inputs": ["3\n1 2 3\n4 5 6\n7 8 9"], "sample_outputs": ["0\nDDRR"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.conv;\nimport std.algorithm;\n\nvoid printmat(int[][] mat)\n{\n\tfor (int i = 0; i < mat.length; i++)\n\tfor (int j = 0; j < mat[i].length; j++)\n\t{\n\t\twrite(to!string(mat[i][j]) ~ \" \");\n\t\tif (j == mat[i].length - 1)\n\t\t\twriteln();\n\t}\n}\n\nint getpow5(int num)\n{\n\tif (num == 0)\n\t\treturn 0;\n\tint count = 0;\n\tint n = num;\n\twhile ((n % 5) == 0)\n\t{\n\t\tcount++;\n\t\tn /= 5;\n\t}\n\treturn count;\n}\n\nint getpow2(int num)\n{\n\tif (num == 0)\n\t\treturn 0;\n\tint count = 0;\n\tint n = num;\n\twhile ((n & 1) == 0)\n\t{\n\t\tcount++;\n\t\tn >>= 1;\n\t}\n\treturn count;\n}\n\nint main(string[] argv)\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint[][] mat; mat.length = n;\n\tbool hasZero = false;\n\tint zx = -1;\n\tint zy = -1;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tmat[i].length = n;\n\t\tfor (int j = 0; j < n; j++)\n\t\t{\n\t\t\tscanf(\"%d\", &mat[i][j]);\n\t\t\tif (mat[i][j] == 0)\n\t\t\t{\n\t\t\t\thasZero = true;\n\t\t\t\tzx = i;\n\t\t\t\tzy = j;\n\t\t\t}\n\t\t}\n\t}\t\n\t//printmat(mat);\n\n\tint[][] num5; num5.length = n;\n\tnum5[0].length = n;\n\tnum5[0][0] = getpow5(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum5[0][j] = getpow5(mat[0][j]);\n\t\tnum5[0][j] += num5[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum5[i].length = n;\n\t\tnum5[i][0] = getpow5(mat[i][0]);\n\t\tnum5[i][0] += num5[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum5[i][j] = getpow5(mat[i][j]);\t\n\t\t\tnum5[i][j] += min(num5[i-1][j], num5[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num5);\n\n\tint[][] num2; num2.length = n;\n\tnum2[0].length = n;\n\tnum2[0][0] = getpow2(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum2[0][j] = getpow2(mat[0][j]);\n\t\tnum2[0][j] += num2[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum2[i].length = n;\n\t\tnum2[i][0] = getpow2(mat[i][0]);\n\t\tnum2[i][0] += num2[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum2[i][j] = getpow2(mat[i][j]);\t\n\t\t\tnum2[i][j] += min(num2[i-1][j], num2[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num2);\n\t//writeln();\n\n\tint[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);\n\tif (hasZero && sol[n-1][n-1] >= 1)\n\t{\n\t\twriteln(\"1\");\n\t\tfor (int i = 0; i < zx; i++)\n\t\t{\n\t\t\twrite(\"D\");\n\t\t}\n\t\tfor (int i = 0; i < zy; i++)\n\t\t{\n\t\t\twrite(\"R\");\n\t\t}\n\t\tfor (int i = zx; i < (n-1); i++)\n\t\t{\n\t\t\twrite(\"D\");\n\t\t}\n\t\tfor (int i = zy; i < (n-1); i++)\n\t\t{\n\t\t\twrite(\"R\");\n\t\t}\n\t\twriteln();\n\t} else {\t\t\t\n\t\tint cx = n - 1;\n\t\tint cy = n - 1;\n\t\tint st = 2*(n-1);\n\t\tchar[] trail = new char[st];\n\t\tfor (int i = 0; i < st; i++)\n\t\t{\n\t\t\tif (cx == 0 && cy != 0)\n\t\t\t{\n\t\t\t\ttrail[st - i - 1] = 'R';\n\t\t\t\tcy--;\n\t\t\t} else\n\t\t\tif (cx != 0 && cy == 0)\n\t\t\t{\n\t\t\t\ttrail[st - i - 1] = 'D';\n\t\t\t\tcx--;\n\t\t\t} else {\n\t\t\t\tint t = sol[cx-1][cy];\n\t\t\t\tint l = sol[cx][cy-1];\n\t\t\t\tif (t < l)\n\t\t\t\t{\n\t\t\t\t\ttrail[st - i - 1] = 'D';\n\t\t\t\t\tcx--;\n\t\t\t\t} else {\n\t\t\t\t\ttrail[st - i - 1] = 'R';\n\t\t\t\t\tcy--;\n\t\t\t\t}\t\t\t\n\t\t\t}\n\t\t}\t\t\t\t\n\t\twriteln(sol[n-1][n-1]);\n\t\twriteln(trail);\n\t}\n\treturn 0;\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.conv;\nimport std.algorithm;\n\nvoid printmat(int[][] mat)\n{\n\tfor (int i = 0; i < mat.length; i++)\n\tfor (int j = 0; j < mat[i].length; j++)\n\t{\n\t\twrite(to!string(mat[i][j]) ~ \" \");\n\t\tif (j == mat[i].length - 1)\n\t\t\twriteln();\n\t}\n}\n\nint getpow5(int num)\n{\n\tif (num == 0)\n\t\treturn 0;\n\tint count = 0;\n\tint n = num;\n\twhile ((n % 5) == 0)\n\t{\n\t\tcount++;\n\t\tn /= 5;\n\t}\n\treturn count;\n}\n\nint getpow2(int num)\n{\n\tif (num == 0)\n\t\treturn 0;\n\tint count = 0;\n\tint n = num;\n\twhile ((n & 1) == 0)\n\t{\n\t\tcount++;\n\t\tn >>= 1;\n\t}\n\treturn count;\n}\n\nint main(string[] argv)\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint[][] mat; mat.length = n;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tmat[i].length = n;\n\t\tfor (int j = 0; j < n; j++)\n\t\t{\n\t\t\tscanf(\"%d\", &mat[i][j]);\n\t\t}\n\t}\t\n\t//printmat(mat);\n\n\tint[][] num5; num5.length = n;\n\tnum5[0].length = n;\n\tnum5[0][0] = getpow5(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum5[0][j] = getpow5(mat[0][j]);\n\t\tnum5[0][j] += num5[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum5[i].length = n;\n\t\tnum5[i][0] = getpow5(mat[i][0]);\n\t\tnum5[i][0] += num5[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum5[i][j] = getpow5(mat[i][j]);\t\n\t\t\tnum5[i][j] += min(num5[i-1][j], num5[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num5);\n\n\tint[][] num2; num2.length = n;\n\tnum2[0].length = n;\n\tnum2[0][0] = getpow2(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum2[0][j] = getpow2(mat[0][j]);\n\t\tnum2[0][j] += num2[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum2[i].length = n;\n\t\tnum2[i][0] = getpow2(mat[i][0]);\n\t\tnum2[i][0] += num2[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum2[i][j] = getpow2(mat[i][j]);\t\n\t\t\tnum2[i][j] += min(num2[i-1][j], num2[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num2);\n\t//writeln();\n\n\tint[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);\n\tint cx = n - 1;\n\tint cy = n - 1;\n\tint st = 2*(n-1);\n\tchar[] trail = new char[st];\n\tfor (int i = 0; i < st; i++)\n\t{\n\t\tif (cx == 0 && cy != 0)\n\t\t{\n\t\t\ttrail[st - i - 1] = 'R';\n\t\t\tcy--;\n\t\t} else\n\t\tif (cx != 0 && cy == 0)\n\t\t{\n\t\t\ttrail[st - i - 1] = 'D';\n\t\t\tcx--;\n\t\t} else {\n\t\t\tint t = sol[cx-1][cy];\n\t\t\tint l = sol[cx][cy-1];\n\t\t\tif (t < l)\n\t\t\t{\n\t\t\t\ttrail[st - i - 1] = 'D';\n\t\t\t\tcx--;\n\t\t\t} else {\n\t\t\t\ttrail[st - i - 1] = 'R';\n\t\t\t\tcy--;\n\t\t\t}\t\t\t\n\t\t}\n\t}\t\t\t\t\n\twriteln(sol[n-1][n-1]);\n\twriteln(trail);\n\treturn 0;\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.algorithm;\nimport std.string;\n\nint main()\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint[][] m; m.length = n;\n\tforeach (int i; 0..n)\n\t{\n\t\tm[i].length = n;\n\t\tforeach (int j; 0..n)\n\t\t{\n\t\t\tscanf(\"%d\", &m[i][j]);\n\t\t}\n\t}\n\n\tint[][] ml; ml.length = n;\n\tforeach (int i; 0..n)\n\t{\n\t\tml[i].length = n;\n\t\tforeach (int j; 0..n)\n\t\t{\n\t\t\tint n10 = m[i][j] % 10 == 0 ? m[i][j] / 10 : 0;\n\t\t\tint n5 = n10 == 0 && m[i][j] % 5 == 0 ? m[i][j] / 5 : 0;\n\t\t\tint n2 = n10 == 0 && m[i][j] % 2 == 0 ? m[i][j] / 2 : 0;\n\t\t\tml[i][j] = n10 * 3 + n5 + n2;\n\t\t}\n\t}\n\n\tforeach (int i; 0..n)\n\t{\n\t\tforeach (int j; 0..n)\n\t\t{\n\t\t\tint a = i - 1 >= 0 ? ml[i-1][j] : 0;\n\t\t\tint b = j - 1 >= 0 ? ml[i][j-1] : 0;\n\t\t\tml[i][j] += min(a, b);\n\t\t}\n\t}\n\n\tint i = n-1; int j = n-1;\n\tint total = 0;\n\tstring sol = \"\";\n\twhile (i > 0 || j > 0)\n\t{\n\t\tint nz = m[i][j] % 10 == 0 ? m[i][j] / 10 : 0;\n\t\ttotal += nz;\n\t\tint ni = i - 1 >= 0 ? m[i - 1][j] : int.max;\n\t\tint nj = j - 1 >= 0 ? m[i][j - 1] : int.max;\n\t\tif (ni < nj)\n\t\t{\n\t\t\tsol ~= \"D\";\n\t\t\ti--;\n\t\t} else {\n\t\t\tsol ~= \"R\";\n\t\t\tj--;\n\t\t}\n\t}\n\tchar[] ns = sol.dup;\n\tns.reverse();\n\tprintf(\"%d\\n%s\", total, toStringz(ns));\n\treturn 0;\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.algorithm;\nimport std.string;\n\nint main()\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint[][] m; m.length = n;\n\tforeach (int i; 0..n)\n\t{\n\t\tm[i].length = n;\n\t\tforeach (int j; 0..n)\n\t\t{\n\t\t\tscanf(\"%d\", &m[i][j]);\n\t\t}\n\t}\n\n\tint[][] ml; ml.length = n;\n\tforeach (int i; 0..n)\n\t{\n\t\tml[i].length = n;\n\t\tforeach (int j; 0..n)\n\t\t{\n\t\t\tint n10 = m[i][j] % 10 == 0 ? m[i][j] / 10 : 0;\n\t\t\tint n5 = n10 == 0 && m[i][j] % 5 == 0 ? m[i][j] / 5 : 0;\n\t\t\tint n2 = n10 == 0 && m[i][j] % 2 == 0 ? m[i][j] / 2 : 0;\n\t\t\tml[i][j] = n10 * 3 + n5 + n2;\n\t\t}\n\t}\n\n\tforeach (int i; 0..n)\n\t{\n\t\tforeach (int j; 0..n)\n\t\t{\n\t\t\tint a = i - 1 >= 0 ? ml[i-1][j] : 0;\n\t\t\tint b = j - 1 >= 0 ? ml[i][j-1] : 0;\n\t\t\tml[i][j] += min(a, b);\n\t\t}\n\t}\n\n\tint i = n-1; int j = n-1;\n\tint total = 0;\n\tstring sol = \"\";\n\tint mnwz = 1;\n\twhile (i > 0 || j > 0)\n\t{\n\t\tmnwz *= m[i][j];\n\t\tint nz = 0;\n\t\twhile (mnwz % 10 == 0)\n\t\t{\n\t\t\tnz++; mnwz /= 10;\n\t\t}\n\t\ttotal += nz;\n\t\tint ni = i - 1 >= 0 ? m[i - 1][j] : int.max;\n\t\tint nj = j - 1 >= 0 ? m[i][j - 1] : int.max;\n\t\tif (ni < nj)\n\t\t{\n\t\t\tsol ~= \"D\";\n\t\t\ti--;\n\t\t} else {\n\t\t\tsol ~= \"R\";\n\t\t\tj--;\n\t\t}\n\t}\n\tchar[] ns = sol.dup;\n\tns.reverse();\n\tprintf(\"%d\\n%s\", total, toStringz(ns));\n\treturn 0;\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.conv;\nimport std.algorithm;\n\nvoid printmat(int[][] mat)\n{\n\tfor (int i = 0; i < mat.length; i++)\n\tfor (int j = 0; j < mat[i].length; j++)\n\t{\n\t\twrite(to!string(mat[i][j]) ~ \" \");\n\t\tif (j == mat[i].length - 1)\n\t\t\twriteln();\n\t}\n}\n\nint getpow5(int num)\n{\n\tint count = 0;\n\tint n = num;\n\twhile ((n % 5) == 0)\n\t{\n\t\tcount++;\n\t\tn /= 5;\n\t}\n\treturn count;\n}\n\nint getpow2(int num)\n{\n\tint count = 0;\n\tint n = num;\n\twhile ((n & 1) == 0)\n\t{\n\t\tcount++;\n\t\tn >>= 1;\n\t}\n\treturn count;\n}\n\nint main(string[] argv)\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint[][] mat; mat.length = n;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tmat[i].length = n;\n\t\tfor (int j = 0; j < n; j++)\n\t\t{\n\t\t\tscanf(\"%d\", &mat[i][j]);\n\t\t}\n\t}\t\n\t//printmat(mat);\n\n\tint[][] num5; num5.length = n;\n\tnum5[0].length = n;\n\tnum5[0][0] = getpow5(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum5[0][j] = getpow5(mat[0][j]);\n\t\tnum5[0][j] += num5[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum5[i].length = n;\n\t\tnum5[i][0] = getpow5(mat[i][0]);\n\t\tnum5[i][0] += num5[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum5[i][j] = getpow5(mat[i][j]);\t\n\t\t\tnum5[i][j] += min(num5[i-1][j], num5[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num5);\n\n\tint[][] num2; num2.length = n;\n\tnum2[0].length = n;\n\tnum2[0][0] = getpow2(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum2[0][j] = getpow2(mat[0][j]);\n\t\tnum2[0][j] += num2[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum2[i].length = n;\n\t\tnum2[i][0] = getpow2(mat[i][0]);\n\t\tnum2[i][0] += num2[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum2[i][j] = getpow2(mat[i][j]);\t\n\t\t\tnum2[i][j] += min(num2[i-1][j], num2[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num2);\n\t//writeln();\n\n\tint[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);\n\tint cx = n - 1;\n\tint cy = n - 1;\n\tstring trail = \"\";\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tif (cx == 0 && cy != 0)\n\t\t{\n\t\t\ttrail ~= \"R\";\n\t\t\tcy--;\n\t\t} else\n\t\tif (cx != 0 && cy == 0)\n\t\t{\n\t\t\ttrail ~= \"D\";\n\t\t\tcx--;\n\t\t} else {\n\t\t\tint t = sol[cx-1][cy];\n\t\t\tint l = sol[cx][cy-1];\n\t\t\tif (t < l)\n\t\t\t{\n\t\t\t\ttrail ~= \"D\";\n\t\t\t\tcx--;\n\t\t\t} else {\n\t\t\t\ttrail ~= \"R\";\n\t\t\t\tcy--;\n\t\t\t}\t\t\t\n\t\t}\n\t}\t\t\t\t\n\twriteln(sol[n-1][n-1]);\n\tchar[] mt = trail.dup;\n\tmt.reverse();\n\twriteln(mt);\n\treturn 0;\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.conv;\nimport std.algorithm;\n\nvoid printmat(int[][] mat)\n{\n\tfor (int i = 0; i < mat.length; i++)\n\tfor (int j = 0; j < mat[i].length; j++)\n\t{\n\t\twrite(to!string(mat[i][j]) ~ \" \");\n\t\tif (j == mat[i].length - 1)\n\t\t\twriteln();\n\t}\n}\n\nint getpow5(int num)\n{\n\tif (num == 0)\n\t\treturn 0;\n\tint count = 0;\n\tint n = num;\n\twhile ((n % 5) == 0)\n\t{\n\t\tcount++;\n\t\tn /= 5;\n\t}\n\treturn count;\n}\n\nint getpow2(int num)\n{\n\tif (num == 0)\n\t\treturn 0;\n\tint count = 0;\n\tint n = num;\n\twhile ((n & 1) == 0)\n\t{\n\t\tcount++;\n\t\tn >>= 1;\n\t}\n\treturn count;\n}\n\nint main(string[] argv)\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint[][] mat; mat.length = n;\n\tbool hasZero = false;\n\tint zx = -1;\n\tint zy = -1;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tmat[i].length = n;\n\t\tfor (int j = 0; j < n; j++)\n\t\t{\n\t\t\tscanf(\"%d\", &mat[i][j]);\n\t\t\tif (mat[i][j] == 0)\n\t\t\t{\n\t\t\t\thasZero = true;\n\t\t\t\tzx = i;\n\t\t\t\tzy = j;\n\t\t\t}\n\t\t}\n\t}\t\n\t//printmat(mat);\n\n\tint[][] num5; num5.length = n;\n\tnum5[0].length = n;\n\tnum5[0][0] = getpow5(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum5[0][j] = getpow5(mat[0][j]);\n\t\tnum5[0][j] += num5[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum5[i].length = n;\n\t\tnum5[i][0] = getpow5(mat[i][0]);\n\t\tnum5[i][0] += num5[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum5[i][j] = getpow5(mat[i][j]);\t\n\t\t\tnum5[i][j] += min(num5[i-1][j], num5[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num5);\n\n\tint[][] num2; num2.length = n;\n\tnum2[0].length = n;\n\tnum2[0][0] = getpow2(mat[0][0]);\n\tfor (int j = 1; j < n; j++)\n\t{\n\t\tnum2[0][j] = getpow2(mat[0][j]);\n\t\tnum2[0][j] += num2[0][j-1]; \n\t}\t\n\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tnum2[i].length = n;\n\t\tnum2[i][0] = getpow2(mat[i][0]);\n\t\tnum2[i][0] += num2[i-1][0];\n\t\tfor (int j = 1; j < n; j++)\n\t\t{\n\t\t\tnum2[i][j] = getpow2(mat[i][j]);\t\n\t\t\tnum2[i][j] += min(num2[i-1][j], num2[i][j-1]);\n\t\t}\n\t}\n\t//printmat(num2);\n\t//writeln();\n\n\tint[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);\n\tif (hasZero && sol[n-1][n-1] >= 1)\n\t{\n\t\twriteln(\"1\");\n\t\tfor (int i = 0; i < zx; i++)\n\t\t{\n\t\t\twrite(\"D\");\n\t\t}\n\t\tfor (int i = 0; i < zy; i++)\n\t\t{\n\t\t\twrite(\"R\");\n\t\t}\n\t\tfor (int i = zx; i < n; i++)\n\t\t{\n\t\t\twrite(\"D\");\n\t\t}\n\t\tfor (int i = zy; i < n; i++)\n\t\t{\n\t\t\twrite(\"R\");\n\t\t}\n\t\twriteln();\n\t} else {\t\t\t\n\t\tint cx = n - 1;\n\t\tint cy = n - 1;\n\t\tint st = 2*(n-1);\n\t\tchar[] trail = new char[st];\n\t\tfor (int i = 0; i < st; i++)\n\t\t{\n\t\t\tif (cx == 0 && cy != 0)\n\t\t\t{\n\t\t\t\ttrail[st - i - 1] = 'R';\n\t\t\t\tcy--;\n\t\t\t} else\n\t\t\tif (cx != 0 && cy == 0)\n\t\t\t{\n\t\t\t\ttrail[st - i - 1] = 'D';\n\t\t\t\tcx--;\n\t\t\t} else {\n\t\t\t\tint t = sol[cx-1][cy];\n\t\t\t\tint l = sol[cx][cy-1];\n\t\t\t\tif (t < l)\n\t\t\t\t{\n\t\t\t\t\ttrail[st - i - 1] = 'D';\n\t\t\t\t\tcx--;\n\t\t\t\t} else {\n\t\t\t\t\ttrail[st - i - 1] = 'R';\n\t\t\t\t\tcy--;\n\t\t\t\t}\t\t\t\n\t\t\t}\n\t\t}\t\t\t\t\n\t\twriteln(sol[n-1][n-1]);\n\t\twriteln(trail);\n\t}\n\treturn 0;\n}"}], "src_uid": "13c58291ab9cf7ad1b8c466c3e36aacf"} {"nl": {"description": "In this problem we consider Boolean functions of four variables A, B, C, D. Variables A, B, C and D are logical and can take values 0 or 1. We will define a function using the following grammar:<expression> ::= <variable> | (<expression>) <operator> (<expression>)<variable> ::= 'A' | 'B' | 'C' | 'D' | 'a' | 'b' | 'c' | 'd'<operator> ::= '&' | '|'Here large letters A, B, C, D represent variables, and small letters represent their negations. For example, if A = 1, then character 'A' corresponds to value 1, and value character 'a' corresponds to value 0. Here character '&' corresponds to the operation of logical AND, character '|' corresponds to the operation of logical OR.You are given expression s, defining function f, where some operations and variables are missing. Also you know the values of the function f(A, B, C, D) for some n distinct sets of variable values. Count the number of ways to restore the elements that are missing in the expression so that the resulting expression corresponded to the given information about function f in the given variable sets. As the value of the result can be rather large, print its remainder modulo 109 + 7.", "input_spec": "The first line contains expression s (1 ≤ |s| ≤ 500), where some characters of the operators and/or variables are replaced by character '?'. The second line contains number n (0 ≤ n ≤ 24) — the number of integers sets for which we know the value of function f(A, B, C, D). Next n lines contain the descriptions of the sets: the i-th of them contains five integers ai, bi, ci, di, ei (0 ≤ ai, bi, ci, di, ei ≤ 1), separated by spaces and meaning that f(ai, bi, ci, di) = ei. It is guaranteed that all the tuples (ai, bi, ci, di) are distinct.", "output_spec": "In a single line print the answer to the problem.", "sample_inputs": ["?\n2\n1 0 1 0 1\n0 1 1 0 1", "(A)?(?)\n1\n1 1 0 0 0", "((?)&(?))|((?)&(?))\n0", "b\n1\n1 0 1 1 1"], "sample_outputs": ["2", "4", "4096", "1"], "notes": "NoteIn the first sample the two valid expressions are 'C' and 'd'.In the second sample the expressions look as follows: '(A)&(a)', '(A)&(b)', '(A)&(C)', '(A)&(D)'."}, "positive_code": [{"source_code": "import std.stdio, std.conv;\nimport std.algorithm, std.range, std.random;\nimport std.string, std.array, std.container, std.bigint;\n\n\nimmutable S = 4;\nimmutable N = 1<> j) & 1) == i) {\n\t\t\t\t\tm |= (1<>l[j][0]) & 1) != l[j][1]) {\n\t\t\t\tf = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!f) continue;\n\t\tsm += p[i];\n\t\tsm %= MD;\n\t}\n\tsm = (sm + MD) % MD;\n\twriteln(sm);\n\treturn 0;\n}"}], "negative_code": [], "src_uid": "34443b67ce495d17c61fc15154c7326d"} {"nl": {"description": "There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).", "input_spec": "The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.", "output_spec": "Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.", "sample_inputs": ["7 3\n1 2 6 1 1 7 1"], "sample_outputs": ["3"], "notes": "NoteIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\n\nvoid main() {\n int n, k; readf(\"%d %d\\n\", &n, &k);\n int[] xs = stdin.readln.chomp.split(\" \").map!(to!int).array;\n int[] ss = new int[xs.length+1];\n ss[0] = 0;\n for (int i = 0; i < xs.length; i++) {\n ss[i+1] = ss[i] + xs[i];\n }\n int ans = int.max;\n int index = -1;\n for (int i = 0; i <= xs.length - k; i++) {\n if (ans > ss[i+k] - ss[i]) {\n ans = ss[i+k] - ss[i];\n index = i + 1;\n }\n }\n index.writeln;\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1];\n auto H = readln.split.map!(to!int).array;\n\n auto acm = new int[](N+1);\n foreach (i; 0..N) {\n acm[i+1] = acm[i] + H[i];\n }\n\n \n int minv = int.max;\n int mini = -1;\n\n foreach (i; 0..N-K+1) {\n auto a = acm[i+K] - acm[i];\n if (a < minv) {\n minv = a;\n mini = i+1;\n }\n }\n mini.writeln;\n}\n"}, {"source_code": "import std.stdio\n , std.typecons\n , std.traits\n , std.algorithm\n , std.range\n , std.container;\n\n\nvoid main() {\n uint n, k;\n readf(\"%s %s\\n\", &n, &k);\n \n auto fence = new uint[n];\n auto sum_arr = new uint[n+1];\n \n uint f_sum;\n \n foreach(i, ref f; fence) {\n //uint f;\n readf(\"%s \", &f);\n sum_arr[i+1] = f_sum + f;\n f_sum += f;\n }\n \n uint min = sum_arr[k] - sum_arr[0], min_idx = 1;\n \n for(uint j = k; j < n+1; ++j) {\n if(sum_arr[j] - sum_arr[j - k] < min) {\n min = sum_arr[j] - sum_arr[j - k];\n min_idx = j - k + 1;\n }\n }\n \n min_idx.writeln;\n //foreach()\n}\n\n\nalias id(alias token) = token;\n\nalias var(alias elem) = std.traits.Unqual!(typeof(elem));\n\nauto mut(T)(auto ref T in_val) {\n std.traits.Unqual!(T) rv = in_val;\n return rv;\n}\n\nauto imut(T)(auto ref T in_val) {\n immutable immrv = in_val;\n return immrv;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "import std.stdio\n , std.typecons\n , std.traits\n , std.algorithm\n , std.range\n , std.container;\n\n\nvoid main() {\n uint n, k;\n readf(\"%s %s\\n\", &n, &k);\n \n //auto fence = new uint[n];\n auto sum_arr = new uint[n+1];\n \n uint f_sum;\n \n foreach(i; 0..n) {\n uint f;\n readf(\"%s \", &f);\n sum_arr[i+1] = f_sum + f;\n f_sum += f;\n }\n \n uint min = sum_arr[k] - sum_arr[0], min_idx = 1;\n \n for(uint j = k; j < n+1; ++j) {\n if(sum_arr[j] - sum_arr[j - k] < min) {\n min = sum_arr[j] - sum_arr[j - k];\n min_idx = j - k + 1;\n }\n }\n \n min_idx.writeln;\n //foreach()\n}\n\n\nalias id(alias token) = token;\n\nalias var(alias elem) = std.traits.Unqual!(typeof(elem));\n\nauto mut(T)(auto ref T in_val) {\n std.traits.Unqual!(T) rv = in_val;\n return rv;\n}\n\nauto imut(T)(auto ref T in_val) {\n immutable immrv = in_val;\n return immrv;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n int n,k;\n readf!\"%d %d\"(n,k);\n readln;\n auto a=readln.splitter\n .map!(to!int)\n .cumulativeFold!\"a+b\"\n .array;\n int m=int.max,r;\n foreach(i;k-1..n){\n int cur=a[i];\n if(i>=k) cur-=a[i-k];\n if(cur=k) cur-=a[i-k];\n if(cur v) {\n maxsum = v;\n j = r - k + 1;\n }\n }\n\n writeln(j + 1);\n}\n\n\nvoid readVars(T...)(auto ref T args)\n{\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}, {"source_code": "import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;\nimport std.uni, std.math, std.container, std.typecons, std.typetuple;\nimport core.bitop, std.datetime;\n\nimmutable int mod = 10^^9 + 7;\n\nvoid main()\n{\n int n, k;\n readVars(n, k);\n auto h = readln.split.to!(int[]);\n auto hps = new int[](n + 1);\n\n foreach(i ; 0 .. n) hps[i + 1] += hps[i] + h[i];\n\n debug {\n writeln(hps);\n }\n\n int minsum = mod, j;\n\n foreach(i ; 0 .. n - k + 1) {\n if (minsum > hps[i + k] - hps[i]) {\n minsum = hps[i + k] - hps[i];\n j = i;\n }\n }\n\n writeln(j + 1);\n}\n\n\nvoid readVars(T...)(auto ref T args)\n{\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\n\nvoid main() {\n int n, k; readf(\"%d %d\\n\", &n, &k);\n int[] xs = stdin.readln.chomp.split(\" \").map!(to!int).array;\n int[] ss = new int[xs.length+1];\n ss[0] = 0;\n for (int i = 0; i < xs.length - 1; i++) {\n ss[i+1] = ss[i] + xs[i];\n }\n int ans = int.max;\n int index = -1;\n for (int i = 0; i < xs.length - k; i++) {\n if (ans > ss[i+k] - ss[i]) {\n ans = ss[i+k] - ss[i];\n index = i + 1;\n }\n }\n index.writeln;\n}\n"}, {"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\n\nvoid main() {\n int n, k; readf(\"%d %d\\n\", &n, &k);\n int[] xs = stdin.readln.chomp.split(\" \").map!(to!int).array;\n int[] ss = new int[xs.length+1];\n ss[0] = 0;\n for (int i = 0; i < xs.length - 1; i++) {\n ss[i+1] = ss[i] + xs[i];\n }\n int ans = int.max;\n int index = 1;\n for (int i = 0; i < xs.length - k; i++) {\n if (ans > ss[i+k] - ss[i]) {\n ans = ss[i+k] - ss[i];\n index = i + 1;\n }\n }\n index.writeln;\n}\n"}, {"source_code": "import std.stdio\n , std.typecons\n , std.traits\n , std.algorithm\n , std.range\n , std.container;\n\n\nvoid main() {\n uint n, k;\n readf(\"%s %s\\n\", &n, &k);\n \n //auto fence = new uint[n];\n auto sum_arr = new uint[n+2];\n \n uint f_sum;\n \n foreach(i; 0..n) {\n uint f;\n readf(\"%s \", &f);\n sum_arr[i+1] = f_sum + f;\n f_sum += f;\n }\n \n uint min = sum_arr[k] - sum_arr[0], min_idx = k+1;\n \n for(uint j = k; j < n+1; ++j) {\n if(sum_arr[j] - sum_arr[j - k] < min) {\n min = sum_arr[j] - sum_arr[j - k];\n min_idx = j - k + 1;\n }\n }\n \n min_idx.writeln;\n //foreach()\n}\n\n\nalias id(alias token) = token;\n\nalias var(alias elem) = std.traits.Unqual!(typeof(elem));\n\nauto mut(T)(auto ref T in_val) {\n std.traits.Unqual!(T) rv = in_val;\n return rv;\n}\n\nauto imut(T)(auto ref T in_val) {\n immutable immrv = in_val;\n return immrv;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}], "src_uid": "69f4e340b3f6e1d807e0545ebea1fe2f"} {"nl": {"description": "Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by $$$2$$$, $$$4$$$ or $$$8$$$, and division is only allowed if the number is divisible by the chosen divisor. Formally, if the register contains a positive integer $$$x$$$, in one operation it can be replaced by one of the following: $$$x \\cdot 2$$$ $$$x \\cdot 4$$$ $$$x \\cdot 8$$$ $$$x / 2$$$, if $$$x$$$ is divisible by $$$2$$$ $$$x / 4$$$, if $$$x$$$ is divisible by $$$4$$$ $$$x / 8$$$, if $$$x$$$ is divisible by $$$8$$$ For example, if $$$x = 6$$$, in one operation it can be replaced by $$$12$$$, $$$24$$$, $$$48$$$ or $$$3$$$. Value $$$6$$$ isn't divisible by $$$4$$$ or $$$8$$$, so there're only four variants of replacement.Now Johnny wonders how many operations he needs to perform if he puts $$$a$$$ in the register and wants to get $$$b$$$ at the end.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The following $$$t$$$ lines contain a description of test cases. The first and only line in each test case contains integers $$$a$$$ and $$$b$$$ ($$$1 \\leq a, b \\leq 10^{18}$$$) — the initial and target value of the variable, respectively.", "output_spec": "Output $$$t$$$ lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get $$$b$$$ at the end, then write $$$-1$$$.", "sample_inputs": ["10\n10 5\n11 44\n17 21\n1 1\n96 3\n2 128\n1001 1100611139403776\n1000000000000000000 1000000000000000000\n7 1\n10 8"], "sample_outputs": ["1\n1\n-1\n0\n2\n2\n14\n0\n-1\n-1"], "notes": "NoteIn the first test case, Johnny can reach $$$5$$$ from $$$10$$$ by using the shift to the right by one (i.e. divide by $$$2$$$).In the second test case, Johnny can reach $$$44$$$ from $$$11$$$ by using the shift to the left by two (i.e. multiply by $$$4$$$).In the third test case, it is impossible for Johnny to reach $$$21$$$ from $$$17$$$.In the fourth test case, initial and target values are equal, so Johnny has to do $$$0$$$ operations.In the fifth test case, Johnny can reach $$$3$$$ from $$$96$$$ by using two shifts to the right: one by $$$2$$$, and another by $$$3$$$ (i.e. divide by $$$4$$$ and by $$$8$$$)."}, "positive_code": [{"source_code": "import core.bitop, std.stdio;\nvoid main() {\n\treadln;\n\tlong a, b;\n\twhile (readf (\" %s %s\", &a, &b) > 0) {\n\t\tif (a > b) a ^= b ^= a ^= b;\n\t\twriteln (!(b % a) && !((b /= a) & (b - 1)) ? (b.bsr + 2) / 3 : -1);\n\t}\n}\n"}, {"source_code": "import core.bitop,std.stdio;\nvoid main(){\n\treadln;\n\tlong a,b;\n\twhile(readf(\" %s %s\",&a,&b)>0){\n\t\tif(a>b)a^=b^=a^=b;\n\t\twriteln(!(b%a)&&!((b/=a)&(b-1))?(b.bsr + 2)/3:-1);\n\t}\n}\n"}, {"source_code": "import core.bitop,std.stdio;void main(){readln;long a,b;while(readf!\" %s %s\"(a,b)>0)\n{if(a>b)a^=b^=a^=b;writeln((b%a)|((b/=a)&(b-1))?-1:(b.bsr+2)/3);}}"}, {"source_code": "import core.bitop,std.stdio;void main(){readln;long a,b;while(readf(\" %s %s\",&a,&b)>0){if(a>b)a^=b^=a^=b;writeln(!(b%a)&&!((b/=a)&(b-1))?(b.bsr + 2)/3:-1);}}"}, {"source_code": "import core.bitop, std.algorithm, std.stdio;\nvoid main() {\n\treadln;\n\tlong a, b, r;\n\twhile (readf (\" %s %s\", &a, &b) > 0) {\n\t\tif (a > b) swap (a, b);\n\t\twriteln (!(b % a) && !((b /= a) & (b - 1)) ? (b.bsr + 2) / 3 : -1);\n\t}\n}\n"}, {"source_code": "import core.bitop, std.algorithm, std.conv, std.stdio, std.string;\nvoid main () {\n\tforeach (t; 0..readln.strip.to!int) {\n\t\tlong a, b, r = -1;\n\t\treadf !(\" %s %s\") (a, b);\n\t\tif (a > b) swap (a, b);\n\t\tif (b % a == 0) {\n\t\t\tauto c = b / a;\n\t\t\tif (!(c & (c - 1))) r = (bsr (c) + 2) / 3;\n\t\t}\n\t\tr.writeln;\n\t}\n}\n"}, {"source_code": "import std.stdio;\nimport std.stdio;\nimport std.algorithm;\nimport std.range;\nimport std.functional;\nimport std.conv;\nimport std.string;\nimport std.math;\nimport core.bitop;\n\nvoid main() {\n\tint tt = readln().chomp.to!int;\n\tforeach (t; 0 .. tt) {\n\t\tauto arr = readln().chomp.split(\" \").map!(to!ulong);\n\t\tulong a = arr[0];\n\t\tulong b = arr[1];\n\t\tif (b < a) {\n\t\t\tulong tm = a;\n\t\t\ta = b;\n\t\t\tb = tm;\n\t\t}\n\t\tif (b > a) {\n\t\t\tif (b % a != 0) {\n\t\t\t\twriteln(-1);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tulong diff = b / a;\n\t\t\tulong high = bsr(diff);\n\t\t\tif ((1UL << high) == diff) {\n\t\t\t\twriteln(\n\t\t\t\t\thigh / 3 + (high % 3) / 2 + (high % 3) % 2\n\t\t\t\t);\n\t\t\t}\n\t\t\telse {\n\t\t\t\twriteln(-1);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\twriteln(0);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto a = RD;\n\t\tauto b = RD;\n\n\t\tstring s, s2;\n\t\twhile (a != 0)\n\t\t{\n\t\t\ts ~= cast(char)('0'+(a%2));\n\t\t\ta /= 2;\n\t\t}\n\t\twhile (b != 0)\n\t\t{\n\t\t\ts2 ~= cast(char)('0'+(b%2));\n\t\t\tb /= 2;\n\t\t}\n\t\tdebug writeln(s);\n\t\tdebug writeln(s2);\n\n\t\tans[ti] = abs(cast(int)s.length - cast(int)s2.length);\n\t\tans[ti] += 2;\n\t\tans[ti] /= 3;\n\t\twhile (s.length < s2.length)\n\t\t{\n\t\t\ts = '0' ~ s;\n\t\t}\n\t\twhile (s.length > s2.length)\n\t\t{\n\t\t\ts2 = '0' ~ s2;\n\t\t}\n\t\tdebug writeln(s);\n\t\tdebug writeln(s2);\n\n\t\tforeach (i; 0..s.length)\n\t\t{\n\t\t\tif (s[i] != s2[i])\n\t\t\t\tans[ti] = -1;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "541039ef3c9b8251b758608811533e06"} {"nl": {"description": "Drazil likes heap very much. So he created a problem with heap:There is a max heap with a height $$$h$$$ implemented on the array. The details of this heap are the following:This heap contains exactly $$$2^h - 1$$$ distinct positive non-zero integers. All integers are distinct. These numbers are stored in the array $$$a$$$ indexed from $$$1$$$ to $$$2^h-1$$$. For any $$$1 < i < 2^h$$$, $$$a[i] < a[\\left \\lfloor{\\frac{i}{2}}\\right \\rfloor]$$$.Now we want to reduce the height of this heap such that the height becomes $$$g$$$ with exactly $$$2^g-1$$$ numbers in heap. To reduce the height, we should perform the following action $$$2^h-2^g$$$ times:Choose an index $$$i$$$, which contains an element and call the following function $$$f$$$ in index $$$i$$$:Note that we suppose that if $$$a[i]=0$$$, then index $$$i$$$ don't contain an element.After all operations, the remaining $$$2^g-1$$$ element must be located in indices from $$$1$$$ to $$$2^g-1$$$. Now Drazil wonders what's the minimum possible sum of the remaining $$$2^g-1$$$ elements. Please find this sum and find a sequence of the function calls to achieve this value.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 70\\,000$$$): the number of test cases. Each test case contain two lines. The first line contains two integers $$$h$$$ and $$$g$$$ ($$$1 \\leq g < h \\leq 20$$$). The second line contains $$$n = 2^h-1$$$ distinct positive integers $$$a[1], a[2], \\ldots, a[n]$$$ ($$$1 \\leq a[i] < 2^{20}$$$). For all $$$i$$$ from $$$2$$$ to $$$2^h - 1$$$, $$$a[i] < a[\\left \\lfloor{\\frac{i}{2}}\\right \\rfloor]$$$. The total sum of $$$n$$$ is less than $$$2^{20}$$$.", "output_spec": "For each test case, print two lines. The first line should contain one integer denoting the minimum sum after reducing the height of heap to $$$g$$$. The second line should contain $$$2^h - 2^g$$$ integers $$$v_1, v_2, \\ldots, v_{2^h-2^g}$$$. In $$$i$$$-th operation $$$f(v_i)$$$ should be called.", "sample_inputs": ["2\n3 2\n7 6 3 5 4 2 1\n3 2\n7 6 5 4 3 2 1"], "sample_outputs": ["10\n3 2 3 1\n8\n2 1 3 1"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint tests;\n\treadf !(\" %s\") (tests);\n\tforeach (test; 0..tests)\n\t{\n\t\tint h, g;\n\t\treadf !(\" %s %s\") (h, g);\n\t\tauto n = 1 << h;\n\t\tauto m = 1 << g;\n\t\tauto a = new int [n * 2];\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\treadf !(\" %s\") (a[i]);\n\t\t}\n\n\t\tbool doFake (int pos)\n\t\t{\n\t\t\twhile (pos < m)\n\t\t\t{\n\t\t\t\tint next = pos * 2;\n\t\t\t\tif (a[next] < a[next + 1])\n\t\t\t\t{\n\t\t\t\t\tnext += 1;\n\t\t\t\t}\n\t\t\t\tif (a[next] == 0)\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tpos = next;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t\tvoid doReal (int pos)\n\t\t{\n\t\t\twhile (a[pos] != 0)\n\t\t\t{\n\t\t\t\tint next = pos * 2;\n\t\t\t\tif (a[next] < a[next + 1])\n\t\t\t\t{\n\t\t\t\t\tnext += 1;\n\t\t\t\t}\n\t\t\t\ta[pos] = a[next];\n\t\t\t\tpos = next;\n\t\t\t}\n\t\t}\n\n\t\tint [] ans;\n\t\tans.reserve (n - m);\n\n\t\tvoid fun (int pos)\n\t\t{\n\t\t\tif (a[pos] == 0)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\t\t\twhile (doFake (pos))\n\t\t\t{\n\t\t\t\tans ~= pos;\n\t\t\t\tdoReal (pos);\n\t\t\t}\n\t\t\tfun (pos * 2 + 0);\n\t\t\tfun (pos * 2 + 1);\n\t\t}\n\n\t\tfun (1);\n\t\twriteln (sum (a, 0L));\n\t\twritefln !(\"%(%s %)\") (ans);\n\t}\n}\n"}], "negative_code": [], "src_uid": "3daa5e0aed7d18479171b3ad5eafb5d1"} {"nl": {"description": "Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. Artem wants to increment the values in some cells by one to make $$$a$$$ good.More formally, find a good matrix $$$b$$$ that satisfies the following condition — For all valid ($$$i,j$$$), either $$$b_{i,j} = a_{i,j}$$$ or $$$b_{i,j} = a_{i,j}+1$$$. For the constraints of this problem, it can be shown that such a matrix $$$b$$$ always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le m \\le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i,j}$$$ ($$$1 \\leq a_{i,j} \\leq 10^9$$$).", "output_spec": "For each case, output $$$n$$$ lines each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$b_{i,j}$$$.", "sample_inputs": ["3\n3 2\n1 2\n4 5\n7 8\n2 2\n1 1\n3 3\n2 2\n1 3\n2 2"], "sample_outputs": ["1 2\n5 6\n7 8\n2 1\n4 3\n2 4\n3 2"], "notes": "NoteIn all the cases, you can verify that no two adjacent cells have the same value and that $$$b$$$ is the same as $$$a$$$ with some values incremented by one. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint rows, cols;\n\t\treadf !(\" %s %s\") (rows, cols);\n\t\treadln;\n\t\tint [] [] a;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\ta ~= readln.splitter.map !(to !(int)).array;\n\t\t}\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\tforeach (col; 0..cols)\n\t\t\t{\n\t\t\t\tif ((a[row][col] ^ row ^ col) & 1)\n\t\t\t\t{\n\t\t\t\t\ta[row][col] += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twritefln !(\"%(%(%s %)\\n%)\") (a);\n\t}\n}\n"}], "negative_code": [], "src_uid": "fd11967b07870be3294b9191603b17e8"} {"nl": {"description": "Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square.A number x is said to be a perfect square if there exists an integer y such that x = y2.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array. The second line contains n integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — the elements of the array. It is guaranteed that at least one element of the array is not a perfect square.", "output_spec": "Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.", "sample_inputs": ["2\n4 2", "8\n1 2 4 8 16 32 64 576"], "sample_outputs": ["2", "32"], "notes": "NoteIn the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto A = readln.split.map!(to!int).array;\n\n bool[int] S;\n for (int i = 0; i * i <= 10^^6; ++i) {\n S[i*i] = true;\n }\n\n int ans = -(10^^6)-1;\n foreach (a; A) if (!(a in S)) ans = max(ans, a);\n\n ans.writeln;\n}\n"}, {"source_code": "import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,\n\tstd.conv, std.math;\nimport std.numeric : Fft, fft, gcd, inverseFft;\nimport std.random, std.range, std.stdio, std.string, std.traits, std.typecons,\n\tstd.uni;\n\nalias pair(X, Y) = Tuple!(X, \"fi\", Y, \"se\");\nalias mp = make_pair;\nalias pii = pair!(int, int);\nalias pll = pair!(long, long);\n// alias gcd = std.numeric.gcd;\nalias lint = long;\nalias rbt = RedBlackTree;\n/// struct vector as c++ vector\nstruct Vec(T)\n{\n\tprivate Array!T buf;\n\talias buf this;\n\tthis(this)\n\t{\n\t\tbuf = buf.dup;\n\t}\n\t/// construct from length\n\tthis(U)(U length)\n\t\t\tif (is(U == int) || is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint))\n\t{\n\t\tbuf.length = length;\n\t}\n\t/// construct from length and default value\n\tthis(U, X)(U length, X val)\n\t\t\tif (isImplicitlyConvertible!(X, T) || (is(U == int)\n\t\t\t\t|| is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint)))\n\t{\n\t\tbuf.length = length;\n\t\tforeach (ref x; buf[])\n\t\t{\n\t\t\tx = val;\n\t\t}\n\t}\n\t/// construct from other array\n\tthis(U)(U[] values...) if (isImplicitlyConvertible!(U, T))\n\t{\n\t\tbuf = Array!(T)(values);\n\t}\n\t/// construct from other range\n\tthis(Range)(Range r)\n\t\t\tif (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range,\n\t\t\t\tT) && !is(Range == T[]))\n\t{\n\t\tbuf = Array!(T)(r);\n\t}\n\t/// integer length\n\tint size() @property const\n\t{\n\t\treturn cast(int) buf.length;\n\t}\n}\n///modulo\nconst int mod = 10 ^^ 9 + 7, mod2 = mod + 2;\npublic\n{\n\tpure nothrow\n\t{\n\t\t///lcm\n\t\tT lcm(T)(auto ref T a, auto ref T b)\n\t\t{\n\t\t\treturn a / gcd(a, b) * b;\n\t\t}\n\t\t///make_pair\n\t\tpair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)\n\t\t{\n\t\t\treturn tuple!(X, \"fi\", Y, \"se\")(x_, y_);\n\t\t}\n\t\t///BigInt greatest common divizor\n\t\tBigInt biggcd(BigInt a, BigInt b)\n\t\t{\n\t\t\tBigInt res = 1;\n\t\t\twhile (b)\n\t\t\t{\n\t\t\t\tif ((a & 1) && (b & 1))\n\t\t\t\t{\n\t\t\t\t\ta -= b;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (a & 1)\n\t\t\t\t{\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (b & 1)\n\t\t\t\t{\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres <<= 1;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\tif (a < b)\n\t\t\t\t\tswap(a, b);\n\t\t\t}\n\t\t\treturn res * a;\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).lowerBound(g).length;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn a.length - assumeSorted(a).upperBound(g).length;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).contains(g);\n\t\t}\n\t}\n\t///write an array\n\tvoid putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)\n\t{\n\t\twritefln(\"%(%s\" ~ ch ~ \"%)\", a);\n\t}\n\t///write a matrix\n\tvoid putarr(X)(in X a)\n\t\t\tif (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))\n\t{\n\t\twritefln(\"%(%(%s %)\\n%)\", a);\n\t}\n\t///get an array\n\tbool getarr(X)(X a, in size_t n)\n\t{\n\t\tbool b = 1;\n\t\tforeach (ref i; a[0 .. n])\n\t\t\tb = input(&i);\n\t\treturn b;\n\t}\n\n\tstatic if (version_minor >= 74)\n\t{\n\t\t///read without format\n\t\tbool input(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\treturn readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tconst bool fl = readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t\treadln;\n\t\t\treturn fl;\n\t\t}\n\t}\n\telse\n\t{\n\t\t///read without format\n\t\tbool input(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treadln;\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tint n;\n\tread(n);\n\tauto a = arread!int;\n\tint ans = int.min;\n\tforeach (x; a)\n\t{\n\t\tif (x < 0)\n\t\t{\n\t\t\tans = max(ans, x);\n\t\t\tcontinue;\n\t\t}\n\t\tint s = cast(int) sqrt(1.0L * x);\n\t\tbool fl = true;\n\t\tforeach (g; s - 2 .. s + 3)\n\t\t{\n\t\t\tif (x == g * g)\n\t\t\t{\n\t\t\t\tfl = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (fl)\n\t\t\tans = max(ans, x);\n\t}\n\twriteln(ans);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tint res = int.min;\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tauto isGood = (c < 0);\n\t\t\tif (!isGood)\n\t\t\t{\n\t\t\t\tauto d = sqrt (c * 1.0);\n\t\t\t\tisGood = (d * d != c);\n\t\t\t}\n\t\t\tif (isGood)\n\t\t\t{\n\t\t\t\tres = max (res, c);\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n// floor(sqrt(a))\nlong floorSqrt(long a) {\n import core.bitop : bsr;\n import std.algorithm : min;\n long b = a, x = 0, y = 0;\n for (int e = bsr(a) & ~1; e >= 0; e -= 2) {\n x <<= 1;\n y <<= 1;\n if (b >= (y | 1) << e) {\n b -= (y | 1) << e;\n x |= 1;\n y += 2;\n }\n }\n return x;\n}\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n \n long ans = long.min;\n foreach (i; 0 .. N) {\n if (A[i] != floorSqrt(A[i])^^2) {\n chmax(ans, A[i]);\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,\n\tstd.conv, std.math;\nimport std.numeric : Fft, fft, gcd, inverseFft;\nimport std.random, std.range, std.stdio, std.string, std.traits, std.typecons,\n\tstd.uni;\n\nalias pair(X, Y) = Tuple!(X, \"fi\", Y, \"se\");\nalias mp = make_pair;\nalias pii = pair!(int, int);\nalias pll = pair!(long, long);\n// alias gcd = std.numeric.gcd;\nalias lint = long;\nalias rbt = RedBlackTree;\n/// struct vector as c++ vector\nstruct Vec(T)\n{\n\tprivate Array!T buf;\n\talias buf this;\n\tthis(this)\n\t{\n\t\tbuf = buf.dup;\n\t}\n\t/// construct from length\n\tthis(U)(U length)\n\t\t\tif (is(U == int) || is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint))\n\t{\n\t\tbuf.length = length;\n\t}\n\t/// construct from length and default value\n\tthis(U, X)(U length, X val)\n\t\t\tif (isImplicitlyConvertible!(X, T) || (is(U == int)\n\t\t\t\t|| is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint)))\n\t{\n\t\tbuf.length = length;\n\t\tforeach (ref x; buf[])\n\t\t{\n\t\t\tx = val;\n\t\t}\n\t}\n\t/// construct from other array\n\tthis(U)(U[] values...) if (isImplicitlyConvertible!(U, T))\n\t{\n\t\tbuf = Array!(T)(values);\n\t}\n\t/// construct from other range\n\tthis(Range)(Range r)\n\t\t\tif (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range,\n\t\t\t\tT) && !is(Range == T[]))\n\t{\n\t\tbuf = Array!(T)(r);\n\t}\n\t/// integer length\n\tint size() @property const\n\t{\n\t\treturn cast(int) buf.length;\n\t}\n}\n///modulo\nconst int mod = 10 ^^ 9 + 7, mod2 = mod + 2;\npublic\n{\n\tpure nothrow\n\t{\n\t\t///lcm\n\t\tT lcm(T)(auto ref T a, auto ref T b)\n\t\t{\n\t\t\treturn a / gcd(a, b) * b;\n\t\t}\n\t\t///make_pair\n\t\tpair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)\n\t\t{\n\t\t\treturn tuple!(X, \"fi\", Y, \"se\")(x_, y_);\n\t\t}\n\t\t///BigInt greatest common divizor\n\t\tBigInt biggcd(BigInt a, BigInt b)\n\t\t{\n\t\t\tBigInt res = 1;\n\t\t\twhile (b)\n\t\t\t{\n\t\t\t\tif ((a & 1) && (b & 1))\n\t\t\t\t{\n\t\t\t\t\ta -= b;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (a & 1)\n\t\t\t\t{\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\telse if (b & 1)\n\t\t\t\t{\n\t\t\t\t\ta >>= 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres <<= 1;\n\t\t\t\t\ta >>= 1;\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\tif (a < b)\n\t\t\t\t\tswap(a, b);\n\t\t\t}\n\t\t\treturn res * a;\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).lowerBound(g).length;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn a.length - assumeSorted(a).upperBound(g).length;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).contains(g);\n\t\t}\n\t}\n\t///write an array\n\tvoid putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)\n\t{\n\t\twritefln(\"%(%s\" ~ ch ~ \"%)\", a);\n\t}\n\t///write a matrix\n\tvoid putarr(X)(in X a)\n\t\t\tif (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))\n\t{\n\t\twritefln(\"%(%(%s %)\\n%)\", a);\n\t}\n\t///get an array\n\tbool getarr(X)(X a, in size_t n)\n\t{\n\t\tbool b = 1;\n\t\tforeach (ref i; a[0 .. n])\n\t\t\tb = input(&i);\n\t\treturn b;\n\t}\n\n\tstatic if (version_minor >= 74)\n\t{\n\t\t///read without format\n\t\tbool input(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\treturn readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tconst bool fl = readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t\treadln;\n\t\t\treturn fl;\n\t\t}\n\t}\n\telse\n\t{\n\t\t///read without format\n\t\tbool input(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treadln;\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tint n;\n\tread(n);\n\tauto a = arread!int;\n\tint ans;\n\tforeach (x; a)\n\t{\n\t\tif (x < 0)\n\t\t{\n\t\t\tans = max(ans, x);\n\t\t\tcontinue;\n\t\t}\n\t\tint s = cast(int) sqrt(1.0L * x);\n\t\tbool fl = true;\n\t\tforeach (g; s - 2 .. s + 3)\n\t\t{\n\t\t\tif (x == g * g)\n\t\t\t{\n\t\t\t\tfl = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (fl)\n\t\t\tans = max(ans, x);\n\t}\n\twriteln(ans);\n}\n"}], "src_uid": "d46d5f130d8c443f28b52096c384fef3"} {"nl": {"description": "Igor the analyst has adopted n little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into n pieces of equal area. Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to h. Igor wants to make n - 1 cuts parallel to the base to cut the carrot into n pieces. He wants to make sure that all n pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? Illustration to the first example. ", "input_spec": "The first and only line of input contains two space-separated integers, n and h (2 ≤ n ≤ 1000, 1 ≤ h ≤ 105).", "output_spec": "The output should contain n - 1 real numbers x1, x2, ..., xn - 1. The number xi denotes that the i-th cut must be made xi units away from the apex of the carrot. In addition, 0 < x1 < x2 < ... < xn - 1 < h must hold. Your output will be considered correct if absolute or relative error of every number in your output doesn't exceed 10 - 6. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .", "sample_inputs": ["3 2", "2 100000"], "sample_outputs": ["1.154700538379 1.632993161855", "70710.678118654752"], "notes": "NoteDefinition of isosceles triangle: https://en.wikipedia.org/wiki/Isosceles_triangle."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto H = s[1].to!real;\n real A = H / 2;\n real B = A / N;\n\n auto rate = new real[](N-1);\n auto ans = new real[](N-1);\n rate[0] = sqrt(2 * B / H);\n ans[0] = H * rate[0];\n\n foreach (i; 0..N-2) {\n real hi = 1;\n real lo = rate[i];\n foreach (_; 0..100) {\n real mid = (hi + lo) / 2;\n real area = (rate[i] + mid) * (mid - rate[i]) * H / 2;\n if (area < B) lo = mid;\n else hi = mid;\n }\n rate[i+1] = hi;\n ans[i+1] = hi * H;\n }\n\n writef(\"%.9f\", ans[0]);\n iota(1, N-1).each!(i => writef(\" %.9f\", ans[i]));\n writeln;\n}\n"}, {"source_code": "module main;\nimport std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,\n\tstd.functional,std.math,std.string,std.range,std.complex,std.bitmanip,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,core.bitop,\n\tstd.meta,core.stdc.stdio : freopen;\nimport std.numeric: fft,Fft,gcd,inverseFft;\nalias big=BigInt;alias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");\nalias lint=long;private const int mod=1_000_000_007;\nalias pii=pair!(int,int);alias pll=pair!(long,long);alias mp=make_pair;alias gcd=std.numeric.gcd;\nprivate\n{\n\tpure nothrow \n\t{\n\t\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\t\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\t\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\t\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\t\tsize_t lowb(T,X)(T a,auto ref X g)\n\t\tif(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t\t{\n\t\t\t\tauto m=(l+r)>>1;\n\t\t\t\tif(!(a[m]1)\n\t\t\t{\n\t\t\t\tauto m=(l+r)>>1;\n\t\t\t\tif(g 0)\n\t{\n\t\tsize_t s=0;\n\t\tforeach(ref e;ptrs)\n\t\t{\n\t\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\t\telse s+=readf(\" %s\",&e);\n\t\t}\n\t\treturn s==ptrs.length;\n\t}\n\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\tsize_t s=0;\n\t\tforeach(ref e;ptrs)\n\t\t{\n\t\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\t\telse s+=readf(\" %s\",&e);\n\t\t}\n\t\treadln;\n\t\treturn s==ptrs.length;\n\t}\n\tnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\n\tnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n\t@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nprivate\n{\n\n}\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tint n,h;\nloop:while(read(n,h))\n\t{\n\t\tforeach(i;1..n)\n\t\t{\n\t\t\twritef(\"%.10f \",h*sqrt(i*1.0000L/n));\n\t\t}\n\t\tdebug writeln;\n\t}\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const H = readReal();\n \n auto ans = new real[N];\n foreach (i; 1 .. N) {\n ans[i] = H * sqrt(1.0L * i / N);\n }\n foreach (i; 1 .. N) {\n if (i > 1) write(\" \");\n writef(\"%.12f\", ans[i]);\n }\n writeln;\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "6f8a1a138ea2620f2013f426e29e4d98"} {"nl": {"description": "Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark \"?\". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s=\"ab?b\" as a result, it will appear in t=\"aabrbb\" as a substring.Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol \"?\" should be considered equal to any other symbol.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t.", "output_spec": "In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.", "sample_inputs": ["3 5\nabc\nxaybz", "4 10\nabcd\nebceabazcd"], "sample_outputs": ["2\n2 3", "1\n2"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.exception;\nimport std.conv;\nimport std.array;\nimport std.range;\nimport std.math;\n\nint CompareToStrings ( dstring first, dstring second )\n{\n\t//writeln(first, second);\n\tint returnVal = 0;\n\tzip(first, second).each!( a => a[0] == a[1] ? returnVal++ : returnVal );\n\t//writeln(returnVal);\n\treturn returnVal;\n}\n\nint[] FindDiffPositions ( dstring first, dstring second )\n{\n\tint[] returnVal;\n\tfor ( int i = 0; i < first.length; i++ )\n\t{\n\t\tif ( first[i] != second[i] )\n\t\t\treturnVal ~= (i + 1);\n\t}\n\treturn returnVal;\n}\n\nvoid main() \n{\n auto ilkSatir = stdin.readln.strip.split().map!(a => to!int(a)).array();\n\tauto s = stdin.readln.strip.to!dstring;\n\tauto t = stdin.readln.strip.to!dstring;\n\t\n\tint pos = 0;\n\tint max = 0;\n\tfor ( int i = 0; i <= t.length - s.length; i++ )\n\t{\n\t\tint curVal = CompareToStrings( s, t[i..i+s.length]);\n\t\tif ( curVal > max )\n\t\t{\n\t\t\tpos = i;\n\t\t\tmax = curVal;\n\t\t}\n\t}\n\n\tauto curList = FindDiffPositions( s, t[pos..pos+s.length]);\n\twriteln( s.length - max );\n\tcurList.each!(a => write(a, \" \"));\n}"}, {"source_code": "// tested by Hightail - https://github.com/dj3500/hightail\nimport std.stdio, std.string, std.conv, std.algorithm;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\n\nint n, m;\nstring s, t;\n\nvoid main() {\n scan(n, m);\n scan(s);\n scan(t);\n\n if (n > m) {\n swap(n, m);\n swap(s, t);\n }\n\n int[] ans = new int[](2000);\n\n foreach (i ; 0 .. m - n + 1) {\n int[] tmp = new int[](0);\n\n debug {\n writeln(\"s:\", s);\n writeln(\"t:\", t);\n }\n\n foreach (j ; 0 .. n) {\n if (s[j] != t[j]) {\n tmp ~= j + 1;\n }\n }\n\n if (tmp.length.to!int < ans.length.to!int) {\n ans = tmp.dup;\n }\n\n t.popFront();\n }\n\n writeln(ans.length);\n writefln(\"%(%s %)\", ans);\n}\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.exception;\nimport std.conv;\nimport std.array;\nimport std.range;\nimport std.math;\n\nint CompareToStrings ( dstring first, dstring second )\n{\n\t//writeln(first, second);\n\tint returnVal = 0;\n\tzip(first, second).each!( a => a[0] == a[1] ? returnVal++ : returnVal );\n\t//writeln(returnVal);\n\treturn returnVal;\n}\n\nint[] FindDiffPositions ( dstring first, dstring second )\n{\n\tint[] returnVal;\n\tfor ( int i = 0; i < first.length; i++ )\n\t{\n\t\tif ( first[i] != second[i] )\n\t\t\treturnVal ~= (i + 1);\n\t}\n\treturn returnVal;\n}\n\nvoid main() \n{\n auto ilkSatir = stdin.readln.strip.split().map!(a => to!int(a)).array();\n\tauto s = stdin.readln.strip.to!dstring;\n\tauto t = stdin.readln.strip.to!dstring;\n\t\n\tint pos = 0;\n\tint max = 0;\n\tfor ( int i = 0; i <= t.length - s.length; i++ )\n\t{\n\t\tint curVal = CompareToStrings( s, t[i..i+s.length]);\n\t\tif ( curVal > max )\n\t\t{\n\t\t\tpos = i;\n\t\t\tmax = curVal;\n\t\t}\n\t}\n\n\tauto curList = FindDiffPositions( s, t[pos..pos+s.length]);\n\twriteln( s.length - max );\n\twriteln( curList );\n}"}], "src_uid": "dd26f45869b73137e5e5cc6820cdc2e4"} {"nl": {"description": "Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.", "input_spec": "The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.", "output_spec": "Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.", "sample_inputs": ["3\n5 2\n3 1\n4 2", "3\n6 1\n5 2\n4 3"], "sample_outputs": ["2", "6"], "notes": "NoteIn the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject."}, "positive_code": [{"source_code": "#!/usr/bin/env rdmd\n// Computes average line length for standard input.\nimport std.stdio;\nimport std.algorithm;\nimport std.container;\nimport std.string;\nimport std.math;\n\n// 479c\nvoid main()\n{\n\tint n;\n\treadf(\"%s\\n\", &n);\n\tlong a[] = new long[n];\n\tlong b[] = new long[n];\n\tfor (int i=0; ia[j]) {\n\t\t\t\tswap(a[j-1], a[j]);\n\t\t\t\tswap(b[j-1], b[j]);\n\t\t\t} else \tif (a[j-1]==a[j] && b[j-1]>b[j]) {\n\t\t\t\tswap(b[j-1], b[j]);\t\t\t\t\n\t\t\t}\t\t\t\t\n\t\t}\n\t}\n\t\n\tlong kd=0;\n\tfor (int i=0; ib[i]?a[i]:b[i];\n\t}\t\n\twriteln(kd);\n}\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/env rdmd\n// Computes average line length for standard input.\nimport std.stdio;\nimport std.algorithm;\nimport std.container;\nimport std.string;\nimport std.math;\n\n// 479c\nvoid main()\n{\n\tint n;\n\treadf(\"%s\\n\", &n);\n\tlong a[] = new long[n];\n\tlong b[] = new long[n];\n\tfor (int i=0; ia[j]) {\n\t\t\t\tswap(a[j-1], a[j]);\n\t\t\t\tswap(b[j-1], b[j]);\t\t\t\t\n\t\t\t}\t\n\t\t}\n\t}\n\t\n\tlong kd=0;\n\tfor (int i=0; ib[i]?a[i]:b[i];\n\t}\t\n\twriteln(kd);\n}\n\n"}, {"source_code": "#!/usr/bin/env rdmd\n// Computes average line length for standard input.\nimport std.stdio;\nimport std.algorithm;\nimport std.container;\nimport std.string;\nimport std.math;\n\n// 479c\nvoid main()\n{\n\tint n;\n\treadf(\"%s\\n\", &n);\n\tint a[] = new int[n];\n\tint b[] = new int[n];\n\tfor (int i=0; ia[j]) {\n\t\t\t\tswap(a[j-1], a[j]);\n\t\t\t\tswap(b[j-1], b[j]);\t\t\t\t\n\t\t\t}\t\n\t\t}\n\t}\n\t\n\tint kd=0;\n\tfor (int i=0; ib[i]?a[i]:b[i];\n\t}\t\n\twriteln(kd);\n}\n\n"}, {"source_code": "#!/usr/bin/env rdmd\n// Computes average line length for standard input.\nimport std.stdio;\nimport std.algorithm;\nimport std.container;\nimport std.string;\nimport std.math;\n\n// 479c\nvoid main()\n{\n\tint n;\n\treadf(\"%s\\n\", &n);\n\tlong a[] = new long[n];\n\tlong b[] = new long[n];\n\tfor (int i=0; ia[j]) {\n\t\t\t\tswap(a[j-1], a[j]);\n\t\t\t\tswap(b[j-1], b[j]);\n\t\t\t\tif (a[j-1]==a[j] && b[j-1]>b[j]) {\n\t\t\t\t\tswap(b[j-1], b[j]);\t\t\t\t\n\t\t\t\t}\t\t\t\t\n\t\t\t}\t\n\t\t}\n\t}\n\t\n\tlong kd=0;\n\tfor (int i=0; ib[i]?a[i]:b[i];\n\t}\t\n\twriteln(kd);\n}\n\n"}, {"source_code": "#!/usr/bin/env rdmd\n// Computes average line length for standard input.\nimport std.stdio;\nimport std.algorithm;\nimport std.container;\nimport std.string;\nimport std.math;\n\n// 479c\nvoid main()\n{\n\tint n;\n\treadf(\"%s\\n\", &n);\n\tlong a[] = new long[n];\n\tlong b[] = new long[n];\n\tfor (int i=0; ia[j]) {\n\t\t\t\tswap(a[j-1], a[j]);\n\t\t\t\tswap(b[j-1], b[j]);\n\t\t\t\tif (a[j-1]==a[j] && b[j-1]>b[j]) {\n\t\t\t\t\tswap(b[j-1], b[j]);\t\t\t\t\n\t\t\t\t}\t\t\t\t\n\t\t\t}\t\n\t\t}\n\t}\n\t\n\tlong kd=0;\n\tfor (int i=0; ib[i]?a[i]:b[i];\n\t}\t\n\twriteln(kd);\n}\n\n"}], "src_uid": "71bc7c4eb577441f2563e40d95306752"} {"nl": {"description": "You have $$$n$$$ chains, the $$$i$$$-th chain consists of $$$c_i$$$ vertices. Vertices in each chain are numbered independently from $$$1$$$ to $$$c_i$$$ along the chain. In other words, the $$$i$$$-th chain is the undirected graph with $$$c_i$$$ vertices and $$$(c_i - 1)$$$ edges connecting the $$$j$$$-th and the $$$(j + 1)$$$-th vertices for each $$$1 \\le j < c_i$$$.Now you decided to unite chains in one graph in the following way: the first chain is skipped; the $$$1$$$-st vertex of the $$$i$$$-th chain is connected by an edge with the $$$a_i$$$-th vertex of the $$$(i - 1)$$$-th chain; the last ($$$c_i$$$-th) vertex of the $$$i$$$-th chain is connected by an edge with the $$$b_i$$$-th vertex of the $$$(i - 1)$$$-th chain. Picture of the first test case. Dotted lines are the edges added during uniting process Calculate the length of the longest simple cycle in the resulting graph.A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of chains you have. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$2 \\le c_i \\le 10^9$$$) — the number of vertices in the corresponding chains. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$a_1 = -1$$$; $$$1 \\le a_i \\le c_{i - 1}$$$). The fourth line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$b_1 = -1$$$; $$$1 \\le b_i \\le c_{i - 1}$$$). Both $$$a_1$$$ and $$$b_1$$$ are equal to $$$-1$$$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print the length of the longest simple cycle.", "sample_inputs": ["3\n4\n3 4 3 3\n-1 1 2 2\n-1 2 2 3\n2\n5 6\n-1 5\n-1 1\n3\n3 5 2\n-1 1 1\n-1 3 5"], "sample_outputs": ["7\n11\n8"], "notes": "NoteIn the first test case, the longest simple cycle is shown below: We can't increase it with the first chain, since in such case it won't be simple — the vertex $$$2$$$ on the second chain will break simplicity."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\r\n\r\nvoid get(Args...)(ref Args args)\r\n{\r\n import std.traits, std.meta, std.typecons;\r\n\r\n static if (Args.length == 1) {\r\n alias Arg = Args[0];\r\n \r\n static if (isArray!Arg) {\r\n static if (isSomeChar!(ElementType!Arg)) {\r\n args[0] = readln.chomp.to!Arg;\r\n } else {\r\n args[0] = readln.split.to!Arg;\r\n }\r\n } else static if (isTuple!Arg) {\r\n auto input = readln.split;\r\n static foreach (i; 0..Fields!Arg.length) {\r\n args[0][i] = input[i].to!(Fields!Arg[i]);\r\n }\r\n } else {\r\n args[0] = readln.chomp.to!Arg;\r\n }\r\n } else {\r\n auto input = readln.split;\r\n assert(input.length == Args.length);\r\n\r\n static foreach (i; 0..Args.length) {\r\n args[i] = input[i].to!(Args[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid get_lines(Args...)(size_t N, ref Args args)\r\n{\r\n import std.traits, std.range;\r\n\r\n static foreach (i; 0..Args.length) {\r\n static assert(isArray!(Args[i]));\r\n args[i].length = N;\r\n }\r\n\r\n foreach (i; 0..N) {\r\n static if (Args.length == 1) {\r\n get(args[0][i]);\r\n } else {\r\n auto input = readln.split;\r\n static foreach (j; 0..Args.length) {\r\n args[j][i] = input[j].to!(ElementType!(Args[j]));\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid main()\r\n{\r\n int T; get(T);\r\n while (T--) {\r\n int N; get(N);\r\n long[] ls, aa, bb; get(ls); get(aa); get(bb);\r\n\r\n long x = abs(aa[1] - bb[1]) + 2, res;\r\n foreach (i; 2..N) {\r\n auto a = aa[i];\r\n auto b = bb[i];\r\n if (a > b) swap(a, b);\r\n res = max(res, x + ls[i-1] - 1);\r\n if (a == b) {\r\n x = 2;\r\n } else {\r\n x += 2 + a-1 + ls[i-1]-b;\r\n x = max(x, b - a + 2);\r\n }\r\n }\r\n res = max(res, x + ls[$-1] - 1);\r\n writeln(res);\r\n }\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto c = RDA;\n\t\tauto a = RDA(-1);\n\t\tauto b = RDA(-1);\n\n\t\tlong tot;\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\tif (i == 1)\n\t\t\t{\n\t\t\t\ttot += abs(a[1] - b[1]) + 2;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (a[i] != b[i])\n\t\t\t\t{\n\t\t\t\t\ttot += min(a[i], b[i]) + c[i-1] - 1 - max(a[i], b[i]) + 2;\n\t\t\t\t\ttot.chmax(abs(a[i] - b[i]) + 2);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans[ti].chmax(tot + c[i-1] - 1);\n\t\t\t\t\ttot = 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans[ti].chmax(tot + c[i] - 1);\n\t\t\tdebug writeln(\"i:\", i, \" tot:\", tot, \" ans:\", ans[ti]);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto c = readln.splitter.map !(to !(int)).array;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto b = readln.splitter.map !(to !(int)).array;\r\n\t\tlong res = 0;\r\n\t\tlong cur = c[n - 1] - 1;\r\n\t\tforeach_reverse (i; 1..n)\r\n\t\t{\r\n\t\t\tcur += 2;\r\n\t\t\tif (a[i] > b[i])\r\n\t\t\t{\r\n\t\t\t\tswap (a[i], b[i]);\r\n\t\t\t}\r\n\t\t\tres = max (res, cur + b[i] - a[i]);\r\n\t\t\tcur = max (cur, b[i] - a[i]);\r\n\t\t\tcur += c[i - 1] - b[i];\r\n\t\t\tcur += a[i] - 1;\r\n\t\t\tif (a[i] == b[i])\r\n\t\t\t{\r\n\t\t\t\tcur = c[i - 1] - 1;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\r\n\r\nvoid get(Args...)(ref Args args)\r\n{\r\n import std.traits, std.meta, std.typecons;\r\n\r\n static if (Args.length == 1) {\r\n alias Arg = Args[0];\r\n \r\n static if (isArray!Arg) {\r\n static if (isSomeChar!(ElementType!Arg)) {\r\n args[0] = readln.chomp.to!Arg;\r\n } else {\r\n args[0] = readln.split.to!Arg;\r\n }\r\n } else static if (isTuple!Arg) {\r\n auto input = readln.split;\r\n static foreach (i; 0..Fields!Arg.length) {\r\n args[0][i] = input[i].to!(Fields!Arg[i]);\r\n }\r\n } else {\r\n args[0] = readln.chomp.to!Arg;\r\n }\r\n } else {\r\n auto input = readln.split;\r\n assert(input.length == Args.length);\r\n\r\n static foreach (i; 0..Args.length) {\r\n args[i] = input[i].to!(Args[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid get_lines(Args...)(size_t N, ref Args args)\r\n{\r\n import std.traits, std.range;\r\n\r\n static foreach (i; 0..Args.length) {\r\n static assert(isArray!(Args[i]));\r\n args[i].length = N;\r\n }\r\n\r\n foreach (i; 0..N) {\r\n static if (Args.length == 1) {\r\n get(args[0][i]);\r\n } else {\r\n auto input = readln.split;\r\n static foreach (j; 0..Args.length) {\r\n args[j][i] = input[j].to!(ElementType!(Args[j]));\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid main()\r\n{\r\n int T; get(T);\r\n while (T--) {\r\n int N; get(N);\r\n int[] ls, aa, bb; get(ls); get(aa); get(bb);\r\n\r\n int x = abs(aa[1] - bb[1]) + 2, res;\r\n foreach (i; 2..N) {\r\n auto a = aa[i];\r\n auto b = bb[i];\r\n if (a > b) swap(a, b);\r\n res = max(res, x + ls[i-1] - 1);\r\n if (a == b) {\r\n x = 2;\r\n } else {\r\n x += 2 + a-1 + ls[i-1]-b;\r\n x = max(x, b - a + 2);\r\n }\r\n }\r\n res = max(res, x + ls[$-1] - 1);\r\n writeln(res);\r\n }\r\n}"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\r\n\r\nvoid get(Args...)(ref Args args)\r\n{\r\n import std.traits, std.meta, std.typecons;\r\n\r\n static if (Args.length == 1) {\r\n alias Arg = Args[0];\r\n \r\n static if (isArray!Arg) {\r\n static if (isSomeChar!(ElementType!Arg)) {\r\n args[0] = readln.chomp.to!Arg;\r\n } else {\r\n args[0] = readln.split.to!Arg;\r\n }\r\n } else static if (isTuple!Arg) {\r\n auto input = readln.split;\r\n static foreach (i; 0..Fields!Arg.length) {\r\n args[0][i] = input[i].to!(Fields!Arg[i]);\r\n }\r\n } else {\r\n args[0] = readln.chomp.to!Arg;\r\n }\r\n } else {\r\n auto input = readln.split;\r\n assert(input.length == Args.length);\r\n\r\n static foreach (i; 0..Args.length) {\r\n args[i] = input[i].to!(Args[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid get_lines(Args...)(size_t N, ref Args args)\r\n{\r\n import std.traits, std.range;\r\n\r\n static foreach (i; 0..Args.length) {\r\n static assert(isArray!(Args[i]));\r\n args[i].length = N;\r\n }\r\n\r\n foreach (i; 0..N) {\r\n static if (Args.length == 1) {\r\n get(args[0][i]);\r\n } else {\r\n auto input = readln.split;\r\n static foreach (j; 0..Args.length) {\r\n args[j][i] = input[j].to!(ElementType!(Args[j]));\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid main()\r\n{\r\n int T; get(T);\r\n while (T--) {\r\n int N; get(N);\r\n int[] ls, aa, bb; get(ls); get(aa); get(bb);\r\n\r\n int x = abs(aa[1] - bb[1]) + 2, res;\r\n foreach (i; 2..N) {\r\n auto a = aa[i];\r\n auto b = bb[i];\r\n if (a > b) swap(a, b);\r\n res = max(res, x + ls[i-1] - 1);\r\n if (a == b) {\r\n x = 2;\r\n } else {\r\n x += 2 + a-1 + ls[i-1]-b;\r\n }\r\n }\r\n res = max(res, x + ls[$-1] - 1);\r\n writeln(res);\r\n }\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto c = RDA;\n\t\tauto a = RDA(-1);\n\t\tauto b = RDA(-1);\n\n\t\tlong tot;\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\tif (i == 1)\n\t\t\t{\n\t\t\t\ttot += abs(a[1] - b[1]) + 2;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (a[i] != b[i])\n\t\t\t\t\ttot += min(a[i], b[i]) + c[i-1] - 1 - max(a[i], b[i]) + 2;\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans[ti].chmax(tot + c[i-1] - 1);\n\t\t\t\t\ttot = 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans[ti].chmax(tot + c[i] - 1);\n\t\t\ttot.chmax(abs(a[i] - b[i]));\n\t\t\tdebug writeln(\"i:\", i, \" tot:\", tot, \" ans:\", ans[ti]);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto c = readln.splitter.map !(to !(int)).array;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto b = readln.splitter.map !(to !(int)).array;\r\n\t\tlong res = 0;\r\n\t\tlong cur = c[n - 1] - 1;\r\n\t\tforeach_reverse (i; 1..n)\r\n\t\t{\r\n\t\t\tcur += 2;\r\n\t\t\tif (a[i] > b[i])\r\n\t\t\t{\r\n\t\t\t\tswap (a[i], b[i]);\r\n\t\t\t}\r\n\t\t\tcur = max (cur, b[i] - a[i]);\r\n\t\t\tres = max (res, cur + b[i] - a[i]);\r\n\t\t\tcur += c[i - 1] - b[i];\r\n\t\t\tcur += a[i] - 1;\r\n\t\t\tif (a[i] == b[i])\r\n\t\t\t{\r\n\t\t\t\tcur = c[i - 1] - 1;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "src_uid": "3d898a45ab89b93e006270a77db49017"} {"nl": {"description": "You are given a multiset $$$S$$$ initially consisting of $$$n$$$ distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.You will perform the following operation $$$k$$$ times: Add the element $$$\\lceil\\frac{a+b}{2}\\rceil$$$ (rounded up) into $$$S$$$, where $$$a = \\operatorname{mex}(S)$$$ and $$$b = \\max(S)$$$. If this number is already in the set, it is added again. Here $$$\\operatorname{max}$$$ of a multiset denotes the maximum integer in the multiset, and $$$\\operatorname{mex}$$$ of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example: $$$\\operatorname{mex}(\\{1,4,0,2\\})=3$$$; $$$\\operatorname{mex}(\\{2,5,1\\})=0$$$. Your task is to calculate the number of distinct elements in $$$S$$$ after $$$k$$$ operations will be done.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1\\le n\\le 10^5$$$, $$$0\\le k\\le 10^9$$$) — the initial size of the multiset $$$S$$$ and how many operations you need to perform. The second line of each test case contains $$$n$$$ distinct integers $$$a_1,a_2,\\dots,a_n$$$ ($$$0\\le a_i\\le 10^9$$$) — the numbers in the initial multiset. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print the number of distinct elements in $$$S$$$ after $$$k$$$ operations will be done.", "sample_inputs": ["5\n4 1\n0 1 3 4\n3 1\n0 1 4\n3 0\n0 1 4\n3 2\n0 1 2\n3 2\n1 2 3"], "sample_outputs": ["4\n4\n3\n5\n3"], "notes": "NoteIn the first test case, $$$S=\\{0,1,3,4\\}$$$, $$$a=\\operatorname{mex}(S)=2$$$, $$$b=\\max(S)=4$$$, $$$\\lceil\\frac{a+b}{2}\\rceil=3$$$. So $$$3$$$ is added into $$$S$$$, and $$$S$$$ becomes $$$\\{0,1,3,3,4\\}$$$. The answer is $$$4$$$.In the second test case, $$$S=\\{0,1,4\\}$$$, $$$a=\\operatorname{mex}(S)=2$$$, $$$b=\\max(S)=4$$$, $$$\\lceil\\frac{a+b}{2}\\rceil=3$$$. So $$$3$$$ is added into $$$S$$$, and $$$S$$$ becomes $$$\\{0,1,3,4\\}$$$. The answer is $$$4$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort();\r\n\r\n\t\tif (k == 0)\r\n\t\t{\r\n\t\t\tans[ti] = n;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tint r = n;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] != i)\r\n\t\t\t{\r\n\t\t\t\tr = i;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (r == n)\r\n\t\t\tans[ti] = n + k;\r\n\t\telse\r\n\t\t{\r\n\t\t\tauto y = (r + a[n-1] + 1) / 2;\r\n\t\t\tint add = 1;\r\n\t\t\tforeach (i; 0..n)\r\n\t\t\t{\r\n\t\t\t\tif (a[i] == y)\r\n\t\t\t\t{\r\n\t\t\t\t\tadd = 0;\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tans[ti] = n + add;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1496/problem/B\n// math, greedy\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n long t = readln.chomp.to!long;\n\n long n, k;\nwhile(t--) {\n readf(\"%s %s\\n\", &n, &k);\n long[] a = readln.split.map!(to!long).array;\n a.sort;\n\n long maxima = a[$-1];\n long mex = maxima + 1;\n for(int i = 0; i < n; ++i) {\n if(i != a[i]) {\n mex = i;\n break;\n }\n }\n\n auto rbt = redBlackTree(a);\n\n //a.writeln;\n //writefln(\"max: %s mex: %s\\n\", maxima, mex);\n if(mex < maxima) {\n if(k > 0) {\n rbt.insert((maxima + mex - 1)/2 + 1);\n }\n writefln(\"%s\", rbt.length);\n continue;\n }\n\n writefln(\"%s\", n + k);\n\n}\n}\n\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n int n;\n n = scan!int;\n ll k = scan;\n auto arr = scanArray;\n ll maxx = arr.maxElement;\n auto rbt = redBlackTree!ll(arr);\n bool f = 1;\n for(ll i = 0; i < n.to!long; ++i){\n if(!(i in rbt)){\n f = 0;\n ll val = (maxx + i)/2 + (maxx + i) % 2;\n if(k) rbt.insert(val);\n break;\n }\n }\n if(f){\n writeln(n + k);\n }else{\n writeln(rbt.length);\n }\n}\n\nvoid main(){\n long tests = 1;\n tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort();\r\n\r\n\t\tif (k == 0)\r\n\t\t{\r\n\t\t\tans[ti] = n;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tint r = n;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] != i)\r\n\t\t\t{\r\n\t\t\t\tr = i;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\tauto y = (r + a[n-1] + 1) / 2;\r\n\t\tif (y == n)\r\n\t\t\tans[ti] = n + k;\r\n\t\telse\r\n\t\t{\r\n\t\t\tint add = 1;\r\n\t\t\tforeach (i; 0..n)\r\n\t\t\t{\r\n\t\t\t\tif (a[i] == y)\r\n\t\t\t\t{\r\n\t\t\t\t\tadd = 0;\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tans[ti] = n + add;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort();\r\n\r\n\t\tif (k == 0)\r\n\t\t{\r\n\t\t\tans[ti] = n;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tint r = n;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] != i)\r\n\t\t\t{\r\n\t\t\t\tr = i;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\tauto y = (r + a[n-1] + 1) / 2;\r\n\t\tif (y == n)\r\n\t\t\tans[ti] = n + k;\r\n\t\telse\r\n\t\t{\r\n\t\t\tbool g(int x)\r\n\t\t\t{\r\n\t\t\t\treturn a[x] <= y;\r\n\t\t\t}\r\n\t\t\tauto rr = binarySearch!(g)(-1, n);\r\n\t\t\tif (rr == -1)\r\n\t\t\t\tans[ti] = n + 1;\r\n\t\t\telse if (a[rr] != y)\r\n\t\t\t\tans[ti] = n + 1;\r\n\t\t\telse\r\n\t\t\t\tans[ti] = n;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort();\r\n\r\n\t\tif (k == 0)\r\n\t\t{\r\n\t\t\tans[ti] = n;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\tbool f(int x)\r\n\t\t{\r\n\t\t\treturn a[x] == x;\r\n\t\t}\r\n\t\tauto r = binarySearch!(f)(-1, n) + 1;\r\n\t\tauto y = (r + a[n-1] + 1) / 2;\r\n\t\tif (y == n)\r\n\t\t\tans[ti] = n + k;\r\n\t\telse\r\n\t\t{\r\n\t\t\tbool g(int x)\r\n\t\t\t{\r\n\t\t\t\treturn a[x] <= y;\r\n\t\t\t}\r\n\t\t\tauto rr = binarySearch!(g)(-1, n);\r\n\t\t\tif (rr == -1)\r\n\t\t\t\tans[ti] = n + 1;\r\n\t\t\telse if (a[rr] != y)\r\n\t\t\t\tans[ti] = n + 1;\r\n\t\t\telse\r\n\t\t\t\tans[ti] = n;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto k = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort();\r\n\r\n\t\tbool f(int x)\r\n\t\t{\r\n\t\t\treturn a[x] == x;\r\n\t\t}\r\n\t\tauto r = binarySearch!(f)(-1, n) + 1;\r\n\t\tauto y = (r + a[n-1] + 1) / 2;\r\n\t\tif (y == n)\r\n\t\t\tans[ti] = n + k;\r\n\t\telse\r\n\t\t{\r\n\t\t\tbool g(int x)\r\n\t\t\t{\r\n\t\t\t\treturn a[x] <= y;\r\n\t\t\t}\r\n\t\t\tauto rr = binarySearch!(g)(-1, n);\r\n\t\t\tif (rr == -1)\r\n\t\t\t\tans[ti] = n + 1;\r\n\t\t\telse if (a[rr] != y)\r\n\t\t\t\tans[ti] = n + 1;\r\n\t\t\telse\r\n\t\t\t\tans[ti] = n;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "src_uid": "c0d23fe28ebddbfc960674e3b10122ad"} {"nl": {"description": "Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.", "input_spec": "The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters (\"a\"...\"z\"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.", "output_spec": "Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.", "sample_inputs": ["aaabbac\naabbccac", "a\nz"], "sample_outputs": ["6", "-1"], "notes": "NoteIn the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.typecons;\nimport std.algorithm;\nimport std.array;\nimport std.range;\nimport std.math;\nimport std.container;\n\nvoid main()\n{\n auto fl = readln.chomp;\n auto sl = readln.chomp;\n int cnt;\n foreach (i; 0..26) {\n int a = fl.count!(a => a == 'a' + i);\n int b = sl.count!(a => a == 'a' + i);\n if (a-b > 0) {\n cnt += b;\n } else {\n if (!a && b) {\n cnt = 0;\n break;\n }\n cnt += a;\n } \n }\n (cnt?cnt:-1).writeln;\n \n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.typecons;\nimport std.algorithm;\nimport std.array;\nimport std.range;\nimport std.math;\nimport std.container;\n\nvoid main()\n{\n auto fl = readln.chomp;\n auto sl = readln.chomp;\n int cnt;\n foreach (i; 0..26) {\n int a = fl.count!(a => a == 'a' + i);\n int b = sl.count!(a => a == 'a' + i);\n if (a-b > 0) {\n cnt += b;\n } else {\n cnt += a;\n } \n }\n (cnt?cnt:-1).writeln;\n \n}"}], "src_uid": "b1e09df7c47dbd04992e64826337c28a"} {"nl": {"description": "Karen has just arrived at school, and she has a math test today! The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?Since this number can be quite large, output only the non-negative remainder after dividing it by 109 + 7.", "input_spec": "The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row. The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.", "output_spec": "Output a single integer on a line by itself, the number on the final row after performing the process above. Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.", "sample_inputs": ["5\n3 6 9 12 15", "4\n3 7 5 2"], "sample_outputs": ["36", "1000000006"], "notes": "NoteIn the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.In the second test case, the numbers written on the first row are 3, 7, 5 and 2.Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n import std.conv : to;\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n if (a < 0) return (this = inv()^^(-a));\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e > 0; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op: \"-\")() const { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const {\n return mixin(\"ModInt(this) \" ~ op ~ \"= a\");\n }\n ModInt opBinaryRight(string op)(long a) const {\n return mixin(\"ModInt(a) \" ~ op ~ \"= this\");\n }\n bool opCast(T: bool)() const { return (x != 0); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 7;\nalias Mint = ModInt!MO;\n\n\nenum LIM = 400_005;\nMint[] inv, fac, invFac;\nvoid prepare() {\n inv = new Mint[LIM];\n fac = new Mint[LIM];\n invFac = new Mint[LIM];\n inv[1] = 1;\n foreach (i; 2 .. LIM) {\n inv[i] = -(Mint.M / i) * inv[cast(size_t)(Mint.M % i)];\n }\n fac[0] = invFac[0] = 1;\n foreach (i; 1 .. LIM) {\n fac[i] = fac[i - 1] * i;\n invFac[i] = invFac[i - 1] * inv[i];\n }\n}\nMint binom(long n, long k) {\n if (0 <= k && k <= n) {\n assert(n < LIM);\n return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];\n } else {\n return Mint(0);\n }\n}\n\n\nvoid main() {\n prepare;\n \n debug {\n foreach (n; 1 .. 17 + 1) {\n write(n, \":\");\n foreach (i; 0 .. n) {\n int s = 1;\n auto as = new long[n];\n as[i] = 1;\n foreach_reverse (h; 1 .. n) {\n auto bs = new long[h];\n foreach (j; 0 .. h) {\n bs[j] = as[j] + s * as[j + 1];\n s *= -1;\n }\n as = bs;\n }\n write(\" \", as[0]);\n }\n writeln;\n }\n }\n \n try {\n for (; ; ) {\n const N = readInt();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n \n const k = (N - 1) / 4;\n auto bs = new Mint[N - 4 * k];\n foreach (i; 0 .. N - 4 * k) {\n foreach (j; 0 .. 2 * k + 1) {\n bs[i] += binom(2 * k, j) * A[i + 2 * j];\n }\n }\n debug {\n writeln(\"bs = \", bs);\n }\n \n int s = 1;\n foreach_reverse (h; 1 .. N - 4 * k) {\n auto cs = bs;\n foreach (j; 0 .. h) {\n cs[j] = bs[j] + s * bs[j + 1];\n s *= -1;\n }\n bs = cs;\n }\n writeln(bs[0]);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "cf210ef68d0525dcb1574f450773da39"} {"nl": {"description": "One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.These events are given as a sequence of strings \"name1 reposted name2\", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string \"name1 reposted name2\" user \"name1\" didn't have the joke in his feed yet, and \"name2\" already had it in his feed by the moment of repost. Polycarp was registered as \"Polycarp\" and initially the joke was only in his feed.Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as \"name1 reposted name2\". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive. We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.", "output_spec": "Print a single integer — the maximum length of a repost chain.", "sample_inputs": ["5\ntourist reposted Polycarp\nPetr reposted Tourist\nWJMZBMR reposted Petr\nsdya reposted wjmzbmr\nvepifanov reposted sdya", "6\nMike reposted Polycarp\nMax reposted Polycarp\nEveryOne reposted Polycarp\n111 reposted Polycarp\nVkCup reposted Polycarp\nCodeforces reposted Polycarp", "1\nSoMeStRaNgEgUe reposted PoLyCaRp"], "sample_outputs": ["6", "2", "2"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.string;\n\nvoid main() {\n\n\tubyte n;\n\n\treadf(\"%s\", &n);\n\n\tint ans = 1;\n\tint[string] map;\n\tstring name1, name2;\n\n\tmap[\"polycarp\"] = 1;\n\n\twhile (n--) {\n\t\tname1 = readln(' ').strip;\n\t\treadln(' ');\n\t\tname2 = readln.strip;\n\t\tauto e = map[toLower(name2)] + 1;\n\t\tif (e > ans)\n\t\t\tans = e;\n\t\tmap[toLower(name1)] = e;\n\t}\n\n\twriteln(ans);\n}"}, {"source_code": "import std.stdio;\nimport std.string;\n\nvoid main() {\n\t\n\tubyte n;\n\t\n\treadf(\"%s\", &n);\n\t\n\tint ans = 1;\n\tint[string] map;\n\tstring name1, name2, tmp;\n\t\n\tmap[\"polycarp\"] = 1;\n\n\twhile (n--) {\n\t\treadf(\" %s %s %s\\n\", &name1, &tmp, &name2);\n\t\tauto e = map[toLower(name2)] + 1;\n\t\tif (e > ans)\n\t\t\tans = e;\n\t\tmap[toLower(name1)] = e;\n\t}\n\t\n\twriteln(ans);\n}"}], "negative_code": [], "src_uid": "16d4035b138137bbad247ccd5e560051"} {"nl": {"description": "A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete.Initially, no orders are pending. The factory receives updates of the form di, ai, indicating that ai new orders have been placed for the di-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes.As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day pi. Help the owner answer his questions.", "input_spec": "The first line contains five integers n, k, a, b, and q (1 ≤ k ≤ n ≤ 200 000, 1 ≤ b < a ≤ 10 000, 1 ≤ q ≤ 200 000) — the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively. The next q lines contain the descriptions of the queries. Each query is of one of the following two forms: 1 di ai (1 ≤ di ≤ n, 1 ≤ ai ≤ 10 000), representing an update of ai orders on day di, or 2 pi (1 ≤ pi ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi? It's guaranteed that the input will contain at least one query of the second type.", "output_spec": "For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over all n days.", "sample_inputs": ["5 2 2 1 8\n1 1 2\n1 5 3\n1 2 1\n2 2\n1 4 2\n1 3 2\n2 1\n2 3", "5 4 10 1 6\n1 1 5\n1 5 5\n1 3 2\n1 5 2\n2 1\n2 2"], "sample_outputs": ["3\n6\n4", "7\n1"], "notes": "NoteConsider the first sample.We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled.For the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int LOG_N = 18;\nimmutable int MED_N = 1 << LOG_N;\nimmutable int MAX_N = MED_N << 1;\n\nvoid main ()\n{\n\tint n;\n\tint k;\n\tint a;\n\tint b;\n\tint q;\n\twhile (readf (\" %s %s %s %s %s\", &n, &k, &a, &b, &q) > 0)\n\t{\n\t\tauto fa = new int [MAX_N];\n\t\tauto fb = new int [MAX_N];\n\n\t\tvoid add (int [] f, int day, int delta)\n\t\t{\n\t\t\tfor (day += MED_N; day > 0; day >>= 1)\n\t\t\t{\n\t\t\t\tf[day] += delta;\n\t\t\t}\n\t\t}\n\n\t\tint ask (int [] f, int lo, int hi)\n\t\t{\n\t\t\tint res = 0;\n\t\t\tfor (lo += MED_N, hi += MED_N; lo < hi;\n\t\t\t lo >>= 1, hi >>= 1)\n\t\t\t{\n\t\t\t\tif (lo & 1)\n\t\t\t\t{\n\t\t\t\t\tres += f[lo];\n\t\t\t\t\tlo++;\n\t\t\t\t}\n\t\t\t\tif (hi & 1)\n\t\t\t\t{\n\t\t\t\t\thi--;\n\t\t\t\t\tres += f[hi];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint type;\n\t\t\treadf (\" %s\", &type);\n\t\t\tif (type == 1)\n\t\t\t{\n\t\t\t\tint day;\n\t\t\t\tint num;\n\t\t\t\treadf (\" %s %s\", &day, &num);\n\t\t\t\tday--;\n\t\t\t\tint pos = day + MED_N;\n\t\t\t\tadd (fa, day,\n\t\t\t\t min (fa[pos] + num, a) - fa[pos]);\n\t\t\t\tadd (fb, day,\n\t\t\t\t min (fb[pos] + num, b) - fb[pos]);\n\t\t\t}\n\t\t\telse if (type == 2)\n\t\t\t{\n\t\t\t\tint p;\n\t\t\t\treadf (\" %s\", &p);\n\t\t\t\tp--;\n\t\t\t\twriteln (ask (fb, 0, p) +\n\t\t\t\t ask (fa, p + k, n));\n\t\t\t}\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "d256f8cf105b08624eee21dd76a6ad3d"} {"nl": {"description": "A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.", "input_spec": "A single line contains a single integer n (1 ≤ n ≤ 100) — the permutation size.", "output_spec": "If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn — permutation p, that is perfect. Separate printed numbers by whitespaces.", "sample_inputs": ["1", "2", "4"], "sample_outputs": ["-1", "2 1", "2 1 4 3"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\n\nvoid main() {\n\tint n;\n\treadf(\"%d\", &n);\n\tif (n & 1) writeln(-1);\n\telse {\n\t\tfor (int i = 0; i < n; i += 2) {\n\t\t\tif (i > 0) writef(\" \");\n\t\t\twritef(\"%d %d\", i + 2, i + 1);\n\t\t}\n\t\twriteln();\n\t}\n}"}], "negative_code": [], "src_uid": "204ba74195a384c59fb1357bdd71e16c"} {"nl": {"description": "DZY loves colors, and he enjoys painting.On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.DZY wants to perform m operations, each operation can be one of the following: Paint all the units with numbers between l and r (both inclusive) with color x. Ask the sum of colorfulness of the units between l and r (both inclusive). Can you help DZY?", "input_spec": "The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105). Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation. If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1. If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.", "output_spec": "For each operation 2, print a line containing the answer — sum of colorfulness.", "sample_inputs": ["3 3\n1 1 2 4\n1 2 3 5\n2 1 3", "3 4\n1 1 3 4\n2 1 1\n2 2 2\n2 3 3", "10 6\n1 1 5 3\n1 2 7 9\n1 10 10 11\n1 3 8 12\n1 1 10 3\n2 1 10"], "sample_outputs": ["8", "3\n2\n1", "129"], "notes": "NoteIn the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].So the answer to the only operation of type 2 is 8."}, "positive_code": [{"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nimmutable BLOCK_SIZE = 400;\n\nint N;\n\nbool[] unis;\nint[][] as;\nlong[][] vals;\nlong[] adds;\nlong[] sums;\n\nvoid paint(int l, int r, int x) {\n\tconst jl = l / BLOCK_SIZE, kl = l % BLOCK_SIZE;\n\tconst jr = r / BLOCK_SIZE, kr = r % BLOCK_SIZE;\n\tforeach (j; [ jl, jr ].unique) {\n\t\tif (unis[j]) {\n\t\t\tunis[j] = false;\n\t\t\tas[j][] = as[j][0];\n\t\t}\n\t\tforeach (k; 0 .. as[j].length) {\n\t\t\tconst i = j * BLOCK_SIZE + k;\n\t\t\tif (l <= i && i <= r) {\n\t\t\t\tconst long tmp = abs(as[j][k] - x);\n\t\t\t\tvals[j][k] += tmp;\n\t\t\t\tsums[j] += tmp;\n\t\t\t\tas[j][k] = x;\n\t\t\t}\n\t\t}\n\t}\n\tforeach (j; jl + 1 .. jr) {\n\t\tif (unis[j]) {\n\t\t\tconst long tmp = abs(as[j][0] - x);\n\t\t\tadds[j] += tmp;\n\t\t\tsums[j] += tmp * as[j].length;\n\t\t} else {\n\t\t\tforeach (k; 0 .. as[j].length) {\n\t\t\t\tconst long tmp = abs(as[j][k] - x);\n\t\t\t\tvals[j][k] += tmp;\n\t\t\t\tsums[j] += tmp;\n\t\t\t\tas[j][k] = x;\n\t\t\t}\n\t\t}\n\t\tunis[j] = true;\n\t\tas[j][0] = x;\n\t}\n}\n\nlong getSum(int l, int r) {\n\tconst jl = l / BLOCK_SIZE, kl = l % BLOCK_SIZE;\n\tconst jr = r / BLOCK_SIZE, kr = r % BLOCK_SIZE;\n\tlong ret;\n\tforeach (j; [ jl, jr ].unique) {\n\t\tforeach (k; 0 .. as[j].length) {\n\t\t\tconst i = j * BLOCK_SIZE + k;\n\t\t\tif (l <= i && i <= r) {\n\t\t\t\tret += vals[j][k];\n\t\t\t\tret += adds[j];\n\t\t\t}\n\t\t}\n\t}\n\tforeach (j; jl + 1 .. jr) {\n\t\tret += sums[j];\n\t}\n\treturn ret;\n}\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tN = readInt;\n\t\t\ndebug{\nint[] cs=new int[N];\nlong[] vs=new long[N];\n}\n\t\tconst numBlocks = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;\n\t\tunis = new bool[numBlocks];\n\t\tas = new int[][numBlocks];\n\t\tvals = new long[][numBlocks];\n\t\tadds = new long[numBlocks];\n\t\tsums = new long[numBlocks];\n\t\t\n\t\tforeach (i; 0 .. N) {\n\t\t\tconst jl = i / BLOCK_SIZE;\n\t\t\tas[jl] ~= (i + 1);\n\t\t\tvals[jl] ~= 0;\ndebug{\ncs[i]=i+1;\nvs[i]=0;\n}\n\t\t}\n\t\t\n\t\tconst QRY = readInt;\n\t\tforeach (q; 0 .. QRY) {\ndebug{\nwriteln(\"**** cs = \",cs,\" ****\");\nwriteln(\"**** vs = \",vs,\" ****\");\nwriteln(\"unis = \",unis);\nwriteln(\"as = \",as);\nwriteln(\"vals = \",vals);\nwriteln(\"adds = \",adds);\nwriteln(\"sums = \",sums);\n}\n\t\t\tswitch (readInt) {\n\t\t\t\tcase 1: {\n\t\t\t\t\tconst l = readInt - 1;\n\t\t\t\t\tconst r = readInt - 1;\n\t\t\t\t\tconst x = readInt;\n\t\t\t\t\tpaint(l, r, x);\ndebug{\nforeach(i;l..r+1){vs[i]+=abs(cs[i]-x);cs[i]=x;}\n}\n\t\t\t\t} break;\n\t\t\t\tcase 2: {\n\t\t\t\t\tconst l = readInt - 1;\n\t\t\t\t\tconst r = readInt - 1;\n\t\t\t\t\tconst res = getSum(l, r);\ndebug{\nwriteln(\"brute = \",reduce!\"a+b\"(0L,vs[l..r+1]));\n}\n\t\t\t\t\twriteln(res);\n\t\t\t\t} break;\n\t\t\t\tdefault: assert(false);\n\t\t\t}\n\t\t}\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [], "src_uid": "562656bfc27b7cf06f7c4a373c6bc029"} {"nl": {"description": "Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.Limak competed in n contests in the year 2016. He remembers that in the i-th contest he competed in the division di (i.e. he belonged to this division just before the start of this contest) and his rating changed by ci just after the contest. Note that negative ci denotes the loss of rating.What is the maximum possible rating Limak can have right now, after all n contests? If his rating may be arbitrarily big, print \"Infinity\". If there is no scenario matching the given information, print \"Impossible\".", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000). The i-th of next n lines contains two integers ci and di ( - 100 ≤ ci ≤ 100, 1 ≤ di ≤ 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.", "output_spec": "If Limak's current rating can be arbitrarily big, print \"Infinity\" (without quotes). If the situation is impossible, print \"Impossible\" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.", "sample_inputs": ["3\n-7 1\n5 2\n8 2", "2\n57 1\n22 2", "1\n-5 1", "4\n27 2\n13 1\n-50 1\n8 2"], "sample_outputs": ["1907", "Impossible", "Infinity", "1897"], "notes": "NoteIn the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating: Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. With rating 1894 Limak is in the division 2. His rating increases by 5. Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets  + 8 and ends the year with rating 1907. In the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest."}, "positive_code": [{"source_code": "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,\nstd.random,std.uni,std.traits,std.variant;\nimport core.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\nimport std.stdio;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;\nalias redBlackTree rbt;\nalias BigInt big;\nalias pair!(int,int) pii;\nalias pair!(long,long) pll;\nalias BoyerMooreFinder strfind;\nalias mp=make_pair;\nalias bins=binary_search;\nalias orsq=orient_square;\nimmutable int mod=1000000007;\npure nothrow T lcm (T)(auto ref T a,auto ref T b)\n{\n\treturn a/gcd(a,b)*b;\n}\npure nothrow X binpow(X,Y)(X base,Y exp)\nif(is(typeof(exp&1)))\n{\n\tif(exp<0)return X(0);\n\tX res=X(1);\n\twhile(exp>0)\n\t{\n\t\tif(exp&1)res*=base;\n\t\tbase*=base;\n\t\texp>>=1;\n\t}\n\treturn res;\n}\npure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)\nif(is(typeof(exp&1)))\n{\n\tif(mm==0) return binpow(base,exp);\n\tif(exp<0)return X(0);\n\tauto res=X(1)%mm;\n\twhile(exp>0)\n\t{\n\t\tif(exp&1)res=(res*base)%mm;\n\t\tbase*=base;\n\t\tbase%=mm;\n\t\texp>>=1;\n\t}\n\treturn res;\n}\nvoid putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}\nvoid putarr(X)(in X a,in int n,in int m)\n{\n\tforeach(i;0..n)\n\t{\n\t\tforeach(j;0..m)write(a[i][j],' ');\n\t\twriteln;\n\t}\n}\nbool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}\nbool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nstruct pair(X,Y)\n{\n\tX first;\n\tY second;\n\talias first fi;\n\talias second se;\n\tthis(X a_,Y b_){\n\t\tfi=a_;\n\t\tse=b_;\n\t}\n\tthis(this){fi=fi;se=se;}\n\tthis(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))\n\t{\n\t\tfi=p.fi;\n\t\tse=p.se;\n\t}\n\tint opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fis_.fi);\n\t\tif(cmp!=0)return cmp;\n\t\telse return (0-(ses_.se));\n\t}\n};\npair!(X,Y) make_pair(X,Y)(in X x_,in Y y_) pure nothrow @safe\n{\n\tpair!(X,Y) pp;\n\tpp.fi=x_;\n\tpp.se=y_;\n\treturn pp;\n}\npure nothrow @property X sqr(X)(in X a_) {return a_*a_;}\npure nothrow @property X cub(X)(in X a_) {return a_*a_*a_;}\nnothrow void inf(in char* name) {freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow void ouf(in char* name) {freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.split.map!(to!(T)).array;}\nnothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t{\n\t\tauto m=(l+r)>>1;\n\t\tif(!(a[m]1)\n\t{\n\t\tauto m=(l+r)>>1;\n\t\tif(g1)\n\t{\n\t\tauto m=(l+r)>>1;\n\t\tif(!(a[m]> 1;\n\t\tif(i> 1;\n\t\tif(r<=vm) return cel(2*v,vl,vm,l,r);\n\t\telse if(l>=vm) return cel(2*v+1,vm,vr,l,r);\n\t\telse return fun(cel(2*v,vl,vm,l,vm),cel(2*v+1,vm,vr,vm,r));\n\t}\n\tT cel(uint l,uint r){return cel(1,0,n,l,r);}\n};\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter\n//---------------------------------------------------------------program beginning-----------------------------------------------------------------\n\nvoid main()\n{\n\tint n;\n\twhile(input(&n))\n\t{\n\t\tauto a=new pii[n];\n\t\tforeach_reverse(i;0..n)input(&a[i].fi,&a[i].se);\n\t\tint check(int r)\n\t\t{\n\t\t\tforeach(i;0..n)\n\t\t\t{\n\t\t\t\tr-=a[i].fi;\n\t\t\t\tif(a[i].se==2 && r>1899)return 1;\n\t\t\t\telse if(a[i].se==1 && r<1900) return -1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t\tint l=int.min/2,r=int.max/2;\n\t\twhile(r-l>1)\n\t\t{\n\t\t\tint m=(l+r)>>1;\n\t\t\tauto k=check(m);\n\t\t\tif(k==1) r=m;\n\t\t\telse l=m;\n\t\t}\n\t\tif(l>=int.max/2-1)writeln(\"Infinity\");\n\t\telse if(check(l)!=0)writeln(\"Impossible\");\n\t\telse writeln(l);\n\t}\n\tdebug system(\"pause\");\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tint lo = int.min / 2;\n\t\tint hi = int.max / 2;\n\t\tint delta = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint c;\n\t\t\tint d;\n\t\t\treadf (\" %s %s\", &c, &d);\n\t\t\tif (d == 1)\n\t\t\t{ // rating + delta >= 1900\n\t\t\t\tlo = max (lo, 1900 - delta);\n\t\t\t}\n\t\t\telse if (d == 2)\n\t\t\t{ // rating + delta <= 1899\n\t\t\t\thi = min (hi, 1899 - delta);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert (false);\n\t\t\t}\n\t\t\tdelta += c;\n\t\t}\n\t\tdebug {writeln (lo, \" \", hi);}\n\t\tif (hi > int.max / 4)\n\t\t{\n\t\t\twriteln (\"Infinity\");\n\t\t}\n\t\telse if (lo > hi)\n\t\t{\n\t\t\twriteln (\"Impossible\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (hi + delta);\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "2a4c24341231cabad6021697f15d953a"} {"nl": {"description": "A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily $$$7$$$ days!In detail, she can choose any integer $$$k$$$ which satisfies $$$1 \\leq k \\leq r$$$, and set $$$k$$$ days as the number of days in a week.Alice is going to paint some $$$n$$$ consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.For example, in the picture, a week has $$$4$$$ days and Alice paints $$$5$$$ consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive $$$n$$$ days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.", "input_spec": "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. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains two integers $$$n$$$, $$$r$$$ ($$$1 \\le n \\le 10^9, 1 \\le r \\le 10^9$$$).", "output_spec": "For each test case, print a single integer  — the answer to the problem. Please note, that the answer for some test cases won't fit into $$$32$$$-bit integer type, so you should use at least $$$64$$$-bit integer type in your programming language.", "sample_inputs": ["5\n3 4\n3 2\n3 1\n13 7\n1010000 9999999"], "sample_outputs": ["4\n3\n1\n28\n510049495001"], "notes": "NoteIn the first test case, Alice can set $$$1,2,3$$$ or $$$4$$$ days as the number of days in a week.There are $$$6$$$ possible paintings shown in the picture, but there are only $$$4$$$ different shapes. So, the answer is $$$4$$$. Notice that the last example in the picture is an invalid painting because all cells are not connected by sides. In the last test case, be careful with the overflow issue, described in the output format."}, "positive_code": [{"source_code": "import std.algorithm, std.conv, std.stdio, std.string;\nvoid main () {\n\tforeach (i; 0..readln.strip.to!int) {\n\t\tint n, r;\n\t\treadf !(\" %s %s\") (n, r);\n\t\tint s = min (n, r + 1);\n\t\twriteln (s * (s - 1L) / 2 + (n <= r));\n\t}\n}\n"}, {"source_code": "import std.container: DList;\nimport std.algorithm: all, map, count, filter;\nimport std.range: iota;\nimport std.array: array;\nimport std.stdio;\nimport std.utf;\n\nDList!string _words;\nvoid read(T)(out T t)\n{\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n t = to!T(word);\n}\n\nvoid main()\n{\n int t;\n read(t);\n while(t--)\n {\n long n, r;\n read(n), read(r);\n // k < n\n // k = 1: 1\n // k = 2: 2\n // k = 3: 3\n // ...\n // k = n - 1: n - 1\n // (n - 1)n / 2\n // k >= n\n // k = n: 1\n // k = n + 1: 1\n // ...\n // k = r: r - n\n // (r - n + 1)(r - n + 2) / 2\n\n if (r >= n)\n {\n writeln((n - 1)*n/2 + 1);\n }\n else\n {\n writeln(r * (r + 1) / 2);\n }\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD;\n\t\tauto r = RD;\n\n\t\tauto x = min(n-1, r);\n\t\tans[ti] = x * (x+1) / 2;\n\t\tif (r >= n)\n\t\t\t++ans[ti];\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "eb3d8259ca598c3c455ddfdbe433cb78"} {"nl": {"description": "Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag).You are given an $$$n \\times m$$$ grid of \"R\", \"W\", and \".\" characters. \"R\" is red, \"W\" is white and \".\" is blank. The neighbours of a cell are those that share an edge with it (those that only share a corner do not count).Your job is to colour the blank cells red or white so that every red cell only has white neighbours (and no red ones) and every white cell only has red neighbours (and no white ones). You are not allowed to recolour already coloured cells.", "input_spec": "The first line contains $$$t$$$ ($$$1 \\le t \\le 100$$$), the number of test cases. In each test case, the first line will contain $$$n$$$ ($$$1 \\le n \\le 50$$$) and $$$m$$$ ($$$1 \\le m \\le 50$$$), the height and width of the grid respectively. The next $$$n$$$ lines will contain the grid. Each character of the grid is either 'R', 'W', or '.'.", "output_spec": "For each test case, output \"YES\" if there is a valid grid or \"NO\" if there is not. If there is, output the grid on the next $$$n$$$ lines. If there are multiple answers, print any. In the output, the \"YES\"s and \"NO\"s are case-insensitive, meaning that outputs such as \"yEs\" and \"nO\" are valid. However, the grid is case-sensitive.", "sample_inputs": ["3\n4 6\n.R....\n......\n......\n.W....\n4 4\n.R.W\n....\n....\n....\n5 1\nR\nW\nR\nW\nR"], "sample_outputs": ["YES\nWRWRWR\nRWRWRW\nWRWRWR\nRWRWRW\nNO\nYES\nR\nW\nR\nW\nR"], "notes": "NoteThe answer for the first example case is given in the example output, and it can be proven that no grid exists that satisfies the requirements of the second example case. In the third example all cells are initially coloured, and the colouring is valid."}, "positive_code": [{"source_code": "// code by cutx64\r\n\r\nmodule main;\r\n\r\nimport std.string, std.array, std.container, std.typecons;\r\nimport std.stdio, std.format, std.file;\r\nimport std.algorithm, std.conv, std.functional, std.range, core.bitop;\r\n// import std.bigint, std.complex, std.bitmanip;\r\n// import std.math, std.mathspecial, std.numeric;\r\n\r\nvoid main() {\r\n\r\n int tests; readf!\"%s\\n\"(tests);\r\nmultitest_end:\r\n while(tests--) {\r\n int n, m;\r\n readf!\"%s %s\\n\"(n, m);\r\n char[][] grid;\r\n foreach(i; 0 .. n) {\r\n grid ~= readln.strip.dup;\r\n }\r\n foreach(k; 0 .. 2) {\r\n auto answer = new char[][](n, m);\r\n foreach(i; 0 .. n) {\r\n foreach(j; 0 .. m) {\r\n answer[i][j] = \"RW\"[(i + j + k) % 2];\r\n }\r\n }\r\n bool good = true;\r\n foreach(i; 0 .. n) {\r\n foreach(j; 0 .. m) {\r\n good &= (grid[i][j] == answer[i][j] || grid[i][j] == '.');\r\n }\r\n }\r\n if (good) {\r\n writefln!\"YES\\n%-(%s\\n%)\"(answer);\r\n continue multitest_end;\r\n }\r\n }\r\n writefln!\"NO\";\r\n }\r\n\r\n} // main"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport std.range;\nimport core.stdc.stdio;\n\nvoid main()\n{\n\tint t = readInt!int;\n\twhile (t--) solve;\n}\nvoid solve()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto mat = new char[][](n);\n\tforeach(i; 0 .. n)\n\t{\n\t\tmat[i] = cast(char[]) readString;\n\t}\n\tint color = 0;\n\tforeach(i; 0 .. n)\n\t\tforeach(j; 0 .. m)\n\t\t\tif (mat[i][j] != '.')\n\t\t\t\tcolor = ((i + j + (mat[i][j] == 'R')))%2;\n\tbool can = true;\n\tforeach(i; 0 .. n)\n\t\tforeach(j; 0 .. m)\n\t\t\tif (mat[i][j] != '.')\n\t\t\t{\n\t\t\t\tif ((mat[i][j] == 'R') != (i + j + color)%2)\n\t\t\t\t{\n\t\t\t\t\tcan = false;\n\t\t\t\t}\n\t\t\t}\n\tif (!can) return \"NO\".writeln; \n\t\"YES\".writeln;\n\tforeach(i; 0 .. n)\n\t{\n\t\tforeach(j; 0 .. m)\n\t\t{\n\t\t\tif ((i + j + color)%2 == 0) \"W\".write;\n\t\t\telse \"R\".write;\n\t\t}\n\t\twriteln;\n\t}\n}\n\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n\tcurrChar = getchar();\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.array, std.random;\n\nbool check(string[][] a, int n, int m, bool r11)\n{\n foreach (i ; 0 .. n) {\n foreach (j ; 0 .. m) {\n string expected = ((i + j) % 2 == 0) == r11 ? \"R\" : \"W\";\n if (a[i][j] != expected && a[i][j] != \".\")\n return false;\n }\n }\n return true;\n}\n\nvoid fill(string[][] a, int n, int m, bool r11)\n{\n string c1, c2;\n if (r11) {\n c1 = \"R\";\n c2 = \"W\";\n } else {\n c1 = \"W\";\n c2 = \"R\";\n }\n foreach (i ; 0 .. n) {\n foreach (j ; 0 .. m) {\n a[i][j] = (i + j) % 2 == 0 ? c1 : c2;\n }\n }\n}\n\nvoid main()\n{\n int t;\n readf!\" %d \"(t);\n while (t--) {\n int n, m;\n readf!\" %d %d \"(n, m);\n string[][] a;\n foreach (i ; 0 .. n) {\n a ~= readln.strip.split(\"\").array;\n }\n if (check(a, n, m, true)) {\n writeln(\"YES\");\n fill(a, n, m, true);\n foreach (i ; 0 .. n) { writeln(a[i].join(\"\")); }\n } else if (check(a, n, m, false)) {\n writeln(\"YES\");\n fill(a, n, m, false);\n foreach (i ; 0 .. n) { writeln(a[i].join(\"\")); }\n } else {\n writeln(\"NO\");\n }\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\nmultitest_loop:\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint rows, cols;\r\n\t\treadf !(\" %s %s\") (rows, cols);\r\n\t\treadln;\r\n\t\tchar [] [] board;\r\n\t\tforeach (row; 0..rows)\r\n\t\t{\r\n\t\t\tboard ~= readln.strip.dup;\r\n\t\t}\r\n\t\tforeach (k; 0..2)\r\n\t\t{\r\n\t\t\tauto answer = new char [] [] (rows, cols);\r\n\t\t\tforeach (row; 0..rows)\r\n\t\t\t{\r\n\t\t\t\tforeach (col; 0..cols)\r\n\t\t\t\t{\r\n\t\t\t\t\tanswer[row][col] =\r\n\t\t\t\t\t \"RW\"[(row ^ col ^ k) & 1];\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tbool good = true;\r\n\t\t\tforeach (row; 0..rows)\r\n\t\t\t{\r\n\t\t\t\tforeach (col; 0..cols)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (answer[row][col] !=\r\n\t\t\t\t\t board[row][col] &&\r\n\t\t\t\t\t board[row][col] != '.')\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tgood = false;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (good)\r\n\t\t\t{\r\n\t\t\t\twriteln (\"YES\");\r\n\t\t\t\twritefln !(\"%-(%s\\n%)\") (answer);\r\n\t\t\t\tcontinue multitest_loop;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (\"NO\");\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "// code by cutx64\r\n\r\nmodule main;\r\n\r\nimport std.string, std.array, std.container, std.typecons;\r\nimport std.stdio, std.format, std.file;\r\nimport std.algorithm, std.conv, std.functional, std.range, core.bitop;\r\n// import std.bigint, std.complex, std.bitmanip;\r\n// import std.math, std.mathspecial, std.numeric;\r\n\r\nvoid main() {\r\n\r\n int tests; readf!\"%s\\n\"(tests);\r\n while(tests--) {\r\n int n, m;\r\n readf!\"%s %s\\n\"(n, m);\r\n char[][] grid = new char[][n];\r\n foreach(ref str; grid) {\r\n str = readln.strip.dup;\r\n }\r\n int red = 0, white = 0;\r\n foreach(i; 0 .. n) {\r\n foreach(j; 0 .. m) {\r\n if (grid[i][j] == 'R') {\r\n red |= (1 << ((i + j) % 2));\r\n }\r\n else if (grid[i][j] == 'W') {\r\n white |= (1 << ((i + j) % 2));\r\n }\r\n }\r\n }\r\n if ((red & white) != 0) {\r\n \"NO\".writeln;\r\n }\r\n else {\r\n \"YES\".writeln;\r\n if (red == 0 && white == 0) {\r\n red = 1, white = 2;\r\n }\r\n else if (red == 0) {\r\n red = 3 - white;\r\n }\r\n else if (white == 0) {\r\n white = 3 - red;\r\n }\r\n foreach(i; 0 .. n) {\r\n foreach(j; 0 .. m) {\r\n if ((1 << ((i + j) % 2)) == red) {\r\n grid[i][j] = 'R';\r\n }\r\n else {\r\n grid[i][j] = 'W';\r\n }\r\n }\r\n }\r\n writefln!\"%-(%s\\n%)\"(grid);\r\n }\r\n }\r\n\r\n} // main"}], "src_uid": "12f35743f482b68e3156a45d6ac5bb14"} {"nl": {"description": "Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful. Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters. Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.", "input_spec": "The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.", "output_spec": "If there is no way of replacing '#' characters which leads to a beautiful string print  - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with. If there are several possible answers, you may output any of them.", "sample_inputs": ["(((#)((#)", "()((#((#(#()", "#", "(#)"], "sample_outputs": ["1\n2", "2\n2\n1", "-1", "-1"], "notes": "Note|s| denotes the length of the string s."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n auto s = readln.chomp;\n int n = cast(int)s.count!\"a == '#'\";\n\n int[] ans;\n\n int x;\n int open, close;\n foreach (i, c; s) {\n if (c == '(') open++;\n else if (c == ')') close++;\n else {\n assert(c == '#');\n if (x == n - 1) {\n int d = open - close;\n foreach (j; i + 1 .. s.length) {\n d += (s[j] == '(' ? 1 : -1);\n }\n if (d <= 0) {\n writeln(-1);\n return;\n } else {\n ans ~= d;\n close += d;\n }\n } else {\n ans ~= 1;\n close++;\n x++;\n }\n }\n if (open < close) {\n writeln(-1);\n return;\n }\n }\n writefln(\"%(%s\\n%)\", ans);\n}\n"}, {"source_code": "module main;\n\nimport std.stdio, std.string;\n\nvoid solve(char[] s)\n{\n int cl = 0, cr = 0, sl = 0, sr = 0, pos = -1, cnt = 0;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '(')\n {\n ++ sl;\n }\n else if (s[i] == ')')\n {\n ++ sr;\n }\n else\n {\n pos = i;\n ++ cnt;\n }\n }\n int add = 0;\n auto ans = new int[cnt];\n int idx = 0;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '(')\n {\n ++ cl;\n }\n else if (s[i] == ')')\n {\n ++ cr;\n }\n else\n {\n if (i == pos)\n {\n int need = sl - (sr + add);\n if (need <= 0)\n {\n writeln(-1);\n return;\n }\n ans[idx] = need;\n add += need;\n }\n else\n {\n ans[idx] = 1;\n ++ add;\n }\n ++ idx;\n }\n if (cr + add > cl)\n {\n writeln(-1);\n return;\n }\n }\n if (cr + add != cl)\n {\n writeln(-1);\n }\n else\n {\n foreach (i, val; ans)\n {\n writeln(ans[i]);\n }\n }\n}\n\nint main(string[] args)\n{\n char[] s;\n while (stdin.readln(s))\n {\n solve(chomp(s));\n }\n\treturn 0;\n}"}, {"source_code": "module main;\n\nimport std.stdio, std.string;\n\nvoid solve(char[] s)\n{\n int[] ans;\n int pos = -1;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '#')\n {\n ans ~= 1;\n pos = i;\n }\n }\n int cl = 0, cr = 0;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '(')\n {\n ++ cl;\n }\n else\n {\n ++ cr;\n }\n if (cr > cl)\n {\n writeln(-1);\n return;\n }\n }\n ans[$ - 1] += cl - cr;\n cl = cr = 0;\n int idx = 0;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '(')\n {\n ++ cl;\n }\n else if (s[i] == ')')\n {\n ++ cr;\n }\n else\n {\n cr += ans[idx];\n ++ idx;\n }\n if (cr > cl)\n {\n writeln(-1);\n return;\n }\n }\n foreach (i, val; ans)\n {\n writeln(val);\n }\n}\n\nint main(string[] args)\n{\n char[] s;\n while (stdin.readln(s))\n {\n solve(chomp(s));\n }\n return 0;\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n auto s = readln.chomp;\n int n = cast(int)s.count!\"a == '#'\";\n\n int[] ans;\n\n int x;\n int open, close;\n foreach (i, c; s) {\n if (c == '(') open++;\n else if (c == ')') close++;\n else {\n assert(c == '#');\n if (x == n - 1) {\n int d = open - close;\n foreach (j; i + 1 .. s.length) {\n d += (s[j] == '(' ? 1 : -1);\n }\n if (d <= 0) {\n writeln(-1);\n return;\n } else {\n ans ~= d;\n }\n } else {\n ans ~= 1;\n close++;\n x++;\n }\n }\n }\n\n writefln(\"%(%s\\n%)\", ans);\n}\n"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n auto s = readln.chomp;\n int n = cast(int)s.count!\"a == '#'\";\n\n int[] ans;\n\n int x;\n int open, close;\n foreach (i, c; s) {\n if (c == '(') open++;\n else if (c == ')') close++;\n else {\n assert(c == '#');\n if (x == n - 1) {\n int d = open - close;\n foreach (j; i + 1 .. s.length) {\n d += (s[j] == '(' ? 1 : -1);\n }\n if (d <= 0) {\n writeln(-1);\n return;\n } else {\n ans ~= d;\n }\n } else {\n ans ~= 1;\n close++;\n x++;\n }\n }\n }\n\n x = 0; open = 0; close = 0;\n foreach (i, c; s) {\n if (c == '(') open++;\n else if (c == ')') close++;\n else {\n assert(c == '#');\n close += ans[x];\n }\n if (open < close) {\n writeln(-1); return;\n }\n }\n\n writefln(\"%(%s\\n%)\", ans);\n}\n"}, {"source_code": "module main;\n\nimport std.stdio, std.string;\n\nvoid solve(char[] s)\n{\n int[] ans;\n int pos = -1;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '#')\n {\n ans ~= 1;\n pos = i;\n }\n }\n int cl = 0, cr = 0;\n foreach (i; 0 .. s.length)\n {\n if (s[i] == '(')\n {\n ++ cl;\n }\n else\n {\n ++ cr;\n }\n if (cr > cl)\n {\n writeln(-1);\n return;\n }\n }\n ans[$ - 1] += cl - cr;\n foreach (i, val; ans)\n {\n writeln(val);\n }\n}\n\nint main(string[] args)\n{\n char[] s;\n while (stdin.readln(s))\n {\n solve(chomp(s));\n }\n return 0;\n}"}], "src_uid": "0a30830361b26838b192d7de1efcdd2f"} {"nl": {"description": "Polycarp had an array $$$a$$$ of $$$3$$$ positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array $$$b$$$ of $$$7$$$ integers.For example, if $$$a = \\{1, 4, 3\\}$$$, then Polycarp wrote out $$$1$$$, $$$4$$$, $$$3$$$, $$$1 + 4 = 5$$$, $$$1 + 3 = 4$$$, $$$4 + 3 = 7$$$, $$$1 + 4 + 3 = 8$$$. After sorting, he got an array $$$b = \\{1, 3, 4, 4, 5, 7, 8\\}.$$$Unfortunately, Polycarp lost the array $$$a$$$. He only has the array $$$b$$$ left. Help him to restore the array $$$a$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases. Each test case consists of one line which contains $$$7$$$ integers $$$b_1, b_2, \\dots, b_7$$$ ($$$1 \\le b_i \\le 10^9$$$; $$$b_i \\le b_{i+1}$$$). Additional constraint on the input: there exists at least one array $$$a$$$ which yields this array $$$b$$$ as described in the statement.", "output_spec": "For each test case, print $$$3$$$ integers — $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$. If there can be several answers, print any of them.", "sample_inputs": ["5\n1 3 4 4 5 7 8\n1 2 3 4 5 6 7\n300000000 300000000 300000000 600000000 600000000 600000000 900000000\n1 1 2 999999998 999999999 999999999 1000000000\n1 2 2 3 3 4 5"], "sample_outputs": ["1 4 3\n4 1 2\n300000000 300000000 300000000\n999999998 1 1\n1 2 2"], "notes": "NoteThe subsequence of the array $$$a$$$ is a sequence that can be obtained from $$$a$$$ by removing zero or more of its elements.Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length $$$3$$$ has exactly $$$7$$$ different non-empty subsequences."}, "positive_code": [{"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n auto b = ma(7, readInt!int);\n auto bs = redBlackTree!(true, int)(b);\n int[3] a;\n a[0] = b[0];\n a[1] = b[1];\n bs.removeKey(a[0]);\n bs.removeKey(a[1]);\n bs.removeKey(a[0] + a[1]);\n a[2] = bs.front;\n foreach(ai; a) write(ai, \" \");\n writeln;\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n auto b = readln.splitter.map!(to!int).array;\n writefln(\"%d %d %d\", b[0], b[1], b.back - b[0] - b[1]);\n }\n}\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid solve(){\n auto arr = scanArray;\n auto summ = arr[6];\n long a = summ - arr[5];\n long b = summ - arr[4];\n long c = summ - a - b;\n writeln(a, \" \", b, \" \", c);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) solve;\n}\n\n/************** ***** That's All Folks! ***** **************/\nimport std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}], "negative_code": [], "src_uid": "e0ec0cd81d2ec632ef89d207d80fa8a3"} {"nl": {"description": "Given an array $$$a$$$ of $$$n$$$ integers, find a range of values $$$[x, y]$$$ ($$$x \\le y$$$), and split $$$a$$$ into exactly $$$k$$$ ($$$1 \\le k \\le n$$$) subarrays in such a way that: Each subarray is formed by several continuous elements of $$$a$$$, that is, it is equal to $$$a_l, a_{l+1}, \\ldots, a_r$$$ for some $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$). Each element from $$$a$$$ belongs to exactly one subarray. In each subarray the number of elements inside the range $$$[x, y]$$$ (inclusive) is strictly greater than the number of elements outside the range. An element with index $$$i$$$ is inside the range $$$[x, y]$$$ if and only if $$$x \\le a_i \\le y$$$. Print any solution that minimizes $$$y - x$$$.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 3 \\cdot 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the length of the array $$$a$$$ and the number of subarrays required in the partition. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case, print $$$k+1$$$ lines. In the first line, print $$$x$$$ and $$$y$$$ — the limits of the found range. Then print $$$k$$$ lines, the $$$i$$$-th should contain $$$l_i$$$ and $$$r_i$$$ ($$$1\\leq l_i \\leq r_i \\leq n$$$) — the limits of the $$$i$$$-th subarray. You can print the subarrays in any order.", "sample_inputs": ["3\n2 1\n1 2\n4 2\n1 2 2 2\n11 3\n5 5 5 1 5 5 1 5 5 5 1"], "sample_outputs": ["1 2\n1 2\n2 2\n1 3\n4 4\n5 5\n1 1\n2 2\n3 11"], "notes": "NoteIn the first test, there should be only one subarray, which must be equal to the whole array. There are $$$2$$$ elements inside the range $$$[1, 2]$$$ and $$$0$$$ elements outside, if the chosen range is $$$[1, 1]$$$, there will be $$$1$$$ element inside ($$$a_1$$$) and $$$1$$$ element outside ($$$a_2$$$), and the answer will be invalid.In the second test, it is possible to choose the range $$$[2, 2]$$$, and split the array in subarrays $$$(1, 3)$$$ and $$$(4, 4)$$$, in subarray $$$(1, 3)$$$ there are $$$2$$$ elements inside the range ($$$a_2$$$ and $$$a_3$$$) and $$$1$$$ element outside ($$$a_1$$$), in subarray $$$(4, 4)$$$ there is only $$$1$$$ element ($$$a_4$$$), and it is inside the range.In the third test, it is possible to choose the range $$$[5, 5]$$$, and split the array in subarrays $$$(1, 4)$$$, $$$(5, 7)$$$ and $$$(8, 11)$$$, in the subarray $$$(1, 4)$$$ there are $$$3$$$ elements inside the range and $$$1$$$ element outside, in the subarray $$$(5, 7)$$$ there are $$$2$$$ elements inside and $$$1$$$ element outside and in the subarray $$$(8, 11)$$$ there are $$$3$$$ elements inside and $$$1$$$ element outside."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nvoid solve() {\r\n int N, K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readarray!int;\r\n\r\n int[] C = new int[N + 2];\r\n foreach (a; A) C[a]++;\r\n\r\n int[] S = new int[N + 3];\r\n for (int i = 0; i <= N+1; i++) {\r\n S[i + 1] = S[i] + C[i];\r\n }\r\n\r\n int cd = int.max;\r\n int[] range = [-1, -1];\r\n for (int x = 0; x <= N; x++) {\r\n int lb = x, ub = N + 1;\r\n bool check(int m) {\r\n return S[m] - S[x] >= K + (N - K + 1) / 2;\r\n }\r\n if (! check(ub)) continue;\r\n while (lb + 1 < ub) {\r\n int mid = (lb + ub) / 2;\r\n (check(mid) ? ub : lb) = mid;\r\n }\r\n int y = ub - 1;\r\n if (y - x < cd) {\r\n cd = y - x;\r\n range = [x, y];\r\n }\r\n }\r\n\r\n int x = range[0], y = range[1];\r\n assert(x >= 0 && y >= 0 && x <= y);\r\n\r\n Array!int ins, outs;\r\n int[] ans;\r\n for (int i = 0; i < N; i++) {\r\n int a = A[i];\r\n if (x <= a && a <= y) {\r\n if (outs.empty) {\r\n ins ~= i;\r\n } else {\r\n outs.removeBack;\r\n }\r\n } else {\r\n if (ins.empty) {\r\n outs ~= i;\r\n } else {\r\n ins.removeBack;\r\n }\r\n }\r\n if (ins.length == 1) {\r\n ins.removeBack;\r\n ans ~= i;\r\n if (ans.length == K) break;\r\n }\r\n }\r\n writefln(\"%s %s\", x, y);\r\n //writeln(ans);\r\n\r\n if (K == 1) {\r\n writefln(\"%s %s\", 1, N);\r\n } else {\r\n for (int k = 0; k < K; k++) {\r\n int L, R;\r\n if (k == 0) {\r\n L = 0;\r\n R = ans[k];\r\n } else if (k == K - 1) {\r\n L = ans[k - 1] + 1;\r\n R = N - 1;\r\n } else {\r\n L = ans[k - 1] + 1;\r\n R = ans[k];\r\n }\r\n writefln(\"%s %s\", L+1, R+1);\r\n }\r\n }\r\n\r\n\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, k;\r\n\t\treadf !(\" %s %s\") (n, k);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto b = a.dup;\r\n\t\tsort (b);\r\n\r\n\t\tint best = int.max;\r\n\t\tint bestX = -1;\r\n\t\tint bestY = -1;\r\n\t\tint num = 0;\r\n\t\twhile (num < n - num + k)\r\n\t\t{\r\n\t\t\tnum += 1;\r\n\t\t}\r\n\t\tforeach (val; num..n + 1)\r\n\t\t{\r\n\t\t\tauto x = b[val - num];\r\n\t\t\tauto y = b[val - 1];\r\n\t\t\tif (best > y - x)\r\n\t\t\t{\r\n\t\t\t\tbest = y - x;\r\n\t\t\t\tbestX = x;\r\n\t\t\t\tbestY = y;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint [] [] answer;\r\n\t\tint pos = 0;\r\n\t\twhile (answer.length.to !(int) < k - 1)\r\n\t\t{\r\n\t\t\tanswer ~= [pos + 1];\r\n\t\t\tint balance = 0;\r\n\t\t\twhile (balance <= 0)\r\n\t\t\t{\r\n\t\t\t\tbalance += (bestX <= a[pos] &&\r\n\t\t\t\t a[pos] <= bestY) ? +1 : -1;\r\n\t\t\t\tpos += 1;\r\n\t\t\t}\r\n\t\t\tanswer.back ~= pos;\r\n\t\t}\r\n\t\tanswer ~= [pos + 1, n];\r\n\r\n\t\twriteln (bestX, \" \", bestY);\r\n\t\tforeach (const ref line; answer)\r\n\t\t{\r\n\t\t\twritefln !(\"%(%s %)\") (line);\r\n\t\t}\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nvoid solve() {\r\n int N, K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readarray!int;\r\n\r\n int[] C = new int[N + 2];\r\n foreach (a; A) C[a]++;\r\n\r\n int[] S = new int[N + 3];\r\n for (int i = 0; i <= N+1; i++) {\r\n S[i + 1] = S[i] + C[i];\r\n }\r\n\r\n int cd = int.max;\r\n int[] range = [-1, -1];\r\n for (int x = 0; x <= N; x++) {\r\n int lb = x, ub = N + 1;\r\n bool check(int m) {\r\n return S[m] - S[x] >= K + (N - K + 1) / 2;\r\n }\r\n if (! check(ub)) continue;\r\n while (lb + 1 < ub) {\r\n int mid = (lb + ub) / 2;\r\n (check(mid) ? ub : lb) = mid;\r\n }\r\n int y = ub - 1;\r\n if (y - x < cd) {\r\n cd = y - x;\r\n range = [x, y];\r\n }\r\n }\r\n\r\n int x = range[0], y = range[1];\r\n assert(x >= 0 && y >= 0 && x <= y);\r\n\r\n Array!int ins, outs;\r\n int[] ans;\r\n for (int i = 0; i < N; i++) {\r\n int a = A[i];\r\n if (x <= a && a <= y) {\r\n if (outs.empty) {\r\n ins ~= i;\r\n } else {\r\n outs.removeBack;\r\n }\r\n } else {\r\n if (ins.empty) {\r\n outs ~= i;\r\n } else {\r\n ins.removeBack;\r\n }\r\n }\r\n if (ins.length == 1) {\r\n ins.removeBack;\r\n ans ~= i;\r\n if (ans.length == K) break;\r\n }\r\n }\r\n writefln(\"%s %s\", x, y);\r\n //writeln(ans);\r\n\r\n for (int k = 0; k < K; k++) {\r\n int L, R;\r\n if (k == 0) {\r\n L = 0;\r\n R = ans[k];\r\n } else if (k == K - 1) {\r\n L = ans[k - 1] + 1;\r\n R = N - 1;\r\n } else {\r\n L = ans[k - 1] + 1;\r\n R = ans[k];\r\n }\r\n writefln(\"%s %s\", L+1, R+1);\r\n }\r\n\r\n\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "src_uid": "321423f103e6d9c567079d2dde71b5bb"} {"nl": {"description": "This is the easy version of the problem. The difference between the versions is that in the easy version all prices $$$a_i$$$ are different. You can make hacks if and only if you solved both versions of the problem.Today is Sage's birthday, and she will go shopping to buy ice spheres. All $$$n$$$ ice spheres are placed in a row and they are numbered from $$$1$$$ to $$$n$$$ from left to right. Each ice sphere has a positive integer price. In this version all prices are different.An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.", "input_spec": "The first line contains a single integer $$$n$$$ $$$(1 \\le n \\le 10^5)$$$ — the number of ice spheres in the shop. The second line contains $$$n$$$ different integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 10^9)$$$ — the prices of ice spheres.", "output_spec": "In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.", "sample_inputs": ["5\n1 2 3 4 5"], "sample_outputs": ["2\n3 1 4 2 5"], "notes": "NoteIn the example it's not possible to place ice spheres in any order so that Sage would buy $$$3$$$ of them. If the ice spheres are placed like this $$$(3, 1, 4, 2, 5)$$$, then Sage will buy two spheres: one for $$$1$$$ and one for $$$2$$$, because they are cheap."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;\n\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\n\nvoid solve(){\n int n;\n n = rd!int;\n ll[] arr = rdarr;\n arr.sort();\n ll[] res = new ll[](n);\n foreach(i; 0..n/2){\n res[2*i + 1] = arr[i]; \n }\n foreach(i;(n/2)..n){\n res[2*(i - n/2)] = arr[i];\n }\n ll num = 0;\n foreach(i; 1..n-1){\n if(res[i] < res[i-1] && res[i] < res[i+1]){\n ++num;\n }\n }\n writeln(num);\n foreach(el; res){\n write(el, \" \");\n }\n writeln;\n}\n\nvoid main(){\n long t = 1;\n /* t = rd; */\n while(t--) solve();\n stdout.flush;\n}\n\n"}, {"source_code": "// Vicfred\n// https://codeforces.com/contest/1419/problem/D1\n// greedy\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.container;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n long n = readln.chomp.to!long;\n\n long[] a = readln.split.map!(to!long).array;\n a.sort;\n\n auto dlist = DList!long(a);\n\n long[] solution;\n\n for(int i = 0; i < n; i++) {\n if(i % 2 == 0) {\n solution ~= dlist.back;\n dlist.removeBack;\n } else {\n solution ~= dlist.front;\n dlist.removeFront;\n }\n }\n\n int ans = 0;\n for(int i = 1; i + 1 < n; i++) {\n if(solution[i] < solution[i - 1] &&\n solution[i] < solution[i + 1])\n ans += 1;\n }\n ans.writeln;\n foreach(item; solution)\n writef(\"%s \", item);\n \"\".writeln;\n}\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\n\nvoid main()\n{\n auto N = readln.chomp.to!int;\n auto AS = readln.split.to!(int[]);\n sort!\"a > b\"(AS);\n\n auto BS = new int[](N);\n int i, j;\n while (j < N) {\n BS[j] = AS[i];\n ++i;\n j += 2;\n }\n foreach (k; 0..N) if (BS[k] == 0) BS[k] = AS[i++];\n if (N%2 == 0) {\n int k = 1;\n while (k < N-1) {\n if (BS[k-1] > BS[k] && BS[k+1] > BS[k]) {\n k += 2;\n continue;\n }\n BS[$-1] = BS[k];\n BS[k] = AS[i-1];\n goto end;\n }\n }\n end:\n\n int r;\n foreach (k; 1..N-1) if (BS[k-1] > BS[k] && BS[k+1] > BS[k]) ++r;\n writeln(r);\n writeln(BS.to!(string[]).join(\" \"));\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto a = RDA;\n\ta.sort();\n\n\tlong[] ans;\n\tdebug writeln(a);\n\tforeach (i; 0..n)\n\t{\n\t\tif (i % 2 == 0)\n\t\t{\n\t\t\tans ~= a.back; a.popBack;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tans ~= a.front; a.popFront;\n\t\t}\n\t\tdebug writeln(a);\n\t}\n\n\twriteln((ans.length-1) / 2);\n\tans.map!(to!string).join(\" \").writeln;\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1419/problem/D1\n// greedy\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.container;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n long n = readln.chomp.to!long;\n\n long[] a = readln.split.map!(to!long).array;\n a.sort;\n\n auto dlist = DList!long(a);\n\n long[] solution;\n\n for(int i = 0; i < n; i++) {\n if(i % 2 == 0) {\n solution ~= dlist.back;\n dlist.removeBack;\n } else {\n solution ~= dlist.front;\n dlist.removeFront;\n }\n }\n\n (n/2).writeln;\n foreach(item; solution) {\n writef(\"%s \", item);\n } \"\".writeln;\n}\n"}], "src_uid": "bcd9439fbf6aedf6882612d5f7d6220f"} {"nl": {"description": "CQXYM is counting permutations length of $$$2n$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).A permutation $$$p$$$(length of $$$2n$$$) will be counted only if the number of $$$i$$$ satisfying $$$p_i<p_{i+1}$$$ is no less than $$$n$$$. For example: Permutation $$$[1, 2, 3, 4]$$$ will count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$3$$$ ($$$i = 1$$$, $$$i = 2$$$, $$$i = 3$$$). Permutation $$$[3, 2, 1, 4]$$$ won't count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$1$$$ ($$$i = 3$$$). CQXYM wants you to help him to count the number of such permutations modulo $$$1000000007$$$ ($$$10^9+7$$$).In addition, modulo operation is to get the remainder. For example: $$$7 \\mod 3=1$$$, because $$$7 = 3 \\cdot 2 + 1$$$, $$$15 \\mod 4=3$$$, because $$$15 = 4 \\cdot 3 + 3$$$. ", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t (t \\geq 1)$$$ — the number of test cases. The description of the test cases follows. Only one line of each test case contains an integer $$$n(1 \\leq n \\leq 10^5)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$", "output_spec": "For each test case, print the answer in a single line.", "sample_inputs": ["4\n1\n2\n9\n91234"], "sample_outputs": ["1\n12\n830455698\n890287984"], "notes": "Note$$$n=1$$$, there is only one permutation that satisfies the condition: $$$[1,2].$$$In permutation $$$[1,2]$$$, $$$p_1<p_2$$$, and there is one $$$i=1$$$ satisfy the condition. Since $$$1 \\geq n$$$, this permutation should be counted. In permutation $$$[2,1]$$$, $$$p_1>p_2$$$. Because $$$0<n$$$, this permutation should not be counted.$$$n=2$$$, there are $$$12$$$ permutations: $$$[1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,3,1,4],[2,3,4,1],[2,4,1,3],[3,1,2,4],[3,4,1,2],[4,1,2,3].$$$"}, "positive_code": [{"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\nimport std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.digest, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib;\n\nconst long MAXN = 200000;\nconst long m = 1000000007;\n\nvoid main()\n{\n long[] factorial;\n factorial ~= 1;\n factorial ~= 1;\n factorial ~= 1;\n for (int i = 3; i <= MAXN; i++)\n factorial ~= factorial[i - 1] * i % m;\n\n long t;\n readf(\" %d \", &t);\n while (t--) {\n int n;\n readf(\" %d \", &n);\n writeln(factorial[2 * n]);\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\r\n\t\tans[ti] = 1;\r\n\t\tforeach (i; 2..n*2)\r\n\t\t\tans[ti].modm(i+1);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "19a2550af6a46308fd92c7a352f12a5f"} {"nl": {"description": "Valera has array a, consisting of n integers a0, a1, ..., an - 1, and function f(x), taking an integer from 0 to 2n - 1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number x contains a 1 on the i-th position, and zero otherwise.For example, if n = 4 and x = 11 (11 = 20 + 21 + 23), then f(x) = a0 + a1 + a3.Help Valera find the maximum of function f(x) among all x, for which an inequality holds: 0 ≤ x ≤ m.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of array elements. The next line contains n space-separated integers a0, a1, ..., an - 1 (0 ≤ ai ≤ 104) — elements of array a. The third line contains a sequence of digits zero and one without spaces s0s1... sn - 1 — the binary representation of number m. Number m equals .", "output_spec": "Print a single integer — the maximum value of function f(x) for all .", "sample_inputs": ["2\n3 8\n10", "5\n17 0 10 2 1\n11010"], "sample_outputs": ["3", "27"], "notes": "NoteIn the first test case m = 20 = 1, f(0) = 0, f(1) = a0 = 3.In the second sample m = 20 + 21 + 23 = 11, the maximum value of function equals f(5) = a0 + a2 = 17 + 10 = 27."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm.comparison;\n\n__gshared int[100_100] lastone;\n__gshared long[100_100] cumsum;\n__gshared int[100_100] arr;\n\nlong solvit(int idx) {\n if (lastone[idx] == -1) return 0;\n if (lastone[idx] == 0) return cumsum[0];\n return max(cumsum[lastone[idx] - 1], \n arr[lastone[idx]] + solvit(lastone[idx] - 1));\n}\n\nint main() {\n debug stdin = File(\"in.txt\", \"r\");\n\n char[100_100] bin;\n long binrep = 0, aml = 0;\n int n;\n readf(\" %s\\n\", &n);\n foreach (i; 0 .. n) {\n readf(\" %s\", &arr[i]);\n }\n char[] pbin = bin;\n string dummy = readln;\n readln(pbin);\n cumsum[0] = arr[0];\n if (bin[0] == '1') lastone[0] = 0;\n else lastone[0] = -1;\n foreach (i; 1 .. n) {\n cumsum[i] = arr[i] + cumsum[i-1];\n if (bin[i] == '1') lastone[i] = i;\n else lastone[i] = lastone[i-1];\n }\n writeln(solvit(n - 1));\n\n return 0;\n}"}, {"source_code": "import std.stdio;\nimport std.algorithm.comparison;\n\n__gshared int[100_100] lastone;\n__gshared long[100_100] cumsum;\n__gshared int[100_100] arr;\n\nlong solvit(int idx) {\n if (lastone[idx] == -1) return 0;\n if (lastone[idx] == 0) return cumsum[0];\n return max(cumsum[lastone[idx] - 1], \n arr[lastone[idx]] + solvit(lastone[idx] - 1));\n}\n\nint main() {\n debug stdin = File(\"in.txt\", \"r\");\n\n int n;\n readf(\" %s\\n\", &n);\n foreach (i; 0 .. n) {\n readf(\" %s\", &arr[i]);\n }\n string dummy = readln;\n string bin = readln();\n cumsum[0] = arr[0];\n if (bin[0] == '1') lastone[0] = 0;\n else lastone[0] = -1;\n foreach (i; 1 .. n) {\n cumsum[i] = arr[i] + cumsum[i-1];\n if (bin[i] == '1') lastone[i] = i;\n else lastone[i] = lastone[i-1];\n }\n writeln(solvit(n - 1));\n\n return 0;\n}"}, {"source_code": "import std.stdio, std.string;\nimport std.algorithm;\n\nvoid solve(int[] a, int[] s, int n)\n{\n auto acc = new int[n];\n acc[0] = a[0];\n foreach (i; 1 .. n)\n {\n acc[i] = acc[i - 1] + a[i];\n }\n auto ans = 0, sum = 0;\n foreach (i; 0 .. n)\n {\n if (s[i] == 1)\n {\n if (n - i - 2 >= 0)\n {\n auto val = sum + acc[n - i - 2];\n ans = max(ans, val);\n }\n sum += a[n - i - 1];\n }\n }\n ans = max(ans, sum);\n writeln(ans);\n}\n\nint main(string[] args)\n{\n int n;\n while (readf(\"%d\\n\", &n) == 1)\n {\n auto a = new int[n];\n foreach (i; 0 .. n)\n {\n readf(\" %d\", &a[i]);\n }\n readln;\n auto str = readln().strip();\n auto s = new int[n];\n foreach (i; 0 .. n)\n {\n s[i] = str[i] - '0';\n }\n s = s.reverse;\n solve(a, s, n);\n }\n return 0;\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.algorithm.comparison;\n\nint main() {\n debug stdin = File(\"in.txt\", \"r\");\n\n int[100_100] arr;\n char[100_100] bin;\n long binrep = 0, aml = 0;\n int n;\n readf(\" %s\\n\", &n);\n foreach (i; 0 .. n) {\n readf(\" %s\", &arr[i]);\n }\n char[] pbin = bin;\n string dummy = readln;\n readln(pbin);\n int lastone = -1;\n foreach (i; 0 .. n) {\n if (bin[i] == '1') lastone = i;\n }\n if (lastone == -1) {\n writeln(\"0\");\n return 0;\n }\n foreach (i; 0 .. lastone) {\n if (bin[i] == '1') binrep += arr[i];\n aml += arr[i];\n }\n binrep += arr[lastone];\n writeln(max(binrep, aml));\n \n return 0;\n}"}], "src_uid": "9366e1626b33b4f4e49cf35200c0448f"} {"nl": {"description": "There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters. Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries: Add xi liters of water to the pi-th vessel; Print the number of liters of water in the ki-th vessel. When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.", "input_spec": "The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as \"1 pi xi\", the query of the second type is represented as \"2 ki\" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).", "output_spec": "For each query, print on a single line the number of liters of water in the corresponding vessel.", "sample_inputs": ["2\n5 10\n6\n1 1 4\n2 1\n1 2 5\n1 1 4\n2 1\n2 2", "3\n5 10 8\n6\n1 1 12\n2 2\n1 1 6\n1 3 2\n2 2\n2 3"], "sample_outputs": ["4\n5\n8", "7\n10\n5"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.bigint;\nimport std.container;\nimport std.functional;\nimport std.math;\nimport std.complex;\nimport std.range;\nimport std.string;\n\nalias RedBlackTree!(int) IntSet;\n\nvoid main() {\n int n, m;\n int [] a, amt;\n while (readf(\" %d\", &n)) {\n a = new int[](n);\n amt = new int[](n);\n amt[0..n] = 0;\n IntSet S = new IntSet(-1, -2);\n foreach(i; 0..n) {\n readf(\" %d\", &a[i]);\n S.insert(i);\n }\n readf(\" %d\", &m);\n foreach(i ; 0..m) {\n int t;\n readf(\" %d\", &t);\n if (t == 1) {\n int p, x;\n readf(\" %d %d\", &p, &x);\n --p;\n int f = min(x, a[p] - amt[p]);\n x -= f;\n amt[p] += f;\n if (amt[p] == a[p]) S.removeKey(p);\n if (x > 0) {\n auto next = S.upperBound(p);\n while (!next.empty() && x > 0) {\n f = min(x, a[next.front()] - amt[next.front()]);\n x -= f;\n amt[next.front()] += f;\n if (amt[next.front()] == a[next.front()]) S.removeKey(next.front());\n next = S.upperBound(next.front());\n }\n }\n } else {\n int k;\n readf(\" %d\", &k);\n writeln(amt[--k]);\n }\n }\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [n];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %s\", &a[i]);\n\t\t}\n\t\tauto next = n.iota.map !(a => a + 1).array;\n\t\ta ~= 0;\n\t\tnext ~= n;\n\t\tauto b = new int [n + 1];\n\t\tb[] = 0;\n\t\tint m;\n\t\treadf (\" %s\", &m);\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\tint t;\n\t\t\treadf (\" %s\", &t);\n\t\t\tif (t == 1)\n\t\t\t{\n\t\t\t\tint p, x;\n\t\t\t\treadf (\" %s %s\", &p, &x);\n\t\t\t\tp--;\n\t\t\t\tb[p] += x;\n\t\t\t\twhile (p < n && b[p] > a[p])\n\t\t\t\t{\n\t\t\t\t\tb[next[p]] += b[p] - a[p];\n\t\t\t\t\tint q = next[p];\n\t\t\t\t\tif (b[next[p]] >= a[next[p]])\n\t\t\t\t\t{\n\t\t\t\t\t\tnext[p] = next[next[p]];\n\t\t\t\t\t}\n\t\t\t\t\tb[p] = a[p];\n\t\t\t\t\tp = q;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (t == 2)\n\t\t\t{\n\t\t\t\tint k;\n\t\t\t\treadf (\" %s\", &k);\n\t\t\t\tk--;\n\t\t\t\twriteln (b[k]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tenforce (false);\n\t\t\t}\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.bigint;\nimport std.container;\nimport std.functional;\nimport std.math;\nimport core.memory;\nimport std.complex;\nimport std.range;\nimport std.string;\n\nalias RedBlackTree!(int) IntSet;\n\nvoid main() {\n int n, m;\n int [] a, amt;\n while (readf(\" %d\", &n)) {\n a = new int[](n);\n amt = new int[](n);\n amt[0..n] = 0;\n IntSet S = new IntSet(-1, -2);\n foreach(i; 0..n) {\n readf(\" %d\", &a[i]);\n S.insert(i);\n }\n readf(\" %d\", &m);\n foreach(i ; 0..m) {\n int t;\n readf(\" %d\", &t);\n if (t == 1) {\n int p, x;\n readf(\" %d %d\", &p, &x);\n --p;\n int f = min(x, a[p] - amt[p]);\n x -= f;\n amt[p] += f;\n if (amt[p] == a[p]) S.removeKey(p);\n if (x > 0) {\n auto next = S.upperBound(p);\n foreach (j; next) {\n if (x == 0) break;\n f = min(x, a[j] - amt[j]);\n x -= f;\n amt[j] += f;\n if (amt[j] == a[j]) S.removeKey(j);\n }\n }\n } else {\n int k;\n readf(\" %d\", &k);\n --k;\n writeln(amt[k]);\n }\n }\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.bigint;\nimport std.container;\nimport std.functional;\nimport std.math;\nimport std.complex;\nimport std.range;\nimport std.string;\n\nalias RedBlackTree!(int) IntSet;\n\nvoid main() {\n int n, m;\n int [] a, amt;\n while (readf(\" %d\", &n)) {\n a = new int[](n);\n amt = new int[](n);\n IntSet S = new IntSet;\n S.insert(-1);\n S.insert(-2);\n foreach(i; 0..n) {\n readf(\" %d\", &a[i]);\n S.insert(i);\n }\n readf(\" %d\", &m);\n foreach(i ; 0..m) {\n int t;\n readf(\" %d\", &t);\n if (t == 1) {\n int p, x;\n readf(\" %d %d\", &p, &x);\n --p;\n auto next = S.upperBound(p);\n int f = min(x, a[p] - amt[p]);\n x -= f;\n amt[p] += f;\n if (amt[p] == a[p] && !S.empty()) S.removeKey(p);\n if (x > 0) {\n foreach (j; next) {\n if (x == 0) break;\n f = min(x, a[j] - amt[j]);\n x -= f;\n amt[j] += f;\n if (amt[j] == a[j]) S.removeKey(j);\n }\n }\n } else {\n int p;\n readf(\" %d\", &p);\n --p;\n writeln(amt[p]);\n }\n }\n }\n}"}], "src_uid": "37e2bb1c7caeeae7f8a7c837a2b390c9"} {"nl": {"description": "Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·ai + q·aj + r·ak for given p, q, r and array a1, a2, ... an such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.", "input_spec": "First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105). Next line of input contains n space separated integers a1, a2, ... an ( - 109 ≤ ai ≤ 109).", "output_spec": "Output a single integer the maximum value of p·ai + q·aj + r·ak that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.", "sample_inputs": ["5 1 2 3\n1 2 3 4 5", "5 1 2 -3\n-1 -2 -3 -4 -5"], "sample_outputs": ["30", "12"], "notes": "NoteIn the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.In second sample case, selecting i = j = 1 and k = 5 gives the answer 12."}, "positive_code": [{"source_code": "module main;\n__gshared:\nimport core.bitop, std.algorithm, std.bigint, std.bitmanip, std.complex,\n\tstd.container, std.conv, std.datetime, std.functional, std.math, std.meta;\nimport std.numeric : Fft, fft, gcd, inverseFft;\nimport std.parallelism, std.random, std.range, std.regex, std.stdio, std.string,\n\tstd.traits, std.typecons, std.uni, std.variant;\n\nalias pair(X, Y) = Tuple!(X, \"fi\", Y, \"se\");\nalias mp = make_pair;\nalias pii = pair!(int, int);\nalias pll = pair!(long, long);\nalias gcd = std.numeric.gcd;\nalias lint = long;\nalias rbt = RedBlackTree;\n///modulo\nenum mod = 10 ^^ 9 + 7, mod2 = mod + 2;\npublic\n{\n\tpure nothrow\n\t{\n\t\t///lcm\n\t\tT lcm(T)(auto ref T a, auto ref T b)\n\t\t{\n\t\t\treturn a / gcd(a, b) * b;\n\t\t}\n\t\t///make_pair\n\t\tpair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)\n\t\t{\n\t\t\treturn tuple!(X, \"fi\", Y, \"se\")(x_, y_);\n\t\t}\n\t\t///BigInt greatest common divizor\n\t\tBigInt gcd(BigInt a, BigInt b)\n\t\t{\n\t\t\twhile (b)\n\t\t\t{\n\t\t\t\ta %= b;\n\t\t\t\tswap(a, b);\n\t\t\t}\n\t\t\treturn a;\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tsize_t l, r = a.length;\n\t\t\twhile (r - l > 1)\n\t\t\t{\n\t\t\t\tauto m = (l + r) >> 1;\n\t\t\t\tif (!(a[m] < g))\n\t\t\t\t\tr = m;\n\t\t\t\telse\n\t\t\t\t\tl = m;\n\t\t\t}\n\t\t\treturn (!(a[l] < g)) ? l : r;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tsize_t l, r = a.length;\n\t\t\twhile (r - l > 1)\n\t\t\t{\n\t\t\t\tauto m = (l + r) >> 1;\n\t\t\t\tif (g < a[m])\n\t\t\t\t\tr = m;\n\t\t\t\telse\n\t\t\t\t\tl = m;\n\t\t\t}\n\t\t\treturn (g < a[l]) ? l : r;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn binf(a, g) != a.length;\n\t\t}\n\t}\n\t///write an array\n\tvoid putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)\n\t{\n\t\twritefln(\"%(%s\" ~ ch ~ \"%)\", a);\n\t}\n\t///write a matrix\n\tvoid putarr(X)(in X a)\n\t\t\tif (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))\n\t{\n\t\twritefln(\"%(%(%s %)\\n%)\", a);\n\t}\n\t///get an array\n\tbool getarr(X)(X a, in size_t n)\n\t{\n\t\tbool b = 1;\n\t\tforeach (ref i; a[0 .. n])\n\t\t\tb = input(&i);\n\t\treturn b;\n\t}\n\t///read without format\n\tbool input(T...)(ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\tsize_t s;\n\t\tforeach (ref e; ptrs)\n\t\t{\n\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\ts += readf(\" %c\", &e);\n\t\t\telse\n\t\t\t\ts += readf(\" %s\", &e);\n\t\t}\n\t\treturn s == ptrs.length;\n\t}\n\t///readln without format\n\tbool read(T...)(ref T ptrs) if (ptrs.length > 0)\n\t{\n\t\tsize_t s;\n\t\tforeach (ref e; ptrs)\n\t\t{\n\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\ts += readf(\" %c\", &e);\n\t\t\telse\n\t\t\t\ts += readf(\" %s\", &e);\n\t\t}\n\t\treadln;\n\t\treturn s == ptrs.length;\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tint n, p, q, r;\n\tloop: while (read(n, p, q, r))\n\t{\n\t\tauto a = arread!int;\n\t\tauto mapr = a.cumulativeFold!(max).array, masu = a.retro.cumulativeFold!(max).array;\n\t\tauto mipr = a.cumulativeFold!(min).array, misu = a.retro.cumulativeFold!(min).array;\n\t\treverse(misu);\n\t\treverse(masu);\n\t\tdebug\n\t\t{\n\t\t\twriteln(a);\n\t\t\twriteln(mipr);\n\t\t\twriteln(mapr);\n\t\t\twriteln(misu);\n\t\t\twriteln(masu);\n\t\t}\n\t\tlong ans = long.min;\n\t\tforeach (i; 0 .. n)\n\t\t{\n\t\t\tlong x = q * 1L * a[i];\n\t\t\tif (p < 0)\n\t\t\t\tx += mipr[i] * 1L * p;\n\t\t\telse\n\t\t\t\tx += mapr[i] * 1L * p;\n\t\t\tif (r < 0)\n\t\t\t\tx += misu[i] * 1L * r;\n\t\t\telse\n\t\t\t\tx += masu[i] * 1L * r;\n\t\t\tans = max(ans, x);\n\t\t}\n\t\twriteln(ans);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const P = readLong();\n const Q = readLong();\n const R = readLong();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n \n auto ls = new long[N + 1];\n auto rs = new long[N + 1];\n ls[0] = long.min;\n foreach (i; 0 .. N) {\n ls[i + 1] = max(ls[i], P * A[i]);\n }\n rs[N] = long.min;\n foreach_reverse (i; 0 .. N) {\n rs[i] = max(R * A[i], rs[i + 1]);\n }\n \n long ans = long.min;\n foreach (i; 0 .. N) {\n chmax(ans, ls[i + 1] + Q * A[i] + rs[i]);\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "a8e56ad4de6f0eecbe5521226c0335ab"} {"nl": {"description": "In an ICPC contest, balloons are distributed as follows: Whenever a team solves a problem, that team gets a balloon. The first team to solve a problem gets an additional balloon. A contest has 26 problems, labelled $$$\\textsf{A}$$$, $$$\\textsf{B}$$$, $$$\\textsf{C}$$$, ..., $$$\\textsf{Z}$$$. You are given the order of solved problems in the contest, denoted as a string $$$s$$$, where the $$$i$$$-th character indicates that the problem $$$s_i$$$ has been solved by some team. No team will solve the same problem twice.Determine the total number of balloons that the teams received. Note that some problems may be solved by none of the teams.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of testcases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of uppercase English letters, denoting the order of solved problems.", "output_spec": "For each test case, output a single integer — the total number of balloons that the teams received.", "sample_inputs": ["6\n\n3\n\nABA\n\n1\n\nA\n\n3\n\nORZ\n\n5\n\nBAAAA\n\n4\n\nBKPT\n\n10\n\nCODEFORCES"], "sample_outputs": ["5\n2\n6\n7\n8\n17"], "notes": "NoteIn the first test case, $$$5$$$ balloons are given out: Problem $$$\\textsf{A}$$$ is solved. That team receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\\textsf{A}$$$. Problem $$$\\textsf{B}$$$ is solved. That team receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\\textsf{B}$$$. Problem $$$\\textsf{A}$$$ is solved. That team receives only $$$1$$$ balloon, because they solved the problem. Note that they don't get an additional balloon because they are not the first team to solve problem $$$\\textsf{A}$$$. The total number of balloons given out is $$$2+2+1=5$$$.In the second test case, there is only one problem solved. The team who solved it receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\\textsf{A}$$$."}, "positive_code": [{"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n foreach(_; 0..t)\r\n {\r\n int n;\r\n scanf(\"%d\", &n);\r\n getchar();\r\n long res;\r\n auto s = readln.strip.to!(char[]);\r\n bool[char] solved;\r\n foreach(p; s) {\r\n auto tmp = (p in solved);\r\n if (tmp is null) {\r\n res += 2;\r\n solved[p] = true;\r\n }\r\n else {\r\n res += 1;\r\n }\r\n }\r\n writeln(res);\r\n }\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.utf;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto s = readln.strip;\r\n\t\tauto t = s.dup;\r\n\t\twriteln (s.length + t.byChar.sort.uniq.walkLength);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "66777b8719b1756bf4b6bf93feb2e439"} {"nl": {"description": "A big football championship will occur soon! $$$n$$$ teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game: the game may result in a tie, then both teams get $$$1$$$ point; one team might win in a game, then the winning team gets $$$3$$$ points and the losing team gets $$$0$$$ points. The score of a team is the number of points it gained during all games that it played.You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then the test cases follow. Each test case is described by one line containing one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of teams.", "output_spec": "For each test case, print $$$\\frac{n(n - 1)}{2}$$$ integers describing the results of the games in the following order: the first integer should correspond to the match between team $$$1$$$ and team $$$2$$$, the second — between team $$$1$$$ and team $$$3$$$, then $$$1$$$ and $$$4$$$, ..., $$$1$$$ and $$$n$$$, $$$2$$$ and $$$3$$$, $$$2$$$ and $$$4$$$, ..., $$$2$$$ and $$$n$$$, and so on, until the game between the team $$$n - 1$$$ and the team $$$n$$$. The integer corresponding to the game between the team $$$x$$$ and the team $$$y$$$ should be $$$1$$$ if $$$x$$$ wins, $$$-1$$$ if $$$y$$$ wins, or $$$0$$$ if the game results in a tie. All teams should get the same score, and the number of ties should be the minimum possible. If there are multiple optimal answers, print any of them. It can be shown that there always exists a way to make all teams have the same score.", "sample_inputs": ["2\n2\n3"], "sample_outputs": ["0 \n1 -1 1"], "notes": "NoteIn the first test case of the example, both teams get $$$1$$$ point since the game between them is a tie.In the second test case of the example, team $$$1$$$ defeats team $$$2$$$ (team $$$1$$$ gets $$$3$$$ points), team $$$1$$$ loses to team $$$3$$$ (team $$$3$$$ gets $$$3$$$ points), and team $$$2$$$ wins against team $$$3$$$ (team $$$2$$$ gets $$$3$$$ points)."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[][](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\t\r\n\t\tif (n % 2)\r\n\t\t{\r\n\t\t\tforeach (i; 0..n*(n-1)/2)\r\n\t\t\t{\r\n\t\t\t\tif (i % 2 == 0)\r\n\t\t\t\t\tans[ti] ~= 1;\r\n\t\t\t\telse\r\n\t\t\t\t\tans[ti] ~= -1;\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tauto draw = n-1;\r\n\t\t\tforeach (i; 0..n)\r\n\t\t\t{\r\n\t\t\t\tforeach (j; i+1..n)\r\n\t\t\t\t{\r\n\t\t\t\t\tauto x = i + j;\r\n\t\t\t\t\tif (j > draw)\r\n\t\t\t\t\t\t++x;\r\n\t\t\t\t\tif (j == draw)\r\n\t\t\t\t\t\tans[ti] ~= 0;\r\n\t\t\t\t\telse if (x % 2)\r\n\t\t\t\t\t\tans[ti] ~= 1;\r\n\t\t\t\t\telse\r\n\t\t\t\t\t\tans[ti] ~= -1;\r\n\r\n\t\t\t\t}\r\n\t\t\t\t--draw;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\te.map!(to!string).join(\" \").writeln;\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tint k = (n + 1) / 2;\r\n\t\tint [] ans;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tforeach (j; i + 1..n)\r\n\t\t\t{\r\n\t\t\t\tint d = j - i;\r\n\t\t\t\tans ~= (d < k ? 1 : n - d < k ? -1 : 0);\r\n\t\t\t}\r\n\t\t}\r\n\t\twritefln !(\"%(%s %)\") (ans);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "a89c585ebd9608141399c813385c04c6"} {"nl": {"description": "It is the hard version of the problem. The only difference is that in this version $$$1 \\le n \\le 300$$$.In the cinema seats can be represented as the table with $$$n$$$ rows and $$$m$$$ columns. The rows are numbered with integers from $$$1$$$ to $$$n$$$. The seats in each row are numbered with consecutive integers from left to right: in the $$$k$$$-th row from $$$m (k - 1) + 1$$$ to $$$m k$$$ for all rows $$$1 \\le k \\le n$$$. $$$1$$$$$$2$$$$$$\\cdots$$$$$$m - 1$$$$$$m$$$$$$m + 1$$$$$$m + 2$$$$$$\\cdots$$$$$$2 m - 1$$$$$$2 m$$$$$$2m + 1$$$$$$2m + 2$$$$$$\\cdots$$$$$$3 m - 1$$$$$$3 m$$$$$$\\vdots$$$$$$\\vdots$$$$$$\\ddots$$$$$$\\vdots$$$$$$\\vdots$$$$$$m (n - 1) + 1$$$$$$m (n - 1) + 2$$$$$$\\cdots$$$$$$n m - 1$$$$$$n m$$$ The table with seats indices There are $$$nm$$$ people who want to go to the cinema to watch a new film. They are numbered with integers from $$$1$$$ to $$$nm$$$. You should give exactly one seat to each person.It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $$$i$$$-th person has the level of sight $$$a_i$$$. Let's define $$$s_i$$$ as the seat index, that will be given to $$$i$$$-th person. You want to give better places for people with lower sight levels, so for any two people $$$i$$$, $$$j$$$ such that $$$a_i < a_j$$$ it should be satisfied that $$$s_i < s_j$$$.After you will give seats to all people they will start coming to their seats. In the order from $$$1$$$ to $$$nm$$$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through.Let's consider an example: $$$m = 5$$$, the person has the seat $$$4$$$ in the first row, the seats $$$1$$$, $$$3$$$, $$$5$$$ in the first row are already occupied, the seats $$$2$$$ and $$$4$$$ are free. The inconvenience of this person will be $$$2$$$, because he will go through occupied seats $$$1$$$ and $$$3$$$.Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied).", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 300$$$) — the number of rows and places in each row respectively. The second line of each test case contains $$$n \\cdot m$$$ integers $$$a_1, a_2, \\ldots, a_{n \\cdot m}$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the sight level of $$$i$$$-th person. It's guaranteed that the sum of $$$n \\cdot m$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print a single integer — the minimal total inconvenience that can be achieved.", "sample_inputs": ["7\n1 2\n1 2\n3 2\n1 1 2 2 3 3\n3 3\n3 4 4 1 1 1 1 1 2\n2 2\n1 1 2 1\n4 2\n50 50 50 50 3 50 50 50\n4 2\n6 6 6 6 2 2 9 6\n2 9\n1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3"], "sample_outputs": ["1\n0\n4\n0\n0\n0\n1"], "notes": "NoteIn the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $$$1$$$.In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto b = iota (n * m).map !(i => tuple (a[i], i)).array;\r\n\t\tsort (b);\r\n\t\tforeach (row; 0..n)\r\n\t\t{\r\n\t\t\tb[row * m..row * m + m]\r\n\t\t\t .schwartzSort !(c => tuple (c[0], -c[1]));\r\n\t\t}\r\n//\t\tb.schwartzSort !(c => tuple (c[0], c[1]));\r\n\t\tdebug {writeln (b);}\r\n\r\n\t\tlong res = 0;\r\n\t\tforeach (row; 0..n)\r\n\t\t{\r\n\t\t\tauto d = b[row * m..row * m + m];\r\n\t\t\tforeach (i; 0..m)\r\n\t\t\t{\r\n\t\t\t\tforeach (j; i + 1..m)\r\n\t\t\t\t{\r\n\t\t\t\t\tres += (d[i][1] < d[j][1]);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "immutable multi = true;\n\nvoid solve(int)\n{\n\tauto n = readInt!int;\n\tauto m = readInt!int;\n\tauto a = ma(n*m, readInt!int);\n\tint[2][][int] poss;\n\tint[int] cnt;\n\tforeach(i, ai; a) cnt[ai]++;\n\tauto sa = a.dup; sort(sa);\n\tauto ua = sa.uniq.array;\n\tforeach(i; 0 .. n)\n\t{\n\t\tforeach(j; 0 .. m)\n\t\t{\n\t\t\tassert(cnt[ua[0]] > 0);\n\t\t\trequire(poss, ua[0], null) ~= [i, -j];\n\t\t\tcnt[ua[0]]--;\n\t\t\tif (cnt[ua[0]] == 0) ua = ua[1 .. $];\n\t\t}\n\t}\n\tauto used = new FenwickTree[](n);\n\tforeach(ref usedi; used)\n\t\tusedi = FenwickTree(m);\n\tlong ans = 0;\n\n\tforeach(ai, ref pos; poss)\n\t\tsort(pos);\n\tdebug writeln(poss);\n\tforeach(i, ai; a)\n\t{\n\t\tauto pos = poss[ai][0];\n\t\tposs[ai] = poss[ai][1 .. $];\n\t\tauto pi = pos[0];\n\t\tauto pj = -pos[1];\n\t\tans += used[pi].sum(pj-1);\n\t\tused[pi].add(pj, 1);\n\t}\n\tans.writeln;\n}\nstruct FenwickTree {\n\tint[] bit;\n\tint n;\n\n\tthis(int n) {\n\t\tthis.n = n;\n\t\tbit = new int[](n);\n\t}\n\n\tint sum(int r) {\n\t\tint ret = 0;\n\t\tfor (; r >= 0; r = (r & (r + 1)) - 1)\n\t\t\tret += bit[r];\n\t\treturn ret;\n\t}\n\n\tint sum(int l, int r) {\n\t\treturn sum(r) - sum(l - 1);\n\t}\n\n\tvoid add(int idx, int delta) {\n\t\tfor (; idx < n; idx = idx | (idx + 1))\n\t\t\tbit[idx] += delta;\n\t}\n}\n//{{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1;\n\tforeach(tc; 0 .. t) solve(tc);\n}\n//}}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t{\n\t\tpopChar;\n\t}\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}], "negative_code": [], "src_uid": "99c5e62d8e51e61cfd0c2531a231e7a8"} {"nl": {"description": "You are given $$$n$$$ dominoes. Each domino has a left and a right cell. Each cell can be colored either black or white. Some cells are already colored, while some aren't yet.The coloring is said to be valid if and only if it is possible to rearrange the dominoes in some order such that for each $$$1 \\le i \\le n$$$ the color of the right cell of the $$$i$$$-th domino is different from the color of the left cell of the $$$((i \\bmod n)+1)$$$-st domino. Note that you can't rotate the dominoes, so the left cell always remains the left cell, and the right cell always remains the right cell.Count the number of valid ways to color the yet uncolored cells of dominoes. Two ways are considered different if there is a cell that is colored white in one way and black in the other. In particular, colorings BW WB and WB BW different (and both invalid).As this number can be very big, output it modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of dominoes. The next $$$n$$$ lines describe dominoes. Each line contains two characters which represent the left and the right cell. Character B means that the corresponding cell is black, character W means that the corresponding cell is white, and ? means that the cell is yet to be colored. ", "output_spec": "Print a single integer — the answer to the problem.", "sample_inputs": ["1\n?W", "2\n??\nW?", "4\nBB\n??\nW?\n??"], "sample_outputs": ["1", "2", "10"], "notes": "NoteIn the first test case, there is only one domino, and we need the color of its right cell to be different from the color of its left cell. There is only one way to achieve this.In the second test case, there are only $$$2$$$ such colorings:BB WW and WB WB."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable long limit = 10 ^^ 5 + 3;\r\nimmutable long mod = 998_244_353;\r\n\r\nlong powMod (long a, long p)\r\n{\r\n\tlong res = 1;\r\n\tfor ( ; p > 0; p >>= 1)\r\n\t{\r\n\t\tif (p & 1)\r\n\t\t{\r\n\t\t\tres = (res * 1L * a) % mod;\r\n\t\t}\r\n\t\ta = (a * 1L * a) % mod;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nlong invMod (long a)\r\n{\r\n\treturn powMod (a, mod - 2);\r\n}\r\n\r\nlong [] f;\r\nlong [] fi;\r\n\r\nlong choose (int n, int k)\r\n{\r\n\tif (k < 0 || k > n)\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\tlong res = f[n];\r\n\tres = (res * 1L * fi[k]) % mod;\r\n\tres = (res * 1L * fi[n - k]) % mod;\r\n\treturn res;\r\n}\r\n\r\nvoid prepare ()\r\n{\r\n\tlong cur = 1;\r\n\tf = [];\r\n\tfi = [];\r\n\tforeach (i; 0..limit)\r\n\t{\r\n\t\tcur = (cur * 1L * (i + !i)) % mod;\r\n\t\tf ~= cur;\r\n\t\tfi ~= invMod (cur);\r\n\t}\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tprepare ();\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tint [3] [3] c;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tauto s = readln.strip;\r\n\t\t\tauto x = \"BW?\".countUntil (s.front).to !(int);\r\n\t\t\tauto y = \"BW?\".countUntil (s.back).to !(int);\r\n\t\t\tc[x][y] += 1;\r\n\t\t}\r\n\r\n\t\tauto q0 = c[2][0] + c[2][1] + c[2][2];\r\n\t\tauto q1 = c[0][2] + c[1][2] + c[2][2];\r\n\t\tauto b0 = c[0][0] + c[0][1] + c[0][2];\r\n\t\tauto w1 = c[0][1] + c[1][1] + c[2][1];\r\n\t\tlong res = 0;\r\n\t\tforeach (k; 0..n + 1)\r\n\t\t{ // k \"B*\" and k \"*W\"\r\n\t\t\tint x = k - b0;\r\n\t\t\tint y = k - w1;\r\n\t\t\tlong u = choose (q0, x);\r\n\t\t\tlong v = choose (q1, y);\r\n\t\t\tlong cur = (u * 1L * v) % mod;\r\n//\t\t\twriteln (k, \": \", x, \" \", y, \" \", u, \" \", v, \" \", cur);\r\n\t\t\tres = (res + cur) % mod;\r\n\t\t}\r\n\r\n\t\tlong bad = 0;\r\n\t\tforeach (k; 1..n)\r\n\t\t{ // k \"BW\" and (n-k) \"WB\"\r\n\t\t\tif (c[0][0] > 0 || c[1][1] > 0)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tint x = k - c[0][2] - c[0][1];\r\n\t\t\tint y = (n - k) - c[1][2] - c[1][0];\r\n\t\t\tif (x < 0 || y < 0)\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\t\t\tlong cur = choose (c[2][2], x);\r\n\t\t\tbad = (bad + cur) % mod;\r\n\t\t}\r\n\r\n\t\twriteln ((res + mod - bad) % mod);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int limit = 10 ^^ 5 + 3;\r\nimmutable int mod = 998_244_353;\r\n\r\nint powMod (int a, int p)\r\n{\r\n\tint res = 1;\r\n\tfor ( ; p > 0; p >>= 1)\r\n\t{\r\n\t\tif (p & 1)\r\n\t\t{\r\n\t\t\tres = (res * 1L * a) % mod;\r\n\t\t}\r\n\t\ta = (a * 1L * a) % mod;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nint invMod (int a)\r\n{\r\n\treturn powMod (a, mod - 2);\r\n}\r\n\r\nint [] f;\r\nint [] fi;\r\n\r\nint choose (int n, int k)\r\n{\r\n\tif (k < 0 || k > n)\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\tint res = f[n];\r\n\tres = (res * 1L * fi[k]) % mod;\r\n\tres = (res * 1L * fi[n - k]) % mod;\r\n\treturn res;\r\n}\r\n\r\nvoid prepare ()\r\n{\r\n\tint cur = 1;\r\n\tf = [];\r\n\tfi = [];\r\n\tforeach (i; 0..limit)\r\n\t{\r\n\t\tcur = (cur * 1L * (i + !i)) % mod;\r\n\t\tf ~= cur;\r\n\t\tfi ~= invMod (cur);\r\n\t}\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tprepare ();\r\n\tint n;\r\n\twhile (readf !(\" %s\") (n) > 0)\r\n\t{\r\n\t\treadln;\r\n\t\tint [3] [3] c;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tauto s = readln.strip;\r\n\t\t\tauto x = \"BW?\".countUntil (s.front).to !(int);\r\n\t\t\tauto y = \"BW?\".countUntil (s.back).to !(int);\r\n\t\t\tc[x][y] += 1;\r\n\t\t}\r\n\r\n\t\tauto q0 = c[2][0] + c[2][1] + c[2][2];\r\n\t\tauto q1 = c[0][2] + c[1][2] + c[2][2];\r\n\t\tauto b0 = c[0][0] + c[0][1] + c[0][2];\r\n\t\tauto w1 = c[0][1] + c[1][1] + c[2][1];\r\n\t\tint res = 0;\r\n\t\tforeach (k; 0..n + 1)\r\n\t\t{ // k \"B*\" and k \"*W\"\r\n\t\t\tint x = k - b0;\r\n\t\t\tint y = k - w1;\r\n\t\t\tint u = choose (q0, x);\r\n\t\t\tint v = choose (q1, y);\r\n\t\t\tint cur = (u * 1L * v) % mod;\r\n//\t\t\twriteln (k, \": \", x, \" \", y, \" \", u, \" \", v, \" \", cur);\r\n\t\t\tres = (res + cur) % mod;\r\n\t\t}\r\n\r\n\t\tint bad = 0;\r\n\t\tforeach (k; 1..n)\r\n\t\t{ // k \"BW\" and (n-k) \"WB\"\r\n\t\t\tif (c[0][0] > 0 || c[1][1] > 0)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tint x = k - c[0][2] - c[0][1];\r\n\t\t\tint y = (n - k) - c[1][2] - c[1][0];\r\n\t\t\tif (x < 0 || y < 0)\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\t\t\tint cur = choose (c[2][2], x);\r\n\t\t\tbad = (bad + cur) % mod;\r\n\t\t}\r\n\r\n\t\twriteln ((res + mod - bad) % mod);\r\n\t}\r\n}\r\n"}], "src_uid": "3858151e51f6c97abe8904628b70ad7f"} {"nl": {"description": "And again a misfortune fell on Poor Student. He is being late for an exam.Having rushed to a bus stop that is in point (0, 0), he got on a minibus and they drove along a straight line, parallel to axis OX, in the direction of increasing x.Poor Student knows the following: during one run the minibus makes n stops, the i-th stop is in point (xi, 0) coordinates of all the stops are different the minibus drives at a constant speed, equal to vb it can be assumed the passengers get on and off the minibus at a bus stop momentarily Student can get off the minibus only at a bus stop Student will have to get off the minibus at a terminal stop, if he does not get off earlier the University, where the exam will be held, is in point (xu, yu) Student can run from a bus stop to the University at a constant speed vs as long as needed a distance between two points can be calculated according to the following formula: Student is already on the minibus, so, he cannot get off at the first bus stop Poor Student wants to get to the University as soon as possible. Help him to choose the bus stop, where he should get off. If such bus stops are multiple, choose the bus stop closest to the University.", "input_spec": "The first line contains three integer numbers: 2 ≤ n ≤ 100, 1 ≤ vb, vs ≤ 1000. The second line contains n non-negative integers in ascending order: coordinates xi of the bus stop with index i. It is guaranteed that x1 equals to zero, and xn ≤ 105. The third line contains the coordinates of the University, integers xu and yu, not exceeding 105 in absolute value. ", "output_spec": "In the only line output the answer to the problem — index of the optimum bus stop.", "sample_inputs": ["4 5 2\n0 2 4 6\n4 1", "2 1 1\n0 100000\n100000 100000"], "sample_outputs": ["3", "2"], "notes": "NoteAs you know, students are a special sort of people, and minibuses usually do not hurry. That's why you should not be surprised, if Student's speed is higher than the speed of the minibus."}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string read_string() {\n while (tokens.empty) {\n tokens = readln.split;\n }\n auto token = tokens.front;\n tokens.popFront;\n return token;\n }\n int read_int() {\n return read_string.to!int;\n }\n\n double read_double() {\n return read_string.to!double;\n }\n \n string[] tokens;\n}\n\ndouble distance(int x1, int y1, int x2, int y2) {\n double dist = sqrt((x2.to!double - x1.to!double) ^^ 2 + (y1.to!double - y2.to!double) ^^ 2);\n return dist;\n}\n\nvoid main() {\n IO cin;\n int t = 1;\n // t = cin.read_int;\n while (t--) {\n int n = cin.read_int;\n int vb = cin.read_int;\n int vs = cin.read_int;\n\n int[] arr = new int[n];\n double[] time_s = new double[n];\n\n time_s[0] = 999999999;\n foreach (ref i; arr) i = cin.read_int;\n \n int ux = cin.read_int;\n int uy = cin.read_int;\n \n for (int i = 1; i < n; i++) {\n // writeln(distance(arr[i], 0, ux, uy));\n time_s[i] = (distance(arr[i], 0, ux, uy)) / vs;\n // writeln(time_s[i]);\n }\n double min_total_time = 999999999;\n for (int i = 1; i < n; i++) {\n double total_time = (arr[i] / vb.to!double) + time_s[i];\n // writeln(total_time);\n min_total_time = min(min_total_time, total_time);\n }\n // writeln(min_total_time);\n int ans = 0;\n double min_time = 999999999.0;\n for (int i = 1; i < n; i++) {\n if (abs((arr[i] / vb.to!double) + time_s[i] - min_total_time) < 0.00001) {\n if (time_s[i] < min_time) {\n min_time = time_s[i];\n ans = i;\n }\n }\n }\n writeln(ans + 1); \n } \n}"}], "negative_code": [], "src_uid": "15fa49860e978d3b3fb7a20bf9f8aa86"} {"nl": {"description": "It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices.It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?", "input_spec": "The first line of input will contain integers N and S (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively. N lines follow. The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.", "output_spec": "Print the maximum total happiness that can be achieved.", "sample_inputs": ["3 12\n3 5 7\n4 6 7\n5 9 5", "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6"], "sample_outputs": ["84", "314"], "notes": "NoteIn the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74."}, "positive_code": [{"source_code": "/+ dub.sdl:\n name \"B\"\n dependency \"dunkelheit\" version=\">=0.9.0\"\n+/\n\nimport std.stdio, std.algorithm, std.range, std.conv;\n// import dkh.foundation, dkh.scanner;\n\nint main() {\n Scanner sc = new Scanner(stdin);\n scope(exit) assert(!sc.hasNext);\n\n int n; long S;\n sc.read(n, S);\n\n long[3][] av, bv;\n long ma = 0;\n long ssm, sasm, sbsm;\n foreach (i; 0..n) {\n long s, a, b;\n sc.read(s, a, b);\n ssm += s;\n ma += s * max(a, b);\n if (a >= b) {\n sasm += s;\n av ~= [s, a, b];\n } else {\n sbsm += s;\n bv ~= [s, a, b];\n }\n }\n\n if (sasm % S + sbsm % S > S) {\n writeln(ma);\n return 0;\n }\n\n sasm %= S; sbsm %= S;\n av.sort!\"a[1]-a[2]= 1) {\n import core.exception : RangeError;\n\n private T* _data;\n private uint len, cap;\n\n @property bool empty() const { return len == 0; }\n @property size_t length() const { return len; }\n alias opDollar = length;\n\n \n inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }\n \n ref inout(T) opIndex(size_t i) inout {\n version(assert) if (len <= i) throw new RangeError();\n return _data[i];\n } \n ref inout(T) front() inout { return this[0]; } \n ref inout(T) back() inout { return this[$-1]; } \n\n void reserve(size_t newCap) {\n import core.memory : GC;\n import core.stdc.string : memcpy;\n import std.conv : to;\n if (newCap <= cap) return;\n void* newData = GC.malloc(newCap * T.sizeof);\n cap = newCap.to!uint;\n if (len) memcpy(newData, _data, len * T.sizeof);\n _data = cast(T*)(newData);\n } \n void free() {\n import core.memory : GC;\n GC.free(_data);\n } \n \n void clear() {\n len = 0;\n }\n\n void insertBack(T item) {\n import std.algorithm : max;\n if (len == cap) reserve(max(cap * 2, MINCAP));\n _data[len++] = item;\n } \n alias opOpAssign(string op : \"~\") = insertBack; \n void removeBack() {\n assert(!empty, \"StackPayload.removeBack: Stack is empty\");\n len--;\n } \n}\n\n \n/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */\n \n// module dkh.foundation;\n\n \nstatic if (__VERSION__ <= 2070) {\n /*\n Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d\n Copyright: Andrei Alexandrescu 2008-.\n License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).\n */\n template fold(fun...) if (fun.length >= 1) {\n auto fold(R, S...)(R r, S seed) {\n import std.algorithm : reduce;\n static if (S.length < 2) {\n return reduce!fun(seed, r);\n } else {\n import std.typecons : tuple;\n return reduce!fun(tuple(seed), r);\n }\n }\n }\n \n}\n/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */\n// module dkh.scanner;\n\n// import dkh.container.stackpayload;\n\n \nclass Scanner {\n import std.stdio : File;\n import std.conv : to;\n import std.range : front, popFront, array, ElementType;\n import std.array : split;\n import std.traits : isSomeChar, isStaticArray, isArray; \n import std.algorithm : map;\n File f;\n this(File f) {\n this.f = f;\n }\n char[512] lineBuf;\n char[] line;\n private bool succW() {\n import std.range.primitives : empty, front, popFront;\n import std.ascii : isWhite;\n while (!line.empty && line.front.isWhite) {\n line.popFront;\n }\n return !line.empty;\n }\n private bool succ() {\n import std.range.primitives : empty, front, popFront;\n import std.ascii : isWhite;\n while (true) {\n while (!line.empty && line.front.isWhite) {\n line.popFront;\n }\n if (!line.empty) break;\n line = lineBuf[];\n f.readln(line);\n if (!line.length) return false;\n }\n return true;\n }\n\n private bool readSingle(T)(ref T x) {\n import std.algorithm : findSplitBefore;\n import std.string : strip;\n import std.conv : parse;\n if (!succ()) return false;\n static if (isArray!T) {\n alias E = ElementType!T;\n static if (isSomeChar!E) {\n \n \n auto r = line.findSplitBefore(\" \");\n x = r[0].strip.dup;\n line = r[1];\n } else static if (isStaticArray!T) {\n foreach (i; 0..T.length) {\n bool f = succW();\n assert(f);\n x[i] = line.parse!E;\n }\n } else {\n StackPayload!E buf;\n while (succW()) {\n buf ~= line.parse!E;\n }\n x = buf.data;\n }\n } else {\n x = line.parse!T;\n }\n return true;\n }\n\n int unsafeRead(T, Args...)(ref T x, auto ref Args args) {\n if (!readSingle(x)) return 0;\n static if (args.length == 0) {\n return 1;\n } else {\n return 1 + read(args);\n }\n }\n void read(Args...)(auto ref Args args) {\n import std.exception;\n static if (args.length != 0) {\n enforce(readSingle(args[0]));\n read(args[1..$]);\n }\n }\n bool hasNext() {\n return succ();\n }\n}\n\n\n \n \n\n \n\n/*\nThis source code generated by dunkelheit and include dunkelheit's source code.\ndunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)\ndunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)\n*/\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const R = readLong();\n auto S = new long[N];\n auto A = new long[N];\n auto B = new long[N];\n foreach (i; 0 .. N) {\n S[i] = readLong();\n A[i] = readLong();\n B[i] = readLong();\n }\n \n long sumS, sumS0, sumS1;\n long ansBase;\n alias Entry = Tuple!(long, \"d\", long, \"s\");\n Entry[] es0, es1;\n foreach (i; 0 .. N) {\n sumS += S[i];\n if (A[i] >= B[i]) {\n sumS0 += S[i];\n ansBase += S[i] * A[i];\n es0 ~= Entry(A[i] - B[i], S[i]);\n } else {\n sumS1 += S[i];\n ansBase += S[i] * B[i];\n es1 ~= Entry(B[i] - A[i], S[i]);\n }\n }\n es0.sort;\n es1.sort;\n \n const k = (sumS + R - 1) / R;\n long ans;\n foreach (l; sumS0 / R - 2 .. sumS0 / R + 2 + 1) {\n if (0 <= l && l <= k) {\n long score = ansBase;\n {\n long lot = max(sumS0 - R * l, 0);\n foreach (ref e; es0) {\n const tmp = min(e.s, lot);\n score -= e.d * tmp;\n lot -= tmp;\n }\n }\n {\n long lot = max(sumS1 - R * (k - l), 0);\n foreach (ref e; es1) {\n const tmp = min(e.s, lot);\n score -= e.d * tmp;\n lot -= tmp;\n }\n }\n debug {\n writeln(l, \" \", k - l, \": \", score);\n }\n chmax(ans, score);\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "769859d86a3ceb2d89a444cd64c9a73b"} {"nl": {"description": "Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment $$$k$$$ times, where $$$k$$$ is the index of the corresponding letter in the alphabet. For example, if the question is about the substring \"abbcb\", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c\" three times, so that the resulting string is \"abbbbcccbb\", its length is $$$10$$$. Vasya is interested about the length of the resulting string.Help Petya find the length of each string obtained by Vasya.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\\leq n\\leq 100\\,000$$$, $$$1\\leq q \\leq 100\\,000$$$) — the length of the song and the number of questions. The second line contains one string $$$s$$$ — the song, consisting of $$$n$$$ lowercase letters of English letters. Vasya's questions are contained in the next $$$q$$$ lines. Each line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$) — the bounds of the question.", "output_spec": "Print $$$q$$$ lines: for each question print the length of the string obtained by Vasya.", "sample_inputs": ["7 3\nabacaba\n1 3\n2 5\n1 7", "7 4\nabbabaa\n1 3\n5 7\n6 6\n2 4", "13 7\nsonoshikumiwo\n1 5\n2 10\n7 7\n1 13\n4 8\n2 5\n3 9"], "sample_outputs": ["4\n7\n11", "5\n4\n1\n5", "82\n125\n9\n191\n62\n63\n97"], "notes": "NoteIn the first example Vasya is interested in three questions. In the first question Vasya considers the substring \"aba\", that transforms to \"abba\", so the answer is equal to $$$4$$$. In the second question Vasya considers \"baca\", that transforms to \"bbaccca\", so the answer is $$$7$$$. In the third question Vasya considers the string \"abacaba\",that transforms to \"abbacccabba\" of length $$$11$$$."}, "positive_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1539/problem/B\n//\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int n, q;\n readf(\"%s %s\\n\", &n, &q);\n string s = readln.strip;\n\n int[][] index = new int[][](26, n + 1);\n foreach(ch; 0 .. 26) {\n int count = 0;\n foreach(i; 0 .. n) {\n if(s[i] == 'a' + ch) {\n count += 1;\n }\n index[ch][i + 1] = count;\n }\n }\n\n for(int i = 0; i < q; ++i) {\n int l, r;\n readf(\"%s %s\\n\", &l, &r);\n int ans = 0;\n foreach(ch; 0 .. 26) {\n int letter = index[ch][r] - index[ch][l - 1];\n ans += letter * (ch + 1);\n }\n ans.writeln;\n }\n}\n"}, {"source_code": "import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,\n std.container, std.typecons, std.conv, std.random, std.bigint;\n\nvoid read(S...)(ref S args) {\n auto input = readln.split;\n enforce(input.length == args.length);\n foreach (i, ref arg; args) {\n arg = input[i].to!(S[i]);\n }\n}\n\nauto list(T)() { return readln.split.map!(e => e.to!T); }\n\nvoid main() {\n int n, q;\n read(n, q);\n \n string s = readln;\n \n auto table = new int[][](26, n + 1);\n \n foreach (c; 0 .. 26) {\n int t = 0;\n foreach (i; 0 .. n) {\n if (s[i] == 'a' + c) t++;\n table[c][i + 1] = t;\n }\n }\n \n foreach (i; 0 .. q) {\n int l, r;\n read(l, r);\n \n long total = 0;\n \n foreach (c; 0 .. 26) {\n auto rep = table[c][r] - table[c][l - 1];\n total += rep * (c + 1);\n }\n \n writeln(total);\n }\n}\n\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.array, std.random;\n\nvoid main()\n{\n int n, q;\n readf!\" %d %d \"(n, q);\n string s = readln.strip;\n ulong[] a;\n ulong sum = 0;\n a ~= 0;\n foreach (ch ; s) {\n sum += ch - 'a' + 1;\n a ~= sum;\n }\n while (q--) {\n int l, r;\n readf!\" %d %d \"(l, r);\n writeln(a[r] - a[l - 1]);\n }\n}\n"}], "negative_code": [], "src_uid": "461378e9179c9de454674ea9dc49c56c"} {"nl": {"description": "You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n1 washing machines, n2 drying machines and n3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine.It takes t1 minutes to wash one piece of laundry in a washing machine, t2 minutes to dry it in a drying machine, and t3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have.", "input_spec": "The only line of the input contains seven integers: k, n1, n2, n3, t1, t2, t3 (1 ≤ k ≤ 104; 1 ≤ n1, n2, n3, t1, t2, t3 ≤ 1000).", "output_spec": "Print one integer — smallest number of minutes to do all your laundry.", "sample_inputs": ["1 1 1 1 5 5 5", "8 4 3 2 10 5 2"], "sample_outputs": ["15", "32"], "notes": "NoteIn the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint k;\n\tint [3] n;\n\tint [3] t;\n\twhile (readf (\" %s\", &k) > 0)\n\t{\n\t\treadf (\" %s %s %s\", &n[0], &n[1], &n[2]);\n\t\treadf (\" %s %s %s\", &t[0], &t[1], &t[2]);\n\n\t\tlong res = 0;\n\t\tint [] [3] s =\n\t\t [new int [n[0]], new int [n[1]], new int [n[2]]];\n\n\t\tforeach (i; 0..k)\n\t\t{\n\t\t\tint r0 = n[0] - minPos (s[0]).length;\n\t\t\tint r1 = n[1] - minPos (s[1]).length;\n\t\t\tint r2 = n[2] - minPos (s[2]).length;\n\n\t\t\tint v1 = max (s[1][r1], s[0][r0] + t[0]);\n\t\t\tint v2 = max (s[2][r2], v1 + t[1]);\n\t\t\tint v3 = v2 + t[2];\n\n\t\t\tint x2 = v3 - t[2];\n\t\t\tint x1 = x2 - t[1];\n\t\t\tint x0 = x1 - t[0];\n\n\t\t\tassert (x0 >= r0 && x1 >= r1 && x2 >= r2);\n\n\t\t\ts[0][r0] = x0 + t[0];\n\t\t\ts[1][r1] = x1 + t[1];\n\t\t\ts[2][r2] = x2 + t[2];\n\n\t\t\tres = max (res, s[2][r2]);\n\t\t}\n\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "dd1d166772ee06b383d4ceb94b530fd1"} {"nl": {"description": "A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not.A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0, a1, ..., an - 1 if and only if ai = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points.You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.", "output_spec": "Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.", "sample_inputs": ["5\n0 1 3 4 2"], "sample_outputs": ["3"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\n\nint fixed(int[] c) {\n int p = 0;\n for (int i = 0; i < c.length; i++) {\n if (i == c[i]) {\n p++;\n }\n }\n return p;\n}\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] c = stdin.readln.chomp.split.map!(to!int).array;\n int ans = fixed(c);\n if (ans == n) {\n writeln(ans);\n return;\n }\n int f = 1;\n for (int i = 0; i < n; i++) {\n int j = c[i];\n if (i != j && i == c[j]) {\n f = 2;\n }\n }\n writeln(ans + f);\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\n\nint fixed(int[] c) {\n int p = 0;\n for (int i = 0; i < c.length; i++) {\n if (i == c[i]) {\n p++;\n }\n }\n return p;\n}\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n int[] c = stdin.readln.chomp.split.map!(to!int).array;\n int ans = fixed(c);\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n auto c_ = c.dup;\n swap(c[i], c[j]);\n ans = max(ans, fixed(c_));\n }\n }\n ans.writeln;\n}\n"}], "src_uid": "e63de0fffd00b2da103545a7f1e405be"} {"nl": {"description": "This problem is same as the next one, but has smaller constraints.It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $$$(x_i, y_i)$$$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — the number of electric poles. Each of the following $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^4 \\le x_i, y_i \\le 10^4$$$) — the coordinates of the poles. It is guaranteed that all of these $$$n$$$ points are distinct.", "output_spec": "Print a single integer — the number of pairs of wires that are intersecting.", "sample_inputs": ["4\n0 0\n1 1\n0 3\n1 2", "4\n0 0\n0 2\n0 4\n2 0", "3\n-1 -1\n1 0\n3 1"], "sample_outputs": ["14", "6", "0"], "notes": "NoteIn the first example: In the second example: Note that the three poles $$$(0, 0)$$$, $$$(0, 2)$$$ and $$$(0, 4)$$$ are connected by a single wire.In the third example: "}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string;\nimport std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n\nint DEBUG_LEVEL = 0;\nvoid print()(){ writeln(\"\"); }\nvoid print(T, A ...)(T t, lazy A a){ write(t), print(a); }\nvoid print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }\n\nvoid main(string[] args){\n\tif(args.length > 1 && args[1] == \"-debug\"){\n\t\tif(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;\n\t\telse DEBUG_LEVEL = 1;\n\t}\n\t\n\tint n = read.to!int;\n\tlong[] xs, ys;\n\tforeach(i; 0 .. n) xs ~= read.to!int, ys ~= read.to!int;\n\t\n\tprint!1(\"xs:\", xs);\n\tprint!1(\"ys:\", ys);\n\t\n\tlong[long] kcnt;\n\tbool[long] mset;\n\tforeach(i; 0 .. n) foreach(j; 0 .. i){\n\t\tlong dx = xs[i] - xs[j];\n\t\tlong dy = ys[i] - ys[j];\n\t\tlong c = dx * ys[i] - dy * xs[i];\n\t\tprint!1(\"dx:\", dx, \" dy:\", dy, \" c:\", c);\n\t\t\n\t\tif(dx == 0){\n\t\t\tif(dy < 0) dy = -dy, c = -c;\n\t\t\tlong g = gcd(dy, c);\n\t\t\tdy /= g, c /= g;\n\t\t}\n\t\telse if(dy == 0){\n\t\t\tif(dx < 0) dx = -dx, c = -c;\n\t\t\tlong g = gcd(dx, c);\n\t\t\tdx /= g, c /= g;\n\t\t}\n\t\telse if(c == 0){\n\t\t\tif(dx < 0) dx = -dx, dy = -dy;\n\t\t\tlong g = gcd(dx, dy);\n\t\t\tdx /= g, dy /= g;\n\t\t}\n\t\telse{\n\t\t\tif(dx < 0) dx = -dx, dy = -dy, c = -c;\n\t\t\tlong g = gcd(gcd(dx, dy), c);\n\t\t\tdx /= g, dy /= g, c /= g;\n\t\t}\n\t\t\n\t\tprint!1(\" -> dx:\", dx, \" dy:\", dy, \" c:\", c);\n\t\t\n\t\tlong k = 100000 * dy + dx;\n\t\tlong m = k * 10000000 + c;\n\t\tif(m !in mset){\n\t\t\tif(k !in kcnt) kcnt[k] = 0;\n\t\t\tmset[m] = 1;\n\t\t\tkcnt[k] += 1;\n\t\t}\n\t}\n\tprint!1(\"kcnt:\", kcnt);\n\tprint!1(\"mset:\", mset);\n\t\n\tlong[] ks = kcnt.keys;\n\tlong[] ms = mset.keys;\n\tlong ans = ms.length * (ms.length - 1) / 2;\n\tforeach(k; ks) ans -= kcnt[k] * (kcnt[k] - 1) / 2;\n\tans.writeln;\n\t\t\n\t\n}\nlong gcd(long a, long b){\n\tif(b == 0) return a;\n\tif(b < 0) return gcd(a, -b);\n\tif(a % b == 0) return b;\n\tif(a < b) return gcd(b, a);\n\telse return gcd(b, a % b);\n}\n\n\n/*\n\n\"How many different direction vectors can we have?\"\n\n(x1, y1), (x2, y2) -> (x1 - x2, y1 - y2).\nhere x1 - x2 and y1 - y2 are relatively prime and x1 - x2 > 0,\nor (1, 0), or (0, 1).\n\nO(N^2) = 1000 x 1000.\n\n(y1 - y2)(x - x1) - (x1 - x2)(y - y1) = 0\ndy x - dx y + dx y1 - dy x1 = 0\n\n*/\n"}, {"source_code": "import std.typecons;\nimport std.stdio;\nT gcd(T)(T a, T b) {\n\tif (a % b == 0) return b;\n\treturn gcd(b, a % b);\n}\nstruct Rat(T) {\n\timport std.math : abs;\n\tT n, d;\n\tthis(T n_, T d_) {\n\t\tif (d_ == 0) {\n\t\t\tn = 1;\n\t\t\td = 0;\n\t\t} else if (n_ == 0) {\n\t\t\td = 1;\n\t\t\tn = 0;\n\t\t} else {\n\t\t\tauto c = gcd(abs(n_), abs(d_));\n\t\t\tbool neg = (n_ < 0) ^ (d_ < 0);\n\t\t\tn = abs(n_ / c);\n\t\t\td = abs(d_ / c);\n\t\t\tif (neg) {\n\t\t\t\tn = -n;\n\t\t\t}\n\t\t}\n\t}\n\tRat!T opBinary(string op)(T o) if (op == \"*\"){\n\t\tif (d == 0) {\n\t\t\treturn this;\n\t\t}\n\t\treturn Rat!T(n * o, d);\n\t}\n\tRat!T opBinary(string op)(T o) if (op == \"/\"){\n\t\tif (d == 0) {\n\t\t\treturn this;\n\t\t}\n\t\treturn Rat!T(n, d * o);\n\t}\n\tRat!T opBinary(string op)(T o) if (op == \"+\"){\n\t\tif (d == 0) {\n\t\t\treturn this;\n\t\t}\n\t\treturn Rat!T(n + d * o, d);\n\t}\n\tbool opEqual(Rat!T o) {\n\t\treturn n == o.n && d == o.d;\n\t}\n}\nint[2][50] points;\nvoid main() {\n\tbool[Tuple!(Rat!long, Rat!long)] lines;\n\tint n;\n\tscanf(\"%d \", &n);\n\n\tforeach(i; 0..n) {\n\t\tscanf(\"%d %d \", &points[i][0], &points[i][1]);\n\t}\n\n\tdebug writeln(points);\n\n\tforeach(i; 0..n) {\n\t\tforeach(j; 0..i) {\n\t\t\tauto t = Rat!long(points[i][1]-points[j][1], points[i][0]-points[j][0]);\n\t\t\tauto d = t.d ? (t * -points[i][0]) + points[i][1] : Rat!long(points[i][0], 1);\n\t\t\tdebug writeln(t, d, t == d);\n\t\t\tlines[tuple(t, d)] = true;\n\t\t}\n\t}\n\n\tdebug writeln(lines);\n\tint[Rat!long] tgroup;\n\tforeach(t, d; lines.byKey) {\n\t\ttgroup[t]++;\n\t}\n\tlong ans = lines.length * (lines.length - 1) / 2;\n\tforeach(t; tgroup.byKey) {\n\t\tans -= tgroup[t] * (tgroup[t]-1) / 2;\n\t}\n\twriteln(ans);\n}\n"}], "negative_code": [{"source_code": "void main() {\nimport std.stdio;\nwriteln(long.sizeof);\n}"}], "src_uid": "8c2e0cd780cf9390e933e28e57643cba"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$ and an array $$$b_1, b_2, \\dots, b_n$$$.For one operation you can sort in non-decreasing order any subarray $$$a[l \\dots r]$$$ of the array $$$a$$$.For example, if $$$a = [4, 2, 2, 1, 3, 1]$$$ and you choose subbarray $$$a[2 \\dots 5]$$$, then the array turns into $$$[4, 1, 2, 2, 3, 1]$$$. You are asked to determine whether it is possible to obtain the array $$$b$$$ by applying this operation any number of times (possibly zero) to the array $$$a$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 3 \\cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$). The second line of each query contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$). The third line of each query contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le n$$$). It is guaranteed that $$$\\sum n \\le 3 \\cdot 10^5$$$ over all queries in a test.", "output_spec": "For each query print YES (in any letter case) if it is possible to obtain an array $$$b$$$ and NO (in any letter case) otherwise.", "sample_inputs": ["4\n7\n1 7 1 4 4 5 6\n1 1 4 4 5 7 6\n5\n1 1 3 3 5\n1 1 3 3 5\n2\n1 1\n1 2\n3\n1 2 3\n3 2 1"], "sample_outputs": ["YES\nYES\nNO\nNO"], "notes": "NoteIn first test case the can sort subarray $$$a_1 \\dots a_5$$$, then $$$a$$$ will turn into $$$[1, 1, 4, 4, 7, 5, 6]$$$, and then sort subarray $$$a_5 \\dots a_6$$$."}, "positive_code": [{"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\nimport std.container.rbtree;\n \nclass SegmentTree(T = int, alias op=\"a+b\", T zero = T.init) {\n private:\n T [] t;\n size_t n;\n final void build () pure nothrow @nogc {\n foreach_reverse (i; 1 .. n) {\n immutable k = i << 1;\n t[i] = binaryFun!op (t[k], t[k+1]);\n }\n }\n public:\n final void update (size_t p, T v) pure nothrow @nogc {\n for (t[p += n] = v; p > 1; p >>= 1) {\n t[p>>1] = binaryFun!op (t[p], t[p ^ 1]);\n }\n }\n final T reduce (size_t l, size_t r) const pure nothrow @nogc {\n T res = zero;\n for (l += n, r += n; l < r; l >>= 1, r >>= 1) {\n if (l & 1) {\n res = binaryFun!op (res, t[l++]);\n }\n if (r & 1) {\n res = binaryFun!op (t[--r], res);\n }\n }\n return res;\n }\n this (const T[] a) pure nothrow {\n n = a.length;\n t = uninitializedArray!(T[])(n) ~ a;\n build ();\n }\n}\n \nclass InputReader {\n private:\n ubyte[] p;\n ubyte[] buffer;\n size_t cur;\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n p = stdin.rawRead (buffer);\n }\n final ubyte skipByte (ubyte lo) {\n while (true) {\n auto a = p[cur .. $];\n auto r = a.find! (c => c >= lo);\n if (!r.empty) {\n cur += a.length - r.length;\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n cur = 0;\n if (p.empty) return 0;\n }\n }\n final ubyte nextByte () {\n if (cur < p.length) {\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n if (p.empty) return 0;\n cur = 1;\n return p[0];\n }\n \n template next(T) if (isSigned!T) {\n final T next () {\n T res;\n ubyte b = skipByte (45);\n if (b == 45) {\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n final T next () {\n T res = skipByte (48) - 48;\n while (true) {\n ubyte b = nextByte ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n final T[] nextA(T) (int n) {\n auto a = uninitializedArray!(T[]) (n);\n foreach (i; 0 .. n) {\n a[i] = next!T;\n }\n return a;\n }\n}\n \nvoid main() {\n auto r = new InputReader;\n immutable nt = r.next!uint;\n foreach (tid; 0 .. nt) {\n immutable n = r.next!uint;\n auto a = r.nextA!uint (n);\n auto b = r.nextA!uint (n);\n bool test () {\n debug stderr.writeln (\"TEST\");\n auto idx = new int[][n+1];\n foreach (i; 0 .. n) {\n idx[a[i]] ~= i;\n }\n auto st = new SegmentTree!(uint, min, int.max) (a);\n auto t = new RedBlackTree!(uint) (iota (0, n));\n foreach (i; 0 .. n) {\n debug stderr.writefln (\"i = %d, b[i] = %d\", i, b[i]);\n debug stderr.writeln (t);\n auto k = t.front;\n debug stderr.writeln (\"k = \", k);\n debug stderr.writeln (\"a[k] = \", a[k]);\n if (a[k] < b[i]) {\n debug stderr.writeln (\"CHECK: a[k] < b[i]\");\n return false;\n } else if (a[k] == b[i]) {\n t.removeFront ();\n st.update (k, uint.max);\n } else {\n while (true) {\n if (idx[b[i]].empty) {\n debug stderr.writeln (\"CHECK: idx[b[i]].empty\");\n return false;\n }\n int j = idx[b[i]].front;\n debug stderr.writeln (\"j = \", j);\n idx[b[i]].popFront ();\n if (j !in t) {\n continue;\n }\n if (st.reduce (0, j) < b[i]) {\n debug stderr.writefln (\"CHECK: st.reduce (0, %d) = %d\", j, st.reduce (0, j));\n return false;\n }\n t.removeKey (j);\n st.update (j, uint.max);\n break;\n }\n }\n }\n return true;\n }\n writeln (test () ? \"YES\" : \"NO\");\n }\n}\n"}], "negative_code": [{"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\nimport std.container.rbtree;\n \nclass SegmentTree(T = int, alias op=\"a+b\", T zero = T.init) {\n private:\n T [] t;\n size_t n;\n final void build () pure nothrow @nogc {\n foreach_reverse (i; 1 .. n) {\n immutable k = i << 1;\n t[i] = binaryFun!op (t[k], t[k+1]);\n }\n }\n public:\n final void update (size_t p, T v) pure nothrow @nogc {\n for (t[p += n] = v; p > 1; p >>= 1) {\n t[p>>1] = binaryFun!op (t[p], t[p ^ 1]);\n }\n }\n final T reduce (size_t l, size_t r) const pure nothrow @nogc {\n T res = zero;\n for (l += n, r += n; l < r; l >>= 1, r >>= 1) {\n if (l & 1) {\n res = binaryFun!op (res, t[l++]);\n }\n if (r & 1) {\n res = binaryFun!op (t[--r], res);\n }\n }\n return res;\n }\n this (const T[] a) pure nothrow {\n n = a.length;\n t = uninitializedArray!(T[])(n) ~ a;\n build ();\n }\n}\n \nclass InputReader {\n private:\n ubyte[] p;\n ubyte[] buffer;\n size_t cur;\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n p = stdin.rawRead (buffer);\n }\n final ubyte skipByte (ubyte lo) {\n while (true) {\n auto a = p[cur .. $];\n auto r = a.find! (c => c >= lo);\n if (!r.empty) {\n cur += a.length - r.length;\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n cur = 0;\n if (p.empty) return 0;\n }\n }\n final ubyte nextByte () {\n if (cur < p.length) {\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n if (p.empty) return 0;\n cur = 1;\n return p[0];\n }\n \n template next(T) if (isSigned!T) {\n final T next () {\n T res;\n ubyte b = skipByte (45);\n if (b == 45) {\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n final T next () {\n T res = skipByte (48) - 48;\n while (true) {\n ubyte b = nextByte ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n final T[] nextA(T) (int n) {\n auto a = uninitializedArray!(T[]) (n);\n foreach (i; 0 .. n) {\n a[i] = next!T;\n }\n return a;\n }\n}\n \nvoid main() {\n auto r = new InputReader;\n immutable nt = r.next!uint;\n foreach (tid; 0 .. nt) {\n immutable n = r.next!uint;\n auto a = r.nextA!uint (n);\n auto b = r.nextA!uint (n);\n bool test () {\n debug stderr.writeln (\"TEST\");\n auto idx = new int[][n+1];\n foreach (i; 0 .. n) {\n idx[a[i]] ~= i;\n }\n auto st = new SegmentTree!(uint, min) (a);\n auto t = new RedBlackTree!(uint) (iota (0, n));\n foreach (i; 0 .. n) {\n debug stderr.writefln (\"i = %d, b[i] = %d\", i, b[i]);\n debug stderr.writeln (t);\n auto k = t.front;\n debug stderr.writeln (\"k = \", k);\n debug stderr.writeln (\"a[k] = \", a[k]);\n if (a[k] < b[i]) {\n return false;\n } else if (a[k] == b[i]) {\n t.removeFront ();\n st.update (k, uint.max);\n } else {\n while (true) {\n if (idx[b[i]].empty) return false;\n int j = idx[b[i]].front;\n debug stderr.writeln (\"j = \", j);\n idx[b[i]].popFront ();\n if (j !in t) {\n continue;\n }\n debug stderr.writefln (\"st.reduce (0, %d) = %d\", j, st.reduce (0, j));\n if (st.reduce (0, j) < b[i]) {\n return false;\n }\n t.removeKey (j);\n st.update (j, uint.max);\n break;\n }\n }\n }\n return true;\n }\n writeln (test () ? \"YES\" : \"NO\");\n }\n}\n"}, {"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\nimport std.container.rbtree;\n \nclass SegmentTree(T = int, alias op=\"a+b\", T zero = T.init) {\n private:\n T [] t;\n size_t n;\n final void build () pure nothrow @nogc {\n foreach_reverse (i; 1 .. n) {\n immutable k = i << 1;\n t[i] = binaryFun!op (t[k], t[k+1]);\n }\n }\n public:\n final void update (size_t p, T v) pure nothrow @nogc {\n for (t[p += n] = v; p > 1; p >>= 1) {\n t[p>>1] = binaryFun!op (t[p], t[p ^ 1]);\n }\n }\n final T reduce (size_t l, size_t r) const pure nothrow @nogc {\n T res = zero;\n for (l += n, r += n; l < r; l >>= 1, r >>= 1) {\n if (l & 1) {\n res = binaryFun!op (res, t[l++]);\n }\n if (r & 1) {\n res = binaryFun!op (t[--r], res);\n }\n }\n return res;\n }\n this (const T[] a) pure nothrow {\n n = a.length;\n t = uninitializedArray!(T[])(n) ~ a;\n build ();\n }\n}\n \nclass InputReader {\n private:\n ubyte[] p;\n ubyte[] buffer;\n size_t cur;\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n p = stdin.rawRead (buffer);\n }\n final ubyte skipByte (ubyte lo) {\n while (true) {\n auto a = p[cur .. $];\n auto r = a.find! (c => c >= lo);\n if (!r.empty) {\n cur += a.length - r.length;\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n cur = 0;\n if (p.empty) return 0;\n }\n }\n final ubyte nextByte () {\n if (cur < p.length) {\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n if (p.empty) return 0;\n cur = 1;\n return p[0];\n }\n \n template next(T) if (isSigned!T) {\n final T next () {\n T res;\n ubyte b = skipByte (45);\n if (b == 45) {\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n final T next () {\n T res = skipByte (48) - 48;\n while (true) {\n ubyte b = nextByte ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n final T[] nextA(T) (int n) {\n auto a = uninitializedArray!(T[]) (n);\n foreach (i; 0 .. n) {\n a[i] = next!T;\n }\n return a;\n }\n}\n \nvoid main() {\n auto r = new InputReader;\n immutable nt = r.next!uint;\n foreach (tid; 0 .. nt) {\n immutable n = r.next!uint;\n auto a = r.nextA!uint (n);\n auto b = r.nextA!uint (n);\n bool test () {\n auto idx = new int[][n+1];\n foreach (i; 0 .. n) {\n idx[a[i]] ~= i;\n }\n auto st = new SegmentTree!(uint, max) (a);\n auto t = new RedBlackTree!(uint) (iota (0, n));\n foreach (i; 0 .. n) {\n auto k = t.front;\n if (a[k] < b[i]) {\n return false;\n } else if (a[k] == b[i]) {\n t.removeFront ();\n st.update (k, uint.max);\n } else {\n while (true) {\n if (idx[b[i]].empty) return false;\n int j = idx[b[i]].front;\n idx[b[i]].popFront ();\n if (j !in t) {\n continue;\n }\n if (st.reduce (0, j) < b[i]) {\n return false;\n }\n t.removeKey (j);\n st.update (j, uint.max);\n break;\n }\n }\n }\n return true;\n }\n writeln (test () ? \"YES\" : \"NO\");\n }\n}\n"}], "src_uid": "0bc73dfd5745b00c24e3553b161d75a4"} {"nl": {"description": "Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are $$$n$$$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $$$a_1, a_2, \\dots, a_n$$$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$1, 2, \\dots, i$$$ by $$$1$$$. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$i, i + 1, \\dots, n$$$ by $$$1$$$. Increase the level of moisture of all trees by $$$1$$$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $$$0$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$)  — the number of test cases. The description of $$$t$$$ test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 200\\,000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\leq a_i \\leq 10^9$$$) — the initial levels of trees moisture. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200\\,000$$$.", "output_spec": "For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists.", "sample_inputs": ["4\n3\n-2 -2 -2\n3\n10 4 7\n4\n4 -4 4 -4\n5\n1 -2 3 -4 5"], "sample_outputs": ["2\n13\n36\n33"], "notes": "NoteIn the first test case it's enough to apply the operation of adding $$$1$$$ to the whole array $$$2$$$ times. In the second test case you can apply the operation of decreasing $$$4$$$ times on the prefix of length $$$3$$$ and get an array $$$6, 0, 3$$$. After that apply the operation of decreasing $$$6$$$ times on the prefix of length $$$1$$$ and $$$3$$$ times on the suffix of length $$$1$$$. In total, the number of actions will be $$$4 + 6 + 3 = 13$$$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $$$13$$$. "}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.traits;\r\nimport std.math;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nstruct LazySegmentTree(T, F) {\r\n static assert(hasMember!(T, \"binop\"));\r\n static assert(isCallable!(T.binop));\r\n static assert(hasMember!(T, \"unit\"));\r\n static assert(hasMember!(F, \"composite\"));\r\n static assert(isCallable!(F.composite));\r\n static assert(hasMember!(F, \"id\"));\r\n int n;\r\n T[] dat;\r\n F[] laz; // composition of all uniformly applied `F`s\r\n this(int n_) {\r\n n = 1;\r\n while (n < n_) n *= 2;\r\n dat = new T[2*n-1];\r\n dat[] = T.unit;\r\n laz = new F[2*n-1];\r\n laz[] = F.id;\r\n }\r\n this(T[] xs) {\r\n this(cast(int)(xs.length));\r\n for (int i = 0; i < xs.length; i++) { this.dat[this.n - 1 + i] = xs[i]; }\r\n for (int k = this.n-2; k >= 0; k--) { this.dat[k] = T.binop(this.dat[2*k+1], this.dat[2*k+2]); }\r\n }\r\n void push(int k) {\r\n if (laz[k] == F.id) return;\r\n if (k < n - 1) {\r\n laz[k*2+1] = laz[k].composite(laz[k*2+1]);\r\n laz[k*2+2] = laz[k].composite(laz[k*2+2]);\r\n }\r\n dat[k] = laz[k](dat[k]);\r\n laz[k] = F.id;\r\n }\r\n // apply `f` to range [a, b). i.e. x[a] <- f(x[a]), ..., x[b-1] <- f(x[b-1])\r\n void apply(int a, int b, F f) { apply(a, b, f, 0, 0, n); }\r\n void apply(int a, int b, F f, int k, int l, int r) {\r\n push(k);\r\n if (a <= l && r <= b) {\r\n laz[k] = f.composite(laz[k]);\r\n } else if (l < b && a < r) {\r\n apply(a, b, f, k*2+1, l, (l+r)/2);\r\n apply(a, b, f, k*2+2, (l+r)/2, r);\r\n dat[k] = T.binop(dat[k*2+1], dat[k*2+2]);\r\n }\r\n }\r\n // return x[a] + x[a+1] + ... + x[b-1] (here '+' corresponds to `T.binop`)\r\n T query(int a, int b) { return query(a, b, 0, 0, n); }\r\n T query(int a, int b, int k, int l, int r) {\r\n push(k);\r\n if (b <= l || r <= a) return T.unit;\r\n else if (a <= l && r <= b) return dat[k];\r\n else {\r\n auto lres = query(a, b, k*2+1, l, (l+r)/2);\r\n auto rres = query(a, b, k*2+2, (l+r)/2, r);\r\n return T.binop(lres, rres);\r\n }\r\n }\r\n // for debugging. apply all propagated function to leaves.\r\n void push_all() {\r\n for (int k = 0; k < n-1; k++) push(k);\r\n }\r\n}\r\n\r\nstruct RSQ(Integer) {\r\n struct T {\r\n Integer x;\r\n Integer len;\r\n static T binop(T a, T b) { return T(a.x + b.x, a.len + b.len); }\r\n enum T unit = T(0, 0);\r\n }\r\n struct F {\r\n Integer x;\r\n this(Integer x) { this.x = x; }\r\n T opCall(T t) { return T(x * t.len + t.x, t.len); }\r\n F composite(F g) { return F(x + g.x); }\r\n enum F id = F(0);\r\n }\r\n LazySegmentTree!(T, F) lst;\r\n this(int n_) { lst = LazySegmentTree!(T, F)(n_); }\r\n this(Integer[] xs) {\r\n lst = LazySegmentTree!(T, F)(xs.map!((x) => T(x, 1)).array);\r\n }\r\n void add(int a, int b, Integer x) {\r\n lst.apply(a, b, F(x));\r\n }\r\n Integer query(int a, int b) {\r\n return lst.query(a, b).x;\r\n }\r\n}\r\n\r\nvoid solve() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").map!(to!long).array;\r\n auto rsq = RSQ!long(A);\r\n long ans = 0;\r\n for (int i = 0; i+1 < N; i++) {\r\n long a = rsq.query(i, i+1);\r\n long b = rsq.query(i+1, i+2);\r\n if (a > b) {\r\n long d = a - b;\r\n ans += d;\r\n rsq.add(0, i+1, -d);\r\n } else if (a < b) {\r\n long d = b - a;\r\n ans += d;\r\n rsq.add(i+1, N, -d);\r\n }\r\n }\r\n ans += abs(rsq.query(0, 1));\r\n writeln(ans);\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}], "negative_code": [], "src_uid": "f54c1448a279e59f96847a158f993101"} {"nl": {"description": "Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\\,010\\,111$$$ is a binary decimal, while $$$10\\,201$$$ and $$$787\\,788$$$ are not.Given a number $$$n$$$, you are asked to represent $$$n$$$ as a sum of some (not necessarily distinct) binary decimals. Compute the smallest number of binary decimals required for that.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$), denoting the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$), denoting the number to be represented.", "output_spec": "For each test case, output the smallest number of binary decimals required to represent $$$n$$$ as a sum.", "sample_inputs": ["3\n121\n5\n1000000000"], "sample_outputs": ["2\n5\n1"], "notes": "NoteIn the first test case, $$$121$$$ can be represented as $$$121 = 110 + 11$$$ or $$$121 = 111 + 10$$$.In the second test case, $$$5$$$ can be represented as $$$5 = 1 + 1 + 1 + 1 + 1$$$.In the third test case, $$$1\\,000\\,000\\,000$$$ is a binary decimal itself, thus the answer is $$$1$$$."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport std.range;\nimport core.stdc.stdio;\n\nvoid main()\n{\n\tint t = readInt!int;\n\twhile (t--) solve;\n}\nvoid solve()\n{\n\tauto n = readInt!long;\n\tint md = int.min;\n\twhile (n)\n\t{\n\t\tmd = cast(int)max(md, n % 10);\n\t\tn /= 10;\n\t}\n\tmd.writeln;\n}\n\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n\tcurrChar = getchar();\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,\r\n std.container, std.typecons, std.conv, std.random, std.bigint;\r\n\r\nvoid read(S...)(ref S args) {\r\n auto input = readln.split;\r\n assert(input.length == args.length);\r\n foreach (i, ref arg; args) {\r\n arg = input[i].to!(S[i]);\r\n }\r\n}\r\n\r\nauto list(T)() { return readln.split.map!(e => e.to!T); }\r\n\r\nvoid main() {\r\n int t;\r\n read(t);\r\n \r\n while (t--) {\r\n string s;\r\n read(s);\r\n \r\n char c = s[0];\r\n foreach (e; s) {\r\n c = max(c, e);\r\n }\r\n \r\n writeln(c);\r\n }\r\n}\r\n\r\n"}, {"source_code": "// code by cutx64\r\n\r\nmodule main;\r\n\r\nimport std.string, std.array, std.container, std.typecons;\r\nimport std.stdio, std.format, std.file, std.outbuffer;\r\nimport std.algorithm, std.conv, std.functional, std.range, core.bitop;\r\n// import std.bigint, std.complex, std.bitmanip;\r\n// import std.math, std.mathspecial, std.numeric;\r\n\r\nvoid main() {\r\n\r\n int tests; readf!\"%s\\n\"(tests);\r\n while(tests--) {\r\n auto s = readln.strip;\r\n writeln(s.maxElement);\r\n }\r\n\r\n} // main\r\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n long t;\n readf!\" %d \"(t);\n while (t--) {\n long n;\n readf!\" %d \"(n);\n writeln(n.text.split(\"\").map!(to!int).maxElement);\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!string;\r\n\r\n\t\tforeach (c; n)\r\n\t\t{\r\n\t\t\tans[ti].chmax(c-'0');\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readToken();\n int ans;\n foreach (c; N) {\n chmax(ans, c - '0');\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto s = readln.strip;\r\n\t\twriteln (s.maxElement);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "1a6881aeb197b8ed429f46850eb27b9c"} {"nl": {"description": "The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.Find any longest k-good segment.As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.", "input_spec": "The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a.", "output_spec": "Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right.", "sample_inputs": ["5 5\n1 2 3 4 5", "9 3\n6 5 1 2 3 2 1 4 5", "3 1\n1 2 3"], "sample_outputs": ["1 5", "3 7", "1 1"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.string;\nimport std.algorithm;\n\nvoid solve(int[] a, int k)\n{\n auto n = cast(int)a.length;\n int[int] cnt;\n auto longest = 0;\n auto left = -1, right = -1;\n auto i = 0, j = 0;\n auto count = 0;\n while (j < n)\n {\n if (!(a[j] in cnt) || cnt[a[j]] == 0)\n {\n ++ count;\n }\n if (!(a[j] in cnt))\n {\n cnt[a[j]] = 0;\n }\n ++ cnt[a[j]];\n if (count <= k)\n {\n if (j - i + 1 > longest)\n {\n left = i + 1;\n right = j + 1;\n longest = j - i + 1;\n }\n }\n else\n {\n while (i <= j && count > k)\n {\n -- cnt[a[i]];\n if (cnt[a[i]] == 0)\n {\n -- count;\n }\n ++ i;\n }\n }\n ++ j;\n }\n writeln(left, \" \", right);\n}\n\nint main(string[] args)\n{\n int n, k;\n while (readf(\"%d %d\\n\", &n, &k) == 2)\n {\n auto a = new int[n];\n foreach (i; 0 .. n)\n {\n readf(\" %d\", &a[i]);\n }\n readln;\n solve(a, k);\n }\n return 0;\n}"}, {"source_code": "import std.stdio, std.string;\nimport std.algorithm;\n\nvoid solve(int[] a, int k)\n{\n auto n = cast(int)a.length;\n auto m = reduce!(max)(a);\n auto cnt = new int[m + 1];\n auto longest = 0;\n auto left = -1, right = -1;\n auto i = 0, j = 0;\n auto count = 0;\n while (j < n)\n {\n if (cnt[a[j]] == 0)\n {\n ++ count;\n }\n ++ cnt[a[j]];\n if (count <= k)\n {\n if (j - i + 1 > longest)\n {\n left = i + 1;\n right = j + 1;\n longest = j - i + 1;\n }\n }\n else\n {\n while (i <= j && count > k)\n {\n -- cnt[a[i]];\n if (cnt[a[i]] == 0)\n {\n -- count;\n }\n ++ i;\n }\n }\n ++ j;\n }\n writeln(left, \" \", right);\n}\n\nint main(string[] args)\n{\n int n, k;\n while (readf(\"%d %d\\n\", &n, &k) == 2)\n {\n auto a = new int[n];\n foreach (i; 0 .. n)\n {\n readf(\" %d\", &a[i]);\n }\n readln;\n solve(a, k);\n }\n return 0;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.string;\nimport std.algorithm;\n\nvoid solve(int[] a, int k)\n{\n auto n = cast(int)a.length;\n int[int] cnt;\n auto longest = 0;\n auto left = -1, right = -1;\n auto i = 0, j = 0;\n auto count = 0;\n while (j < n)\n {\n if (!(a[j] in cnt) || cnt[a[j]] == 0)\n {\n ++ count;\n }\n if (!a[j] in cnt)\n {\n cnt[a[j]] = 0;\n }\n ++ cnt[a[j]];\n if (count <= k)\n {\n if (j - i + 1 > longest)\n {\n left = i + 1;\n right = j + 1;\n longest = j - i + 1;\n }\n }\n else\n {\n while (i <= j && count > k)\n {\n -- cnt[a[i]];\n if (cnt[a[i]] == 0)\n {\n -- count;\n }\n ++ i;\n }\n }\n ++ j;\n }\n writeln(left, \" \", right);\n}\n\nint main(string[] args)\n{\n int n, k;\n while (readf(\"%d %d\\n\", &n, &k) == 2)\n {\n auto a = new int[n];\n foreach (i; 0 .. n)\n {\n readf(\" %d\", &a[i]);\n }\n readln;\n solve(a, k);\n }\n return 0;\n}"}], "src_uid": "e0ea798c8ce0d8a4340e0fa3399bcc3b"} {"nl": {"description": "A rectangle with its opposite corners in $$$(0, 0)$$$ and $$$(w, h)$$$ and sides parallel to the axes is drawn on a plane.You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle.Your task is to choose three points in such a way that: exactly two of them belong to the same side of a rectangle; the area of a triangle formed by them is maximum possible. Print the doubled area of this triangle. It can be shown that the doubled area of any triangle formed by lattice points is always an integer.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The first line of each testcase contains two integers $$$w$$$ and $$$h$$$ ($$$3 \\le w, h \\le 10^6$$$) — the coordinates of the corner of a rectangle. The next two lines contain the description of the points on two horizontal sides. First, an integer $$$k$$$ ($$$2 \\le k \\le 2 \\cdot 10^5$$$) — the number of points. Then, $$$k$$$ integers $$$x_1 < x_2 < \\dots < x_k$$$ ($$$0 < x_i < w$$$) — the $$$x$$$ coordinates of the points in the ascending order. The $$$y$$$ coordinate for the first line is $$$0$$$ and for the second line is $$$h$$$. The next two lines contain the description of the points on two vertical sides. First, an integer $$$k$$$ ($$$2 \\le k \\le 2 \\cdot 10^5$$$) — the number of points. Then, $$$k$$$ integers $$$y_1 < y_2 < \\dots < y_k$$$ ($$$0 < y_i < h$$$) — the $$$y$$$ coordinates of the points in the ascending order. The $$$x$$$ coordinate for the first line is $$$0$$$ and for the second line is $$$w$$$. The total number of points on all sides in all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase print a single integer — the doubled maximum area of a triangle formed by such three points that exactly two of them belong to the same side.", "sample_inputs": ["3\n5 8\n2 1 2\n3 2 3 4\n3 1 4 6\n2 4 5\n10 7\n2 3 9\n2 1 7\n3 1 3 4\n3 4 5 6\n11 5\n3 1 6 8\n3 3 6 8\n3 1 3 4\n2 2 4"], "sample_outputs": ["25\n42\n35"], "notes": "NoteThe points in the first testcase of the example: $$$(1, 0)$$$, $$$(2, 0)$$$; $$$(2, 8)$$$, $$$(3, 8)$$$, $$$(4, 8)$$$; $$$(0, 1)$$$, $$$(0, 4)$$$, $$$(0, 6)$$$; $$$(5, 4)$$$, $$$(5, 5)$$$. The largest triangle is formed by points $$$(0, 1)$$$, $$$(0, 6)$$$ and $$$(5, 4)$$$ — its area is $$$\\frac{25}{2}$$$. Thus, the doubled area is $$$25$$$. Two points that are on the same side are: $$$(0, 1)$$$ and $$$(0, 6)$$$."}, "positive_code": [{"source_code": "import std;\n\nalias MinMax = Tuple!(long, \"min\", long, \"max\");\n\nlong\nreadLong () {\n long a;\n readf!\" %s\"(a);\n return a;\n}\n\nMinMax\nminMax () {\n immutable nb_to_read = readLong();\n long min = long.max, max = long.min;\n foreach (i; 0 .. nb_to_read) {\n immutable x = readLong();\n\n if (x < min)\n min = x;\n if (x > max)\n max = x;\n }\n return MinMax(min, max);\n}\n\nlong\nbase_length (in MinMax seg) {\n return seg.max - seg.min;\n}\n\nvoid main () {\n immutable t = readLong();\n\n foreach (test_index; 0 .. t) {\n immutable w = readLong(), h = readLong();\n\n long area = -1;\n area = max(area, base_length(minMax()) * h);\n area = max(area, base_length(minMax()) * h);\n area = max(area, base_length(minMax()) * w);\n area = max(area, base_length(minMax()) * w);\n\n writeln(area);\n }\n}\n\n// \"\"\n"}, {"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n auto w = readInt!long;\n auto h = readInt!long;\n auto x1n = readInt!int;\n auto x1 = ma(x1n, readInt!long);\n auto x2n = readInt!int;\n auto x2 = ma(x2n, readInt!long);\n auto y1n = readInt!int;\n auto y1 = ma(y1n, readInt!long);\n auto y2n = readInt!int;\n auto y2 = ma(y2n, readInt!long);\n max((x1.back - x1.front) * h,\n (x2.back - x2.front) * h,\n (y1.back - y1.front) * w,\n (y2.back - y2.front) * w).writeln;\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n long w, h;\n readf!\" %d %d \"(w, h);\n auto h1 = readln.splitter.map!(to!long).array[1 .. $];\n auto h2 = readln.splitter.map!(to!long).array[1 .. $];\n auto v1 = readln.splitter.map!(to!long).array[1 .. $];\n auto v2 = readln.splitter.map!(to!long).array[1 .. $];\n writeln(max((h1.maxElement - h1.minElement) * h,\n (h2.maxElement - h2.minElement) * h,\n (v1.maxElement - v1.minElement) * w,\n (v2.maxElement - v2.minElement) * w));\n }\n}\n"}], "negative_code": [{"source_code": "import std;\n\nalias MinMax = Tuple!(int, \"min\", int, \"max\");\n\nint\nreadInt () {\n int a;\n readf!\" %s\"(a);\n return a;\n}\n\nMinMax\nminMax () {\n immutable nb_to_read = readInt();\n int min = int.max, max = int.min;\n foreach (i; 0 .. nb_to_read) {\n immutable x = readInt();\n\n if (x < min)\n min = x;\n if (x > max)\n max = x;\n }\n return MinMax(min, max);\n}\n\nint\nbase_length (in MinMax seg) {\n return seg.max - seg.min;\n}\n\nvoid main () {\n immutable t = readInt();\n\n foreach (test_index; 0 .. t) {\n immutable w = readInt(), h = readInt();\n\n int area = -1;\n area = max(area, base_length(minMax()) * h);\n area = max(area, base_length(minMax()) * h);\n area = max(area, base_length(minMax()) * w);\n area = max(area, base_length(minMax()) * w);\n\n writeln(area);\n }\n}\n\n// \"\"\n"}, {"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n auto w = readInt!long;\n auto h = readInt!long;\n auto x1n = readInt!int;\n auto x1 = ma(x1n, readInt!long);\n auto x2n = readInt!int;\n auto x2 = ma(x2n, readInt!long);\n auto y1n = readInt!int;\n auto y1 = ma(y1n, readInt!long);\n auto y2n = readInt!int;\n auto y2 = ma(y2n, readInt!long);\n max((x1.back - x1.front) * h,\n (x2.back - x2.front) * h,\n (y1.back - x1.front) * w,\n (y2.back - y2.front) * w).writeln;\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}], "src_uid": "2c67ee95eba7ffbbed99cb488abb5f3d"} {"nl": {"description": "Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.", "input_spec": "The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.", "output_spec": "Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["4 3\n3 2 3", "4 3\n2 3 3"], "sample_outputs": ["6", "2"], "notes": "NoteIn the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units."}, "positive_code": [{"source_code": "import std.stdio: writeln, stdin;\nimport std.regex: split, regex;\nimport std.string: strip;\nimport std.conv: to;\nimport std.algorithm.iteration: map;\n\n\nvoid main()\n{\n auto nm = stdin.readln.strip.split(regex(` +`)).map!(to!int);\n auto as = stdin.readln.strip.split(regex(` +`)).map!(to!int);\n auto n = nm[0], m = nm[1];\n int b;\n long time = 0;\n foreach(a; as )\n {\n if (a a - 1);\n\n //stderr.writeln(a);\n\n long ans;\n int pos;\n\n foreach(ai ; a){\n ans += (ai - pos + n) % n;\n pos = ai;\n }\n\n writeln(ans);\n}\n\n\nvoid readVars(T...)(auto ref T args){\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}], "negative_code": [{"source_code": "import std.stdio: writeln, stdin;\nimport std.regex: split, regex;\nimport std.string: strip;\nimport std.conv: to;\nimport std.algorithm.iteration: map;\n\n\nvoid main()\n{\n auto nm = stdin.readln.strip.split(regex(` +`)).map!(to!int);\n auto as = stdin.readln.strip.split(regex(` +`)).map!(to!int);\n auto n = nm[0], m = nm[1];\n int b, time = 0;\n foreach(a; as )\n {\n if (a= m)\n\t\t\tcontinue;\n\n\t\tif (tasks[t] - 1 >= s)\n\t\t{\n\t\t\tts += (tasks[t] - 1 - s);\n\t\t\ts = tasks[t] - 1;\n\t\t} else {\n\t\t\tts += n - (s - tasks[t] + 1);\n\t\t\ts = tasks[t] - 1;\n\t\t}\n\t}\n\tprintf(\"%d\", ts);\n\treturn 0;\n}\n"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\n\nint main(string[] argv)\n{\n\tint n, m;\n\tscanf(\"%d %d\", &n, &m);\n\tint[] tasks; tasks.length = m;\n\tforeach (int i; 0..m)\n\t{\n\t\tscanf(\"%d\", &tasks[i]);\n\t}\n\tint s = 0;\n\tlong ts = 0;\n\tint t = 0;\n\twhile (t < m)\n\t{\n\t\twhile (t < m && tasks[t] == s+1)\n\t\t{\n\t\t\ttasks[t] = 0;\n\t\t\tt++;\n\t\t}\n\t\tif (t >= m)\n\t\t\tcontinue;\n\n\t\tif (tasks[t] - 1 >= s)\n\t\t{\n\t\t\tts += (tasks[t] - 1 - s);\n\t\t\ts = tasks[t] - 1;\n\t\t} else {\n\t\t\tts += n - (s - tasks[t] + 1);\n\t\t\ts = tasks[t] - 1;\n\t\t}\n\t}\n\tprintf(\"%d\", ts);\n\treturn 0;\n}\n"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\n\nint main(string[] argv)\n{\n\tint n, m;\n\tscanf(\"%d %d\", &n, &m);\n\tint[] tasks; tasks.length = m;\n\tforeach (int i; 0..m)\n\t{\n\t\tscanf(\"%d\", &tasks[i]);\n\t}\n\tint s = 0;\n\tint ts = 0;\n\tint t = 0;\n\twhile (t < m)\n\t{\n\t\twhile (t < m && tasks[t] == s+1)\n\t\t{\n\t\t\ttasks[t] = 0;\n\t\t\tt++;\n\t\t}\n\t\tif (t >= m)\n\t\t\tcontinue;\n\n\t\tif (tasks[t] - 1 >= s)\n\t\t{\n\t\t\tts += (tasks[t] - 1 - s);\n\t\t\ts = tasks[t] - 1;\n\t\t} else {\n\t\t\tts += (n - s) + (s - tasks[t] + 1);\n\t\t\ts = tasks[t] - 1;\n\t\t}\n\t}\n\tprintf(\"%d\", ts);\n\treturn 0;\n}\n"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\n\nint main(string[] argv)\n{\n\tint n, m;\n\tscanf(\"%d %d\", &n, &m);\n\tint[] tasks; tasks.length = m;\n\tforeach (int i; 0..m)\n\t{\n\t\tscanf(\"%d\", &tasks[i]);\n\t}\n\tint s = 0;\n\tlong ts = 0;\n\tint t = 0;\n\twhile (t < m)\n\t{\n\t\twhile (t < m && tasks[t] == s+1)\n\t\t{\n\t\t\ttasks[t] = 0;\n\t\t\tt++;\n\t\t}\n\t\tif (t >= m)\n\t\t\tcontinue;\n\n\t\tif (tasks[t] - 1 >= s)\n\t\t{\n\t\t\tts += (tasks[t] - 1 - s);\n\t\t\ts = tasks[t] - 1;\n\t\t} else {\n\t\t\tts += n - s + tasks[t] - 1;\n\t\t\ts = tasks[t] - 1;\n\t\t}\n\t}\n\tprintf(\"%d\", ts);\n\treturn 0;\n}\n"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\n\nint main(string[] argv)\n{\n\tint n, m;\n\tscanf(\"%d %d\", &n, &m);\n\tint[] tasks; tasks.length = m;\n\tforeach (int i; 0..m)\n\t{\n\t\tscanf(\"%d\", &tasks[i]);\n\t\ttasks[i]--;\n\t}\n\t\n\tint cp = 0;\n\tlong steps = 0;\n\tforeach (int i; 0..m)\n\t{\n\t\tint ts = tasks[i] - cp;\n\t\tts = ts < 0 ? n + ts : ts;\n\t\tsteps += ts;\n\t\tcp = tasks[i];\n\t}\n\tprintf(\"%d\", steps);\n\treturn 0;\n}\n"}], "src_uid": "2c9c96dc5b6f8d1f0ddeea8e07640d3e"} {"nl": {"description": "Adieu l'ami.Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.The space is represented by a rectangular grid of n × m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by \"1 r1 c1 r2 c2\" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, \"2 r1 c1 r2 c2\" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n × m area.Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. \"3 r1 c1 r2 c2\" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.And you're here to tell Koyomi the feasibility of each of his attempts.", "input_spec": "The first line of input contains three space-separated integers n, m and q (1 ≤ n, m ≤ 2 500, 1 ≤ q ≤ 100 000) — the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively. The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 ≤ t ≤ 3, 1 ≤ r1, r2 ≤ n, 1 ≤ c1, c2 ≤ m) — the type and two coordinates of an action. Additionally, the following holds depending on the value of t: If t = 1: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1; If t = 2: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1, the specified group of barriers exist on the ground before the removal. If t = 3: no extra restrictions. ", "output_spec": "For each of Koyomi's attempts (actions with t = 3), output one line — containing \"Yes\" (without quotes) if it's feasible, and \"No\" (without quotes) otherwise.", "sample_inputs": ["5 6 5\n1 2 2 4 5\n1 3 3 3 3\n3 4 4 1 1\n2 2 2 4 5\n3 1 1 4 4", "2500 2500 8\n1 549 1279 1263 2189\n1 303 795 1888 2432\n1 2227 622 2418 1161\n3 771 2492 1335 1433\n1 2017 2100 2408 2160\n3 48 60 798 729\n1 347 708 1868 792\n3 1940 2080 377 1546"], "sample_outputs": ["No\nYes", "No\nYes\nNo"], "notes": "NoteFor the first example, the situations of Koyomi's actions are illustrated below. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int logHalf = 12;\nimmutable int half = 1 << logHalf;\nimmutable int limit = half + 2505;\n\nulong [] [] v;\n\nulong tGet (int r0, int c0)\n{\n\tr0 += half;\n\tc0 += half;\n\tulong res = 0;\n\tfor (int r = r0; r > 0; r >>= 1)\n\t{\n\t\tfor (int c = c0; c > 0; c >>= 1)\n\t\t{\n\t\t\tres ^= v[r][c];\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid tMark (int r1, int c1, int r2, int c2, ulong h)\n{\n\tr1 += half;\n\tc1 += half;\n\tr2 += half;\n\tc2 += half;\n\tfor (int rLo = r1, rHi = r2; rLo < rHi; rLo >>= 1, rHi >>= 1)\n\t{\n\t\tif ((rHi & 1) == 1)\n\t\t{\n\t\t\trHi -= 1;\n\t\t\tfor (int cLo = c1, cHi = c2; cLo < cHi;\n\t\t\t cLo >>= 1, cHi >>= 1)\n\t\t\t{\n\t\t\t\tif ((cHi & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tcHi -= 1;\n\t\t\t\t\tv[rHi][cHi] ^= h;\n\t\t\t\t}\n\t\t\t\tif ((cLo & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tv[rHi][cLo] ^= h;\n\t\t\t\t\tcLo += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif ((rLo & 1) == 1)\n\t\t{\n\t\t\tfor (int cLo = c1, cHi = c2; cLo < cHi;\n\t\t\t cLo >>= 1, cHi >>= 1)\n\t\t\t{\n\t\t\t\tif ((cHi & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tcHi -= 1;\n\t\t\t\t\tv[rLo][cHi] ^= h;\n\t\t\t\t}\n\t\t\t\tif ((cLo & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tv[rLo][cLo] ^= h;\n\t\t\t\t\tcLo += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\trLo += 1;\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\tint n, m, q;\n\twhile (readf (\" %s %s %s\", &n, &m, &q) > 0)\n\t{\n\t\tv = new ulong [] [] (limit, limit);\n\t\tulong [int [4]] h;\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint t, r1, c1, r2, c2;\n\t\t\treadf (\" %s %s %s %s %s\", &t, &r1, &c1, &r2, &c2);\n\t\t\tif (t == 3)\n\t\t\t{\n\t\t\t\tr1 -= 1;\n\t\t\t\tc1 -= 1;\n\t\t\t\tr2 -= 1;\n\t\t\t\tc2 -= 1;\n\t\t\t\twriteln (tGet (r1, c1) == tGet (r2, c2) ?\n\t\t\t\t \"Yes\" : \"No\");\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tr1 -= 1;\n\t\t\t\tc1 -= 1;\n\t\t\t\tint [4] c = [r1, c1, r2, c2];\n\t\t\t\tif (c !in h)\n\t\t\t\t{\n\t\t\t\t\th[c] = uniform !(ulong) ();\n\t\t\t\t}\n\t\t\t\ttMark (r1, c1, r2, c2, h[c]);\n\t\t\t}\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int logHalf = 12;\nimmutable int half = 1 << logHalf;\nimmutable int limit = half + 2505;\n\nulong [] [] v;\n\nulong tGet (int r0, int c0)\n{\n\tr0 += half;\n\tc0 += half;\n\tulong res = 0;\n\tfor (int r = r0; r > 0; r >>= 1)\n\t{\n\t\tfor (int c = c0; c > 0; c >>= 1)\n\t\t{\n\t\t\tres ^= v[r][c];\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid tMark (int r1, int c1, int r2, int c2, ulong h)\n{\n\tr1 += half;\n\tc1 += half;\n\tr2 += half;\n\tc2 += half;\n\tfor (int rLo = r1, rHi = r2; rLo < rHi; rLo >>= 1, rHi >>= 1)\n\t{\n\t\tif ((rHi & 1) == 1)\n\t\t{\n\t\t\trHi -= 1;\n\t\t\tfor (int cLo = c1, cHi = c2; cLo < cHi;\n\t\t\t cLo >>= 1, cHi >>= 1)\n\t\t\t{\n\t\t\t\tif ((cHi & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tcHi -= 1;\n\t\t\t\t\tv[rHi][cHi] ^= h;\n\t\t\t\t}\n\t\t\t\tif ((cLo & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tv[rHi][cLo] ^= h;\n\t\t\t\t\tcLo += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif ((rLo & 1) == 1)\n\t\t{\n\t\t\tfor (int cLo = c1, cHi = c2; cLo < cHi;\n\t\t\t cLo >>= 1, cHi >>= 1)\n\t\t\t{\n\t\t\t\tif ((cHi & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tcHi -= 1;\n\t\t\t\t\tv[rLo][cHi] ^= h;\n\t\t\t\t}\n\t\t\t\tif ((cLo & 1) == 1)\n\t\t\t\t{\n\t\t\t\t\tv[rLo][cLo] ^= h;\n\t\t\t\t\tcLo += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\trLo += 1;\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\tint n, m, q;\n\twhile (readf (\" %s %s %s\", &n, &m, &q) > 0)\n\t{\n\t\tv = new ulong [] [] (limit, limit);\n\t\tulong [int [4]] h;\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint t, r1, c1, r2, c2;\n\t\t\treadf (\" %s %s %s %s %s\", &t, &r1, &c1, &r2, &c2);\n\t\t\tif (t == 3)\n\t\t\t{\n\t\t\t\twriteln (tGet (r1, c1) == tGet (r2, c2) ?\n\t\t\t\t \"Yes\" : \"No\");\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tr1 -= 1;\n\t\t\t\tc1 -= 1;\n\t\t\t\tint [4] c = [r1, c1, r2, c2];\n\t\t\t\tif (c !in h)\n\t\t\t\t{\n\t\t\t\t\th[c] = uniform !(ulong) ();\n\t\t\t\t}\n\t\t\t\ttMark (r1, c1, r2, c2, h[c]);\n\t\t\t}\n\t\t}\n\t}\n}\n"}], "src_uid": "263e0f82e7c8c749f5699d19b99ef418"} {"nl": {"description": "We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.", "input_spec": "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 2 \\cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print \"Yes\" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and \"No\" (without quotes) otherwise. You can output \"Yes\" and \"No\" in any case (for example, strings \"yEs\", \"yes\" and \"Yes\" will be recognized as a positive response).", "sample_inputs": ["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"], "sample_outputs": ["No\nYes\nNo\nNo\nYes\nYes\nYes"], "notes": "NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\\langle \\underline{0}, 0, 0, 0\\rangle \\to \\langle 1, \\underline{0}, 0, 0 \\rangle \\to \\langle \\underline{1}, -1, 0, 0\\rangle \\to \\langle 2, \\underline{-1}, 0, 0\\rangle \\to \\langle 2, 0, \\underline{0}, 0\\rangle \\to \\langle 2, \\underline{0}, -1, 0\\rangle \\to \\langle \\underline{2}, -1, -1, 0\\rangle$$$"}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nlong[] A;\n\nbool solve() {\n long b;\n foreach (i; 0 .. N) {\n const c = A[i] + b;\n if (c < 0) {\n return false;\n } else if (c == 0) {\n foreach (j; i + 1 .. N) {\n if (A[j] != 0) {\n return false;\n }\n }\n return true;\n } else {\n b = c;\n }\n }\n return (b == 0);\n}\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n N = readInt;\n A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong;\n }\n \n const ans = solve;\n writeln(ans ? \"Yes\" : \"No\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\twhile (!a.empty && !a.back)\r\n\t\t{\r\n\t\t\ta.popBack ();\r\n\t\t}\r\n\t\tlong cur = 0;\r\n\t\tint good = 0;\r\n\t\tint bad = 0;\r\n\t\tforeach (ref x; a)\r\n\t\t{\r\n\t\t\tcur += x;\r\n\t\t\tgood += (cur >= 0);\r\n\t\t\tbad += (cur == 0);\r\n\t\t}\r\n\t\tauto res = (good == a.length && bad <= 1 && cur == 0);\r\n\t\twriteln (res ? \"YES\" : \"NO\");\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "9f0ffcbd0ce62a365a1ecbb4a2c1de3e"} {"nl": {"description": "There are $$$n$$$ heaps of stone. The $$$i$$$-th heap has $$$h_i$$$ stones. You want to change the number of stones in the heap by performing the following process once: You go through the heaps from the $$$3$$$-rd heap to the $$$n$$$-th heap, in this order. Let $$$i$$$ be the number of the current heap. You can choose a number $$$d$$$ ($$$0 \\le 3 \\cdot d \\le h_i$$$), move $$$d$$$ stones from the $$$i$$$-th heap to the $$$(i - 1)$$$-th heap, and $$$2 \\cdot d$$$ stones from the $$$i$$$-th heap to the $$$(i - 2)$$$-th heap. So after that $$$h_i$$$ is decreased by $$$3 \\cdot d$$$, $$$h_{i - 1}$$$ is increased by $$$d$$$, and $$$h_{i - 2}$$$ is increased by $$$2 \\cdot d$$$. You can choose different or same $$$d$$$ for different operations. Some heaps may become empty, but they still count as heaps. What is the maximum number of stones in the smallest heap after the process?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 2\\cdot 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$). The second lines of each test case contains $$$n$$$ integers $$$h_1, h_2, h_3, \\ldots, h_n$$$ ($$$1 \\le h_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the maximum number of stones that the smallest heap can contain.", "sample_inputs": ["4\n4\n1 2 10 100\n4\n100 100 100 1\n5\n5 1 1 1 8\n6\n1 2 3 4 5 6"], "sample_outputs": ["7\n1\n1\n3"], "notes": "NoteIn the first test case, the initial heap sizes are $$$[1, 2, 10, 100]$$$. We can move the stones as follows. move $$$3$$$ stones and $$$6$$$ from the $$$3$$$-rd heap to the $$$2$$$-nd and $$$1$$$ heap respectively. The heap sizes will be $$$[7, 5, 1, 100]$$$; move $$$6$$$ stones and $$$12$$$ stones from the last heap to the $$$3$$$-rd and $$$2$$$-nd heap respectively. The heap sizes will be $$$[7, 17, 7, 82]$$$. In the second test case, the last heap is $$$1$$$, and we can not increase its size.In the third test case, it is better not to move any stones.In the last test case, the final achievable configuration of the heaps can be $$$[3, 5, 3, 4, 3, 3]$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto h = RDA;\r\n\r\n\t\tbool f(long x)\r\n\t\t{\r\n\t\t\tauto hh = h.dup;\r\n\t\t\tforeach_reverse (i; 2..n)\r\n\t\t\t{\r\n\t\t\t\tif (hh[i] < x) return false;\r\n\t\t\t\tauto rem = hh[i] - x;\r\n\t\t\t\tauto d = min(rem / 3, h[i] / 3);\r\n\t\t\t\thh[i-1] += d;\r\n\t\t\t\thh[i-2] += d*2;\r\n\t\t\t}\r\n\t\t\treturn (hh[0] >= x) && (hh[1] >= x);\r\n\t\t}\r\n\t\tans[ti] = binarySearch!(f)(0, 10L^^10);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,\n std.container, std.typecons, std.conv, std.random, std.bigint;\n\nvoid read(S...)(ref S args) {\n auto input = readln.split;\n enforce(input.length == args.length);\n foreach (i, ref arg; args) {\n arg = input[i].to!(S[i]);\n }\n}\n\nauto list(T)() { return readln.split.map!(e => e.to!T).array; }\n\nbool possible(long[] arr, long answer) {\n auto original = arr.dup;\n\n foreach_reverse(i; 2 .. arr.length) {\n if (arr[i] < answer) return false;\n long d = min(arr[i] - answer, original[i]) / 3;\n arr[i - 1] += d;\n arr[i - 2] += 2 * d;\n }\n\n return arr[0] >= answer && arr[1] >= answer;\n}\n\nvoid main() {\n int T;\n read(T);\n\n while (T--) {\n int n;\n read(n);\n auto arr = list!long;\n long low = 0, high = int.max;\n\n while (high != low) {\n long mid = (high + low) / 2;\n if (mid == low) mid = high;\n\n if (possible(arr.dup, mid)) low = mid;\n else high = mid - 1;\n }\n\n writeln(low);\n }\n}\n\n"}, {"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nvoid solve() {\r\n int N = read!int;\r\n auto H = readarray!int;\r\n\r\n bool C(int x) {\r\n auto G = H.dup;\r\n for (int i = N - 1; i >= 2; i--) {\r\n if (G[i] < x) return false;\r\n int d = min(H[i] / 3, (G[i] - x) / 3);\r\n G[i - 1] += d;\r\n G[i - 2] += 2*d;\r\n }\r\n if (G[0] < x || G[1] < x) return false;\r\n return true;\r\n }\r\n int lb = 0, ub = 1_000_000_001;\r\n while (lb + 1 < ub) {\r\n int mid = (lb + ub) / 2;\r\n (C(mid) ? lb : ub) = mid;\r\n }\r\n writeln(lb);\r\n}\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!long;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!long(N);\r\n\r\n bool isOK(long x) {\r\n auto a = A.dup;\r\n foreach_reverse(int i; 2..N) {\r\n const d = max(0, min(A[i], a[i] - x) / 3);\r\n a[i - 2] += d * 2;\r\n a[i - 1] += d;\r\n a[i] -= d * 3;\r\n }\r\n\r\n // deb(x, a);\r\n return a.all!(z => z >= x);\r\n }\r\n\r\n return binarySearch(&isOK, 1, 10L^^9 + 2);\r\n }\r\n\r\n foreach(_; 0..QN) subSolve().writeln;\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n\r\nK binarySearch(K)(bool delegate(K) cond, K l, K r) { return binarySearch((K k) => k, cond, l, r); }\r\nT binarySearch(T, K)(K delegate(T) fn, bool delegate(K) cond, T l, T r) {\r\n auto ok = l;\r\n auto ng = r;\r\n const T TWO = 2;\r\n \r\n bool again() {\r\n static if (is(T == float) || is(T == double) || is(T == real)) {\r\n return !ng.approxEqual(ok, 1e-08, 1e-08);\r\n } else {\r\n return abs(ng - ok) > 1;\r\n }\r\n }\r\n \r\n while(again()) {\r\n const half = (ng + ok) / TWO;\r\n const halfValue = fn(half);\r\n \r\n if (cond(halfValue)) {\r\n ok = half;\r\n } else {\r\n ng = half;\r\n }\r\n }\r\n \r\n return ok;\r\n}"}, {"source_code": "// 提出解\r\nvoid solve(){\r\n\tforeach(_; 0 .. scan!int){\r\n\t\tint n = scan!int;\r\n\t\tlong[] hs = scan!long(n);\r\n\r\n\t\tbool isOK(long k){\r\n\t\t\tlog(\"k:\", k);\r\n\t\t\tlong[] us = hs.dup;\r\n\t\t\tforeach_reverse(i; 2 .. n){\r\n\t\t\t\tif(us[i] <= k) continue;\r\n\t\t\t\tlong d = min(hs[i], us[i] - k) / 3;\r\n\t\t\t\tus[i - 2] += d * 2, us[i - 1] += d, us[i] -= d * 3;\r\n\t\t\t\tlog(\"i:\", i, \"d:\", d, \"us:\", us);\r\n\t\t\t}\r\n\t\t\tlog(\"us:\", us);\r\n\t\t\tforeach(u; us) if(u < k) return 0;\r\n\t\t\tlog(\"k:\", k, \"-> OK\");\r\n\t\t\treturn 1;\r\n\t\t}\r\n\r\n\t\tlong high = hs.reduce!max + 1;\r\n\t\tlong ans = mid(0, high, &isOK) - 1;\r\n\r\n\t\tans.print;\r\n\r\n\t}\r\n}\r\n\r\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\r\n// 愚直解\r\nvoid jury(){\r\n}\r\n\r\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\r\n// テストケース\r\nvoid gen(){\r\n}\r\n\r\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\r\n// テンプレ\r\nimport std;\r\nbool DEBUG = 0;\r\nvoid main(string[] args){\r\n\tif(args.canFind(\"-debug\")) DEBUG = 1;\r\n\tif(args.canFind(\"-gen\")) gen; else if(args.canFind(\"-jury\")) jury; else solve;\r\n}\r\nvoid log(A ...)(lazy A a){ if(DEBUG) print(a); }\r\nvoid print(){ writeln(\"\"); }\r\nvoid print(T)(T t){ writeln(t); }\r\nvoid print(T, A ...)(T t, A a){ stdout.write(t, \" \"), print(a); }\r\nstring unsplit(T)(T xs, string d = \" \"){ return xs.array.to!(string[]).join(d); }\r\nstring scan(){\r\n\tstatic string[] ss; while(!ss.length) ss = readln.chomp.split;\r\n\tstring res = ss[0]; ss.popFront; return res;\r\n}\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }\r\nT mini(T)(ref T x, T y){ if(x > y) x = y; return x; }\r\nT maxi(T)(ref T x, T y){ if(x < y) x = y; return x; }\r\nT mid(T)(T l, T r, bool delegate(T) f){\r\n\tT m = (l + r) / 2; (f(m)? l: r) = m; return f(l)? f(r - 1)? r: mid(l, r, f): l;\r\n}\r\n\r\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\r\n// ライブラリ(基本)\r\n\r\nclass UnionFind{\r\n\tthis(int n){ foreach(i; 0 .. n) R ~= i, K ~= [i]; } int[] R; int[][] K;\r\n\tvoid unite(int a, int b){\r\n\t\tint ra = R[a], rb = R[b]; if(ra == rb) return;\r\n\t\tif(K[ra].length < K[rb].length) unite(b, a);\r\n\t\telse foreach(k; K[rb]) R[k] = ra, K[ra] ~= k;\r\n\t}\r\n\tint find(int a){ return R[a]; }\r\n\tint getSize(int a){ return K[R[a]].length.to!int; }\r\n}\r\nclass Queue(T){\r\n\tprivate T[] xs; private uint i, j;\r\n\tthis(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }\r\n\tuint length(){ return j - i; }\r\n\tbool isEmpty(){ return j == i; } \r\n\tvoid enq(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }\r\n\tT deq(){ assert(i < j); return xs[i ++]; }\r\n\tT peek(){ assert(i < j); return xs[i]; }\r\n\tvoid flush(){ i = j; }\r\n\talias empty = isEmpty, front = peek, popFront = deq, pop = deq, push = enq, top = peek;\r\n\tQueue opOpAssign(string op)(T x) if(op == \"~\"){ enq(x); return this; }\r\n\tT opIndex(uint li){ assert(i + li < j); return xs[i + li]; }\r\n\tstatic Queue!T opCall(T[] xs){ return new Queue!T(xs); }\r\n\tT[] array(){ return xs[i .. j]; }\r\n\toverride string toString(){ return array.to!string; }\r\n}\r\nclass Stack(T){\r\n\tprivate T[] xs; private uint j;\r\n\tthis(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }\r\n\tuint length(){ return j; }\r\n\tbool isEmpty(){ return j == 0; }\r\n\tvoid push(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }\r\n\tT pop(){ assert(j > 0); return xs[-- j]; }\r\n\tT peek(){ assert(j > 0); return xs[j - 1]; }\r\n\tvoid clear(){ j = 0; }\r\n\talias empty = isEmpty, front = peek, popFront = pop, top = peek;\r\n\tStack opOpAssign(string op)(T x) if(op == \"~\"){ push(x); return this; }\r\n\tT opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }\r\n\tstatic Stack!T opCall(T[] xs = []){ return new Stack!T(xs); }\r\n\tT[] array(){ return xs[0 .. j]; }\r\n\toverride string toString(){ return array.to!string; }\r\n}\r\n\r\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\r\n// ライブラリ(追加)\r\n\r\n\r\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto h = RDA;\r\n\r\n\t\tbool f(long x)\r\n\t\t{\r\n\t\t\tauto hh = h.dup;\r\n\t\t\tforeach_reverse (i; 2..n)\r\n\t\t\t{\r\n\t\t\t\tif (hh[i] < x) return false;\r\n\t\t\t\tauto rem = hh[i] - x;\r\n\t\t\t\tauto d = min(rem / 3, h[i] / 3);\r\n\t\t\t\thh[i-1] += d;\r\n\t\t\t\thh[i-2] += d*2;\r\n\t\t\t}\r\n\t\t\treturn (hh[0] >= x) && (hh[1] >= x);\r\n\t\t}\r\n\t\tans[ti] = binarySearch!(f)(-1L, 2L^^10);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto h = RDA;\r\n\r\n\t\tbool f(long x)\r\n\t\t{\r\n\t\t\tauto hh = h.dup;\r\n\t\t\tforeach_reverse (i; 2..n)\r\n\t\t\t{\r\n\t\t\t\tif (hh[i] < x) return false;\r\n\t\t\t\tauto rem = hh[i] - x;\r\n\t\t\t\tauto d = min(rem / 3, h[i] / 3);\r\n\t\t\t\thh[i-1] += d;\r\n\t\t\t\thh[i-2] += d*2;\r\n\t\t\t}\r\n\t\t\treturn (hh[0] >= x) && (hh[1] >= x);\r\n\t\t}\r\n\t\tans[ti] = binarySearch!(f)(0, 2L^^10);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto h = RDA;\r\n\r\n\t\tbool f(long x)\r\n\t\t{\r\n\t\t\tauto hh = h.dup;\r\n\t\t\tforeach_reverse (i; 2..n)\r\n\t\t\t{\r\n\t\t\t\tif (hh[i] < x) return false;\r\n\t\t\t\tauto rem = hh[i] - x;\r\n\t\t\t\tauto d = min(rem / 3, h[i] / 3);\r\n\t\t\t\thh[i-1] += d;\r\n\t\t\t\thh[i-2] += d*2;\r\n\t\t\t}\r\n\t\t\treturn hh[0] >= x && hh[1] >= x;\r\n\t\t}\r\n\t\tans[ti] = binarySearch!(f)(0, 2L^^10);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nT binarySearch(alias pred, T)(T ok, T ng)\r\n{ \r\n\twhile (abs(ok-ng) > 1)\r\n\t{\r\n\t\tauto mid = (ok+ng)/2;\r\n\t\tif (unaryFun!pred(mid)) ok = mid;\r\n\t\telse ng = mid;\r\n\t}\r\n\treturn ok;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto h = RDA;\r\n\r\n\t\tbool f(long x)\r\n\t\t{\r\n\t\t\tauto hh = h.dup;\r\n\t\t\tforeach_reverse (i; 2..n)\r\n\t\t\t{\r\n\t\t\t\tauto rem = hh[i] - x;\r\n\t\t\t\tif (rem < 0) return false;\r\n\t\t\t\tauto d = min(rem / 3, h[i] / 3);\r\n\t\t\t\thh[i-1] += d;\r\n\t\t\t\thh[i-2] += d*2;\r\n\t\t\t}\r\n\t\t\treturn hh[0] >= x && hh[1] >= x;\r\n\t\t}\r\n\t\tans[ti] = binarySearch!(f)(0, 2L^^9+1);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,\n std.container, std.typecons, std.conv, std.random, std.bigint;\n\nvoid read(S...)(ref S args) {\n auto input = readln.split;\n enforce(input.length == args.length);\n foreach (i, ref arg; args) {\n arg = input[i].to!(S[i]);\n }\n}\n\nauto list(T)() { return readln.split.map!(e => e.to!T).array; }\n\nbool possible(long[] arr, long answer) {\n arr = [0L, 0L] ~ arr;\n auto original = arr.dup;\n\n foreach_reverse(i; 2 .. arr.length) {\n if (arr[i] < answer) return false;\n long d = min(arr[i] - answer, original[i]) / 3;\n arr[i - 1] += d;\n arr[i - 2] += 2 * d;\n arr[i] -= 3 * d;\n }\n\n return true;\n}\n\nvoid main() {\n int T;\n read(T);\n\n while (T--) {\n int n;\n read(n);\n auto arr = list!long;\n long low = 0, high = int.max;\n\n while (high != low) {\n long mid = (high + low) / 2;\n if (mid == low) mid = high;\n\n if (possible(arr, mid)) low = mid;\n else high = mid - 1;\n }\n\n writeln(low);\n }\n}\n\n"}], "src_uid": "895d5850e420a36ae3b5f0a50e359423"} {"nl": {"description": "Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \\leq n \\leq 10^9$$$).", "output_spec": "For each test case, print \"Ashishgup\" if he wins, and \"FastestFinger\" otherwise (without quotes).", "sample_inputs": ["7\n1\n2\n3\n4\n5\n6\n12"], "sample_outputs": ["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"], "notes": "NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto N = readln.chomp.to!long;\n if (N == 1) {\n writeln(\"FastestFinger\");\n continue;\n }\n if (N == 2 || N%2 == 1) {\n writeln(\"Ashishgup\");\n continue;\n }\n auto n = N;\n int t;\n while (n%2 == 0) {\n ++t;\n n /= 2;\n }\n if (n == 1) {\n writeln(\"FastestFinger\");\n continue;\n }\n if (t > 1) {\n writeln(\"Ashishgup\");\n continue;\n }\n\n int c;\n for (long k = 3; k^^2 <= N; k += 2) {\n while (n%k == 0) {\n ++c;\n n /= k;\n }\n }\n if (n != 1) {\n ++c;\n }\n writeln(c == 1 ? \"FastestFinger\": \"Ashishgup\");\n }\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nbool isPrime (int n)\n{\n\tif (n < 2)\n\t{\n\t\treturn false;\n\t}\n\tfor (int d = 2; d * d <= n; d++)\n\t{\n\t\tif (n % d == 0)\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nbool solve (int n)\n{\n\tint p2 = bsf (n);\n\tint rem = n >> p2;\n\tif (p2 == 0)\n\t{\n\t\treturn rem > 1;\n\t}\n\telse if (p2 == 1)\n\t{\n\t\treturn !isPrime (rem);\n\t}\n\telse\n\t{\n\t\treturn rem > 1;\n\t}\n}\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\twriteln (solve (n) ? \"Ashishgup\" : \"FastestFinger\");\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new bool[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD;\n\n\t\tbool dfs(long x, bool turn)\n\t\t{\n\t\t\tif (x == 1) return !turn;\n\t\t\tif (x == 2 || x % 2) return turn;\n\n\t\t\tbool res = !turn;\n\t\t\tfor (long i = 3; i*i <= x; ++i)\n\t\t\t{\n\t\t\t\tif (x % i) continue;\n\t\t\t\tif (i % 2)\n\t\t\t\t\tres |= dfs(x/i, !turn) == turn;\n\t\t\t\tif ((x / i) % 2)\n\t\t\t\t\tres |= dfs(i, !turn) == turn;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\t\tans[ti] = dfs(n, true);\n\t}\n\t\n\tforeach (e; ans)\n\t{\n\t\twriteln(e ? \"Ashishgup\" : \"FastestFinger\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_t; 0..T) {\n auto N = readln.chomp.to!long;\n if (N == 1) {\n writeln(\"FastestFinger\");\n continue;\n }\n if (N == 2 || N%2 == 1) {\n writeln(\"Ashishgup\");\n continue;\n }\n auto n = N;\n int t;\n while (n%2 == 0) {\n ++t;\n n /= 2;\n }\n if (n == 1) {\n writeln(\"FastestFinger\");\n continue;\n }\n if (t > 1) {\n writeln(\"Ashishgup\");\n continue;\n }\n\n int c;\n for (long k = 3; k^^2 <= N; k += 2) {\n while (n%k == 0) {\n ++c;\n n /= k;\n }\n }\n if (n%2 != 0 && n != 1) {\n ++c;\n }\n writeln(c%2 == 1 ? \"FastestFinger\": \"Ashishgup\");\n }\n}"}], "src_uid": "b533572dd6d5fe7350589c7f4d5e1c8c"} {"nl": {"description": "We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings \"ab\" in the string and replace it with the string \"bba\". If we have no \"ab\" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.The string \"ab\" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.", "input_spec": "The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.", "output_spec": "Print the minimum number of steps modulo 109 + 7.", "sample_inputs": ["ab", "aab"], "sample_outputs": ["1", "3"], "notes": "NoteThe first example: \"ab\"  →  \"bba\".The second example: \"aab\"  →  \"abba\"  →  \"bbaba\"  →  \"bbbbaa\"."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nlong powmod(long a, long x, long m) {\n long ret = 1;\n while (x) {\n if (x % 2) ret = ret * a % m;\n a = a * a % m;\n x /= 2;\n }\n return ret;\n}\n\nvoid main() {\n immutable long MOD = 10^^9 + 7;\n\n auto S = readln.chomp;\n auto N = S.length.to!int;\n\n long a = 0;\n long b = 0;\n long ans = 0;\n char prev = 'x';\n foreach (i; 0..N) {\n if (S[i] == 'a' && prev == 'b') {\n ans += b * (powmod(2, a, MOD) - 1) % MOD;\n ans = (ans + MOD) % MOD;\n a++;\n b = 0;\n }\n else if (S[i] == 'a') {\n a++;\n }\n else {\n b++;\n }\n\n prev = S[i];\n }\n\n ans += b * (powmod(2, a, MOD) - 1) % MOD;\n ans = (ans + MOD) % MOD;\n\n ans.writeln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nlong powmod(long a, long x, long m) {\n long ret = 1;\n while (x) {\n if (x % 2) ret = ret * a % m;\n a = a * a % m;\n x /= 2;\n }\n return ret;\n}\n\nvoid main() {\n immutable long MOD = 10^^9 + 7;\n\n auto S = readln.chomp;\n auto N = S.length.to!int;\n\n long a = 0;\n long b = 0;\n long ans = 0;\n char prev = 'x';\n foreach (i; 0..N) {\n if (S[i] == 'a' && prev == 'b') {\n ans += b * powmod(2, a, MOD) % MOD - 1;\n ans = (ans + MOD) % MOD;\n a++;\n b = 0;\n }\n else if (S[i] == 'a') {\n a++;\n }\n else {\n b++;\n }\n\n prev = S[i];\n }\n\n ans += b * powmod(2, a, MOD) % MOD - 1;\n ans = (ans + MOD) % MOD;\n\n ans.writeln;\n}\n"}], "src_uid": "8f52241c690ec4f9af71a52904fb19a0"} {"nl": {"description": "Once archaeologists found m mysterious papers, each of which had a pair of integers written on them. Ancient people were known to like writing down the indexes of the roads they walked along, as «a b» or «b a», where a, b are the indexes of two different cities joint by the road . It is also known that the mysterious papers are pages of two travel journals (those days a new journal was written for every new journey).During one journey the traveler could walk along one and the same road several times in one or several directions but in that case he wrote a new entry for each time in his journal. Besides, the archaeologists think that the direction the traveler took on a road had no effect upon the entry: the entry that looks like «a b» could refer to the road from a to b as well as to the road from b to a.The archaeologists want to put the pages in the right order and reconstruct the two travel paths but unfortunately, they are bad at programming. That’s where you come in. Go help them!", "input_spec": "The first input line contains integer m (1 ≤ m ≤ 10000). Each of the following m lines describes one paper. Each description consists of two integers a, b (1 ≤ a, b ≤ 10000, a ≠ b).", "output_spec": "In the first line output the number L1. That is the length of the first path, i.e. the amount of papers in its description. In the following line output L1 space-separated numbers — the indexes of the papers that describe the first path. In the third and fourth lines output similarly the length of the second path L2 and the path itself. Both paths must contain at least one road, i.e. condition L1 > 0 and L2 > 0 must be met. The papers are numbered from 1 to m according to the order of their appearance in the input file. The numbers should be output in the order in which the traveler passed the corresponding roads. If the answer is not unique, output any. If it’s impossible to find such two paths, output «-1». Don’t forget that each paper should be used exactly once, i.e L1 + L2 = m.", "sample_inputs": ["2\n4 5\n4 3", "1\n1 2"], "sample_outputs": ["1\n2 \n1\n1", "-1"], "notes": null}, "positive_code": [{"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint root(int[] uf, int u) {\n\treturn (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool conn(int[] uf, int u, int v) {\n\tu = uf.root(u);\n\tv = uf.root(v);\n\tif (u == v) return 0;\n\tif (uf[u] > uf[v]) swap(u, v);\n\tuf[u] += uf[v];\n\tuf[v] = u;\n\treturn 1;\n}\n\nimmutable N = 10005;\nint M;\nint[] A, B;\n\nint[][] G;\nint[] deg;\n\nbool[] used;\nint[] path;\nvoid dfs(int u) {\n\tforeach (i; G[u]) {\n\t\tif (!used[i]) {\n\t\t\tused[i] = true;\n\t\t\tdfs(A[i] ^ B[i] ^ u);\n\t\t\tpath ~= i;\n\t\t}\n\t}\n}\n\nint[] eulerianPath(int[] us) {\n\tint[] odds;\n\tforeach (u; us) {\n\t\tif (deg[u] % 2 != 0) {\n\t\t\todds ~= u;\n\t\t}\n\t}\n\tswitch (odds.length) {\n\t\tcase 0: {\n\t\t\tpath.clear;\n\t\t\tdfs(us[0]);\n\t\t\treturn path;\n\t\t} break;\n\t\tcase 2: {\n\t\t\tpath.clear;\n\t\t\tdfs(odds[0]);\n\t\t\treturn path;\n\t\t} break;\n\t\tdefault: {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n\nint[][] solve() {\n\tG = new int[][N];\n\tdeg = new int[N];\n\tforeach (i; 0 .. M) {\n\t\tG[A[i]] ~= i;\n\t\tG[B[i]] ~= i;\n\t\t++deg[A[i]];\n\t\t++deg[B[i]];\n\t}\n\tused = new bool[M];\n\t\n\tif (M < 2) {\n\t\treturn null;\n\t}\n\t\n\tint[] uf = new int[N];\n\tuf[] = -1;\n\tforeach (i; 0 .. M) {\n\t\tuf.conn(A[i], B[i]);\n\t}\n\tint[][] us = new int[][N];\n\tforeach (u; 0 .. N) {\n\t\tus[uf.root(u)] ~= u;\n\t}\n\tint[][] edges = new int[][N];\n\tforeach (i; 0 .. M) {\n\t\tedges[uf.root(A[i])] ~= i;\n\t}\n\tconst numCompos = edges.count!\"!a.empty\";\n\tif (numCompos == 1) {\n\t\tint[] odds;\n\t\tforeach (u; 0 .. N) {\n\t\t\tif (deg[u] % 2 != 0) {\n\t\t\t\todds ~= u;\n\t\t\t}\n\t\t}\n\t\tswitch (odds.length) {\n\t\t\tcase 0: {\n\t\t\t\tpath.clear;\n\t\t\t\tdfs(A[0]);\n\t\t\t\treturn [ path[0 .. 1], path[1 .. $] ];\n\t\t\t} break;\n\t\t\tcase 2: {\n\t\t\t\tpath.clear;\n\t\t\t\tdfs(odds[0]);\n\t\t\t\treturn [ path[0 .. 1], path[1 .. $] ];\n\t\t\t} break;\n\t\t\tcase 4: {\n\t\t\t\tA ~= odds[0];\n\t\t\t\tB ~= odds[1];\n\t\t\t\tG[A[M]] ~= M;\n\t\t\t\tG[B[M]] ~= M;\n\t\t\t\tused ~= false;\n\t\t\t\tpath.clear;\n\t\t\t\tdfs(odds[2]);\n\t\t\t\tconst int idx = path.length - path.find(M).length;\n\t\t\t\treturn [ path[0 .. idx], path[idx + 1 .. $] ];\n\t\t\t} break;\n\t\t\tdefault: {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\t} else if (numCompos == 2) {\n\t\tint[][] ret;\n\t\tforeach (r; 0 .. N) if (!edges[r].empty) {\n\t\t\tif (auto res = us[r].eulerianPath) {\n\t\t\t\tret ~= res;\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t} else {\n\t\treturn null;\n\t}\n}\n\nvoid main(string[] args) {\n\tauto inp = File(\"input.txt\", \"r\");\n\tauto outp = File(\"output.txt\", \"w\");\n\tfor (string line; (line = inp.readln) != null; ) {\n\t\tM = line.chomp.to!int;\n\t\tA = new int[M];\n\t\tB = new int[M];\n\t\tforeach (i; 0 .. M) {\n\t\t\tauto tks = inp.readln.split;\n\t\t\tA[i] = tks[0].to!int;\n\t\t\tB[i] = tks[1].to!int;\n\t\t}\ndebug{\nwriteln(\"A = \",A);\nwriteln(\"B = \",B);\n}\n\t\t\n\t\tif (auto res = solve) {\ndebug{\nwriteln(\"res = \",res);\n}\n\t\t\tforeach (path; res) {\n\t\t\t\toutp.writeln(path.length);\n\t\t\t\tpath[] += 1;\n\t\t\t\toutp.writeln(path.to!string.removechars(\"[],\"));\n\t\t\t}\n\t\t} else {\n\t\t\toutp.writeln(-1);\n\t\t}\n\t}\n}\n\n"}], "negative_code": [], "src_uid": "d3744b78b3f14486bf3dd289156f527d"} {"nl": {"description": "Mocha is a young girl from high school. She has learned so much interesting knowledge from her teachers, especially her math teacher. Recently, Mocha is learning about binary system and very interested in bitwise operation.This day, Mocha got a sequence $$$a$$$ of length $$$n$$$. In each operation, she can select an arbitrary interval $$$[l, r]$$$ and for all values $$$i$$$ ($$$0\\leq i \\leq r-l$$$), replace $$$a_{l+i}$$$ with $$$a_{l+i} \\,\\&\\, a_{r-i}$$$ at the same time, where $$$\\&$$$ denotes the bitwise AND operation. This operation can be performed any number of times.For example, if $$$n=5$$$, the array is $$$[a_1,a_2,a_3,a_4,a_5]$$$, and Mocha selects the interval $$$[2,5]$$$, then the new array is $$$[a_1,a_2\\,\\&\\, a_5, a_3\\,\\&\\, a_4, a_4\\,\\&\\, a_3, a_5\\,\\&\\, a_2]$$$.Now Mocha wants to minimize the maximum value in the sequence. As her best friend, can you help her to get the answer?", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$).", "output_spec": "For each test case, print one integer — the minimal value of the maximum value in the sequence.", "sample_inputs": ["4\n2\n1 2\n3\n1 1 3\n4\n3 11 3 7\n5\n11 7 15 3 7"], "sample_outputs": ["0\n1\n3\n3"], "notes": "NoteIn the first test case, Mocha can choose the interval $$$[1,2]$$$, then the sequence becomes $$$[ 0, 0]$$$, where the first element is $$$1\\,\\&\\,2$$$, and the second element is $$$2\\,\\&\\,1$$$.In the second test case, Mocha can choose the interval $$$[1,3]$$$, then the sequence becomes $$$[ 1,1,1]$$$, where the first element is $$$1\\,\\&\\,3$$$, the second element is $$$1\\,\\&\\,1$$$, and the third element is $$$3\\,\\&\\,1$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto T = RD!int;\r\n\tauto ans = new long[](T);\r\n\tforeach (ti; 0..T)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = RDA;\r\n\r\n\t\tans[ti] = a[0];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tans[ti] &= a[i];\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "\nimmutable multi = true;\n\nvoid solve(int tc)\n{\n\tauto n = readInt!int;\n\tauto a = ma(n, readInt!int);\n\ta.fold!((ai, aj) => ai&aj).writeln;\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n int n = scan!int;\n ll[] arr = scanArray;\n ll minn = arr[0];\n for(int i = 0; i < n; ++i){\n minn = minn & arr[i];\n }\n writeln(minn);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}], "negative_code": [], "src_uid": "4f8eac547bbd469a69de2f4aae4a87f0"} {"nl": {"description": "This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers $$$a_1, a_2, \\dots, a_n$$$.A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.Let's denote a function that alternates digits of two numbers $$$f(a_1 a_2 \\dots a_{p - 1} a_p, b_1 b_2 \\dots b_{q - 1} b_q)$$$, where $$$a_1 \\dots a_p$$$ and $$$b_1 \\dots b_q$$$ are digits of two integers written in the decimal notation without leading zeros.In other words, the function $$$f(x, y)$$$ alternately shuffles the digits of the numbers $$$x$$$ and $$$y$$$ by writing them from the lowest digits to the older ones, starting with the number $$$y$$$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.For example: $$$$$$f(1111, 2222) = 12121212$$$$$$ $$$$$$f(7777, 888) = 7787878$$$$$$ $$$$$$f(33, 44444) = 4443434$$$$$$ $$$$$$f(555, 6) = 5556$$$$$$ $$$$$$f(111, 2222) = 2121212$$$$$$Formally, if $$$p \\ge q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = a_1 a_2 \\dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$; if $$$p < q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = b_1 b_2 \\dots b_{q - p} a_1 b_{q - p + 1} a_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$. Mishanya gives you an array consisting of $$$n$$$ integers $$$a_i$$$, your task is to help students to calculate $$$\\sum_{i = 1}^{n}\\sum_{j = 1}^{n} f(a_i, a_j)$$$ modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$) — the number of elements in the array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the elements of the array.", "output_spec": "Print the answer modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["3\n12 3 45", "2\n123 456"], "sample_outputs": ["12330", "1115598"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.typecons;\nimport std.math : log10, pow;\nimport std.algorithm;\n\nconst int MAX = 100005;\nint MOD = 998244353;\nint n;\nlong[MAX] a;\nint[20] count;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n foreach(i; 0..n) count[numOfDigits(a[i])]++;\n\n long result = 0;\n foreach(i; 0..n){\n foreach(j; 1..11) {\n if (count[j] == 0) continue;\n auto d = min(numOfDigits(a[i]), j);\n auto r = calc(a[i], d);\n auto x = r[0];\n auto y = r[1] % MOD;\n result += (y * 11 * count[j]) % MOD + ((((pow10(d * 2) % MOD) * x) % MOD) * 2 * count[j]) % MOD;\n result %= MOD;\n }\n }\n writeln(result);\n}\n\nauto pow10(long x) {\n return pow(10, x);\n}\n\nauto numOfDigits(long x) {\n if (x == 0) return 1;\n return 1 + cast(int) log10(x);\n}\n\nauto calc(long x, int digits) {\n long base = 1;\n long y = 0;\n int k = 0;\n while(x > 0) {\n auto d = x % 10;\n x = x / 10;\n y += d * base;\n base *= 100;\n k++;\n if (k >= digits) {\n break;\n }\n }\n return tuple(x, y);\n}\n\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nconst long mod = 998_244_353;\n\nvoid solve(){\n\tint n = read.to!int;\n\t\n\tlong[] as;\n\tforeach(i; 0 .. n) as ~= read.to!long;\n\t\n\tlong[][] ws;\n\tforeach(a; as){\n\t\tlong [] w;\n\t\tfor(long x = a; x > 0; x /= 10) w ~= x % 10;\n\t\tws ~= w;\n\t}\n\tlog(\"ws:\", ws);\n\t\n\tint[] ks;\n\tforeach(a; as){\n\t\tint k = 0;\n\t\tlong m = 1;\n\t\twhile(a >= m) k += 1, m *= 10;\n\t\tks ~= k;\n\t}\n\tlog(\"ks:\", ks);\n\t\n\tlong[] ms = [1];\n\tforeach(i; 1 .. 24){\n\t\tms ~= ms[$ - 1] * 10 % mod;\n\t}\n\tlog(\"ms:\", ms);\n\t\n\tlong[] rs;\n\tforeach(i; 0 .. 11){\n\t\tlong r;\n\t\tforeach(k; ks){\n\t\t\tif(i < k) r += 11 * ms[i * 2], r %= mod;\n\t\t\telse r += 2 * ms[k * 2] * ms[i - k], r %= mod;\n\t\t}\n\t\trs ~= r;\n\t}\n\tlog(\"rs:\", rs);\n\t\n\tlong ans;\n\tforeach(w; ws){\n\t\tforeach(i, t; w){\n\t\t\tans += t * rs[i];\n\t\t\tans %= mod;\n\t\t}\n\t}\n\t\n\tans.writeln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.typecons;\nimport std.math : log10, pow;\nimport std.algorithm;\n\nconst int MAX = 100005;\nint MOD = 998244353;\nint n;\nlong[MAX] a;\nint[20] count;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n foreach(i; 0..n) count[numOfDigits(a[i])]++;\n\n long result = 0;\n foreach(i; 0..n){\n foreach(j; 1..11) {\n if (count[j] == 0) continue;\n auto d = min(numOfDigits(a[i]), j);\n auto r = calc(a[i], d);\n auto x = r[0];\n auto y = r[1] % MOD;\n result += (y * 11 * count[j]) % MOD + ((((pow10(d * 2) % MOD) * x) % MOD) * 2 * count[j]) % MOD;\n result %= MOD;\n }\n }\n writeln(result);\n}\n\nauto pow10(int x) {\n return cast(long) pow(10, x);\n}\n\nauto numOfDigits(long x) {\n return 1 + cast(int) log10(x);\n}\n\nauto calc(long x, int digits) {\n long base = 1;\n long y = 0;\n int k = 0;\n while(x > 0) {\n auto d = x % 10;\n x = x / 10;\n y += d * base;\n base *= 100;\n k++;\n if (k >= digits) {\n break;\n }\n }\n return tuple(x, y);\n}\n\n"}, {"source_code": "import std.stdio;\nimport std.typecons;\nimport std.math : log10, pow;\nimport std.algorithm;\n\nconst int MAX = 100005;\nint MOD = 998244353;\nint n;\nlong[MAX] a;\nint[10] count;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n foreach(i; 0..n) count[numOfDigits(a[i])]++;\n\n long result = 0;\n foreach(i; 0..n){\n foreach(j; 1..10) {\n if (count[j] == 0) continue;\n auto d = min(numOfDigits(a[i]), j);\n auto r = calc(a[i], d);\n auto x = r[0];\n auto y = r[1] % MOD;\n result += (y * 11 * count[j]) % MOD + ((((pow10(d * 2) % MOD) * x) % MOD) * 2 * count[j]) % MOD;\n result %= MOD;\n }\n }\n writeln(result);\n}\n\nauto pow10(int x) {\n return cast(long) pow(10, x);\n}\n\nauto numOfDigits(long x) {\n return 1 + cast(int) log10(x);\n}\n\nauto calc(long x, int digits) {\n long base = 1;\n long y = 0;\n int k = 0;\n while(x > 0) {\n auto d = x % 10;\n x = x / 10;\n y += d * base;\n base *= 100;\n k++;\n if (k >= digits) {\n break;\n }\n }\n return tuple(x, y);\n}\n\n"}, {"source_code": "import std.stdio;\nimport std.typecons;\nimport std.math : log10, pow;\nimport std.algorithm;\n\nconst int MAX = 100005;\nint MOD = 998244353;\nint n;\nlong[MAX] a;\nint[10] count;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n foreach(i; 0..n) count[numOfDigits(a[i])]++;\n\n long result = 0;\n foreach(i; 0..n){\n foreach(j; 1..10) {\n if (count[j] == 0) continue;\n auto d = min(numOfDigits(a[i]), j);\n auto r = calc(a[i], d);\n auto x = r[0];\n auto y = r[1] % MOD;\n result += (y * 11 * count[j]) % MOD + ((((pow10(d * 2) % MOD) * x) % MOD) * 2 * count[j]) % MOD;\n result %= MOD;\n }\n }\n writeln(result);\n}\n\nauto pow10(int x) {\n return cast(int) pow(10, x);\n}\n\nauto numOfDigits(long x) {\n return 1 + cast(int) log10(x);\n}\n\nauto calc(long x, int digits) {\n long base = 1;\n long y = 0;\n int k = 0;\n while(x > 0) {\n auto d = x % 10;\n x = x / 10;\n y += d * base;\n base *= 100;\n k++;\n if (k >= digits) {\n break;\n }\n }\n return tuple(x, y);\n}\n\n"}], "src_uid": "b4cd60296083ee2ae8a560209433dcaf"} {"nl": {"description": "Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper.There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not.", "input_spec": "The first line contains a non-empty name that needs to be checked. The name contains at most 105 large English letters. The name will be written with the next sans serif font: ", "output_spec": "Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).", "sample_inputs": ["AHA", "Z", "XO"], "sample_outputs": ["YES", "NO", "NO"], "notes": null}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string[] tk;\n string readString() {\n while (tk.empty) \n tk = readln.split;\n auto tkt = tk.front;\n tk.popFront;\n return tkt;\n }\n int readInt() { \n return readString.to!int; \n }\n double readDouble() { \n return readString.to!double; \n }\n}\n\nvoid main() {\n IO cin;\n int n_Cases = 1;\n // n_Cases = cin.readInt;\n \n\tfor (int case_i = 1; case_i <= n_Cases; case_i++) {\n char[] s = cin.readString.dup;\n char[] r = \"AHIMOTUVWXY\".dup;\n foreach (i; s) {\n if (!r.canFind(i)) {\n writeln(\"NO\");\n return;\n }\n }\n writeln(s.dup.reverse == s.dup ? \"YES\" : \"NO\");\n\t} \n}"}, {"source_code": "module main;\n\nimport std.stdio, std.string, std.algorithm;\n\nauto table = \"AHIMOTUVWXY\";\n\nvoid solve(char[] str)\n{\n int i = 0, j = str.length - 1;\n while (i < j)\n {\n if (str[i] != str[j] || find(table, str[i]) == \"\")\n {\n writefln(\"NO\");\n return;\n }\n ++ i;\n -- j;\n }\n if (i == j && find(table, str[i]) == \"\")\n {\n writefln(\"NO\");\n return;\n }\n writefln(\"YES\");\n}\n\nint main(string[] args)\n{\n char[] str;\n while (stdin.readln(str))\n {\n solve(chomp(str));\n }\n return 0;\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.bigint;\nimport std.conv;\nimport std.exception;\nimport std.math;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int NA = -1;\nimmutable string A = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nimmutable string B = \"AxxxxxxHIxxxMxOxxxxTUVWXYx\";\n\nvoid main ()\n{\n\tchar [dchar] d;\n\tforeach (i; 0..A.length)\n\t{\n\t\td[A[i]] = B[i];\n\t}\n\n\tchar [] s;\n\twhile ((s = to !(char []) (readln ().strip ())) != \"\")\n\t{\n\t\tchar [] b = s.dup;\n\t\treverse (b);\n\t\tchar [] c;\n\t\tforeach (x; b)\n\t\t{\n\t\t\tc ~= d[x];\n\t\t}\n\t\twriteln (s == c ? \"YES\" : \"NO\");\n\t}\n}\n"}], "negative_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string[] tk;\n string readString() {\n while (tk.empty) \n tk = readln.split;\n auto tkt = tk.front;\n tk.popFront;\n return tkt;\n }\n int readInt() { \n return readString.to!int; \n }\n double readDouble() { \n return readString.to!double; \n }\n}\n\nvoid main() {\n IO cin;\n int n_Cases = 1;\n // n_Cases = cin.readInt;\n \n\tfor (int case_i = 1; case_i <= n_Cases; case_i++) {\n char[] s = cin.readString.dup;\n if (s.length == 1) writeln(\"NO\");\n else writeln(s.dup.reverse != s ? \"NO\" : \"YES\");\n\t} \n}"}, {"source_code": "module main;\n\nimport std.stdio, std.string, std.algorithm;\n\nauto table = \"AHIMOTUVWXY\";\n\nvoid solve(char[] str)\n{\n int i = 0, j = str.length - 1;\n while (i < j)\n {\n if (str[i] != str[j])\n {\n writefln(\"NO\");\n return;\n }\n ++ i;\n -- j;\n }\n if (i == j && find(table, str[i]) == \"\")\n {\n writefln(\"NO\");\n return;\n }\n writefln(\"YES\");\n}\n\nint main(string[] args)\n{\n char[] str;\n while (stdin.readln(str))\n {\n solve(chomp(str));\n }\n return 0;\n}"}], "src_uid": "8135173b23c6b48df11b1d2609b08080"} {"nl": {"description": "The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.", "input_spec": "The first line of input will contain an integer n (1 ≤ n ≤ 105), the number of events. The next line will contain n space-separated integers. If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.", "output_spec": "Print a single integer, the number of crimes which will go untreated.", "sample_inputs": ["3\n-1 -1 1", "8\n1 -1 1 -1 -1 1 1 1", "11\n-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1"], "sample_outputs": ["2", "1", "8"], "notes": "NoteLets consider the second example: Firstly one person is hired. Then crime appears, the last hired person will investigate this crime. One more person is hired. One more crime appears, the last hired person will investigate this crime. Crime appears. There is no free policeman at the time, so this crime will go untreated. One more person is hired. One more person is hired. One more person is hired. The answer is one, as one crime (on step 5) will go untreated."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.array;\nimport std.algorithm;\nimport std.range;\n\nvoid main() {\n auto n = readln.strip.to!int;\n auto a = readln.split.map!(to!int).array;\n int s = 0, r = 0;\n foreach (v; a) {\n s += v;\n if (s < 0) {\n r++;\n s = 0;\n }\n }\n writeln(r);\n}\n"}, {"source_code": "void main() {\n\timport std.stdio, std.conv, std.string, std.algorithm;\n\n\treadln;\n\n\tint sumCrimes, emplPols;\n\tforeach (el; readln.split.map!(to!int)) {\n\t\tif (el == -1) {\n\t\t\tif (emplPols > 0)\n\t\t\t\t--emplPols;\n\t\t\telse\n\t\t\t\t++sumCrimes;\n\t\t}\n\t\telse\n\t\t\templPols += el;\n\t}\n\n\twriteln(sumCrimes);\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.string;\nimport std.regex;\nimport std.math;\nimport std.algorithm;\n\nint main(string[] argv)\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint crimes, police_officers;\n\tforeach (int i; 0..n)\n\t{\n\t\tint tbd; scanf(\"%d\", &tbd);\n\t\tif (tbd < 0)\n\t\t{\n\t\t\tif (police_officers > 0)\n\t\t\t{\n\t\t\t\tpolice_officers += tbd;\n\t\t\t} else {\n\t\t\t\tcrimes -= tbd;\n\t\t\t}\n\t\t} else {\n\t\t\tpolice_officers += tbd;\n\t\t}\n\t}\n\tprintf(\"%d\", crimes);\n\treturn 0;\n}"}], "negative_code": [], "src_uid": "af47635f631381b4578ba599a4f8b317"} {"nl": {"description": "Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.", "input_spec": "The single line contains three integers k, d and t (1 ≤ k, d, t ≤ 1018).", "output_spec": "Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9. Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if .", "sample_inputs": ["3 2 6", "4 2 20"], "sample_outputs": ["6.5", "20.0"], "notes": "NoteIn the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready .In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes."}, "positive_code": [{"source_code": "import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,\n\tstd.bitmanip, std.conv, std.math;\nimport std.numeric : Fft, fft, gcd, inverseFft;\nimport std.random, std.range, std.regex, std.stdio, std.string, std.traits,\n\tstd.typecons, std.uni;\n\nalias pair(X, Y) = Tuple!(X, \"fi\", Y, \"se\");\nalias mp = make_pair;\nalias pii = pair!(int, int);\nalias pll = pair!(long, long);\n// alias gcd = std.numeric.gcd;\nalias lint = long;\nalias rbt = RedBlackTree;\n///modulo\nconst int mod = 10 ^^ 9 + 7, mod2 = mod + 2;\npublic\n{\n\tpure nothrow\n\t{\n\t\t///make_pair\n\t\tpair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)\n\t\t{\n\t\t\treturn tuple!(X, \"fi\", Y, \"se\")(x_, y_);\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).lowerBound(g).length;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn a.length - assumeSorted(a).upperBound(g).length;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).contains(g);\n\t\t}\n\t}\n\tstatic if (version_minor >= 74)\n\t{\n\t\t///read without format\n\t\tbool input(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\treturn readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tconst bool fl = readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t\treadln;\n\t\t\treturn fl;\n\t\t}\n\t}\n\telse\n\t{\n\t\t///read without format\n\t\tbool input(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treadln;\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nvoid main()\n{\n\tversion (home)\n\t{\n\t\tassert(freopen(\"input.txt\", \"r\", core.stdc.stdio.stdin));\n\t}\n\tlong k, d, t;\n\tread(k, d, t);\n\tt *= 2;\n\tk *= 2;\n\td *= 2;\n\tauto r = d * ((k + d - 1) / d);\n\tauto e = k + (d - k % d) % d / 2;\n\tauto ans = t / e * r;\n\tt %= e;\n\tans += min(k, t);\n\tt -= k;\n\tif (t > 0)\n\t\tans += 2 * t;\n\twritefln(\"%.10f\", 1.0L * ans / 2);\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nreal solve (long k, long d, long t)\n{\n\tif (k % d == 0)\n\t{\n\t\treturn t;\n\t}\n\treal goodParts = k / d;\n\treal goodAdd = k % d;\n\treal goodTime = goodParts * d + goodAdd;\n\treal badTime = d - goodAdd;\n\treal periodTime = goodTime + badTime;\n\treal periodCook = goodTime + badTime * 0.5;\n\tdebug {writefln (\"%.20f %.20f\", goodTime, badTime);}\n\n\treal res = 0.0;\n\treal toCook = t;\n\tfor (real times = 1L << 60; times >= 1; times = times * 0.5)\n\t{\n\t\tif (toCook >= periodCook * times)\n\t\t{\n\t\t\ttoCook -= periodCook * times;\n\t\t\tres += periodTime * times;\n\t\t}\n\t}\n\n\treal extra = min (toCook, goodTime);\n\tres += extra;\n\ttoCook -= extra;\n\tif (toCook > 0)\n\t{\n\t\tres += toCook * 2;\n\t}\n\treturn res;\n}\n\nvoid main ()\n{\n\tlong k, d, t;\n\twhile (readf (\" %s %s %s\", &k, &d, &t) > 0)\n\t{\n\t\treal res = solve (k, d, t);\n\t\twritefln (\"%.20f\", res);\n\t}\n}\n"}], "negative_code": [{"source_code": "import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,\n\tstd.bitmanip, std.conv, std.math;\nimport std.numeric : Fft, fft, gcd, inverseFft;\nimport std.random, std.range, std.regex, std.stdio, std.string, std.traits,\n\tstd.typecons, std.uni;\n\nalias pair(X, Y) = Tuple!(X, \"fi\", Y, \"se\");\nalias mp = make_pair;\nalias pii = pair!(int, int);\nalias pll = pair!(long, long);\n// alias gcd = std.numeric.gcd;\nalias lint = long;\nalias rbt = RedBlackTree;\n///modulo\nconst int mod = 10 ^^ 9 + 7, mod2 = mod + 2;\npublic\n{\n\tpure nothrow\n\t{\n\t\t///make_pair\n\t\tpair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)\n\t\t{\n\t\t\treturn tuple!(X, \"fi\", Y, \"se\")(x_, y_);\n\t\t}\n\t\t///square\n\t\t@property X sqr(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_;\n\t\t}\n\t\t///cube\n\t\t@property X cub(X)(in X a_)\n\t\t{\n\t\t\treturn a_ * a_ * a_;\n\t\t}\n\t\t///posicion lower bound\n\t\tsize_t lowb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).lowerBound(g).length;\n\t\t}\n\t\t///posicion upper bound\n\t\tsize_t upb(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn a.length - assumeSorted(a).upperBound(g).length;\n\t\t}\n\t\t///posicion binary search\n\t\tsize_t binf(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\tauto pos = lowb(a, g);\n\t\t\treturn (g == a[pos]) ? pos : a.length;\n\t\t}\n\t\t///binary search\n\t\tbool binary_search(T, X)(T a, auto ref X g)\n\t\t\t\tif (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))\n\t\t{\n\t\t\treturn assumeSorted(a).contains(g);\n\t\t}\n\t}\n\tstatic if (version_minor >= 74)\n\t{\n\t\t///read without format\n\t\tbool input(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\treturn readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tconst bool fl = readf!(replicate(\" %s\", ptrs.length))(ptrs) == ptrs.length;\n\t\t\treadln;\n\t\t\treturn fl;\n\t\t}\n\t}\n\telse\n\t{\n\t\t///read without format\n\t\tbool input(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t\t///readln without format\n\t\tbool read(T...)(ref T ptrs) if (ptrs.length > 0)\n\t\t{\n\t\t\tsize_t s;\n\t\t\tforeach (ref e; ptrs)\n\t\t\t{\n\t\t\t\tif (isSomeChar!(Unqual!(typeof(e))))\n\t\t\t\t\ts += readf(\" %c\", &e);\n\t\t\t\telse\n\t\t\t\t\ts += readf(\" %s\", &e);\n\t\t\t}\n\t\t\treadln;\n\t\t\treturn s == ptrs.length;\n\t\t}\n\t}\n\t///opening file\n\tnothrow auto inf(in char* name)\n\t{\n\t\treturn freopen(name, \"r\", core.stdc.stdio.stdin);\n\t}\n\t///closing file\n\tnothrow auto ouf(in char* name)\n\t{\n\t\treturn freopen(name, \"w\", core.stdc.stdio.stdout);\n\t}\n\t///parsing array from line\n\t@property auto arread(T)()\n\t{\n\t\treturn readln.splitter.map!(to!(T)).array;\n\t}\n\t///outbuffer\n\tvoid enter(T...)(T args)\n\t{\n\t\twriteln(args);\n\t\tstdout.flush;\n\t}\n}\n//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold return static heapify join filter foreach\n//------------------------------------------program beginning--------------------------\nvoid main()\n{\n\tversion (home)\n\t{\n\t\tassert(freopen(\"input.txt\", \"r\", core.stdc.stdio.stdin));\n\t}\n\tlong k, d, t;\n\tread(k, d, t);\n\tt *= 2;\n\tk *= 2;\n\td *= 2;\n\tauto r = d * ((k + d - 1) / d);\n\tauto e = k + (d - k % d) % d / 2;\n\tauto ans = t / e * r;\n\tt %= e;\n\tans += min(k, t);\n\tt -= k;\n\tif (t > 0)\n\t\tans += 2 * t;\n\twriteln(1.0L * ans / 2);\n}\n"}], "src_uid": "9df3a94dfa537cabdc52388a771cd24f"} {"nl": {"description": "You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.", "input_spec": "The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105). The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).", "output_spec": "Print the answer to the problem.", "sample_inputs": ["3\n3 4 5"], "sample_outputs": ["2"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\n\nint n;\nint[200005] a;\nint[1000005] f;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n auto m = a[0..n].fold!max;\n foreach(x; a[0..n]) f[x] = x;\n foreach(i; 1..m+1) \n if (f[i] == 0) f[i] = f[i-1];\n\n auto result = 0;\n a[0..n].sort();\n foreach(i, x; a[0..n]) {\n // ensure that the algorithm run in harmonic sum time: 1/2 + 1/3 + 1/4 ....\n if (i >= 1 && a[i] == a[i-1]) continue;\n auto y = x + x;\n while (y <= m) {\n result = max(result, f[y-1] % x);\n y += x;\n }\n result = max(result, m % x);\n }\n writeln(result);\n}\n"}, {"source_code": "import std.stdio;\nimport std.algorithm;\n\nint n;\nint[200005] a;\nint[1000005] f;\n\nvoid main() {\n readf(\" %s\", n);\n foreach(i; 0..n) readf(\" %s\", a[i]);\n\n auto m = a[0..n].fold!max;\n foreach(x; a[0..n]) f[x] = x;\n foreach(i; 1..m+1) \n if (f[i] == 0) f[i] = f[i-1];\n\n auto result = 0;\n a[0..n].sort();\n foreach(i, x; a[0..n]) {\n if (x == 1) continue;\n if (i >= 1 && a[i] == a[i-1]) continue;\n auto y = x + x;\n while (y <= m) {\n result = max(result, f[y-1] % x);\n y += x;\n }\n result = max(result, m % x);\n }\n writeln(result);\n}\n"}], "negative_code": [], "src_uid": "0ef324e3e314ea17c01aafb822e90c63"} {"nl": {"description": "Medicine faculty of Berland State University has just finished their admission campaign. As usual, about $$$80\\%$$$ of applicants are girls and majority of them are going to live in the university dormitory for the next $$$4$$$ (hopefully) years.The dormitory consists of $$$n$$$ rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number $$$i$$$ costs $$$c_i$$$ burles. Rooms are numbered from $$$1$$$ to $$$n$$$.Mouse doesn't sit in place all the time, it constantly runs. If it is in room $$$i$$$ in second $$$t$$$ then it will run to room $$$a_i$$$ in second $$$t + 1$$$ without visiting any other rooms inbetween ($$$i = a_i$$$ means that mouse won't leave room $$$i$$$). It's second $$$0$$$ in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from $$$1$$$ to $$$n$$$ at second $$$0$$$.What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?", "input_spec": "The first line contains as single integers $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of rooms in the dormitory. The second line contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$1 \\le c_i \\le 10^4$$$) — $$$c_i$$$ is the cost of setting the trap in room number $$$i$$$. The third line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — $$$a_i$$$ is the room the mouse will run to the next second after being in room $$$i$$$.", "output_spec": "Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.", "sample_inputs": ["5\n1 2 3 2 10\n1 3 4 3 3", "4\n1 10 2 10\n2 4 2 2", "7\n1 1 1 1 1 1 1\n2 2 2 3 6 7 6"], "sample_outputs": ["3", "10", "2"], "notes": "NoteIn the first example it is enough to set mouse trap in rooms $$$1$$$ and $$$4$$$. If mouse starts in room $$$1$$$ then it gets caught immideately. If mouse starts in any other room then it eventually comes to room $$$4$$$.In the second example it is enough to set mouse trap in room $$$2$$$. If mouse starts in room $$$2$$$ then it gets caught immideately. If mouse starts in any other room then it runs to room $$$2$$$ in second $$$1$$$.Here are the paths of the mouse from different starts from the third example: $$$1 \\rightarrow 2 \\rightarrow 2 \\rightarrow \\dots$$$; $$$2 \\rightarrow 2 \\rightarrow \\dots$$$; $$$3 \\rightarrow 2 \\rightarrow 2 \\rightarrow \\dots$$$; $$$4 \\rightarrow 3 \\rightarrow 2 \\rightarrow 2 \\rightarrow \\dots$$$; $$$5 \\rightarrow 6 \\rightarrow 7 \\rightarrow 6 \\rightarrow \\dots$$$; $$$6 \\rightarrow 7 \\rightarrow 6 \\rightarrow \\dots$$$; $$$7 \\rightarrow 6 \\rightarrow 7 \\rightarrow \\dots$$$; So it's enough to set traps in rooms $$$2$$$ and $$$6$$$."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto C = readln.split.map!(to!long).array;\n auto A = readln.split.map!(x => x.to!int-1).array;\n auto used = new int[](N);\n int cnt = 0;\n long ans = 0;\n\n foreach (i; 0..N) {\n if (used[i]) continue;\n ++cnt;\n int[] nodes;\n int n = i;\n while (!used[n]) {\n nodes ~= n;\n used[n] = cnt;\n n = A[n];\n }\n if (used[n] != cnt) continue;\n bool flag = false;\n long tmp = 1L << 59;\n foreach (j; nodes) {\n if (j == n) flag = true;\n if (flag) tmp = min(tmp, C[j]);\n }\n\n ans += tmp;\n }\n\n ans.writeln;\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto cost = readln.chomp.split.map!(to!int).array;\n \n auto p = readln.chomp.split.map!(to!int).array;\n p[] -= 1;\n \n int getMinForCycle(int v) {\n auto ans = int.max;\n int u = v;\n do {\n ans = min(ans, cost[u]);\n u = p[u];\n } while (u != v);\n \n debug { writeln(v, ' ', ans); }\n \n return ans;\n }\n \n auto vis = new int[n];\n int dfs(int v) {\n if (vis[v] == 2) {\n return 0;\n }\n \n vis[v] = 1;\n debug { v.writeln; }\n \n auto u = p[v];\n auto ret = 0;\n if (vis[u] == 0) {\n ret = dfs(u);\n } else if (vis[u] == 1) {\n ret = getMinForCycle(v);\n } else if (vis[u] == 2) {\n ret = 0;\n }\n vis[v] = 2;\n return ret;\n }\n \n auto ans = n.iota.map!(x => dfs(x)).sum;\n \n ans.writeln;\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto cost = readln.chomp.split.map!(to!int).array;\n \n auto p = readln.chomp.split.map!(to!int).array;\n p[] -= 1;\n \n int getMinForCycle(int v) {\n auto ans = int.max;\n int u = v;\n do {\n ans = min(ans, cost[u]);\n u = p[u];\n } while (u != v);\n \n debug { writeln(v, ' ', ans); }\n \n return ans;\n }\n \n auto vis = new int[n];\n int dfs(int v) {\n if (vis[v] == 2) {\n return 0;\n }\n \n vis[v] = 1;\n debug { v.writeln; }\n \n auto u = p[v];\n auto ret = 0;\n if (vis[u] == 0) {\n ret = dfs(u);\n } else if (vis[u] == 1) {\n ret = getMinForCycle(v);\n } else if (vis[u] == 2) {\n ret = 0;\n }\n vis[v] = 2;\n return ret;\n }\n \n auto ans = 0;\n foreach (i; 0 .. n) {\n ans += dfs(i);\n }\n \n ans.writeln;\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n\n auto cost = readln.chomp.split.map!(to!int).array;\n \n auto p = readln.chomp.split.map!(to!int).array;\n p[] -= 1;\n \n int getMinForCycle(int v) {\n auto ans = int.max;\n int u = v;\n do {\n ans = min(ans, cost[u]);\n u = p[u];\n } while (u != v);\n \n debug { writeln(v, ' ', ans); }\n \n return ans;\n }\n \n auto ans = 0;\n auto vis = new int[n];\n void dfs(int v) {\n if (vis[v] == 2) {\n return;\n }\n \n vis[v] = 1;\n debug { v.writeln; }\n \n auto u = p[v];\n if (vis[u] == 0) {\n dfs(u);\n } else if (vis[u] == 1) {\n ans += getMinForCycle(v);\n }\n vis[v] = 2;\n }\n \n foreach (i; 0 .. n) {\n dfs(i);\n }\n \n ans.writeln;\n}"}], "negative_code": [], "src_uid": "4278fece7812148863688c67218aca7b"} {"nl": {"description": "Guy-Manuel and Thomas are planning $$$144$$$ trips around the world.You are given a simple weighted undirected connected graph with $$$n$$$ vertexes and $$$m$$$ edges with the following restriction: there isn't any simple cycle (i. e. a cycle which doesn't pass through any vertex more than once) of length greater than $$$3$$$ which passes through the vertex $$$1$$$. The cost of a path (not necessarily simple) in this graph is defined as the XOR of weights of all edges in that path with each edge being counted as many times as the path passes through it.But the trips with cost $$$0$$$ aren't exciting. You may choose any subset of edges incident to the vertex $$$1$$$ and remove them. How many are there such subsets, that, when removed, there is not any nontrivial cycle with the cost equal to $$$0$$$ which passes through the vertex $$$1$$$ in the resulting graph? A cycle is called nontrivial if it passes through some edge odd number of times. As the answer can be very big, output it modulo $$$10^9+7$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n,m \\le 10^5$$$) — the number of vertexes and edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$ and $$$w_i$$$ ($$$1 \\le a_i, b_i \\le n, a_i \\neq b_i, 0 \\le w_i < 32$$$) — the endpoints of the $$$i$$$-th edge and its weight. It's guaranteed there aren't any multiple edges, the graph is connected and there isn't any simple cycle of length greater than $$$3$$$ which passes through the vertex $$$1$$$.", "output_spec": "Output the answer modulo $$$10^9+7$$$.", "sample_inputs": ["6 8\n1 2 0\n2 3 1\n2 4 3\n2 6 2\n3 4 8\n3 5 4\n5 4 5\n5 6 6", "7 9\n1 2 0\n1 3 1\n2 3 9\n2 4 3\n2 5 4\n4 5 7\n3 6 6\n3 7 7\n6 7 8", "4 4\n1 2 27\n1 3 1\n1 4 1\n3 4 0"], "sample_outputs": ["2", "1", "6"], "notes": "NoteThe pictures below represent the graphs from examples. In the first example, there aren't any nontrivial cycles with cost $$$0$$$, so we can either remove or keep the only edge incident to the vertex $$$1$$$. In the second example, if we don't remove the edge $$$1-2$$$, then there is a cycle $$$1-2-4-5-2-1$$$ with cost $$$0$$$; also if we don't remove the edge $$$1-3$$$, then there is a cycle $$$1-3-2-4-5-2-3-1$$$ of cost $$$0$$$. The only valid subset consists of both edges. In the third example, all subsets are valid except for those two in which both edges $$$1-3$$$ and $$$1-4$$$ are kept."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op)() const if (op == \"-\") { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const { return mixin(\"ModInt(this) \" ~ op ~ \"= a\"); }\n ModInt opBinaryRight(string op)(long a) const { return mixin(\"ModInt(a) \" ~ op ~ \"= this\"); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 7;\nalias Mint = ModInt!MO;\n\n\nenum L = 32;\n\nuint addVector(uint p, int x) {\n uint pp = p;\n foreach (y; 0 .. L) {\n if (p & 1U << y) {\n pp |= 1U << (y ^ x);\n }\n }\n return pp;\n}\n\nint N, M;\nint[] A, B, W;\nint[][] G;\n\nint[] dep, sums;\nint[] cycs, cycs0;\n\nvoid dfs(int u, int p, int d, int s) {\n debug {\n writeln(\"dfs \", u, \" \", p, \" \", d, \" \", s);\n }\n dep[u] = d;\n sums[u] = s;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n if (dep[v] == -1) {\n dfs(v, u, d + 1, s ^ W[i]);\n } else {\n if (dep[u] > dep[v]) {\n debug {\n writeln(\"back edge \", u, \" \", v, \" \", (sums[u] ^ sums[v] ^ W[i]));\n }\n ((v == 0) ? cycs0 : cycs) ~= (sums[u] ^ sums[v] ^ W[i]);\n }\n }\n }\n }\n \n}\n\nvoid main() {\n int V;\n int[uint] ids;\n int[] ps;\n DList!int que;\n que ~= 1U << 0;\n for (; !que.empty; ) {\n const p = que.front;\n que.removeFront;\n foreach (x; 0 .. L) {\n const pp = addVector(p, x);\n if (pp !in ids) {\n ids[pp] = V++;\n ps ~= pp;\n que ~= pp;\n }\n }\n }\n auto add = new int[][](V + 1, V + 1);\n foreach (u; 0 .. V + 1) foreach (v; 0 .. V + 1) {\n add[u][v] = V;\n }\n foreach (u; 0 .. V) foreach (v; 0 .. V) {\n bool orth = true;\n int pp;\n foreach (x; 0 .. L) foreach (y; 0 .. L) {\n if ((ps[u] & 1U << x) && (ps[v] & 1U << y)) {\n pp |= 1U << (x ^ y);\n if (x != 0 && y != 0) {\n orth = orth && ((x ^ y) != 0);\n }\n }\n }\n if (orth) {\n assert(pp in ids);\n add[u][v] = ids[pp];\n }\n }\n debug {\n writeln(\"V = \", V);\n /*\n foreach (u; 0 .. V) {\n foreach (x; 0 .. L) {\n write((ps[u] >> x) & 1U);\n }\n writeln();\n }\n */\n }\n \n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n W = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n W[i] = readInt();\n }\n G = new int[][N];\n foreach (i; 0 .. M) {\n G[A[i]] ~= i;\n G[B[i]] ~= i;\n }\n dep = new int[N];\n dep[] = -1;\n dep[0] = 0;\n sums = new int[N];\n int[] us, us1;\n foreach (i; G[0]) {\n const int v = A[i] ^ B[i] ^ 0;\n if (dep[v] == -1) {\n cycs = [];\n cycs0 = [];\n dfs(v, 0, 1, W[i]);\n debug {\n writeln(\"0 -> \", v, \": \", cycs, \" \", cycs0);\n }\n \n uint q = 1U << 0;\n bool orth = true;\n foreach (cyc; cycs) {\n orth = orth && !(q & 1U << cyc);\n q = addVector(q, cyc);\n }\n foreach (cyc; cycs0) {\n orth = orth && !(q & 1U << cyc);\n q = addVector(q, cyc);\n }\n us ~= orth ? ids[q] : V;\n debug {\n write(\"q = \");\n foreach (x; 0 .. L) {\n write((q >> x) & 1U);\n }\n writeln();\n }\n \n if (cycs0) {\n uint q1 = 1U << 0;\n bool orth1 = true;\n foreach (cyc; cycs) {\n orth1 = orth1 && !(q1 & 1U << cyc);\n q1 = addVector(q1, cyc);\n }\n us1 ~= orth1 ? ids[q1] : V;\n debug {\n write(\"q1 = \");\n foreach (x; 0 .. L) {\n write((q1 >> x) & 1U);\n }\n writeln();\n }\n } else {\n us1 ~= -1;\n }\n }\n }\n debug {\n writeln(\"us = \", us);\n writeln(\"us1 = \", us1);\n }\n \n const len = cast(int)(us.length);\n auto dp = new Mint[][](len + 1, V + 1);\n dp[0][0] = 1;\n foreach (j; 0 .. len) {\n if (us1[j] != -1) {\n foreach (u; 0 .. V + 1) {\n dp[j + 1][u] += dp[j][u];\n dp[j + 1][add[u][us1[j]]] += dp[j][u] * 2;\n dp[j + 1][add[u][us[j]]] += dp[j][u];\n }\n } else {\n foreach (u; 0 .. V + 1) {\n dp[j + 1][u] += dp[j][u];\n dp[j + 1][add[u][us[j]]] += dp[j][u];\n }\n }\n }\n debug {\n foreach (j; 0 .. len + 1) {\n write(j);\n foreach (u; 0 .. V + 1) {\n if (dp[j][u].x != 0) {\n write(\" \", u, \":\", dp[j][u]);\n }\n }\n writeln();\n }\n }\n const ans = dp[len][0 .. V].sum;\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "d1db6b8a61366c04e3f1a996f45f22e3"} {"nl": {"description": "Consider a sequence of integers $$$a_1, a_2, \\ldots, a_n$$$. In one move, you can select any element of the sequence and delete it. After an element is deleted, all elements to the right are shifted to the left by $$$1$$$ position, so there are no empty spaces in the sequence. So after you make a move, the sequence's length decreases by $$$1$$$. The indices of the elements after the move are recalculated.E. g. let the sequence be $$$a=[3, 2, 2, 1, 5]$$$. Let's select the element $$$a_3=2$$$ in a move. Then after the move the sequence will be equal to $$$a=[3, 2, 1, 5]$$$, so the $$$3$$$-rd element of the new sequence will be $$$a_3=1$$$ and the $$$4$$$-th element will be $$$a_4=5$$$.You are given a sequence $$$a_1, a_2, \\ldots, a_n$$$ and a number $$$k$$$. You need to find the minimum number of moves you have to make so that in the resulting sequence there will be at least $$$k$$$ elements that are equal to their indices, i. e. the resulting sequence $$$b_1, b_2, \\ldots, b_m$$$ will contain at least $$$k$$$ indices $$$i$$$ such that $$$b_i = i$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two consecutive lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2000$$$). The second line contains a sequence of integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$). The numbers in the sequence are not necessarily different. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2000$$$.", "output_spec": "For each test case output in a single line: $$$-1$$$ if there's no desired move sequence; otherwise, the integer $$$x$$$ ($$$0 \\le x \\le n$$$) — the minimum number of the moves to be made so that the resulting sequence will contain at least $$$k$$$ elements that are equal to their indices. ", "sample_inputs": ["4\n7 6\n1 1 2 3 4 5 6\n5 2\n5 1 3 2 3\n5 2\n5 5 5 5 4\n8 4\n1 2 3 3 2 2 5 5"], "sample_outputs": ["1\n2\n-1\n2"], "notes": "NoteIn the first test case the sequence doesn't satisfy the desired condition, but it can be provided by deleting the first element, hence the sequence will be $$$[1, 2, 3, 4, 5, 6]$$$ and $$$6$$$ elements will be equal to their indices.In the second test case there are two ways to get the desired result in $$$2$$$ moves: the first one is to delete the $$$1$$$-st and the $$$3$$$-rd elements so that the sequence will be $$$[1, 2, 3]$$$ and have $$$3$$$ elements equal to their indices; the second way is to delete the $$$2$$$-nd and the $$$3$$$-rd elements to get the sequence $$$[5, 2, 3]$$$ with $$$2$$$ desired elements."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport std.range;\nimport core.stdc.stdio;\n\nvoid main()\n{\n\tint t = readInt!int;\n\twhile (t--)\n\t{\n\t\tsolve();\n\t}\n}\n\nvoid solve()\n{\n\tint n = readInt!int;\n\tint k = readInt!int;\n\tauto a = new int[](n); foreach(ref ai; a) ai = readInt!int - 1;\n\tauto reqrem = new int[](n);\n\tforeach(i, ref remi; reqrem)\n\t{\n\t\tif (a[i] <= i)\n\t\t{\n\t\t\tremi = cast(int)(i - a[i]);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tremi = -1;\n\t\t}\n\t}\n\tauto mxlen = new int[](n);\n\tauto visited = new bool[](n);\n\tint maxLength(int i)\n\t{\n\t\tif (reqrem[i] == -1) return 0; \n\t\tif (visited[i]) return mxlen[i];\n\t\tvisited[i] = true;\n\t\tmxlen[i] = 0;\n\t\tforeach(j; 0 .. i)\n\t\t{\n\t\t\tif (reqrem[j] == -1) continue;\n\t\t\tif (reqrem[j] <= reqrem[i])\n\t\t\t{\n\t\t\t\tint delta = reqrem[i] - reqrem[j];\n\t\t\t\tdebug writeln(i, \" -> \", j);\n\t\t\t\tif (delta <= i - j - 1)\n\t\t\t\t{\n\t\t\t\t\tmxlen[i] = max(mxlen[i], maxLength(j));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tmxlen[i]++;\n\t\treturn mxlen[i];\n\t}\n\tdebug writeln(reqrem);\n\tdebug \n\t{\n\t\tiota(0, n).map!(i=>maxLength(i)).writeln;\n\t}\n\tint minmoves = int.max;\n\tforeach(i; 0 .. n)\n\t{\n\t\tif (reqrem[i] != -1)\n\t\t{\n\t\t\tif (maxLength(i) >= k)\n\t\t\t{\n\t\t\t\tminmoves = min(minmoves, reqrem[i]);\n\t\t\t}\n\t\t}\n\t}\n\tif (minmoves == int.max) writeln(-1);\n\telse writeln(minmoves);\n}\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n\tcurrChar = getchar();\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\n/**MODULAR SYSTEM*/\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\n"}], "negative_code": [], "src_uid": "1042641f46ec636ca8820f0509c77a6f"} {"nl": {"description": "There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $$$y$$$-coordinates, and their $$$x$$$-coordinate is equal to $$$-100$$$, while the second group is positioned in such a way that they all have integer $$$y$$$-coordinates, and their $$$x$$$-coordinate is equal to $$$100$$$.Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with $$$x=0$$$ (with not necessarily integer $$$y$$$-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 60$$$), the number of enemy spaceships with $$$x = -100$$$ and the number of enemy spaceships with $$$x = 100$$$, respectively. The second line contains $$$n$$$ integers $$$y_{1,1}, y_{1,2}, \\ldots, y_{1,n}$$$ ($$$|y_{1,i}| \\le 10\\,000$$$) — the $$$y$$$-coordinates of the spaceships in the first group. The third line contains $$$m$$$ integers $$$y_{2,1}, y_{2,2}, \\ldots, y_{2,m}$$$ ($$$|y_{2,i}| \\le 10\\,000$$$) — the $$$y$$$-coordinates of the spaceships in the second group. The $$$y$$$ coordinates are not guaranteed to be unique, even within a group.", "output_spec": "Print a single integer – the largest number of enemy spaceships that can be destroyed.", "sample_inputs": ["3 9\n1 2 3\n1 2 3 7 8 9 11 12 13", "5 5\n1 2 3 4 5\n1 2 3 4 5"], "sample_outputs": ["9", "10"], "notes": "NoteIn the first example the first spaceship can be positioned at $$$(0, 2)$$$, and the second – at $$$(0, 7)$$$. This way all the enemy spaceships in the first group and $$$6$$$ out of $$$9$$$ spaceships in the second group will be destroyed.In the second example the first spaceship can be positioned at $$$(0, 3)$$$, and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships."}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int half = 20005;\nimmutable int limit = (half << 1) + 1;\n\nvoid main ()\n{\n\tint m, n;\n\twhile (readf (\" %s %s\", &m, &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto b = readln.splitter.map !(to !(int)).array;\n\t\ta[] += half;\n\t\tb[] += half;\n\t\tauto u = new int [limit];\n\t\tauto v = new int [limit];\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tu[c] += 1;\n\t\t}\n\t\tforeach (ref c; b)\n\t\t{\n\t\t\tv[c] += 1;\n\t\t}\n\t\tauto f = new bool [limit];\n\t\tauto g = new bool [limit];\n\t\tauto mask = new long [2] [] [] (m, n);\n\t\tforeach (p; 0..m)\n\t\t{\n\t\t\tforeach (q; 0..n)\n\t\t\t{\n\t\t\t\tforeach (r; 0..m)\n\t\t\t\t{\n\t\t\t\t\tforeach (s; 0..n)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (a[r] - a[p] == b[q] - b[s])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmask[p][q][0] |=\n\t\t\t\t\t\t\t 1L << r;\n\t\t\t\t\t\t\tmask[p][q][1] |=\n\t\t\t\t\t\t\t 1L << s;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint res = 0;\n\t\tforeach (p; 0..m)\n\t\t{\n\t\t\tforeach (q; 0..n)\n\t\t\t{\n\t\t\t\tforeach (r; 0..m)\n\t\t\t\t{\n\t\t\t\t\tforeach (s; 0..n)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto cur = 0;\n\t\t\t\t\t\tcur += popcnt (mask[p][q][0] |\n\t\t\t\t\t\t mask[r][s][0]);\n\t\t\t\t\t\tcur += popcnt (mask[p][q][1] |\n\t\t\t\t\t\t mask[r][s][1]);\n\t\t\t\t\t\tres = max (res, cur);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const M = readInt();\n const N = readInt();\n auto A = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt();\n }\n auto B = new int[N];\n foreach (j; 0 .. N) {\n B[j] = readInt();\n }\n A.sort;\n B.sort;\n \n alias Entry = Tuple!(int, \"s\", int, \"i\", int, \"j\");\n Entry[] es;\n foreach (i; 0 .. M) foreach (j; 0 .. N) {\n es ~= Entry(A[i] + B[j], i, j);\n }\n es.sort;\n const esLen = cast(int)(es.length);\n Tuple!(long, long)[] fs;\n for (int k = 0, l; k < esLen; k = l) {\n for (l = k; l < esLen && es[k].s == es[l].s; ++l) {}\n long f0, f1;\n foreach (ref e; es[k .. l]) {\n f0 |= 1L << e.i;\n f1 |= 1L << e.j;\n fs ~= tuple(f0, f1);\n }\n }\n const fsLen = cast(int)(fs.length);\n int ans;\n foreach (k; 0 .. fsLen) foreach (l; k .. fsLen) {\n int score;\n score += popcnt(fs[k][0] | fs[l][0]);\n score += popcnt(fs[k][1] | fs[l][1]);\n chmax(ans, score);\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const M = readInt();\n const N = readInt();\n auto A = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt();\n }\n auto B = new int[N];\n foreach (j; 0 .. N) {\n B[j] = readInt();\n }\n A.sort;\n B.sort;\n \n alias Entry = Tuple!(int, \"s\", int, \"i\", int, \"j\");\n Entry[] es;\n foreach (i; 0 .. M) foreach (j; 0 .. N) {\n es ~= Entry(A[i] + B[j], i, j);\n }\n es.sort;\n const esLen = cast(int)(es.length);\n Tuple!(long, long)[] fs;\n for (int k = 0, l; k < esLen; k = l) {\n for (l = k; l < esLen && es[k].s == es[l].s; ++l) {}\n long f0, f1;\n foreach (ref e; es[k .. l]) {\n f0 |= 1L << e.i;\n f1 |= 1L << e.j;\n fs ~= tuple(f0, f1);\n }\n }\n const fsLen = cast(int)(fs.length);\n int ans;\n foreach (k; 0 .. fsLen) foreach (l; k + 1 .. fsLen) {\n int score;\n score += popcnt(fs[k][0] | fs[l][0]);\n score += popcnt(fs[k][1] | fs[l][1]);\n chmax(ans, score);\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "7dd891cef0aa40cc1522ca4b37963b92"} {"nl": {"description": "Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees.First, he'll provide you a tree (not 1-tree) with $$$n$$$ vertices, then he will ask you $$$q$$$ queries. Each query contains $$$5$$$ integers: $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$, and $$$k$$$. This means you're asked to determine if there exists a path from vertex $$$a$$$ to $$$b$$$ that contains exactly $$$k$$$ edges after adding a bidirectional edge between vertices $$$x$$$ and $$$y$$$. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.", "input_spec": "The first line contains an integer $$$n$$$ ($$$3 \\le n \\le 10^5$$$), the number of vertices of the tree. Next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u,v \\le n$$$, $$$u \\ne v$$$) each, which means there is an edge between vertex $$$u$$$ and $$$v$$$. All edges are bidirectional and distinct. Next line contains an integer $$$q$$$ ($$$1 \\le q \\le 10^5$$$), the number of queries Gildong wants to ask. Next $$$q$$$ lines contain five integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$, and $$$k$$$ each ($$$1 \\le x,y,a,b \\le n$$$, $$$x \\ne y$$$, $$$1 \\le k \\le 10^9$$$) – the integers explained in the description. It is guaranteed that the edge between $$$x$$$ and $$$y$$$ does not exist in the original tree.", "output_spec": "For each query, print \"YES\" if there exists a path that contains exactly $$$k$$$ edges from vertex $$$a$$$ to $$$b$$$ after adding an edge between vertices $$$x$$$ and $$$y$$$. Otherwise, print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["5\n1 2\n2 3\n3 4\n4 5\n5\n1 3 1 2 2\n1 4 1 3 2\n1 4 1 3 3\n4 2 3 3 9\n5 2 3 3 9"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO"], "notes": "NoteThe image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines). Possible paths for the queries with \"YES\" answers are: $$$1$$$-st query: $$$1$$$ – $$$3$$$ – $$$2$$$ $$$2$$$-nd query: $$$1$$$ – $$$2$$$ – $$$3$$$ $$$4$$$-th query: $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ "}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\tNode[] nodes;\n\tforeach(i; 0 .. n) nodes ~= new Node(i);\n\tforeach(_; 0 .. n - 1){\n\t\tNode p = nodes[rint - 1], q = nodes[rint - 1];\n\t\tp.connectTo(q);\n\t}\n\n\tnodes[0].treefy(null);\n\tforeach(d; 0 .. 30) foreach(nd; nodes) nd.makeparents(d);\n\n\tforeach(_;0 .. rint){\n\t\tNode x = nodes[rint - 1], y = nodes[rint - 1], a = nodes[rint - 1], b = nodes[rint - 1];\n\t\tint k = rint;\n\n\t\tbool isOK(int d){\n\t\t\treturn d <= k && d % 2 == k % 2;\n\t\t}\n\n\t\tstring ans = \"NO\";\n\t\tif(isOK(a.distTo(b))) ans = \"YES\";\n\t\tif(isOK(a.distTo(x) + b.distTo(y) + 1)) ans = \"YES\";\n\t\tif(isOK(a.distTo(y) + b.distTo(x) + 1)) ans = \"YES\";\n\n\t\tans.writeln;\n\t}\n}\n\nclass Node{\n\tint id;\n\tNode[] nodes;\n\tint depth;\n\tNode parent;\n\tNode[] parents;\n\tbool isDone;\n\tthis(int id){\n\t\tthis.id = id;\n\t}\n\tvoid connectTo(Node nd){\n\t\tthis.nodes ~= nd;\n\t\tnd.nodes ~= this;\n\t}\n\tvoid treefy(Node par){\n\t\tthis.isDone = 1;\n\t\tif(!par) this.depth = 0;\n\t\telse this.parent = par, this.depth = par.depth + 1;\n\t\tforeach(nd; nodes) if(! nd.isDone) nd.treefy(this);\n\t}\n\tvoid makeparents(int depth){\n\t\tif(depth == 0){\n\t\t\tif(parent) parents ~= parent;\n\t\t\treturn;\n\t\t}\n\t\tif(depth > parents.length) return;\n\t\tlog(\"id:\", id + 1, \"depth:\", depth, \"parents:\", parents.map!(p => p.id + 1).array);\n\t\tNode par = parents[depth - 1];\n\t\tif(depth <= par.parents.length) parents ~= par.parents[depth - 1];\n\t}\n\tint distTo(Node nd){\n\t\t//log(\"this:\", this.id + 1, \"nd:\", nd.id + 1);\n\t\tNode p = this.lca(nd);\n\t\tint res = (this.depth - p.depth) + (nd.depth - p.depth);\n\t\tlog(\"this:\", this.id + 1, \"nd:\", nd.id + 1, \"lca:\", p.id + 1, \"dist:\", res);\n\t\treturn res;\n\t}\n\tNode lca(Node nd){\n\t\tif(this.id == nd.id) return this;\n\t\tif(! this.parent) return this;\n\t\tif(! nd.parent) return nd;\n\t\tif(this.depth > nd.depth) return nd.lca(this);\n\t\tif(this.depth < nd.depth && nd.depth - this.depth > 10){\n\t\t\tforeach_reverse(p; nd.parents){\n\t\t\t\tif(p.depth > this.depth) return lca(p);\n\t\t\t}\n\t\t}\n\t\tif(this.depth < nd.depth){\n\t\t\twhile(this.depth < nd.depth) if(nd.parent) nd = nd.parent; else return nd;\n\t\t\treturn this.lca(nd);\n\t\t}\n\n\t\tint i = 0;\n\t\twhile(i < this.parents.length && i < nd.parents.length &&\n\t\t\t\tthis.parents[i].id != nd.parents[i].id) i += 1;\n\t\tif(i == 0) return this.parent;\n\t\telse return this.parents[i - 1].lca(nd.parents[i - 1]);\n\t}\n}"}], "negative_code": [], "src_uid": "c53e3b38a345dcf65bf984e819c289ef"} {"nl": {"description": "$$$n$$$ people gathered to hold a jury meeting of the upcoming competition, the $$$i$$$-th member of the jury came up with $$$a_i$$$ tasks, which they want to share with each other.First, the jury decides on the order which they will follow while describing the tasks. Let that be a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$ (an array of size $$$n$$$ where each integer from $$$1$$$ to $$$n$$$ occurs exactly once).Then the discussion goes as follows: If a jury member $$$p_1$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. If a jury member $$$p_2$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. ... If a jury member $$$p_n$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. If there are still members with tasks left, then the process repeats from the start. Otherwise, the discussion ends. A permutation $$$p$$$ is nice if none of the jury members tell two or more of their own tasks in a row. Count the number of nice permutations. The answer may be really large, so print it modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — number of jury members. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the number of problems that the $$$i$$$-th member of the jury came up with. The sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the number of nice permutations, taken modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["4\n2\n1 2\n3\n5 5 5\n4\n1 3 3 7\n6\n3 4 2 1 3 3"], "sample_outputs": ["1\n6\n0\n540"], "notes": "NoteExplanation of the first test case from the example:There are two possible permutations, $$$p = [1, 2]$$$ and $$$p = [2, 1]$$$. For $$$p = [1, 2]$$$, the process is the following: the first jury member tells a task; the second jury member tells a task; the first jury member doesn't have any tasks left to tell, so they are skipped; the second jury member tells a task. So, the second jury member has told two tasks in a row (in succession), so the permutation is not nice.For $$$p = [2, 1]$$$, the process is the following: the second jury member tells a task; the first jury member tells a task; the second jury member tells a task. So, this permutation is nice."}, "positive_code": [{"source_code": "immutable multi = true;\n\nimmutable long p = 998244353;\nalias Zp = Z!p;\n\nstatic this()\n{\n\tZp.makeFactorials(200_000+1);\n}\n\nvoid solve(int)\n{\n\tint n = readInt!int;\n\tauto a = ma(n, readInt!int);\n\tauto mx = a.fold!max;\n\tint cntmx = 0, cntmxp = 0;\n\tforeach(i, ai; a) { if (ai == mx) cntmx++; else if (ai == mx-1) cntmxp++; }\n\tassert(cntmx > 0);\n\tif (cntmx > 1)\n\t{\n\t\treturn writeln(Zp.fact[n].rep);\n\t}\n\tassert(cntmx == 1);\n\tdebug writeln(cntmx);\n\tdebug writeln(cntmxp);\n\tZp ans = Zp.fact[n];\n\tdebug writeln(ans);\n\tauto rem = n - cntmx - cntmxp;\n\tdebug writeln(rem);\n\tforeach(i; 0 .. n)\n\t{\n\t\tdebug writeln(\"C(\", cntmxp, \", \", i, \") = \", Zp.C(cntmxp, i));\n\t\tans = ans - (Zp.C(i, cntmxp) * Zp.fact[cntmxp] * Zp.fact[rem]);\n\t\tdebug writeln(ans);\n\t}\n\treturn writeln(ans.rep);\n}\n\n//{{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1;\n\tforeach(tc; 0 .. t) solve(tc);\n}\n//}}}\n\n// modular {{{\nstruct Z(immutable long m)\n{\n\tlong rep;\n\tstatic immutable bool primeModulus = isPrime(m);\n\tstatic Z!m[] fact;\n\tstatic if (primeModulus) { static Z!m[] invFact; }\n\tstatic makeFactorials(int n)\n\t{\n\t\tfact = new Z!m[](n + 1);\n\t\tfact[0] = Z!m(1);\n\t\tforeach(i; 1 .. n + 1) fact[i] = Z!m(i) * fact[i - 1];\n\t\tstatic if (primeModulus)\n\t\t{\n\t\t\tinvFact = new Z!m[](n + 1);\n\t\t\tinvFact[n] = fact[n].inv;\n\t\t\tforeach_reverse(i; 0 .. n) invFact[i] = Z!m(i + 1) * invFact[i + 1];\n\t\t}\n\t}\n\n\tthis(long num)\n\t{\n\t\trep = num;\n\t}\n\n\tZ!m opBinary(string operator)(Z!m rhs)\n\t{\n\t\tstatic if (operator == \"+\")\n\t\t{\n\t\t\tlong result = rhs.rep + this.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult -= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"-\")\n\t\t{\n\t\t\tlong result = this.rep - rhs.rep;\n\t\t\tif (result < 0)\n\t\t\t\tresult += m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"*\")\n\t\t{\n\t\t\tlong result = this.rep * rhs.rep;\n\t\t\tif (result >= m)\n\t\t\t\tresult %= m;\n\t\t\treturn Z!m(result);\n\t\t}\n\t\telse static if (operator == \"/\" && primeModulus)\n\t\t{\n\t\t\tassert(rhs != Z!m(0));\n\t\t\treturn this * rhs.inv;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tstatic assert(text(\"Operator \", operator, \" not supported\"));\n\t\t}\n\t}\n\n\tZ!m opBinary(string operator)(long exponent) if (operator == \"^^\")\n\t{\n\t\tassert(exponent >= 0);\n\t\tZ!m base = this;\n\t\tZ!m result = 1;\n\t\twhile (exponent)\n\t\t{\n\t\t\tif (exponent & 1)\n\t\t\t\tresult = result * base;\n\t\t\tbase = base * base;\n\t\t\texponent >>= 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tstatic if (primeModulus)\n\t{\n\t\tZ!m inv()\n\t\t{\n\t\t\treturn this^^(m - 2);\n\t\t}\n\t\tstatic Z!m C(int n, int k)\n\t\t{\n\t\t\tif (k < 0 || k > n) return Z!m(0);\n\t\t\treturn fact[n] * invFact[k] * invFact[n - k];\n\t\t}\n\t}\n\n\tinvariant\n\t{\n\t\tassert(rep >= 0 && rep < m);\n\t}\n}\nbool isPrime(long n)\n{\n\tfor(long d = 2; d * d <= n; d++)\n\t\tif (n % d == 0) return false;\n\treturn true; \n}\nunittest\n{\n\talias Zp = Z!23;\n\tstatic assert(Zp.primeModulus);\n\tforeach(i; 1 .. 23) assert(Zp(i) * Zp(i).inv == Zp(1));\n\tZp.makeFactorials(22);\n\tforeach(i; 0 .. 23) assert(Zp.fact[i] * Zp.invFact[i] == Zp(1));\n\tassert(Zp.C(3, 2) == Zp(3));\n\tassert(Zp.C(4, 2) == Zp(6));\n}\n// }}}\n\n\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t{\n\t\tpopChar;\n\t}\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}], "negative_code": [], "src_uid": "81d4867a7e5128baf316193e9cc3dfce"} {"nl": {"description": "Nauuo is a girl who loves drawing circles.One day she has drawn a circle and wanted to draw a tree on it.The tree is a connected undirected graph consisting of $$$n$$$ nodes and $$$n-1$$$ edges. The nodes are numbered from $$$1$$$ to $$$n$$$.Nauuo wants to draw a tree on the circle, the nodes of the tree should be in $$$n$$$ distinct points on the circle, and the edges should be straight without crossing each other.\"Without crossing each other\" means that every two edges have no common point or the only common point is an endpoint of both edges.Nauuo wants to draw the tree using a permutation of $$$n$$$ elements. A permutation of $$$n$$$ elements is a sequence of integers $$$p_1,p_2,\\ldots,p_n$$$ in which every integer from $$$1$$$ to $$$n$$$ appears exactly once.After a permutation is chosen Nauuo draws the $$$i$$$-th node in the $$$p_i$$$-th point on the circle, then draws the edges connecting the nodes.The tree is given, Nauuo wants to know how many permutations are there so that the tree drawn satisfies the rule (the edges are straight without crossing each other). She only wants to know the answer modulo $$$998244353$$$, can you help her?It is obvious that whether a permutation is valid or not does not depend on which $$$n$$$ points on the circle are chosen.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2\\le n\\le 2\\cdot 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\\le u,v\\le n$$$), denoting there is an edge between $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree.", "output_spec": "The output contains a single integer — the number of permutations suitable to draw the given tree on a circle satisfying the rule, modulo $$$998244353$$$.", "sample_inputs": ["4\n1 2\n1 3\n2 4", "4\n1 2\n1 3\n1 4"], "sample_outputs": ["16", "24"], "notes": "NoteExample 1All valid permutations and their spanning trees are as follows.Here is an example of invalid permutation: the edges $$$(1,3)$$$ and $$$(2,4)$$$ are crossed.Example 2Every permutation leads to a valid tree, so the answer is $$$4! = 24$$$."}, "positive_code": [{"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm : sort, each, maxElement, minElement;\nimport std.conv : to;\nimport std.range : iota, tee;\nimport std.algorithm.comparison : equal, max, min;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nint[][] child;\nbool[] visited;\n\nlong MOD = 998244353;\nlong[] fac;\n\nlong dfs(uint root) {\n\n uint nc = 0;\n\n visited[root] = true;\n long mul = 1;\n\n foreach (v; child[root]) {\n if (!visited[v]) {\n long ll = dfs(v);\n mul = (mul * ll) % MOD;\n nc++;\n }\n }\n\n if (root != 0)\n nc++;\n\n return (fac[nc] * mul) % MOD;\n}\n\nvoid main() {\n GC.disable();\n\n uint n;\n\n readf(\" %s\", n);\n\n child = new int[][n];\n visited = new bool[n];\n visited[] = false;\n fac = new long[n + 10];\n fac[0] = 1;\n foreach (i; 1 .. n + 1)\n fac[i] = (fac[i - 1] * i) % MOD;\n\n foreach (i; 0 .. n - 1) {\n uint u, v;\n readf(\" %s %s\", u, v);\n u--;\n v--;\n child[u] ~= v;\n child[v] ~= u;\n }\n\n long ans = dfs(0);\n writeln((n * ans) % MOD);\n\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int mod = 998_244_353;\nimmutable int NA = -1;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [] [n];\n\t\tforeach (i; 0..n - 1)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf (\" %s %s\", &u, &v);\n\t\t\tu -= 1;\n\t\t\tv -= 1;\n\t\t\ta[u] ~= v;\n\t\t\ta[v] ~= u;\n\t\t}\n\n\t\tint recur (int v, int p)\n\t\t{\n\t\t\tint subtrees = a[v].length.to !(int) + (p == NA);\n\t\t\tint res = 1;\n\t\t\tfor (int i = 1; i < subtrees; i++)\n\t\t\t{\n\t\t\t\tres = (res * 1L * i) % mod;\n\t\t\t}\n\t\t\tif (p == NA)\n\t\t\t{\n\t\t\t\tres = (res * 1L * n) % mod;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tres = (res * 1L * (subtrees)) % mod;\n\t\t\t}\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\tif (u != p)\n\t\t\t\t{\n\t\t\t\t\tres = (res * 1L * recur (u, v)) % mod;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\twriteln (recur (0, NA));\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum long MO = 998244353;\n\nint N;\nint[] U, V;\n\nint[][] G;\nint[] deg;\n\nvoid dfs(int u, int p) {\n foreach (v; G[u]) {\n if (v != p) {\n ++deg[u];\n dfs(v, u);\n }\n }\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n U = new int[N - 1];\n V = new int[N - 1];\n foreach (i; 0 .. N - 1) {\n U[i] = readInt() - 1;\n V[i] = readInt() - 1;\n }\n \n G = new int[][N];\n foreach (i; 0 .. N - 1) {\n G[U[i]] ~= V[i];\n G[V[i]] ~= U[i];\n }\n deg = new int[N];\n const rt = 0;\n dfs(rt, -1);\n \n long ans = N;\n foreach (u; 0 .. N) {\n foreach (k; 1 .. deg[u] + 1) {\n ans *= k;\n ans %= MO;\n }\n if (u != rt) {\n ans *= (deg[u] + 1);\n ans %= MO;\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm : sort, each, maxElement, minElement;\nimport std.conv : to;\nimport std.range : iota, tee;\nimport std.algorithm.comparison : equal, max, min;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nint[][] child;\nbool[] visited;\n\nlong MOD = 998244353;\nlong[] fac;\n\nlong dfs(uint root) {\n\n uint nc = 0;\n\n visited[root] = true;\n long mul = 1;\n\n foreach (v; child[root]) {\n if (!visited[v]) {\n long ll = dfs(v);\n mul = (mul * ll) % MOD;\n nc++;\n }\n }\n\n if (root != 0)\n nc++;\n\n return (fac[nc] * mul) % MOD;\n}\n\nvoid main() {\n GC.disable();\n\n uint n;\n\n readf(\" %s\", n);\n\n child = new int[][n];\n visited = new bool[n];\n visited[] = false;\n fac = new long[n + 10];\n fac[0] = 1;\n foreach (i; 1 .. n + 1)\n fac[i] = (fac[i - 1] * i) % MOD;\n\n foreach (i; 0 .. n - 1) {\n uint u, v;\n readf(\" %s %s\", u, v);\n u--;\n v--;\n child[u] ~= v;\n child[v] ~= u;\n }\n\n long ans = dfs(0);\n writeln(n * ans);\n\n}\n"}], "src_uid": "03bfe285856fa74b89de7a1bebb14bca"} {"nl": {"description": "Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements: k ≥ 1   t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed). As the number of ways can be rather large print it modulo 109 + 7.", "input_spec": "Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.", "output_spec": "Print the answer in a single line.", "sample_inputs": ["ababa\naba", "welcometoroundtwohundredandeightytwo\nd", "ddd\nd"], "sample_outputs": ["5", "274201", "12"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\n/* KMP法による文字列探索\n * 計算量 O(M + N). M: パターンの長さ, N: パターンを探す文字列の長さ */\nclass KMP {\n string s;\n int[] PMA;\n this(in string s) {\n this.s = s;\n buildPMA(s);\n }\n /* tに含まれるsの数を数える.\n * この実装では\n * s = \"ABA\",\n * t = \"ABABA\"\n * のとき2 ( [0, 3), [2, 5)でマッチ ) を返す.\n * [2, 5)でマッチさせたくなければbuildPMAでPMA[M]を0にしておく. */\n int[] match(in string t) {\n auto N = t.length, M = s.length;\n int[] ret;\n int si = 0, ti = 0;\n while (ti < N) {\n if (s[si] == t[ti]) {\n si++; ti++;\n } else {\n if (si == 0) ti++;\n si = PMA[si];\n }\n if (si == M) {\n ret ~= cast(int)ti;\n si = PMA[si];\n }\n }\n return ret;\n }\n /* Pattern Matching Automaton を作る */\n void buildPMA(in string s) {\n auto M = s.length;\n PMA = new int[M + 1];\n PMA[0] = 0; PMA[1] = 0;\n for (int i = 2; i <= M; i++) {\n int j = PMA[i - 1];\n while (true) {\n if (s[j] == s[i - 1]) { PMA[i] = j + 1; break; }\n if (j == 0) { PMA[i] = 0; break; }\n j = PMA[j];\n }\n }\n }\n};\n\nconst MOD = 1_000_000_007;\n\nvoid main() {\n string s = readln.chomp,\n t = readln.chomp;\n auto matcher = new KMP(t);\n auto M = matcher.match(s);\n\n auto end = new bool[s.length + 1];\n foreach (m; M) {\n end[m] = true;\n }\n\n auto dp = new int[s.length + 1];\n auto sum = new int[s.length + 1];\n auto ssum = new int[s.length + 1];\n int T = cast(int)(t.length);\n foreach (int i; 1 .. cast(int)s.length + 1) {\n if (end[i]) {\n dp[i] = ssum[i - T] + i - T + 1;\n } else {\n dp[i] = dp[i - 1];\n }\n dp[i] = dp[i] % MOD;\n sum[i] = (sum[i - 1] + dp[i]) % MOD;\n ssum[i] = (ssum[i - 1] + sum[i]) % MOD;\n }\n writeln(sum.back);\n}\n"}], "negative_code": [], "src_uid": "1e55358988db2fce66b8a53daae50d83"} {"nl": {"description": "Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a \"+=\" operation that adds the right-hand side value to the left-hand side variable. For example, performing \"a += b\" when a = $$$2$$$, b = $$$3$$$ changes the value of a to $$$5$$$ (the value of b does not change).In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations \"a += b\" or \"b += a\". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $$$n$$$. What is the smallest number of operations he has to perform?", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\leq T \\leq 100$$$) — the number of test cases. Each of the following $$$T$$$ lines describes a single test case, and contains three integers $$$a, b, n$$$ ($$$1 \\leq a, b \\leq n \\leq 10^9$$$) — initial values of a and b, and the value one of the variables has to exceed, respectively.", "output_spec": "For each test case print a single integer — the smallest number of operations needed. Separate answers with line breaks.", "sample_inputs": ["2\n1 2 3\n5 4 100"], "sample_outputs": ["2\n7"], "notes": "NoteIn the first case we cannot make a variable exceed $$$3$$$ in one operation. One way of achieving this in two operations is to perform \"b += a\" twice."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint;\nimport std.bitmanip, std.complex, std.container;\nimport std.math, std.mathspecial,std.numeric;\nimport std.regex, std.typecons;\nimport core.bitop;\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\nbool chmin(T)(ref T t, in T f)\n{ if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) \n{ if (t < f) { t = f; return true; } else { return false; } }\nint binarySearch(alias pred, T)(in T[] as) \n{int lo = -1, hi = cast(int)(as.length);\nfor(;lo + 1 < hi;){const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid;}\nreturn hi;}\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const A = readLong();\n const B = readLong();\n const N = readLong();\n\n long ans;\n long a = A, b = B;\n for (; a <= N && b <= N; ) {\n if (a > b) {\n swap(a, b);\n }\n a += b;\n ++ans;\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tforeach (t; 0..readln.strip.to !(int))\n\t{\n\t\tint a, b, n;\n\t\treadf !(\" %s %s %s\") (a, b, n);\n\t\tint res = 0;\n\t\twhile (!(a > n || b > n))\n\t\t{\n\t\t\tif (a > b)\n\t\t\t{\n\t\t\t\tswap (a, b);\n\t\t\t}\n\t\t\tres += 1;\n\t\t\ta += b;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "// Code By H~$~C\n\nimport std.stdio, std.format;\nimport std.math, std.uni, std.bigint, std.numeric;\nimport std.array, std.string, std.container, std.range, std.typecons;\nimport std.algorithm, std.conv, std.functional, std.random;\n\nvoid _Main() {\n int a, b, n;\n readf!\" %d %d %d\"(a, b, n);\n if (a > b) swap(a, b);\n int cnt = 0;\n while (b <= n) {\n a += b;\n swap(a, b);\n cnt++;\n }\n writefln!\"%s\"(cnt);\n}\n\nvoid main(string[] args) {\n int tests = 1;\n readf!\" %d\"(tests);\n foreach (test; 0 .. tests) _Main();\n}\n\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const A = readLong();\n const B = readLong();\n const N = readLong();\n \n long ans;\n long a = A, b = B;\n for (; a <= N && b <= N; ) {\n if (a > b) {\n swap(a, b);\n }\n a += b;\n ++ans;\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto a = RD;\n\t\tauto b = RD;\n\t\tauto n = RD;\n\n\t\twhile (max(a, b) <= n)\n\t\t{\n\t\t\tif (a < b)\n\t\t\t\ta += b;\n\t\t\telse\n\t\t\t\tb += a;\n\t\t\t++ans[ti];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "5999a4e2fac29b5f4804639f6e949279"} {"nl": {"description": "You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones. You can do the following operation: choose two indices $$$1 \\le i , j \\le n$$$, $$$i \\ne j$$$, add $$$a_{i}$$$ to $$$a_{j}$$$, remove $$$a_{i}$$$ from $$$a$$$. Note that elements of $$$a$$$ can become bigger than $$$1$$$ after performing some operations. Also note that $$$n$$$ becomes $$$1$$$ less after the operation.What is the minimum number of operations needed to make $$$a$$$ non-decreasing, i. e. that each element is not less than the previous element?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$), the size of array $$$a$$$. Next line contains $$$n$$$ integers $$$a_{1}, a_{2}, \\ldots a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$. It's guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print a single integer, minimum number of operations needed to make $$$a$$$ non-decreasing.", "sample_inputs": ["4\n\n8\n\n0 0 1 1 1 1 1 1\n\n5\n\n1 0 0 1 1\n\n2\n\n1 0\n\n11\n\n1 1 0 0 1 0 0 1 1 1 0"], "sample_outputs": ["0\n1\n1\n3"], "notes": "NoteIn the first test case, $$$a$$$ is already non-decreasing, so you don't need to do any operations and the answer is $$$0$$$.In the second test case, you can perform an operation for $$$i = 1$$$ and $$$j = 5$$$, so $$$a$$$ will be equal to $$$[0, 0, 1, 2]$$$ and it becomes non-decreasing.In the third test case, you can perform an operation for $$$i = 2$$$ and $$$j = 1$$$, so $$$a$$$ will be equal to $$$[1]$$$ and it becomes non-decreasing."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt;\n }\n \n auto ASum = new int[N + 1];\n foreach (i; 0 .. N) {\n ASum[i + 1] = ASum[i] + A[i];\n }\n \n int ans = N;\n foreach (k; 0 .. N + 1) {\n const l = ASum[k];\n const r = (N - k) - (ASum[N] - ASum[k]);\n const cost = max(l, r);\n chmin(ans, cost);\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tint i = 0;\r\n\t\tint j = n - 1;\r\n\t\tint res = 0;\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\twhile (i < n && a[i] == 0)\r\n\t\t\t{\r\n\t\t\t\ti += 1;\r\n\t\t\t}\r\n\t\t\twhile (j >= 0 && a[j] == 1)\r\n\t\t\t{\r\n\t\t\t\tj -= 1;\r\n\t\t\t}\r\n\t\t\tif (i >= j)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tres += 1;\r\n\t\t\ti += 1;\r\n\t\t\tj -= 1;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "c247c7c382c243ab6b991c4a11bfc421"} {"nl": {"description": "There are $$$n$$$ chests. The $$$i$$$-th chest contains $$$a_i$$$ coins. You need to open all $$$n$$$ chests in order from chest $$$1$$$ to chest $$$n$$$.There are two types of keys you can use to open a chest: a good key, which costs $$$k$$$ coins to use; a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest $$$i$$$ will do $$$a_i = \\lfloor{\\frac{a_i}{2}\\rfloor}$$$, $$$a_{i+1} = \\lfloor\\frac{a_{i+1}}{2}\\rfloor, \\dots, a_n = \\lfloor \\frac{a_n}{2}\\rfloor$$$; any key (both good and bad) breaks after a usage, that is, it is a one-time use. You need to use in total $$$n$$$ keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.During the process, you are allowed to go into debt; for example, if you have $$$1$$$ coin, you are allowed to buy a good key worth $$$k=3$$$ coins, and your balance will become $$$-2$$$ coins.Find the maximum number of coins you can have after opening all $$$n$$$ chests in order from chest $$$1$$$ to chest $$$n$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\leq n \\leq 10^5$$$; $$$0 \\leq k \\leq 10^9$$$) — the number of chests and the cost of a good key respectively. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \\leq a_i \\leq 10^9$$$)  — the amount of coins in each chest. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case output a single integer  — the maximum number of coins you can obtain after opening the chests in order from chest $$$1$$$ to chest $$$n$$$. Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).", "sample_inputs": ["5\n\n4 5\n\n10 10 3 1\n\n1 2\n\n1\n\n3 12\n\n10 10 29\n\n12 51\n\n5 74 89 45 18 69 67 67 11 96 23 59\n\n2 57\n\n85 60"], "sample_outputs": ["11\n0\n13\n60\n58"], "notes": "NoteIn the first test case, one possible strategy is as follows: Buy a good key for $$$5$$$ coins, and open chest $$$1$$$, receiving $$$10$$$ coins. Your current balance is $$$0 + 10 - 5 = 5$$$ coins. Buy a good key for $$$5$$$ coins, and open chest $$$2$$$, receiving $$$10$$$ coins. Your current balance is $$$5 + 10 - 5 = 10$$$ coins. Use a bad key and open chest $$$3$$$. As a result of using a bad key, the number of coins in chest $$$3$$$ becomes $$$\\left\\lfloor \\frac{3}{2} \\right\\rfloor = 1$$$, and the number of coins in chest $$$4$$$ becomes $$$\\left\\lfloor \\frac{1}{2} \\right\\rfloor = 0$$$. Your current balance is $$$10 + 1 = 11$$$. Use a bad key and open chest $$$4$$$. As a result of using a bad key, the number of coins in chest $$$4$$$ becomes $$$\\left\\lfloor \\frac{0}{2} \\right\\rfloor = 0$$$. Your current balance is $$$11 + 0 = 11$$$. At the end of the process, you have $$$11$$$ coins, which can be proven to be maximal."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n long N, K; readf(\"%d %d\\n\", &N, &K);\r\n auto A = readln.chomp.split(\" \").map!(to!long).array;\r\n long C = A.reduce!\"a+b\" - K * N;\r\n long ans = C;\r\n long[] rs;\r\n for (int i = cast(int)(N-1); i >= 0; i--) {\r\n long[] nrs;\r\n long rd = 0;\r\n rs ~= A[i];\r\n foreach (ref r; rs) {\r\n rd += (r - r / 2);\r\n r /= 2;\r\n if (r > 0) {\r\n nrs ~= r;\r\n }\r\n }\r\n C = C - rd + K;\r\n ans = max(ans, C);\r\n rs = nrs;\r\n }\r\n writeln(ans);\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int limit = 32;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, k;\r\n\t\treadf !(\" %s %s\") (n, k);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tauto f = new long [n + 1];\r\n\t\tf[] = long.min / 2;\r\n\t\tf[0] = 0;\r\n\t\tlong res = 0;\r\n\t\tforeach (d; 0..limit)\r\n\t\t{\r\n\t\t\tforeach (i; 0..n)\r\n\t\t\t{\r\n\t\t\t\tf[i + 1] = max (f[i + 1], f[i] + a[i] - k);\r\n\t\t\t}\r\n\t\t\tres = max (res, f[n]);\r\n\t\t\ta[] /= 2;\r\n\t\t\tforeach_reverse (i; 0..n)\r\n\t\t\t{\r\n\t\t\t\tf[i + 1] = max (f[i + 1], f[i] + a[i]);\r\n\t\t\t}\r\n\t\t}\r\n\t\tres = max (res, f.maxElement);\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "9fcc55a137b5ff021fdc8e1e867ce856"} {"nl": {"description": "Monocarp is playing a computer game. Now he wants to complete the first level of this game.A level is a rectangular grid of $$$2$$$ rows and $$$n$$$ columns. Monocarp controls a character, which starts in cell $$$(1, 1)$$$ — at the intersection of the $$$1$$$-st row and the $$$1$$$-st column.Monocarp's character can move from one cell to another in one step if the cells are adjacent by side and/or corner. Formally, it is possible to move from cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$ in one step if $$$|x_1 - x_2| \\le 1$$$ and $$$|y_1 - y_2| \\le 1$$$. Obviously, it is prohibited to go outside the grid.There are traps in some cells. If Monocarp's character finds himself in such a cell, he dies, and the game ends.To complete a level, Monocarp's character should reach cell $$$(2, n)$$$ — at the intersection of row $$$2$$$ and column $$$n$$$.Help Monocarp determine if it is possible to complete the level.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then the test cases follow. Each test case consists of three lines. The first line contains a single integer $$$n$$$ ($$$3 \\le n \\le 100$$$) — the number of columns. The next two lines describe the level. The $$$i$$$-th of these lines describes the $$$i$$$-th line of the level — the line consists of the characters '0' and '1'. The character '0' corresponds to a safe cell, the character '1' corresponds to a trap cell. Additional constraint on the input: cells $$$(1, 1)$$$ and $$$(2, n)$$$ are safe.", "output_spec": "For each test case, output YES if it is possible to complete the level, and NO otherwise.", "sample_inputs": ["4\n3\n000\n000\n4\n0011\n1100\n4\n0111\n1110\n6\n010101\n101010"], "sample_outputs": ["YES\nYES\nNO\nYES"], "notes": "NoteConsider the example from the statement.In the first test case, one of the possible paths is $$$(1, 1) \\rightarrow (2, 2) \\rightarrow (2, 3)$$$.In the second test case, one of the possible paths is $$$(1, 1) \\rightarrow (1, 2) \\rightarrow (2, 3) \\rightarrow (2, 4)$$$.In the fourth test case, one of the possible paths is $$$(1, 1) \\rightarrow (2, 2) \\rightarrow (1, 3) \\rightarrow (2, 4) \\rightarrow (1, 5) \\rightarrow (2, 6)$$$."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.array, std.string, std.conv;\r\n \r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n foreach(_; 0..t)\r\n {\r\n int n;\r\n scanf(\"%d\", &n);\r\n getchar();\r\n auto str1 = readln.strip();\r\n auto str2 = readln.strip();\r\n bool flag = true;\r\n for (int i = 0; i < n; i++)\r\n {\r\n if ((str1[i] == str2[i]) && (str1[i] == '1'))\r\n {\r\n writeln(\"NO\");\r\n flag = false;\r\n break;\r\n }\r\n }\r\n if (flag)\r\n writeln(\"YES\");\r\n }\r\n}"}], "negative_code": [], "src_uid": "fefec879efd4f524de00684adee7cd19"} {"nl": {"description": "Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket. The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type. ", "output_spec": "The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.", "sample_inputs": ["3 2\n2 3 4", "5 4\n3 1 8 9 7"], "sample_outputs": ["3", "5"], "notes": "NoteIn the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.Optimal sequence of actions in the second sample case: In the first day Anastasia collects 8 pebbles of the third type. In the second day she collects 8 pebbles of the fourth type. In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type. In the fourth day she collects 7 pebbles of the fifth type. In the fifth day she collects 1 pebble of the second type. "}, "positive_code": [{"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;alias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;alias long lint;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;immutable int mod=1000000007;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\ts+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\ts+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter\n//---------------------------------------------------------------program beginning-----------------------------------------------------------\n\nvoid main()\n{\n\tint n,k;\nloop:while(read(n,k))\n\t{\n\t\tauto w=arread!int;\n\t\tsort(w);\n\t\tint ans=0;\n\t\tforeach(x;w)\n\t\t{\n\t\t\tans+=(x+k-1)/k;\n\t\t}\n\t\twriteln((ans+1)/2);\n\t}\n\t//debug system(\"pause\");\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;alias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;alias long lint;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;immutable int mod=1000000007;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~\"\\n\", ptrs)==ptrs.length;}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter\n//---------------------------------------------------------------program beginning-----------------------------------------------------------\n\nvoid main()\n{\n\tint n,k;\nloop:while(read(&n,&k))\n\t{\n\t\tauto w=arread!int;\n\t\tsort(w);\n\t\tint ans=0;\n\t\tforeach(x;w)\n\t\t{\n\t\t\tans+=(x+k-1)/k;\n\t\t}\n\t\twriteln((ans+1)/2);\n\t}\n\t//debug system(\"pause\");\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;alias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;alias long lint;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;immutable int mod=1000000007;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\ts+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\ts+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter\n//---------------------------------------------------------------program beginning-----------------------------------------------------------\nT orient_square(T)(pair!(T,T)[] figure...)if(is(typeof(figure.front*figure.front)==T))\n{\n\tauto n=figure.front,t=figure.front;\n\tfigure.popFront();\n\tT ans=0;\n\tfor(;!figure.empty;figure.popFront())\n\t{\n\t\tans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);\n\t\tt=figure.front;\n\t}\n\treturn ans+(n.fi-t.fi)*(n.se+t.se);\n}\n\nX binpow(X,Y)(X base,Y exp)if(is(typeof(exp>>1)))\n{\n\tif(exp<0)return X(0);\n\tX res=X(1);\n\twhile(exp>0)\n\t{\n\t\tif(exp&1)res*=base;\n\t\tbase*=base;\n\t\texp>>=1;\n\t}\n\treturn res;\n}\nauto binpow(X,Y,Z)(X base,Y exp,in Z mm)if(is(typeof(exp>>1)) && is(typeof(base%mm)))\n{\n\tif(mm==0) return binpow(base,exp);\n\tif(exp<0)return X(0);\n\tauto res=X(1)%mm;\n\twhile(exp>0)\n\t{\n\t\tif(exp&1)res=(res*base)%mm;\n\t\tbase*=base;\n\t\tbase%=mm;\n\t\texp>>=1;\n\t}\n\treturn res;\n}\nreal square(T)(pair!(T,T)[] figure...)if(is(typeof(figure.front*figure.front)==T))\n{\n\treturn abs(orient_square(figure))/2.000000000000000;\n}\nalias orient_square orsq;\nclass vertex(X,alias f=\"a+b\",X n=0,size_t lg=0,size_t rg=size_t.max/2)\n{\n\tprotected\n\t{\n\t\tclass node\n\t\t{\n\t\t\tX val;\n\t\t\tnode l,r;\n\t\t\tthis(){}\n\t\t};\n\t\tnode root;\n\t}\n\tpublic\n\t{\n\t\tsize_t lb=lg,rb=rg;\n\t\tX neutral=0;\n\t\tsize_t length;\n\t\talias fun=binaryFun!f;\n\t\tthis()\n\t\t{\n\t\t\troot=new node();\n\t\t\tlength=0;\n\t\t}\n\t\t\n\t}\n\tprotected\n\t{\n\t\tbool insert(node t,size_t tl,size_t tr,size_t pos,X x)\n\t\t{\n\t\t\tbool mark=false;\n\t\t\tif(!t)\n\t\t\t{\n\t\t\t\tt=new node();\n\t\t\t\tmark=true;\n\t\t\t}\n\t\t\tif(tl==tr)\n\t\t\t{\n\t\t\t\tt.val=x;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tsize_t tm=(tl+tr)>>1;\n\t\t\t\tif(pos<=tm)\n\t\t\t\t{\n\t\t\t\t\tmark=insert(t.l,tl,tm,pos,x);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmark=insert(t.r,tm+1,tr,pos,x);\n\t\t\t\t}\n\t\t\t\tif(!t.l)t.val=t.r.val;\n\t\t\t\telse if(!t.r)t.val=t.l.val;\n\t\t\t\telse t.val=fun(t.l.val,t.r.val);\n\t\t\t}\n\t\t\treturn mark;\n\t\t}\n\t\tbool erase(node t,size_t tl,size_t tr,size_t pos)\n\t\t{\n\t\t\tif(!t)return false;\n\t\t\tif(tl==tr)\n\t\t\t{\n\t\t\t\tt=null;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tsize_t tm=(tl+tr)>>1;\n\t\t\t\tif(pos<=tm)\n\t\t\t\t{\n\t\t\t\t\treturn erase(t.l,tl,tm,pos);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\treturn erase(t.r,tm+1,tr,pos);\n\t\t\t\t}\n\t\t\t\tif(!t.l)t.val=t.r.val;\n\t\t\t\telse if(!t.r)t.val=t.l.val;\n\t\t\t\telse t.val=fun(t.l.val,t.r.val);\n\t\t\t}\n\t\t}\n\t\tX cel(node t,size_t tl,size_t tr,size_t l,size_t r)\n\t\t{\n\t\t\tif(!t)return neutral;\n\t\t\tif(tl==l && tr==r)return t.val;\n\t\t\telse\n\t\t\t{\n\t\t\t\tsize_t tm=(tl+tr)>>1;\n\t\t\t\tif(r<=tm)return cel(t.l,tl,tm,l,r);\n\t\t\t\telse if(l>tm)return cel(t.r,tm+1,tr,l,r);\n\t\t\t\telse return fun(cel(t.l,tl,tm,l,tm),cel(t.r,tm+1,tr,tm+1,r));\n\t\t\t}\n\t\t}\n\t}\n\tpublic\n\t{\n\t\tvoid insert(size_t pos,X x)\n\t\t\tin\n\t\t{\n\t\t\tassert(pos>=lb && pos<=rb);\n\t\t}\n\t\tbody\n\t\t{\n\t\t\tlength+=insert(root,lb,rb,pos,x);\n\t\t}\n\t\tvoid erase(size_t pos)\n\t\t\tin\n\t\t{\n\t\t\tassert(pos>=lb && pos<=rb);\n\t\t}\n\t\tbody\n\t\t{\n\t\t\tlength-=erase(root,lb,rb,pos);\n\t\t}\n\t\tX cel(size_t l,size_t r)\n\t\t\tin\n\t\t{\n\t\t\tassert(l<=r && l>=lb && r<=rb);\n\t\t}\n\t\tbody\n\t\t{\n\t\t\treturn cel(root,lb,rb,l,r);\n\t\t}\n\t}\n};\nclass item\n{\n\tsize_t cnt,prior;\n\tint key;\n\titem l,r;\n\tthis(){}\n\tthis(int val,int rnd)\n\t{\n\t\tkey=val;\n\t\tcnt=1;\n\t\tprior=rnd;\n\t}\n};\nint cnt(item t)\n{\n\treturn t?t.cnt:0;\n}\nvoid upd_cnt(item t)\n{\n\tt.cnt=cnt(t.l)+cnt(t.r)+1;\n}\nvoid merge(ref item t,item l,item r)\n{\n\tif(!l || !r)\n\t{\n\t\tt=(l?l:r);\n\t}\n\telse\n\t{\n\t\tif(l.prior>r.prior)\n\t\t{\n\t\t\tmerge(l.r,l.r,r);\n\t\t\tt=l;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmerge(r.l,l,r.l);\n\t\t\tt=r;\n\t\t}\n\t}\n\tupd_cnt(t);\n}\nvoid split(item t,int key,ref item l,ref item r)\n{\n\tif(!t)l=r=null;\n\telse if(keyt.prior)\n\t\t{\n\t\t\tsplit(t,it.key,it.l,it.r);\n\t\t\tt=it;\n\t\t}\n\t\telse insert(it.keyr.prior)\n\t\t{\n\t\t\tmerge(l.r,l.r,r);\n\t\t\tt=l;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmerge(r.l,l,r.l);\n\t\t\tt=r;\n\t\t}\n\t}\n\tupd_cnt(t);\n}\nvoid split(nitem t,int key,ref nitem l,ref nitem r,int add=0)\n{\n\tif(!t){l=r=null;return;}\n\tint cur=add=cnt(t.l);\n\tif(key<=cur)\n\t{\n\t\tsplit(t.l,key,l,t.l,add);\n\t\tr=t;\n\t}\n\telse\n\t{\n\t\tsplit(t.r,key,t.r,r,add+1+cnt(t.l));\n\t\tl=t;\n\t}\n\tupd_cnt(t);\n}\nvoid insert(ref nitem t,nitem it)\n{\n\tnitem l,r;\n\tsplit(t,it.key,l,r);\n\tmerge(l,l,it);\n\tmerge(t,l,r);\n\tupd_cnt(t);\n}\n\nvoid erase(ref nitem t,int key)\n{\n\tif(t.key==key)merge(t,t.l,t.r);\n\telse erase(key1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~\"\\n\", ptrs)==ptrs.length;}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter\n//---------------------------------------------------------------program beginning-----------------------------------------------------------\n\nvoid main()\n{\n\tint n,k;\nloop:while(read(&n,&k))\n\t{\n\t\tauto w=arread!int;\n\t\tsort(w);\n\t\tint ans=0;\n\t\tforeach(p;0..n-1)\n\t\t{\n\t\t\tans+=w[p]/(2*k);\n\t\t\tw[p]%=2*k;\n\t\t\tif(w[p]==0)continue;\n\t\t\tans++;\n\t\t\tif(w[p]<=k)w[p+1]-=k;\n\t\t\tw[p]=0;\n\t\t}\n\t\tif(w[n-1]>0)\n\t\t{\n\t\t\tans+=w[n-1]/(2*k);\n\t\t\tw[n-1]%=2*k;\n\t\t\tif(w[n-1])ans++;\n\t\t}\n\t\twriteln(ans);\n\t}\n\t//debug system(\"pause\");\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nimport core.stdc.stdlib : system;\n//import core.stdc.stdio;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;alias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;alias long lint;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;immutable int mod=1000000007;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tsize_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{return readf(replicate(\" %s\", ptrs.length), ptrs)==ptrs.length;}\nbool read(T...)(T ptrs) if (ptrs.length > 0)\n{return readf(replicate(\" %s\", ptrs.length)~\"\\n\", ptrs)==ptrs.length;}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter\n//---------------------------------------------------------------program beginning-----------------------------------------------------------\n\nvoid main()\n{\n\tint n,k;\nloop:while(read(&n,&k))\n\t{\n\t\tauto w=arread!int;\n\t\tsort(w);\n\t\tint ans=0;\n\t\tforeach(x;w)\n\t\t{\n\t\t\tans+=(x+k-1)/k;\n\t\t}\n\t\twriteln(ans);\n\t}\n\t//debug system(\"pause\");\n}"}], "src_uid": "3992602be20a386f0ec57c51e14b39c5"} {"nl": {"description": "You've got an array consisting of n integers: a[1], a[2], ..., a[n]. Moreover, there are m queries, each query can be described by three integers li, ri, ki. Query li, ri, ki means that we should add to each element a[j], where li ≤ j ≤ ri.Record means the binomial coefficient, or the number of combinations from y elements into groups of x elements.You need to fulfil consecutively all queries and then print the final array.", "input_spec": "The first line contains integers n, m (1 ≤ n, m ≤ 105). The second line contains n integers a[1], a[2], ..., a[n] (0 ≤ ai ≤ 109) — the initial array. Next m lines contain queries in the format li, ri, ki — to all elements of the segment li... ri add number (1 ≤ li ≤ ri ≤ n; 0 ≤ k ≤ 100).", "output_spec": "Print n integers: the i-th number is the value of element a[i] after all the queries. As the values can be rather large, print them modulo 1000000007 (109 + 7).", "sample_inputs": ["5 1\n0 0 0 0 0\n1 5 0", "10 2\n1 2 3 4 5 0 0 0 0 0\n1 6 1\n6 10 2"], "sample_outputs": ["1 1 1 1 1", "2 4 6 8 10 7 3 6 10 15"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimmutable int MN = 100010;\nimmutable int MK = 105;\nimmutable int MD = cast(int)(1e9+7.1);\n\nint main() {\n long[MK][] b = new long[MK][](MN);\n long[MK][] c = new long[MK][](MN+MK);\n c[0][0] = 1;\n for (int i = 1; i < MN+MK; i++) {\n c[i][0] = 1;\n c[i][1..MK] = (c[i-1][0..MK-1] + c[i-1][1..MK]) % MD;\n }\n int n, m;\n readf(\"%d %d\\n\", &n, &m);\n string[] input = split(readln());\n long[] a = new long[](n);\n foreach (i, s; input) {\n a[i] = to!int(s);\n }\n for (int i = 0; i < m; i++) {\n int l, r, k;\n readf(\"%d %d %d\\n\", &l, &r, &k); l--;\n int f = 1;\n for (int j = 0; j <= k; j++) {\n b[l][k-j] += c[l][j]*f; b[l][k-j] %= MD;\n b[r][k-j] -= c[l][j]*f; b[r][k-j] %= MD;\n f = -f;\n }\n }\n long b1[MK];\n for (int i = 0; i < n; i++) {\n long r = a[i];\n b1[] += b[i][];\n b1[] %= MD;\n for (int k = 0; k < MK; k++) {\n r += b1[k]*c[i+k][k]%MD; r %= MD;\n }\n r = (r+MD)%MD;\n write(cast(int)r);\n if (i == n-1) {\n write(\"\\n\");\n } else {\n write(\" \");\n }\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimmutable int MN = 100010;\nimmutable int MK = 105;\nimmutable int MD = cast(int)(1e9+7.1);\n\nint main() {\n long[][] b = new long[][](MN, MK);\n long[][] c = new long[][](MN+MK, MK);\n c[0][0] = 1;\n for (int i = 1; i < MN+MK; i++) {\n c[i][0] = 1;\n c[i][1..MK] = (c[i-1][0..MK-1] + c[i-1][1..MK]) % MD;\n }\n int n, m;\n readf(\"%d %d\\n\", &n, &m);\n string[] input = split(readln());\n auto a = map!(to!int)(input[]);\n for (int i = 0; i < m; i++) {\n int l, r, k;\n readf(\"%d %d %d\\n\", &l, &r, &k); l--;\n int f = 1;\n for (int j = 0; j <= k; j++) {\n b[l][k-j] += c[l][j]*f; b[l][k-j] %= MD;\n b[r][k-j] -= c[l][j]*f; b[r][k-j] %= MD;\n f = -f;\n }\n }\n long b1[MK];\n for (int i = 0; i < n; i++) {\n long r = a[i];\n b1[] += b[i][];\n b1[] %= MD;\n for (int k = 0; k < MK; k++) {\n r += b1[k]*c[i+k][k]%MD; r %= MD;\n }\n r = (r+MD)%MD;\n write(cast(int)r);\n if (i == n-1) {\n write(\"\\n\");\n } else {\n write(\" \");\n }\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimmutable int MN = 100010;\nimmutable int MK = 105;\nimmutable int MD = cast(int)(1e9+7.1);\n\nint main() {\n long[MK][] b = new long[MK][MN];\n long[MK][] c = new long[MK][MN+MK];\n c[0][0] = 1;\n for (int i = 1; i < MN+MK; i++) {\n c[i][0] = 1;\n c[i][1..MK] = (c[i-1][0..MK-1] + c[i-1][1..MK]) % MD;\n }\n int n, m;\n readf(\"%d %d\\n\", &n, &m);\n string[] input = split(readln());\n auto a = map!(to!int)(input[]);\n for (int i = 0; i < m; i++) {\n int l, r, k;\n readf(\"%d %d %d\\n\", &l, &r, &k); l--;\n int f = 1;\n for (int j = 0; j <= k; j++) {\n b[l][k-j] += c[l][j]*f; b[l][k-j] %= MD;\n b[r][k-j] -= c[l][j]*f; b[r][k-j] %= MD;\n f = -f;\n }\n }\n long b1[MK];\n for (int i = 0; i < n; i++) {\n long r = a[i];\n b1[] += b[i][];\n b1[] %= MD;\n for (int k = 0; k < MK; k++) {\n r += b1[k]*c[i+k][k]%MD; r %= MD;\n }\n r = (r+MD)%MD;\n write(cast(int)r);\n if (i == n-1) {\n write(\"\\n\");\n } else {\n write(\" \");\n }\n }\n return 0;\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimmutable int MN = 100010;\nimmutable int MK = 105;\nimmutable int MD = cast(int)(1e9+7.1);\n\nint main() {\n long[MK][] b = new long[MK][](MN);\n long[MK][] c = new long[MK][](MN+MK);\n\n c[0][0] = 1;\n foreach (i; 1..MN+MK) {\n c[i][0] = 1;\n c[i][1..MK] = (c[i-1][0..MK-1] + c[i-1][1..MK]) % MD;\n }\n\n int n, m;\n readf(\"%d %d\\n\", &n, &m);\n string[] input = split(readln());\n long[] a = new long[](n);\n foreach (i; 0..n) {\n a[i] = to!int(input[i]);\n }\n foreach (i; 0..m) {\n int l, r, k;\n readf(\"%d %d %d\\n\", &l, &r, &k); l--;\n int f = 1;\n foreach_reverse (j; 0..k+1) {\n b[l][j] += c[l][k-j]*f; b[l][j] %= MD;\n b[r][j] -= c[l][k-j]*f; b[r][j] %= MD;\n f = -f;\n }\n }\n long[] b1 = new long[](MK);\n foreach (i; 0..n) {\n b1[] += b[i][];\n b1[] %= MD;\n for (int k = 0; k < MK; k++) {\n a[i] += b1[k]*c[i+k][k]%MD; a[i] %= MD;\n }\n a[i] = (a[i]+MD)%MD;\n }\n writeln(join(map!(to!string)(a[0..n]), \" \"));\n return 0;\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimmutable int MN = 100010;\nimmutable int MK = 105;\nimmutable int MD = cast(int)(1e9+7.1);\n\nint main() {\n long[MK][] b = new long[MK][](MN);\n long[MK][] c = new long[MK][](MN+MK);\n\n c[0][0] = 1;\n foreach (i; 1..MN+MK) {\n c[i][0] = 1;\n c[i][1..MK] = (c[i-1][0..MK-1] + c[i-1][1..MK]) % MD;\n }\n\n int n, m;\n readf(\"%d %d\\n\", &n, &m);\n string[] input = split(readln());\n long[] a = new long[](n);\n for (int i = 0; i < n; i++) {\n a[i] = to!int(input[i]);\n }\n for (int i = 0; i < m; i++) {\n int l, r, k;\n readf(\"%d %d %d\\n\", &l, &r, &k); l--;\n int f = 1;\n foreach_reverse (j; 0..k+1) {\n b[l][j] += c[l][k-j]*f; b[l][j] %= MD;\n b[r][j] -= c[l][k-j]*f; b[r][j] %= MD;\n f = -f;\n }\n }\n long[] b1 = new long[](MK);\n foreach (i; 0..n) {\n b1[] += b[i][];\n b1[] %= MD;\n for (int k = 0; k < MK; k++) {\n a[i] += b1[k]*c[i+k][k]%MD; a[i] %= MD;\n }\n a[i] = (a[i]+MD)%MD;\n }\n writeln(join(map!(to!string)(a[0..n]), \" \"));\n return 0;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimmutable int MN = 100010;\nimmutable int MK = 105;\nimmutable int MD = cast(int)(1e9+7.1);\n\nint main() {\n long[MK][] b = new long[MK][](MN);\n long[MK][] c = new long[MK][](MN+MK);\n\n c[0][0] = 1;\n foreach (i; 1..MN+MK) {\n c[i][0] = 1;\n c[i][1..MK] = (c[i-1][0..MK-1] + c[i-1][1..MK]) % MD;\n }\n\n int n, m;\n readf(\"%d %d\\n\", &n, &m);\n string[] input = split(readln());\n long[] a = new long[](n);\n for (int i = 0; i < n; i++) {\n a[i] = to!int(input[i]);\n }\n for (int i = 0; i < m; i++) {\n int l, r, k;\n readf(\"%d %d %d\\n\", &l, &r, &k); l--;\n int f = 1;\n foreach_reverse (j; 0..k+1) {\n b[l][j] += c[l][k-j]*f; b[l][j] %= MD;\n b[r][j] -= c[l][k-j]*f; b[r][j] %= MD;\n f = -f;\n }\n }\n long[] b1 = new long[](MK);\n long[] r = new long[](n);\n foreach (i; 0..n) {\n b1[] += b[i][];\n b1[] %= MD;\n for (int k = 0; k < MK; k++) {\n r[i] += b1[k]*c[i+k][k]%MD; r[i] %= MD;\n }\n r[i] = (r[i]+MD)%MD;\n }\n writeln(join(map!(to!string)(r), \" \"));\n return 0;\n}\n"}], "src_uid": "d0f81a3fb97fbdbed1b72d013c2f6ed5"} {"nl": {"description": "Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?There are $$$n$$$ students living in a building, and for each of them the favorite drink $$$a_i$$$ is known. So you know $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i$$$ ($$$1 \\le a_i \\le k$$$) is the type of the favorite drink of the $$$i$$$-th student. The drink types are numbered from $$$1$$$ to $$$k$$$.There are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are $$$k$$$ types of drink sets, the $$$j$$$-th type contains two portions of the drink $$$j$$$. The available number of sets of each of the $$$k$$$ types is infinite.You know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly $$$\\lceil \\frac{n}{2} \\rceil$$$, where $$$\\lceil x \\rceil$$$ is $$$x$$$ rounded up.After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if $$$n$$$ is odd then one portion will remain unused and the students' teacher will drink it.What is the maximum number of students that can get their favorite drink if $$$\\lceil \\frac{n}{2} \\rceil$$$ sets will be chosen optimally and students will distribute portions between themselves optimally?", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 1\\,000$$$) — the number of students in the building and the number of different drinks. The next $$$n$$$ lines contain student's favorite drinks. The $$$i$$$-th line contains a single integer from $$$1$$$ to $$$k$$$ — the type of the favorite drink of the $$$i$$$-th student.", "output_spec": "Print exactly one integer — the maximum number of students that can get a favorite drink.", "sample_inputs": ["5 3\n1\n3\n1\n1\n2", "10 3\n2\n1\n3\n2\n3\n3\n1\n3\n1\n2"], "sample_outputs": ["4", "9"], "notes": "NoteIn the first example, students could choose three sets with drinks $$$1$$$, $$$1$$$ and $$$2$$$ (so they will have two sets with two drinks of the type $$$1$$$ each and one set with two drinks of the type $$$2$$$, so portions will be $$$1, 1, 1, 1, 2, 2$$$). This way all students except the second one will get their favorite drinks.Another possible answer is sets with drinks $$$1$$$, $$$2$$$ and $$$3$$$. In this case the portions will be $$$1, 1, 2, 2, 3, 3$$$. Then all the students except one will gain their favorite drinks. The only student that will not gain the favorite drink will be a student with $$$a_i = 1$$$ (i.e. the first, the third or the fourth)."}, "positive_code": [{"source_code": "import std.stdio;\n\nvoid main() {\n int n, k;\n int[1005] count;\n \n readf(\" %s %s\", n, k);\n foreach(i; 0..n) {\n int a;\n readf(\" %s\", a);\n count[a]++;\n }\n\n int odds = 0;\n foreach(c; count) {\n if (c % 2 == 1) {\n odds++;\n }\n }\n\n int result = n - odds / 2;\n writeln(result);\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\tint k = read.to!int;\n\t\n\tint[] as;\n\tforeach(i; 0 .. n) as ~= read.to!int;\n\t\n\tint[int] ac;\n\tforeach(a; as){\n\t\tif(a !in ac) ac[a] = 0;\n\t\tac[a] += 1;\n\t}\n\t\n\tint ans;\n\tint ocnt = n % 2;\n\tint[] ks = ac.keys;\n\tforeach(a; ks){\n\t\tint c = ac[a];\n\t\tif(c % 2 == 0) ans += c;\n\t\telse{\n\t\t\tif(ocnt >= 1) ans += c, ocnt = 0;\n\t\t\telse ans += c - 1, ocnt = 1;\n\t\t}\n\t\tlog(\"a:\", a, \"cnt:\", c, \"ocnt:\", ocnt, \"ans:\", ans);\n\t}\n\t\n\tans.writeln;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto k = RD!int;\n\tauto cnt = new long[](k);\n\tforeach (i; 0..n)\n\t{\n\t\tauto a = RD!int-1;\n\t\t++cnt[a];\n\t}\n\tlong ans;\n\tlong used;\n\tforeach (i; 0..k)\n\t{\n\t\tauto c = cnt[i] / 2;\n\t\tans += c * 2;\n\t\tused += c;\n\t\tcnt[i] -= c * 2;\n\t}\n\tauto remain = n - used * 2 + n % 2;\n\tans += remain / 2;\n\n\twriteln(ans);\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "dceeb739a56bb799550138aa8c127996"} {"nl": {"description": "A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).You are given a permutation of $$$1,2,\\dots,n$$$, $$$[a_1,a_2,\\dots,a_n]$$$. For integers $$$i$$$, $$$j$$$ such that $$$1\\le i<j\\le n$$$, define $$$\\operatorname{mn}(i,j)$$$ as $$$\\min\\limits_{k=i}^j a_k$$$, and define $$$\\operatorname{mx}(i,j)$$$ as $$$\\max\\limits_{k=i}^j a_k$$$.Let us build an undirected graph of $$$n$$$ vertices, numbered $$$1$$$ to $$$n$$$. For every pair of integers $$$1\\le i<j\\le n$$$, if $$$\\operatorname{mn}(i,j)=a_i$$$ and $$$\\operatorname{mx}(i,j)=a_j$$$ both holds, or $$$\\operatorname{mn}(i,j)=a_j$$$ and $$$\\operatorname{mx}(i,j)=a_i$$$ both holds, add an undirected edge of length $$$1$$$ between vertices $$$i$$$ and $$$j$$$.In this graph, find the length of the shortest path from vertex $$$1$$$ to vertex $$$n$$$. We can prove that $$$1$$$ and $$$n$$$ will always be connected via some path, so a shortest path always exists.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 5\\cdot 10^4$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1\\le n\\le 2.5\\cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\\ldots$$$, $$$a_n$$$ ($$$1\\le a_i\\le n$$$). It's guaranteed that $$$a$$$ is a permutation of $$$1$$$, $$$2$$$, $$$\\dots$$$, $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5\\cdot 10^5$$$.", "output_spec": "For each test case, print a single line containing one integer — the length of the shortest path from $$$1$$$ to $$$n$$$.", "sample_inputs": ["5\n\n1\n\n1\n\n2\n\n1 2\n\n5\n\n1 4 2 3 5\n\n5\n\n2 1 5 3 4\n\n10\n\n7 4 8 1 6 10 3 5 2 9"], "sample_outputs": ["0\n1\n1\n4\n6"], "notes": "NoteThe following are illustrations of constructed graphs in example test cases. the constructed graph in test case 1 the constructed graph in test case 2 the constructed graph in test case 3 the constructed graph in test case 4 the constructed graph in test case 5 "}, "positive_code": [{"source_code": "\nimport std;\nvoid main(){\n\tauto t=readln.strip.to!int;\n\tforeach(_;0..t){\n\t\tauto n=readln.strip.to!int;\n\t\tauto a=readln.strip.split.to!(int[]);\n\t\tif(a.length==1){ writeln(0); continue; }\n\t\tint u=iota(n).maxElement!(i=>a[i]);\n\t\tint v=iota(n).minElement!(i=>a[i]);\n\t\tif(vx[maxi]) swap(mini,maxi);\n\t\t\tfor(int i=2;ix[maxi]){\n\t\t\t\t\tr+=maxia[i]);\n\t\tint v=iota(n).minElement!(i=>a[i]);\n\t\tif(vx[maxi]) swap(mini,maxi);\n\t\t\tfor(int i=2;ix[maxi]){\n\t\t\t\t\tif(minix-1).array;\n\t\tauto maxtree=S!(max,tuple(0,-1))(a.zip(iota(n)));\n\t\tauto mintree=S!(min,tuple(inf,-1))(a.zip(iota(n)));\n\t\tauto ip=0.repeat(n).array;\n\t\tforeach(i,x;a) ip[x]=to!int(i);\n\t\tauto indextree=S!(min,n)(n.repeat(n));\n\t\tauto next_smaller=n.repeat(n).array;\n\t\tauto next_larger=n.repeat(n).array;\n\t\tforeach_reverse(i;0..n){\n\t\t\tnext_smaller[i]=indextree.query(0,a[i]);\n\t\t\tnext_larger[i]=indextree.query(a[i],n);\n\t\t\tindextree[a[i]]=i;\n\t\t}\n\t\tauto dist=chain(only(0),inf.repeat(n)).array;\n\t\tforeach(i;0..n-1){\n\t\t\tint[2] u=[\n\t\t\t\tmaxtree.query(i+1,min(n,next_smaller[i]+1))[1],\n\t\t\t\tmintree.query(i+1,min(n,next_larger[i]+1))[1],\n\t\t\t];\n\t\t\tforeach(j;u) dist[j]=min(dist[j],dist[i]+1);\n\t\t}\n\t\twriteln(dist[n-1]);\n\t}\n}\n"}], "negative_code": [{"source_code": "\nimport std;\nvoid main(){\n\tauto t=readln.strip.to!int;\n\tforeach(_;0..t){\n\t\tauto n=readln.strip.to!int;\n\t\tauto a=readln.strip.split.to!(int[]);\n\t\tint mini=0,maxi=0,r=0;\n\t\tfor(int i=1;ia[maxi]&&maxi<=mini;\n\t\t\tif(a[i]a[maxi]){\n\t\t\t\tif(maxia[maxi]&&maxi<=mini;\n\t\t\tif(a[i]a[maxi]){\n\t\t\t\tif(maxia[maxi]&&maxi<=mini;\n\t\t\tif(a[i]a[maxi]) maxi=i;\n\t\t}\n\t\tr+=(mini!=n-1&&mini!=0)+(maxi!=n-1&&maxi!=0);\n\t\twriteln(r);\n\t}\n}\n"}], "src_uid": "2592836c1457efda9ad333524abfdf56"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$, consisting of $$$n$$$ integers each.Let's define a function $$$f(a, b)$$$ as follows: let's define an array $$$c$$$ of size $$$n$$$, where $$$c_i = a_i \\oplus b_i$$$ ($$$\\oplus$$$ denotes bitwise XOR); the value of the function is $$$c_1 \\mathbin{\\&} c_2 \\mathbin{\\&} \\cdots \\mathbin{\\&} c_n$$$ (i.e. bitwise AND of the entire array $$$c$$$). Find the maximum value of the function $$$f(a, b)$$$ if you can reorder the array $$$b$$$ in an arbitrary way (leaving the initial order is also an option).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the size of arrays $$$a$$$ and $$$b$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i < 2^{30}$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$0 \\le b_i < 2^{30}$$$). The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print one integer — the maximum value of the function $$$f(a, b)$$$ if you can reorder the array $$$b$$$ in an arbitrary way.", "sample_inputs": ["3\n\n5\n\n1 0 0 3 3\n\n2 3 2 1 0\n\n3\n\n1 1 1\n\n0 0 3\n\n8\n\n0 1 2 3 4 5 6 7\n\n7 6 5 4 3 2 1 0"], "sample_outputs": ["2\n0\n7"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\r\nimport core.memory;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").map!(to!uint).array;\r\n auto B = readln.chomp.split(\" \").map!(to!uint).array;\r\n\r\n bool f(int s) {\r\n int[uint] d;\r\n for (int i = 0; i < N; i++) {\r\n uint ap = A[i] & s;\r\n d[ap] = d.get(ap, 0) + 1;\r\n uint nbp = ~B[i] & s;\r\n d[nbp] = d.get(nbp, 0) - 1;\r\n }\r\n foreach (k, v; d) {\r\n if (v != 0) return false;\r\n }\r\n return true;\r\n }\r\n\r\n uint ans = 0;\r\n for (int k = 31; k >= 0; k--) {\r\n if (f(ans | (1< b) ? a : b; }\nT min(T = long)(T a, T b){ return (a < b) ? a : b; }\n"}], "negative_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nint[][] adj;\nbool[] vis;\nll[] citizen;\nll cap;\n\ntup dfs(int v){\n vis[v] = 1;\n ll takeable = 0, leafs = 0;\n foreach(u; adj[v]){\n if(!vis[u]){\n auto res = dfs(u);\n takeable += res.x;\n leafs += res.y;\n }\n }\n if(adj[v].length == 0){\n return tup(cap - citizen[v-1], 1);\n }else{\n ll taken = min(takeable, citizen[v-1]);\n takeable -= taken;\n citizen[v-1] -= taken;\n if(citizen[v-1]){\n return tup(takeable, leafs);\n }else{\n ll add = (citizen[v-1]/leafs) + (citizen[v-1] % leafs != 0);\n if(add % leafs != 0){\n takeable += leafs - (add % leafs);\n }\n citizen[v-1] = add;\n return tup(takeable, leafs);\n }\n }\n}\n\nvoid dfssum(int v, ll summ){\n vis[v] = 1;\n foreach(u; adj[v]){\n if(!vis[u]){\n dfssum(v, summ + citizen[v-1]);\n }\n }\n if(adj[v].length == 0){\n citizen[v-1] += summ;\n }\n}\n\nvoid play(){\n int n;\n n = rd!int;\n adj = new int[][](n+1, 0);\n vis = new bool[](n+1);\n foreach(i; 2..n+1){\n adj[rd!int] ~= i;\n }\n citizen = rdarr;\n ll totsum = 0, leafs = 0;\n foreach(el; citizen){ totsum += el; }\n foreach(i; 2..n+1){\n if(adj[i].length == 0) ++leafs;\n }\n cap = totsum/leafs + (totsum % leafs != 0);\n dfs(1);\n dfssum(1, 0);\n\n ll maxsum = cap;\n // Check leafs for max\n foreach(i; 2..n+1){\n if(adj[i].length == 0){\n maxsum = max(cap, maxsum);\n }\n }\n writeln(maxsum);\n}\n\nint main(){\n long t = 1;\n /* t = rd; // Toggle! */\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, \"x\", long, \"y\");\nT max(T = long)(T a, T b){ return (a > b) ? a : b; }\nT min(T = long)(T a, T b){ return (a < b) ? a : b; }\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nint[][] adj;\nbool[] vis;\nll[] citizen;\n\ntup dfs(int v){\n vis[v] = 1;\n ll leafs = 0, nodesum = 0, maxnode = 0;\n foreach(u; adj[v]){\n if(!vis[u]){\n auto res = dfs(u);\n nodesum += res.x;\n maxnode = max(res.x, maxnode);\n leafs += res.y;\n }\n }\n if(adj[v].length == 0){\n return tup(citizen[v-1], 1);\n }else{\n nodesum += citizen[v-1];\n ll dilute = nodesum/leafs + (nodesum % leafs != 0);\n return tup(citizen[v-1] = max(dilute, maxnode), leafs);\n }\n}\n\n\nvoid play(){\n int n;\n n = rd!int;\n adj = new int[][](n+1, 0);\n vis = new bool[](n+1);\n foreach(i; 2..n+1){\n adj[rd!int] ~= i;\n }\n citizen = rdarr;\n ll totsum = 0, leafs = 0;\n foreach(el; citizen){ totsum += el; }\n foreach(i; 2..n+1){\n if(adj[i].length == 0) ++leafs;\n }\n dfs(1);\n vis[] = 0;\n show(citizen);\n\n // Check leafs for max\n writeln(citizen.maxElement);\n}\n\nint main(){\n long t = 1;\n /* t = rd; // Toggle! */\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, \"x\", long, \"y\");\nT max(T = long)(T a, T b){ return (a > b) ? a : b; }\nT min(T = long)(T a, T b){ return (a < b) ? a : b; }\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nint[][] adj;\nbool[] vis;\nll[] citizen;\nll cap;\n\ntup dfs(int v){\n vis[v] = 1;\n ll takeable = 0, leafs = 0;\n foreach(u; adj[v]){\n if(!vis[u]){\n auto res = dfs(u);\n takeable += res.x;\n leafs += res.y;\n }\n }\n if(adj[v].length == 0){\n return tup(cap - citizen[v-1], 1);\n }else{\n ll taken = min(takeable, citizen[v-1]);\n takeable -= taken;\n citizen[v-1] -= taken;\n if(citizen[v-1]){\n return tup(takeable, leafs);\n }else{\n ll add = (citizen[v-1]/leafs) + (citizen[v-1] % leafs != 0);\n if(add % leafs != 0){\n takeable += leafs - (add % leafs);\n }\n citizen[v-1] = add;\n return tup(takeable, leafs);\n }\n }\n}\n\nvoid dfssum(int v, ll summ){\n vis[v] = 1;\n foreach(u; adj[v]){\n if(!vis[u]){\n dfssum(u, summ + citizen[v-1]);\n }\n }\n if(adj[v].length == 0){\n citizen[v-1] += summ;\n }\n}\n\nvoid play(){\n int n;\n n = rd!int;\n adj = new int[][](n+1, 0);\n vis = new bool[](n+1);\n foreach(i; 2..n+1){\n adj[rd!int] ~= i;\n }\n citizen = rdarr;\n ll totsum = 0, leafs = 0;\n foreach(el; citizen){ totsum += el; }\n foreach(i; 2..n+1){\n if(adj[i].length == 0) ++leafs;\n }\n cap = totsum/leafs + (totsum % leafs != 0);\n dfs(1);\n vis[] = 0;\n /* dfssum(1, 0); */\n show(citizen);\n\n ll maxsum = cap;\n // Check leafs for max\n foreach(i; 1..n+1){\n if(adj[i].length == 0){\n maxsum = max(citizen[i-1], maxsum);\n }else if(citizen[i-1]){\n maxsum = max(cap + citizen[i-1], maxsum);\n }\n }\n writeln(maxsum);\n}\n\nint main(){\n long t = 1;\n /* t = rd; // Toggle! */\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, \"x\", long, \"y\");\nT max(T = long)(T a, T b){ return (a > b) ? a : b; }\nT min(T = long)(T a, T b){ return (a < b) ? a : b; }\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nint[][] adj;\nbool[] vis;\nll[] citizen;\n\ntup dfs(int v){\n vis[v] = 1;\n ll leafs = 0, nodesum = 0, maxnode = 0;\n foreach(u; adj[v]){\n if(!vis[u]){\n auto res = dfs(u);\n nodesum += res.x;\n maxnode = max(res.x, maxnode);\n leafs += res.y;\n }\n }\n if(adj[v].length == 0){\n return tup(citizen[v-1], 1);\n }else{\n nodesum += citizen[v-1];\n ll dilute = nodesum/leafs + (nodesum % leafs != 0);\n citizen[v-1] = max(dilute, maxnode);\n return tup(nodesum, leafs);\n }\n}\n\n\nvoid play(){\n int n;\n n = rd!int;\n adj = new int[][](n+1, 0);\n vis = new bool[](n+1);\n foreach(i; 2..n+1){\n adj[rd!int] ~= i;\n }\n citizen = rdarr;\n ll totsum = 0, leafs = 0;\n foreach(el; citizen){ totsum += el; }\n foreach(i; 2..n+1){\n if(adj[i].length == 0) ++leafs;\n }\n dfs(1);\n vis[] = 0;\n show(citizen);\n\n // Check leafs for max\n writeln(citizen.maxElement); }\n\nint main(){\n long t = 1;\n /* t = rd; // Toggle! */\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, \"x\", long, \"y\");\nT max(T = long)(T a, T b){ return (a > b) ? a : b; }\nT min(T = long)(T a, T b){ return (a < b) ? a : b; }\n"}], "src_uid": "18cf79b50d0a389e6c3afd5d2f6bd9ed"} {"nl": {"description": "You are given a tree (a connected undirected graph without cycles) of $$$n$$$ vertices. Each of the $$$n - 1$$$ edges of the tree is colored in either black or red.You are also given an integer $$$k$$$. Consider sequences of $$$k$$$ vertices. Let's call a sequence $$$[a_1, a_2, \\ldots, a_k]$$$ good if it satisfies the following criterion: We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from $$$a_1$$$ and ending at $$$a_k$$$. Start at $$$a_1$$$, then go to $$$a_2$$$ using the shortest path between $$$a_1$$$ and $$$a_2$$$, then go to $$$a_3$$$ in a similar way, and so on, until you travel the shortest path between $$$a_{k-1}$$$ and $$$a_k$$$. If you walked over at least one black edge during this process, then the sequence is good. Consider the tree on the picture. If $$$k=3$$$ then the following sequences are good: $$$[1, 4, 7]$$$, $$$[5, 5, 3]$$$ and $$$[2, 3, 7]$$$. The following sequences are not good: $$$[1, 4, 6]$$$, $$$[5, 5, 5]$$$, $$$[3, 7, 3]$$$.There are $$$n^k$$$ sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo $$$10^9+7$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 10^5$$$, $$$2 \\le k \\le 100$$$), the size of the tree and the length of the vertex sequence. Each of the next $$$n - 1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$ and $$$x_i$$$ ($$$1 \\le u_i, v_i \\le n$$$, $$$x_i \\in \\{0, 1\\}$$$), where $$$u_i$$$ and $$$v_i$$$ denote the endpoints of the corresponding edge and $$$x_i$$$ is the color of this edge ($$$0$$$ denotes red edge and $$$1$$$ denotes black edge).", "output_spec": "Print the number of good sequences modulo $$$10^9 + 7$$$.", "sample_inputs": ["4 4\n1 2 1\n2 3 1\n3 4 1", "4 6\n1 2 0\n1 3 0\n1 4 0", "3 5\n1 2 1\n2 3 0"], "sample_outputs": ["252", "0", "210"], "notes": "NoteIn the first example, all sequences ($$$4^4$$$) of length $$$4$$$ except the following are good: $$$[1, 1, 1, 1]$$$ $$$[2, 2, 2, 2]$$$ $$$[3, 3, 3, 3]$$$ $$$[4, 4, 4, 4]$$$ In the second example, all edges are red, hence there aren't any good sequences."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nimmutable long MOD = 10^^9 + 7;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1];\n auto uf = new UnionFind(N);\n foreach (_; 0..N-1) {\n s = readln.split.map!(to!int);\n if (s[2] == 0) {\n uf.unite(s[0]-1, s[1]-1);\n }\n }\n\n long ans = powmod(N, K, MOD);\n\n foreach (i; 0..N) {\n if (uf.find(i) != i) continue;\n long x = -uf.table[i];\n ans -= powmod(x, K, MOD);\n ans %= MOD;\n }\n\n ans = (ans + MOD) % MOD;\n ans.writeln;\n}\n\n\nclass UnionFind {\n int N;\n int[] table;\n\n this(int n) {\n N = n;\n table = new int[](N);\n fill(table, -1);\n }\n\n int find(int x) {\n return table[x] < 0 ? x : (table[x] = find(table[x]));\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (table[x] > table[y]) swap(x, y);\n table[x] += table[y];\n table[y] = x;\n }\n}\n\nlong powmod(long a, long x, long m) {\n long ret = 1;\n while (x) {\n if (x % 2) ret = ret * a % m;\n a = a * a % m;\n x /= 2;\n }\n return ret;\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\n\nclass DisjointSet {\n private:\n int [] p, h, s;\n int n;\n public:\n this (int _n) {\n n = _n;\n p = iota (0, n).array;\n h = new int[n];\n s = uninitializedArray!(int[])(n);\n s[] = 1;\n }\n int findSet (int x) pure nothrow @nogc {\n if (p[x] == x) {\n return x;\n }\n return p[x] = findSet (p[x]);\n }\n void merge (int i, int j) pure nothrow @nogc {\n i = findSet (i);\n j = findSet (j);\n if (i != j) {\n if (h[i] < h[j]) {\n p[i] = j;\n s[j] = s[i] + s[j];\n } else if (h[i] > h[j]) {\n p[j] = i;\n s[i] = s[i] + s[j];\n } else {\n p[i] = j;\n ++h[j];\n s[j] = s[i] + s[j];\n }\n }\n }\n}\n\nclass InputReader {\n private:\n ubyte[] p;\n ubyte[] buffer;\n size_t cur;\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n p = stdin.rawRead (buffer);\n }\n final ubyte skipByte (ubyte lo) {\n while (true) {\n auto a = p[cur .. $];\n auto r = a.find! (c => c >= lo);\n if (!r.empty) {\n cur += a.length - r.length;\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n cur = 0;\n if (p.empty) return 0;\n }\n }\n final ubyte nextByte () {\n if (cur < p.length) {\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n if (p.empty) return 0;\n cur = 1;\n return p[0];\n }\n template next(T) if (isUnsigned!T) {\n final T next () {\n T res = skipByte (48) - 48;\n while (true) {\n ubyte b = nextByte ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n}\n\nstruct IntM {\n enum q = 1_000_000_007;\n int v;\n this (int m) pure nothrow @nogc {\n v = m % q;\n if (v < 0) {\n v += q;\n }\n }\n IntM opAssign (int m) pure nothrow @nogc {\n v = m % q;\n if (v < 0) {\n v += q;\n }\n return this;\n }\n IntM opUnary (string op : \"-\")() const pure nothrow @nogc {\n return IntM ((q - v) % q);\n }\n ref IntM opOpAssign (string op : \"+\")(in IntM rhs) pure nothrow @nogc {\n v += rhs.v;\n v %= q;\n return this;\n }\n ref IntM opOpAssign (string op : \"-\")(in IntM rhs) pure nothrow @nogc {\n v -= rhs.v;\n v %= q;\n return this;\n }\n ref IntM opOpAssign (string op : \"*\")(in IntM rhs) pure nothrow @nogc {\n v = ((v.to!(long)) * rhs.v.to!(long)) % q;\n return this;\n }\n IntM opBinary (string op : \"+\")(in IntM rhs) const pure nothrow @nogc {\n return IntM ( (v + rhs.v) % q);\n }\n IntM opBinary (string op : \"-\")(in IntM rhs) const pure nothrow @nogc {\n return IntM ( (v - rhs.v) % q);\n }\n IntM opBinary (string op : \"*\")(in IntM rhs) const pure nothrow @nogc {\n return IntM (((v.to!(long)) * rhs.v.to!(long)) % q);\n }\n IntM opBinary (string op : \"^^\")(in int rhs) const pure nothrow @nogc {\n IntM a = 1, b = this;\n int p = rhs;\n while (p > 0) {\n //a * (b ^ p) == x ^ rhs\n if (p & 1) {\n a *= b;\n }\n b *= b;\n p >>>= 1;\n }\n return a;\n }\n IntM opBinary (string op)(in int v) const pure nothrow @nogc if (op == \"+\" || op == \"-\" || op == \"*\") {\n mixin (\"return this \" ~ op ~ \" IntM(v);\");\n }\n string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }\n}\n\nvoid main() {\n auto r = new InputReader;\n immutable n = r.next!uint;\n immutable k = r.next!uint;\n auto ds = new DisjointSet (n);\n foreach (edge; 1 .. n) {\n immutable i = r.next!uint - 1;\n immutable j = r.next!uint - 1;\n immutable c = r.next!uint;\n if (c == 0) {\n ds.merge (i, j);\n }\n }\n int[] x;\n foreach (i; 0 .. n) {\n if (ds.findSet (i) == i) {\n x ~= ds.s[i];\n }\n }\n immutable m = x.length.to!int;\n IntM s;\n foreach (i; x) {\n auto c = IntM (i);\n s += c ^^ k;\n }\n IntM res = IntM (n) ^^ k;\n res -= s;\n write (res);\n}\n\n"}], "negative_code": [], "src_uid": "94559f08866b6136ba4791c440025a68"} {"nl": {"description": "Suppose you have a special $$$x$$$-$$$y$$$-counter. This counter can store some value as a decimal number; at first, the counter has value $$$0$$$. The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $$$x$$$ or $$$y$$$ to its value. So all sequences this counter generates are starting from $$$0$$$. For example, a $$$4$$$-$$$2$$$-counter can act as follows: it prints $$$0$$$, and adds $$$4$$$ to its value, so the current value is $$$4$$$, and the output is $$$0$$$; it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$8$$$, and the output is $$$04$$$; it prints $$$8$$$, and adds $$$4$$$ to its value, so the current value is $$$12$$$, and the output is $$$048$$$; it prints $$$2$$$, and adds $$$2$$$ to its value, so the current value is $$$14$$$, and the output is $$$0482$$$; it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$18$$$, and the output is $$$04824$$$. This is only one of the possible outputs; for example, the same counter could generate $$$0246802468024$$$ as the output, if we chose to add $$$2$$$ during each step.You wrote down a printed sequence from one of such $$$x$$$-$$$y$$$-counters. But the sequence was corrupted and several elements from the sequence could be erased.Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $$$s$$$ — the remaining data of the sequence. For all $$$0 \\le x, y < 10$$$, calculate the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$x$$$-$$$y$$$-counter. Note that you can't change the order of digits in string $$$s$$$ or erase any of them; only insertions are allowed.", "input_spec": "The first line contains a single string $$$s$$$ ($$$1 \\le |s| \\le 2 \\cdot 10^6$$$, $$$s_i \\in \\{\\text{0} - \\text{9}\\}$$$) — the remaining data you have. It's guaranteed that $$$s_1 = 0$$$.", "output_spec": "Print a $$$10 \\times 10$$$ matrix, where the $$$j$$$-th integer ($$$0$$$-indexed) on the $$$i$$$-th line ($$$0$$$-indexed too) is equal to the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$i$$$-$$$j$$$-counter, or $$$-1$$$ if there is no way to do so.", "sample_inputs": ["0840"], "sample_outputs": ["-1 17 7 7 7 -1 2 17 2 7 \n17 17 7 5 5 5 2 7 2 7 \n7 7 7 4 3 7 1 7 2 5 \n7 5 4 7 3 3 2 5 2 3 \n7 5 3 3 7 7 1 7 2 7 \n-1 5 7 3 7 -1 2 9 2 7 \n2 2 1 2 1 2 2 2 0 1 \n17 7 7 5 7 9 2 17 2 3 \n2 2 2 2 2 2 0 2 2 2 \n7 7 5 3 7 7 1 3 2 7"], "notes": "NoteLet's take, for example, $$$4$$$-$$$3$$$-counter. One of the possible outcomes the counter could print is $$$0(4)8(1)4(7)0$$$ (lost elements are in the brackets).One of the possible outcomes a $$$2$$$-$$$3$$$-counter could print is $$$0(35)8(1)4(7)0$$$.The $$$6$$$-$$$8$$$-counter could print exactly the string $$$0840$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto s = RD!string;\n\tauto memo = new long[][][](10, 10, 10);\n\tforeach (i; 0..10)\n\t{\n\t\tforeach (j; 0..10)\n\t\t{\n\t\t\tforeach (k; 0..10)\n\t\t\t{\n\t\t\t\tlong cnt = 100;\n\t\t\t\t/*if (j != 0)\n\t\t\t\t\tif (i % j == 0)\n\t\t\t\t\t\tcnt = min(i / j, cnt);\n\t\t\t\tif (k != 0)\n\t\t\t\t\tif (i % k == 0)\n\t\t\t\t\t\tcnt = min(i / k, cnt);*/\n\n\t\t\t\tforeach (l; 0..11)\n\t\t\t\t{\n\t\t\t\t\tforeach (n; 0..11)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (l + n == 0) continue;\n\t\t\t\t\t\tif ((j*l + k*n) % 10 == i)\n\t\t\t\t\t\t\tcnt = min(l+n, cnt);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tmemo[i][j][k] = cnt;\n\t\t\t}\n\t\t}\n\t}\n\n\tlong[int] set;\n\tlong last;\n\tforeach (i; 1..s.length)\n\t{\n\t\tauto x = [s[i]].to!long;\n\t\tauto y = (10 + x - last) % 10;\n\t\tdebug writeln(x, \"-\", last, \"=\", y);\n\t\t++set[cast(int)y];\n\t\tlast = x;\n\t}\n\n\tdebug writeln(memo);\n\tdebug writeln(set);\n\n\tauto keys = set.keys;\n\tauto ans = new long[][](10, 10);\n\tforeach (i; 0..10)\n\t{\n\t\tforeach (j; i..10)\n\t\t{\n\t\t\tlong cnt;\n\t\t\tforeach (key; keys)\n\t\t\t{\n\t\t\t\tif (memo[key][i][j] == 100)\n\t\t\t\t{\n\t\t\t\t\tcnt = -1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcnt += (memo[key][i][j] - 1) * set[key];\n\t\t\t}\n\t\t\tans[i][j] = cnt;\n\t\t\tans[j][i] = cnt;\n\t\t}\n\t}\n\n\tforeach (i; 0..10)\n\t{\n\t\tans[i].map!(to!string).join(\" \").writeln;\n\t}\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto s = RD!string;\n\tauto memo = new long[][][](10, 10, 10);\n\tforeach (i; 0..10)\n\t{\n\t\tforeach (j; 0..10)\n\t\t{\n\t\t\tforeach (k; 0..10)\n\t\t\t{\n\t\t\t\tlong cnt = 11;\n\t\t\t\t/*if (j != 0)\n\t\t\t\t\tif (i % j == 0)\n\t\t\t\t\t\tcnt = min(i / j, cnt);\n\t\t\t\tif (k != 0)\n\t\t\t\t\tif (i % k == 0)\n\t\t\t\t\t\tcnt = min(i / k, cnt);*/\n\n\t\t\t\tforeach (l; 0..10)\n\t\t\t\t{\n\t\t\t\t\tforeach (n; 0..10)\n\t\t\t\t\t{\n\t\t\t\t\t\tif ((j*l + k*n) % 10 == i)\n\t\t\t\t\t\t\tcnt = min(l+n, cnt);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tmemo[i][j][k] = cnt;\n\t\t\t}\n\t\t}\n\t}\n\n\tlong[int] set;\n\tlong last;\n\tforeach (i; 1..s.length)\n\t{\n\t\tauto x = [s[i]].to!long;\n\t\tauto y = (10 + x - last) % 10;\n\t\tdebug writeln(x, \"-\", last, \"=\", y);\n\t\t++set[cast(int)y];\n\t\tlast = x;\n\t}\n\n\tdebug writeln(memo);\n\tdebug writeln(set);\n\n\tauto keys = set.keys;\n\tauto ans = new long[][](10, 10);\n\tforeach (i; 0..10)\n\t{\n\t\tforeach (j; i..10)\n\t\t{\n\t\t\tlong cnt;\n\t\t\tforeach (key; keys)\n\t\t\t{\n\t\t\t\tif (memo[key][i][j] == 11)\n\t\t\t\t{\n\t\t\t\t\tcnt = -1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcnt += (memo[key][i][j] - 1) * set[key];\n\t\t\t}\n\t\t\tans[i][j] = cnt;\n\t\t\tans[j][i] = cnt;\n\t\t}\n\t}\n\n\tforeach (i; 0..10)\n\t{\n\t\tans[i].map!(to!string).join(\" \").writeln;\n\t}\n\tstdout.flush();\n\tdebug readln();\n}"}], "src_uid": "2ebfcb54231d115a4aa9e5fc52b4ebe7"} {"nl": {"description": "You are given an array $$$s$$$ consisting of $$$n$$$ integers.You have to find any array $$$t$$$ of length $$$k$$$ such that you can cut out maximum number of copies of array $$$t$$$ from array $$$s$$$.Cutting out the copy of $$$t$$$ means that for each element $$$t_i$$$ of array $$$t$$$ you have to find $$$t_i$$$ in $$$s$$$ and remove it from $$$s$$$. If for some $$$t_i$$$ you cannot find such element in $$$s$$$, then you cannot cut out one more copy of $$$t$$$. The both arrays can contain duplicate elements.For example, if $$$s = [1, 2, 3, 2, 4, 3, 1]$$$ and $$$k = 3$$$ then one of the possible answers is $$$t = [1, 2, 3]$$$. This array $$$t$$$ can be cut out $$$2$$$ times. To cut out the first copy of $$$t$$$ you can use the elements $$$[1, \\underline{\\textbf{2}}, 3, 2, 4, \\underline{\\textbf{3}}, \\underline{\\textbf{1}}]$$$ (use the highlighted elements). After cutting out the first copy of $$$t$$$ the array $$$s$$$ can look like $$$[1, 3, 2, 4]$$$. To cut out the second copy of $$$t$$$ you can use the elements $$$[\\underline{\\textbf{1}}, \\underline{\\textbf{3}}, \\underline{\\textbf{2}}, 4]$$$. After cutting out the second copy of $$$t$$$ the array $$$s$$$ will be $$$[4]$$$. Your task is to find such array $$$t$$$ that you can cut out the copy of $$$t$$$ from $$$s$$$ maximum number of times. If there are multiple answers, you may choose any of them.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in $$$s$$$ and the desired number of elements in $$$t$$$, respectively. The second line of the input contains exactly $$$n$$$ integers $$$s_1, s_2, \\dots, s_n$$$ ($$$1 \\le s_i \\le 2 \\cdot 10^5$$$).", "output_spec": "Print $$$k$$$ integers — the elements of array $$$t$$$ such that you can cut out maximum possible number of copies of this array from $$$s$$$. If there are multiple answers, print any of them. The required array $$$t$$$ can contain duplicate elements. All the elements of $$$t$$$ ($$$t_1, t_2, \\dots, t_k$$$) should satisfy the following condition: $$$1 \\le t_i \\le 2 \\cdot 10^5$$$.", "sample_inputs": ["7 3\n1 2 3 2 4 3 1", "10 4\n1 3 1 3 10 3 7 7 12 3", "15 2\n1 2 1 1 1 2 1 1 2 1 2 1 1 1 1"], "sample_outputs": ["1 2 3", "7 3 1 3", "1 1"], "notes": "NoteThe first example is described in the problem statement.In the second example the only answer is $$$[7, 3, 1, 3]$$$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $$$2$$$.In the third example the array $$$t$$$ can be cut out $$$5$$$ times."}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array.sort().group().array.sort!((t1, t2) => t1[1] > t2[1]).array;\n \n debug { arr.writeln; }\n \n bool f(int m, ref int[] ans) {\n ans = [];\n foreach (t; arr) {\n auto r = t[1] / m;\n foreach (_; 0 .. r) if (ans.length < k) ans ~= t[0];\n }\n \n return ans.length == k;\n }\n \n int[] ans;\n int le = 1, r = n / k;\n while (le < r) {\n auto m = (le + r) / 2 + 1;\n if (f(m, ans)) le = m;\n else r = m-1;\n }\n \n f(le, ans);\n ans.writefln!(\"%(%s %)\");\n}"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array.sort().group().array.sort!((t1, t2) => t1[1] > t2[1]).array;\n \n debug { arr.writeln; }\n \n bool f(int m, ref int[] ans) {\n ans = [];\n foreach (t; arr) {\n auto r = t[1] / m;\n foreach (_; 0 .. r) if (ans.length < k) ans ~= t[0];\n }\n \n return ans.length == k;\n }\n \n int[] ans;\n int le = 1, r = n / k;\n while (le < r) {\n auto m = (le + r) / 2 + 1;\n if (f(m, ans)) le = m;\n else r = m-1;\n }\n \n f(le, ans);\n ans.writeln; //writefln!(\"%(%s %)\");\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array.sort().group().array.sort!((t1, t2) => t1[1] > t2[1]).array;\n \n debug { arr.writeln; }\n \n bool f(int m, ref int[] ans) {\n foreach (t; arr) {\n auto r = t[1] / m;\n foreach (_; 0 .. r) if (ans.length < k) ans ~= t[0];\n }\n \n return ans.length == k;\n }\n \n int[] ans;\n int le = 1, r = n / k;\n while (le < r) {\n auto m = (le + r) / 2 + 1;\n if (f(m, ans)) le = m;\n else r = m-1;\n }\n \n f(le, ans);\n ans.writefln!(\"%(%s %)\");\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array.sort().group().array.sort!((t1, t2) => t1[1] > t2[1]).array;\n \n debug { arr.writeln; }\n \n bool f(int m, ref int[] ans) {\n foreach (t; arr) {\n auto r = t[1] / m;\n foreach (_; 0 .. r) if (ans.length < k) ans ~= t[0];\n }\n \n return ans.length == k;\n }\n \n int[] ans;\n int le = 0, r = n / k;\n while (le < r) {\n auto m = (le + r) / 2 + 1;\n if (f(m, ans)) le = m;\n else r = m-1;\n }\n \n f(le, ans);\n ans.writefln!(\"%(%s %)\");\n}"}], "src_uid": "cfccf06c4d0de89bf0978dc6512265c4"} {"nl": {"description": "You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id i. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change the other weights.You are to determine this maximum weight described above for each edge. You should calculate the answer for each edge independently, it means there can't be two edges with changed weights at the same time.", "input_spec": "The first line contains two integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), where n and m are the number of vertices and the number of edges in the graph, respectively. Each of the next m lines contains three integers u, v and c (1 ≤ v, u ≤ n, v ≠ u, 1 ≤ c ≤ 109) meaning that there is an edge between vertices u and v with weight c. ", "output_spec": "Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer.", "sample_inputs": ["4 4\n1 2 2\n2 3 2\n3 4 2\n4 1 3", "4 3\n1 2 2\n2 3 2\n3 4 2"], "sample_outputs": ["2 2 2 1", "-1 -1 -1"], "notes": null}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum E = 18;\nenum INF = 10^^9 + 10;\n\nint N, M;\nint[] A, B, C;\n\nbool[] on;\nint[][] G;\nint[][] par;\nint[] pari;\nint[] dep;\n\nvoid dfs(int u, int p, int pi) {\n par[0][u] = p;\n pari[u] = pi;\n dep[u] = (p == -1) ? 0 : (dep[p] + 1);\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n dfs(v, u, i);\n }\n }\n}\n\nint[] ans;\nint[][] adds, rems;\n\nalias Multiset = RedBlackTree!(int, \"a < b\", true);\nMultiset solve(int u, int p, int pi) {\n auto ret = new Multiset;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n auto res = solve(v, u, i);\n if (ret.length < res.length) {\n swap(ret, res);\n }\n foreach (c; res) {\n ret.insert(c);\n }\n }\n }\n foreach (i; adds[u]) {\n ret.insert(C[i]);\n }\n foreach (i; rems[u]) {\n ret.removeKey(C[i]);\n ret.removeKey(C[i]);\n }\n if (pi != -1) {\n if (!ret.empty) {\n chmin(ans[pi], ret.front - 1);\n }\n }\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n C = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n C[i] = readInt();\n }\n \n alias Entry = Tuple!(int, \"c\", int, \"i\");\n auto es = new Entry[M];\n foreach (i; 0 .. M) {\n es[i] = Entry(C[i], i);\n }\n es.sort;\n auto uf = new int[N];\n uf[] = -1;\n on = new bool[M];\n foreach (ref e; es) {\n const i = e.i;\n on[i] = uf.connect(A[i], B[i]);\n }\n debug {\n writeln(\"on = \", on);\n }\n assert(on.count(true) == N - 1);\n \n G = new int[][N];\n foreach (i; 0 .. M) {\n if (on[i]) {\n G[A[i]] ~= i;\n G[B[i]] ~= i;\n }\n }\n par = new int[][](E, N);\n pari = new int[N];\n dep = new int[N];\n const rt = 0;\n dfs(rt, -1, -1);\n par[0][rt] = rt;\n auto mxs = new int[][](E, N);\n foreach (u; 0 .. N) {\n mxs[0][u] = (pari[u] == -1) ? -INF : C[pari[u]];\n }\n foreach (e; 1 .. E) {\n foreach (u; 0 .. N) {\n par[e][u] = par[e - 1][par[e - 1][u]];\n mxs[e][u] = max(mxs[e - 1][u], mxs[e - 1][par[e - 1][u]]);\n }\n }\n \n ans = new int[M];\n ans[] = INF;\n adds = new int[][N];\n rems = new int[][N];\n foreach (i; 0 .. M) {\n if (!on[i]) {\n int u = A[i], v = B[i];\n int mx = -INF;\n foreach_reverse (e; 0 .. E) {\n if (dep[u] - (1 << e) >= dep[v]) {\n chmax(mx, mxs[e][u]);\n u = par[e][u];\n }\n if (dep[v] - (1 << e) >= dep[u]) {\n chmax(mx, mxs[e][v]);\n v = par[e][v];\n }\n }\n foreach_reverse (e; 0 .. E) {\n if (par[e][u] != par[e][v]) {\n chmax(mx, mxs[e][u]);\n chmax(mx, mxs[e][v]);\n u = par[e][u];\n v = par[e][v];\n }\n }\n if (u != v) {\n chmax(mx, mxs[0][u]);\n chmax(mx, mxs[0][v]);\n u = par[0][u];\n v = par[0][v];\n }\n debug {\n writeln(i, \": \", A[i], \" \", B[i], \" \", u, \"; \", mx);\n }\n chmin(ans[i], mx - 1);\n adds[A[i]] ~= i;\n adds[B[i]] ~= i;\n rems[u] ~= i;\n }\n }\n solve(rt, -1, -1);\n \n foreach (i; 0 .. M) {\n if (i > 0) write(\" \");\n write((ans[i] >= INF) ? -1 : ans[i]);\n }\n writeln;\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nenum E = 18;\nenum INF = 10^^9 + 10;\n\nint N, M;\nint[] A, B, C;\n\nbool[] on;\nint[][] G;\nint[][] par;\nint[] pari;\nint[] dep;\n\nvoid dfs(int u, int p, int pi) {\n par[0][u] = p;\n pari[u] = pi;\n dep[u] = (p == -1) ? 0 : (dep[p] + 1);\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n dfs(v, u, i);\n }\n }\n}\n\nint[] ans;\nint[][] adds, rems;\n\nalias Multiset = RedBlackTree!(int, \"a < b\", true);\nMultiset solve(int u, int p, int pi) {\n auto ret = new Multiset;\n foreach (i; G[u]) {\n const v = A[i] ^ B[i] ^ u;\n if (v != p) {\n auto res = solve(v, u, i);\n if (ret.length < res.length) {\n swap(ret, res);\n }\n foreach (c; res) {\n ret.insert(c);\n }\n }\n }\n foreach (i; adds[u]) {\n ret.insert(C[i]);\n }\n foreach (i; rems[u]) {\n ret.removeKey(C[i]);\n ret.removeKey(C[i]);\n }\n if (pi != -1) {\n if (!ret.empty) {\n chmin(ans[pi], ret.front - 1);\n }\n }\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n M = readInt();\n A = new int[M];\n B = new int[M];\n C = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt() - 1;\n B[i] = readInt() - 1;\n C[i] = readInt();\n }\n \n alias Entry = Tuple!(int, \"c\", int, \"i\");\n auto es = new Entry[M];\n foreach (i; 0 .. M) {\n es[i] = Entry(C[i], i);\n }\n es.sort;\n auto uf = new int[N];\n uf[] = -1;\n on = new bool[M];\n foreach (ref e; es) {\n const i = e.i;\n on[i] = uf.connect(A[i], B[i]);\n }\n debug {\n writeln(\"on = \", on);\n }\n assert(on.count(true) == N - 1);\n \n G = new int[][N];\n foreach (i; 0 .. M) {\n if (on[i]) {\n G[A[i]] ~= i;\n G[B[i]] ~= i;\n }\n }\n par = new int[][](E, N);\n pari = new int[N];\n dep = new int[N];\n const rt = 0;\n dfs(rt, -1, -1);\n par[0][rt] = rt;\n auto mn = new int[][](E, N);\n foreach (u; 0 .. N) {\n mn[0][u] = (pari[u] == -1) ? INF : C[pari[u]];\n }\n foreach (e; 1 .. E) {\n foreach (u; 0 .. N) {\n par[e][u] = par[e - 1][par[e - 1][u]];\n mn[e][u] = min(mn[e - 1][u], mn[e - 1][par[e - 1][u]]);\n }\n }\n \n ans = new int[M];\n ans[] = INF;\n adds = new int[][N];\n rems = new int[][N];\n foreach (i; 0 .. M) {\n if (!on[i]) {\n int u = A[i], v = B[i];\n foreach_reverse (e; 0 .. E) {\n if (dep[u] - (1 << e) >= dep[v]) {\n chmin(ans[i], mn[e][u] - 1);\n u = par[e][u];\n }\n if (dep[v] - (1 << e) >= dep[u]) {\n chmin(ans[i], mn[e][v] - 1);\n v = par[e][v];\n }\n }\n foreach_reverse (e; 0 .. E) {\n if (par[e][u] != par[e][v]) {\n chmin(ans[i], mn[e][u] - 1);\n chmin(ans[i], mn[e][v] - 1);\n u = par[e][u];\n v = par[e][v];\n }\n }\n if (u != v) {\n chmin(ans[i], mn[0][u] - 1);\n chmin(ans[i], mn[0][v] - 1);\n u = par[0][u];\n v = par[0][v];\n }\n debug {\n writeln(i, \": \", A[i], \" \", B[i], \" \", u);\n }\n adds[A[i]] ~= i;\n adds[B[i]] ~= i;\n rems[u] ~= i;\n }\n }\n solve(rt, -1, -1);\n \n foreach (i; 0 .. M) {\n if (i > 0) write(\" \");\n write((ans[i] >= INF) ? -1 : ans[i]);\n }\n writeln;\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "ab9cc058dc8855e93df365ecb06ccfb6"} {"nl": {"description": "Polycarp is sad — New Year is coming in few days but there is still no snow in his city. To bring himself New Year mood, he decided to decorate his house with some garlands.The local store introduced a new service this year, called \"Build your own garland\". So you can buy some red, green and blue lamps, provide them and the store workers will solder a single garland of them. The resulting garland will have all the lamps you provided put in a line. Moreover, no pair of lamps of the same color will be adjacent to each other in this garland!For example, if you provide $$$3$$$ red, $$$3$$$ green and $$$3$$$ blue lamps, the resulting garland can look like this: \"RGBRBGBGR\" (\"RGB\" being the red, green and blue color, respectively). Note that it's ok to have lamps of the same color on the ends of the garland.However, if you provide, say, $$$1$$$ red, $$$10$$$ green and $$$2$$$ blue lamps then the store workers won't be able to build any garland of them. Any garland consisting of these lamps will have at least one pair of lamps of the same color adjacent to each other. Note that the store workers should use all the lamps you provided.So Polycarp has bought some sets of lamps and now he wants to know if the store workers can build a garland from each of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of sets of lamps Polycarp has bought. Each of the next $$$t$$$ lines contains three integers $$$r$$$, $$$g$$$ and $$$b$$$ ($$$1 \\le r, g, b \\le 10^9$$$) — the number of red, green and blue lamps in the set, respectively.", "output_spec": "Print $$$t$$$ lines — for each set of lamps print \"Yes\" if the store workers can build a garland from them and \"No\" otherwise.", "sample_inputs": ["3\n3 3 3\n1 10 2\n2 1 1"], "sample_outputs": ["Yes\nNo\nYes"], "notes": "NoteThe first two sets are desribed in the statement.The third set produces garland \"RBRG\", for example."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto T = readln.chomp.to!int;\n while (T--) {\n auto s = readln.split.map!(to!int).array;\n s.sort();\n auto a = s[0];\n auto b = s[1];\n auto c = s[2];\n if (c <= a + b + 1) {\n writeln(\"Yes\");\n } else {\n writeln(\"No\");\n }\n }\n}"}, {"source_code": "void main() {\n\tauto t = ri;\n\tforeach(i; 0..t) {\n\t\tauto ip = readAs!(long[]).sort.array;\n\t\tif(ip[2] > ip[0] + ip[1] + 1) writeln(\"No\");\n\t\telse writeln(\"Yes\");\n\t}\n}\n\n// ===================================\n\nimport std.stdio;\nimport std.string;\nimport std.functional;\nimport std.algorithm;\nimport std.range;\nimport std.traits;\nimport std.math;\nimport std.container;\nimport std.bigint;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.uni;\nimport std.ascii;\nimport std.bitmanip;\nimport core.bitop;\n\nT readAs(T)() if (isBasicType!T) {\n\treturn readln.chomp.to!T;\n}\nT readAs(T)() if (isArray!T) {\n\treturn readln.split.to!T;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tres[i] = readAs!(T[]);\n\t}\n\treturn res;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tauto s = rs;\n\t\tforeach(j; 0..width) res[i][j] = s[j].to!T;\n\t}\n\treturn res;\n}\n\nint ri() {\n\treturn readAs!int;\n}\n\ndouble rd() {\n\treturn readAs!double;\n}\n\nstring rs() {\n\treturn readln.chomp;\n}"}, {"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\n\nclass InputReader {\n private:\n ubyte[] p;\n ubyte[] buffer;\n size_t cur;\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n p = stdin.rawRead (buffer);\n }\n final ubyte skipByte (ubyte lo) {\n while (true) {\n auto a = p[cur .. $];\n auto r = a.find! (c => c >= lo);\n if (!r.empty) {\n cur += a.length - r.length;\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n cur = 0;\n if (p.empty) return 0;\n }\n }\n final ubyte nextByte () {\n if (cur < p.length) {\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n if (p.empty) return 0;\n cur = 1;\n return p[0];\n }\n\n template next(T) if (isSigned!T) {\n final T next () {\n T res;\n ubyte b = skipByte (45);\n if (b == 45) {\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n final T next () {\n T res = skipByte (48) - 48;\n while (true) {\n ubyte b = nextByte ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n final T[] nextA(T) (int n) {\n auto a = uninitializedArray!(T[]) (n);\n foreach (i; 0 .. n) {\n a[i] = next!T;\n }\n return a;\n }\n}\n\nvoid main() {\n auto r = new InputReader;\n auto nt = r.next!uint;\n foreach (tid; 0 .. nt) {\n auto a = r.nextA!ulong(3);\n sort (a);\n auto s = sum (a);\n long m = (s + 1) / 2;\n if (a[2] > m) {\n writeln (\"No\");\n } else {\n writeln (\"Yes\");\n }\n }\n}\n\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tforeach(_; 0 .. rint){\n\t\tlong a = rlong, b = rlong, c = rlong;\n\t\tlong n = a + b + c;\n\t\tlong m = (n + 1) / 2;\n\t\tif(a > m || b > m || c > m) \"No\".writeln;\n\t\telse \"Yes\".writeln;\n\t}\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new bool[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto r = RD!int;\n\t\tauto g = RD!int;\n\t\tauto b = RD!int;\n\t\tlong[] arr = [r, g, b];\n\t\tarr.sort();\n\t\tans[ti] = arr[2] <= (arr.sum+1)/2;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e ? \"YES\" : \"NO\");\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "void main() {\n\tauto t = ri;\n\tforeach(i; 0..t) {\n\t\tauto ip = readAs!(int[]), r = ip[0], g = ip[1], b = ip[2];\n\t\tif(r > (g + b) || g > (r + b) || b > (r + g)) writeln(\"No\");\n\t\telse writeln(\"Yes\");\n\t}\n}\n\n// ===================================\n\nimport std.stdio;\nimport std.string;\nimport std.functional;\nimport std.algorithm;\nimport std.range;\nimport std.traits;\nimport std.math;\nimport std.container;\nimport std.bigint;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.uni;\nimport std.ascii;\nimport std.bitmanip;\nimport core.bitop;\n\nT readAs(T)() if (isBasicType!T) {\n\treturn readln.chomp.to!T;\n}\nT readAs(T)() if (isArray!T) {\n\treturn readln.split.to!T;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tres[i] = readAs!(T[]);\n\t}\n\treturn res;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tauto s = rs;\n\t\tforeach(j; 0..width) res[i][j] = s[j].to!T;\n\t}\n\treturn res;\n}\n\nint ri() {\n\treturn readAs!int;\n}\n\ndouble rd() {\n\treturn readAs!double;\n}\n\nstring rs() {\n\treturn readln.chomp;\n}"}, {"source_code": "void main() {\n\tauto t = ri;\n\tforeach(i; 0..t) {\n\t\tauto ip = readAs!(int[]), r = ip[0], g = ip[1], b = ip[2];\n\t\tif(r > (g + b) && g > (r + b) && b > (r + g)) writeln(\"No\");\n\t\telse writeln(\"Yes\");\n\t}\n}\n\n// ===================================\n\nimport std.stdio;\nimport std.string;\nimport std.functional;\nimport std.algorithm;\nimport std.range;\nimport std.traits;\nimport std.math;\nimport std.container;\nimport std.bigint;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.uni;\nimport std.ascii;\nimport std.bitmanip;\nimport core.bitop;\n\nT readAs(T)() if (isBasicType!T) {\n\treturn readln.chomp.to!T;\n}\nT readAs(T)() if (isArray!T) {\n\treturn readln.split.to!T;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tres[i] = readAs!(T[]);\n\t}\n\treturn res;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tauto s = rs;\n\t\tforeach(j; 0..width) res[i][j] = s[j].to!T;\n\t}\n\treturn res;\n}\n\nint ri() {\n\treturn readAs!int;\n}\n\ndouble rd() {\n\treturn readAs!double;\n}\n\nstring rs() {\n\treturn readln.chomp;\n}"}, {"source_code": "void main() {\n\tauto t = ri;\n\tforeach(i; 0..t) {\n\t\tauto ip = readAs!(int[]).sort.uniq.array;\n\t\tif(ip[2] > ip[0] + ip[1] + 1) writeln(\"No\");\n\t\telse writeln(\"Yes\");\n\t}\n}\n\n// ===================================\n\nimport std.stdio;\nimport std.string;\nimport std.functional;\nimport std.algorithm;\nimport std.range;\nimport std.traits;\nimport std.math;\nimport std.container;\nimport std.bigint;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.uni;\nimport std.ascii;\nimport std.bitmanip;\nimport core.bitop;\n\nT readAs(T)() if (isBasicType!T) {\n\treturn readln.chomp.to!T;\n}\nT readAs(T)() if (isArray!T) {\n\treturn readln.split.to!T;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tres[i] = readAs!(T[]);\n\t}\n\treturn res;\n}\n\nT[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {\n\tauto res = new T[][](height, width);\n\tforeach(i; 0..height) {\n\t\tauto s = rs;\n\t\tforeach(j; 0..width) res[i][j] = s[j].to!T;\n\t}\n\treturn res;\n}\n\nint ri() {\n\treturn readAs!int;\n}\n\ndouble rd() {\n\treturn readAs!double;\n}\n\nstring rs() {\n\treturn readln.chomp;\n}"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tforeach(_; 0 .. rint){\n\t\tlong a = rlong, b = rlong, c = rlong;\n\t\tlong n = a + b + c;\n\t\tlong m = n / 2;\n\t\tif(a > m || b > m || c > m) \"No\".writeln;\n\t\telse \"Yes\".writeln;\n\t}\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new bool[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto r = RD!int;\n\t\tauto g = RD!int;\n\t\tauto b = RD!int;\n\t\tauto arr = [r, g, b];\n\t\tarr.sort();\n\t\tans[ti] = arr[2] <= (arr.sum+1)/2;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e ? \"YES\" : \"NO\");\n\tstdout.flush;\n\tdebug readln;\n}"}], "src_uid": "34aa41871ee50f06e8acbd5eee94b493"} {"nl": {"description": "Mr. F has $$$n$$$ positive integers, $$$a_1, a_2, \\ldots, a_n$$$.He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers.But this problem is too simple for him, so he does not want to do it by himself. If you help him, he will give you some scores in reward.Your task is to calculate the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers.", "input_spec": "The first line contains an integer $$$n$$$ ($$$2 \\leq n \\leq 3 \\cdot 10^5$$$) — the number of integers Mr. F has. The second line contains $$$n$$$ integers, $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 1.5 \\cdot 10^7$$$).", "output_spec": "Print an integer — the minimum number of integers you need to remove so that the greatest common divisor of the remaining integers is bigger than that of all integers. You should not remove all of the integers. If there is no solution, print «-1» (without quotes).", "sample_inputs": ["3\n1 2 4", "4\n6 9 15 30", "3\n1 1 1"], "sample_outputs": ["1", "2", "-1"], "notes": "NoteIn the first example, the greatest common divisor is $$$1$$$ in the beginning. You can remove $$$1$$$ so that the greatest common divisor is enlarged to $$$2$$$. The answer is $$$1$$$.In the second example, the greatest common divisor is $$$3$$$ in the beginning. You can remove $$$6$$$ and $$$9$$$ so that the greatest common divisor is enlarged to $$$15$$$. There is no solution which removes only one integer. So the answer is $$$2$$$.In the third example, there is no solution to enlarge the greatest common divisor. So the answer is $$$-1$$$."}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nimmutable int N = 15_000_000 + 10;\n\nvoid main()\n{\n auto pr = new bool[N];\n auto cnt = new int[N];\n \n int n;\n readf(\"%s\", &n);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto gcdnow = arr.fold!gcd;\n debug { gcdnow.writeln; }\n \n arr.each!(e => ++cnt[e / gcdnow]);\n \n auto ans = 0;\n pr[] = true;\n foreach (i; 2 .. N) {\n if (!pr[i]) continue;\n \n auto cur = 0;\n for (int j = i; j < N; j += i) {\n cur += cnt[j];\n pr[j] = false;\n }\n \n ans = max(ans, cur);\n }\n \n ans = ans > 0 ? n - ans : -1;\n writeln(ans);\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nimmutable int N = 15_000_000 + 10;\n\nvoid main()\n{\n auto dvs = new int[N];\n auto cnt = new int[N];\n \n int n;\n readf(\"%s\", &n);\n readln;\n\n foreach (i; 2 .. N) {\n if (dvs[i] != 0) continue;\n \n dvs[i] = i;\n for (int j = i+i; j < N; j += i) dvs[j] = i;\n }\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto gcdnow = arr.fold!gcd;\n arr[] /= gcdnow;\n \n debug { gcdnow.writeln; }\n \n foreach (e; arr) {\n while (e > 1) {\n auto now = dvs[e];\n ++cnt[now];\n while (dvs[e] == now) e /= now;\n }\n }\n \n auto left = cnt[2 .. $].maxElement;\n \n if (left == 0) {\n writeln(-1);\n return;\n }\n \n writeln(n - left);\n}"}], "negative_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nimmutable int N = 15_000_000 + 10;\n\nvoid main()\n{\n auto dvs = new int[N];\n auto cnt = new int[N];\n \n int n;\n readf(\"%s\", &n);\n readln;\n\n foreach (i; 2 .. N) {\n if (dvs[i] != 0) continue;\n \n dvs[i] = i;\n for (int j = i+i; j < N; j += i) dvs[j] = i;\n }\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto gcdnow = arr.fold!gcd;\n \n debug { gcdnow.writeln; }\n \n foreach (e; arr) {\n while (e > 1) {\n auto now = dvs[e];\n ++cnt[now];\n while (dvs[e] == now) e /= now;\n }\n }\n \n auto left = cnt[gcdnow+1 .. $].maxElement;\n \n if (left == 0) {\n writeln(-1);\n return;\n }\n \n writeln(n - left);\n}"}], "src_uid": "9115965ff3421230eac37b22328c6153"} {"nl": {"description": "Given an array $$$a$$$, consisting of $$$n$$$ integers, find:$$$$$$\\max\\limits_{1 \\le i < j \\le n} LCM(a_i,a_j),$$$$$$where $$$LCM(x, y)$$$ is the smallest positive integer that is divisible by both $$$x$$$ and $$$y$$$. For example, $$$LCM(6, 8) = 24$$$, $$$LCM(4, 12) = 12$$$, $$$LCM(2, 3) = 6$$$.", "input_spec": "The first line contains an integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of elements in the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$) — the elements of the array $$$a$$$.", "output_spec": "Print one integer, the maximum value of the least common multiple of two elements in the array $$$a$$$.", "sample_inputs": ["3\n13 35 77", "6\n1 2 4 8 16 32"], "sample_outputs": ["1001", "32"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv : to;\nimport std.numeric : gcd;\nimport std.range;\nimport std.stdio;\nimport std.typecons : Tuple, tuple;\n\nstring readToken()\n{\n static string[] tokens;\n while (tokens.empty)\n tokens = readln.split;\n auto token = tokens.front;\n tokens.popFront;\n return token;\n}\n\nint readInt()\n{\n return readToken.to!int;\n}\n\nvoid main()\n{\n int n = readInt;\n auto a = new int[n];\n for (int i = 0; i < n; ++i)\n a[i] = readInt;\n int m = a.maxElement;\n auto cnt = new int[m + 1];\n auto mu = new int[m + 1];\n mu[1] = 1;\n for (int d = 1; d <= m; ++d)\n {\n if (!mu[d])\n continue;\n cnt[d]++;\n for (int i = d * 2; i <= m; i += d)\n cnt[i]++, mu[i] -= mu[d];\n }\n auto divs = new int[][m + 1];\n for (int i = 1; i <= m; ++i)\n divs[i] = new int[cnt[i]];\n fill(cnt, 0);\n for (int d = 1; d <= m; ++d)\n if (mu[d])\n for (int i = d; i <= m; i += d)\n divs[i][cnt[i]++] = d;\n fill(cnt, 0);\n for (int i = 0; i < n; ++i)\n cnt[a[i]]++;\n long result = m;\n int[] stack = new int[m], subcnt = new int[m + 1];\n for (int g = 1; g <= m; ++g)\n {\n int m1 = m / g;\n int top = 0;\n for (int x = m1; x >= 1; --x)\n if (cnt[g * x])\n {\n int coprimes = 0;\n foreach (d; divs[x])\n coprimes += mu[d] * subcnt[d];\n while (coprimes)\n {\n int y = stack[--top];\n if (gcd(x, y) == 1)\n coprimes--, result = max(result, cast(long) g * x * y);\n foreach (d; divs[y])\n subcnt[d]--;\n }\n foreach (d; divs[x])\n subcnt[d]++;\n stack[top++] = x;\n }\n while (top--)\n foreach (d; divs[stack[top]])\n subcnt[d]--;\n }\n writeln(result);\n}\n"}, {"source_code": "import std.algorithm : max, maxElement, sort, swap;\nimport std.conv : to;\nimport std.numeric : gcd;\nimport std.range;\nimport std.stdio;\nimport std.typecons : Tuple, tuple;\nimport std.algorithm.mutation;\n\n// import std.container.array;\n\nstring readToken()\n{\n static string[] tokens;\n while (tokens.empty)\n {\n tokens = readln.split;\n }\n auto token = tokens.front;\n tokens.popFront;\n return token;\n}\n\nint readInt()\n{\n return readToken.to!int;\n}\n\nvoid main()\n{\n int n = readInt;\n auto a = new int[n];\n for (int i = 0; i < n; ++i)\n a[i] = readInt;\n int m = a.maxElement;\n auto cnt = new int[m + 1];\n auto mu = new int[m + 1];\n mu[1] = 1;\n for (int d = 1; d <= m; ++d)\n {\n cnt[d]++;\n for (int i = d * 2; i <= m; i += d)\n cnt[i]++, mu[i] -= mu[d];\n }\n auto divs = new int[][m + 1];\n for (int i = 1; i <= m; ++i)\n divs[i] = new int[cnt[i]];\n fill(cnt, 0);\n for (int d = 1; d <= m; ++d)\n for (int i = d; i <= m; i += d)\n divs[i][cnt[i]++] = d;\n fill(cnt, 0);\n for (int i = 0; i < n; ++i)\n cnt[a[i]]++;\n long result = m;\n int[] stack = new int[m], subcnt = new int[m + 1];\n for (int g = 1; g <= m; ++g)\n {\n int m1 = m / g;\n int top = 0;\n for (int x = m1; x >= 1; --x)\n if (cnt[g * x])\n {\n int coprimes = 0;\n foreach (d; divs[x])\n coprimes += mu[d] * subcnt[d];\n while (coprimes)\n {\n int y = stack[--top];\n if (gcd(x, y) == 1)\n coprimes--, result = max(result, cast(long) g * x * y);\n foreach (d; divs[y])\n subcnt[d]--;\n }\n foreach (d; divs[x])\n subcnt[d]++;\n stack[top++] = x;\n }\n while (top--)\n foreach (d; divs[stack[top]])\n subcnt[d]--;\n }\n writeln(result);\n}\n"}], "negative_code": [], "src_uid": "14c37283d16cb3aa8dd8fc7ea8f1096d"} {"nl": {"description": "Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...The string $$$s$$$ he found is a binary string of length $$$n$$$ (i. e. string consists only of 0-s and 1-s).In one move he can choose two consecutive characters $$$s_i$$$ and $$$s_{i+1}$$$, and if $$$s_i$$$ is 1 and $$$s_{i + 1}$$$ is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string $$$s$$$ as clean as possible. He thinks for two different strings $$$x$$$ and $$$y$$$, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.Now you should answer $$$t$$$ test cases: for the $$$i$$$-th test case, print the cleanest possible string that Lee can get by doing some number of moves.Small reminder: if we have two strings $$$x$$$ and $$$y$$$ of the same length then $$$x$$$ is lexicographically smaller than $$$y$$$ if there is a position $$$i$$$ such that $$$x_1 = y_1$$$, $$$x_2 = y_2$$$,..., $$$x_{i - 1} = y_{i - 1}$$$ and $$$x_i < y_i$$$.", "input_spec": "The first line contains the integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Next $$$2t$$$ lines contain test cases — one per two lines. The first line of each test case contains the integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of the string $$$s$$$. The second line contains the binary string $$$s$$$. The string $$$s$$$ is a string of length $$$n$$$ which consists only of zeroes and ones. It's guaranteed that sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$.", "output_spec": "Print $$$t$$$ answers — one per test case. The answer to the $$$i$$$-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero).", "sample_inputs": ["5\n10\n0001111111\n4\n0101\n8\n11001101\n10\n1110000000\n1\n1"], "sample_outputs": ["0001111111\n001\n01\n0\n1"], "notes": "NoteIn the first test case, Lee can't perform any moves.In the second test case, Lee should erase $$$s_2$$$.In the third test case, Lee can make moves, for example, in the following order: 11001101 $$$\\rightarrow$$$ 1100101 $$$\\rightarrow$$$ 110101 $$$\\rightarrow$$$ 10101 $$$\\rightarrow$$$ 1101 $$$\\rightarrow$$$ 101 $$$\\rightarrow$$$ 01."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto s = readln.strip;\n\t\tstring res1, res2, res3;\n\t\twhile (!s.empty && s.front == '0')\n\t\t{\n\t\t\tres1 ~= s.front;\n\t\t\ts.popFront ();\n\t\t}\n\t\twhile (!s.empty && s.back == '1')\n\t\t{\n\t\t\tres3 ~= s.back;\n\t\t\ts.popBack ();\n\t\t}\n\t\tif (!s.empty)\n\t\t{\n\t\t\tres2 = \"0\";\n\t\t}\n\t\twriteln (res1 ~ res2 ~ res3);\n\t}\n}\n"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\n\nDList!string _words;\n\nvoid read(T...)(ref T t)\n{\n void singleRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!T(word);\n }\n static foreach(i; 0 .. T.length)\n {\n static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))\n {\n foreach(ref element; t[i])\n read(element);\n }\n else\n {\n singleRead(t[i]);\n }\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value = E.init)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\nvoid main()\n{\n int t;\n read(t);\n while(t--)\n {\n int n;\n read(n);\n string s;\n read(s);\n string rs = s.dup.reverse;\n auto prefix = cast(long) s.countUntil('1');\n if (prefix == -1)\n prefix = n;\n auto suffix = cast(long) rs.countUntil('0');\n if (suffix == -1)\n suffix = n;\n string middle = \"\";\n if (suffix + prefix < n)\n middle = \"0\";\n string res = s[0 .. cast(size_t)prefix] ~ middle ~ s[$ - cast(size_t)suffix .. $];\n writeln(res);\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new string[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto s = RD!string;\n\n\t\tint l = n, r = -1;\n\t\tforeach (int i; 0..n)\n\t\t{\n\t\t\tif (s[i] == '1')\n\t\t\t{\n\t\t\t\tl = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tforeach_reverse (int i; 0..n)\n\t\t{\n\t\t\tif (s[i] == '0')\n\t\t\t{\n\t\t\t\tr = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (l < r)\n\t\t\tans[ti] = s[0..l] ~ '0' ~ s[r+1..s.length];\n\t\telse\n\t\t\tans[ti] = s[0..l] ~ s[r+1..s.length];\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "bb071f1f4fc1c129a32064c1301f4942"} {"nl": {"description": "Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.", "input_spec": "The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes. Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.", "output_spec": "In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids. If there are multiple answers, print any of them.", "sample_inputs": ["3 2", "5 3"], "sample_outputs": ["2\n1 2\n2 3", "3\n1 2\n2 3\n3 4\n3 5"], "notes": "NoteIn the first example the only network is shown on the left picture.In the second example one of optimal networks is shown on the right picture.Exit-nodes are highlighted. "}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n foreach (i; 2 .. n+1) {\n int prev = i-k < 1 ? 1 : i-k;\n ans ~= tuple(prev, i);\n }\n \n int longerCnt = (n-1)%k;\n int longerVal = (n-1 + k-1)/k;\n int shorterVal = (n-1)/k;\n int dst = longerCnt == 0 ? shorterVal * 2\n : longerCnt == 1 ? shorterVal + longerVal : 2 * longerVal;\n writeln(dst);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n foreach (i; 2 .. n+1) {\n int prev = i-k < 1 ? 1 : i-k;\n ans ~= tuple(prev, i);\n }\n \n int longerCnt = (n-1)%k;\n int longerVal = (n-1 + k-1)/k;\n int shorterVal = (n-1)/k;\n int dst = longerCnt == 0 ? shorterVal * 2\n : longerCnt == 1 ? shorterVal + longerVal : longerVal * 2;\n writeln(dst);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nint[][] graph;\n\nalias Result = Tuple!(int, \"d\", int, \"u\");\n\nResult dfs(int u, int p) {\n auto ret = Result(0, u);\n foreach (v; graph[u]) {\n if (v != p) {\n const res = dfs(v, u);\n chmax(ret, Result(1 + res.d, res.u));\n }\n }\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n const K = readInt();\n \n int[][] ans;\n int n = 1 + (N - 1) % K;\n foreach (u; 1 .. n) {\n ans ~= [0, u];\n }\n int a, b;\n if (n == 1) {\n a = 0;\n b = 1;\n } else if (n == 2) {\n a = 0;\n b = 2;\n } else {\n a = 1;\n b = n;\n }\n debug {\n writefln(\"N = %s, K = %s\", N, K);\n writefln(\"n = %s, a = %s, b = %s\", n, a, b);\n }\n for (; n < N; n += K) {\n foreach (k; 0 .. K) {\n ans ~= [a + k % (b - a), n + k];\n }\n a = n;\n b = n + K;\n }\n \n graph = new int[][N];\n foreach (edge; ans) {\n graph[edge[0]] ~= edge[1];\n graph[edge[1]] ~= edge[0];\n }\n const res0 = dfs(0, -1);\n const res1 = dfs(res0.u, -1);\n writeln(res1.d);\n foreach (edge; ans) {\n writeln(edge[0] + 1, \" \", edge[1] + 1);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n \n void star(int sz) {\n foreach (i; 2 .. sz+1) {\n ans ~= tuple(1, i);\n }\n }\n \n if (k == n-1) {\n star(n);\n writeln(2);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n } else {\n star(k+1);\n foreach (i; k+1 .. n) {\n ans ~= tuple(i, i+1);\n }\n \n writeln(n - (k-1));\n ans.each!(t => writeln(t[0], ' ', t[1]));\n }\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n \n void star(int sz) {\n foreach (i; 2 .. sz+1) {\n ans ~= tuple(1, i);\n }\n }\n \n if (k == n-1) {\n star(n);\n writeln(2);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n } else {\n star(k+1);\n foreach (i; k+2 .. n) {\n ans ~= tuple(i, i+1);\n }\n \n writeln(n - (k-1));\n ans.each!(t => writeln(t[0], ' ', t[1]));\n }\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n foreach (i; 2 .. n+1) {\n int prev = i-k < 1 ? 1 : i-k;\n ans ~= tuple(prev, i);\n }\n \n writeln((n-1)/k + (n-1 + k-1)/k);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n \n void star(int sz) {\n foreach (i; 2 .. sz+1) {\n ans ~= tuple(1, i);\n }\n }\n \n if (k == n-1) {\n star(n);\n } else if (k == n-2) {\n star(n-2);\n ans ~= tuple(1, n-1);\n ans ~= tuple(n-1, n);\n } else {\n star(k+1);\n foreach (i; k+2 .. n) {\n ans ~= tuple(i, i+1);\n }\n ans ~= tuple(n, 1);\n }\n \n ans.length.writeln;\n foreach (t; ans) {\n writeln(t[0], ' ', t[1]);\n }\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n\n Tuple!(int, int)[] ans;\n \n void star(int sz) {\n foreach (i; 2 .. sz+1) {\n ans ~= tuple(1, i);\n }\n }\n \n if (k == n-1) {\n star(n);\n writeln(2);\n ans.each!(t => writeln(t[0], ' ', t[1]));\n } else {\n star(k);\n foreach (i; k .. n) {\n ans ~= tuple(i, i+1);\n }\n \n writeln(n - (k-1));\n ans.each!(t => writeln(t[0], ' ', t[1]));\n }\n}"}], "src_uid": "ad49b1b537ca539ea0898ee31a0aecf8"} {"nl": {"description": "Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round.You are a new applicant for his company. Boboniu will test you with the following chess question:Consider a $$$n\\times m$$$ grid (rows are numbered from $$$1$$$ to $$$n$$$, and columns are numbered from $$$1$$$ to $$$m$$$). You have a chess piece, and it stands at some cell $$$(S_x,S_y)$$$ which is not on the border (i.e. $$$2 \\le S_x \\le n-1$$$ and $$$2 \\le S_y \\le m-1$$$).From the cell $$$(x,y)$$$, you can move your chess piece to $$$(x,y')$$$ ($$$1\\le y'\\le m, y' \\neq y$$$) or $$$(x',y)$$$ ($$$1\\le x'\\le n, x'\\neq x$$$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column.Your goal is to visit each cell exactly once. Can you find a solution?Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point.", "input_spec": "The only line of the input contains four integers $$$n$$$, $$$m$$$, $$$S_x$$$ and $$$S_y$$$ ($$$3\\le n,m\\le 100$$$, $$$2 \\le S_x \\le n-1$$$, $$$2 \\le S_y \\le m-1$$$) — the number of rows, the number of columns, and the initial position of your chess piece, respectively.", "output_spec": "You should print $$$n\\cdot m$$$ lines. The $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\leq x_i \\leq n$$$, $$$1 \\leq y_i \\leq m$$$), denoting the $$$i$$$-th cell that you visited. You should print exactly $$$nm$$$ pairs $$$(x_i, y_i)$$$, they should cover all possible pairs $$$(x_i, y_i)$$$, such that $$$1 \\leq x_i \\leq n$$$, $$$1 \\leq y_i \\leq m$$$. We can show that under these constraints there always exists a solution. If there are multiple answers, print any.", "sample_inputs": ["3 3 2 2", "3 4 2 2"], "sample_outputs": ["2 2\n1 2\n1 3\n2 3\n3 3\n3 2\n3 1\n2 1\n1 1", "2 2\n2 1\n2 3\n2 4\n1 4\n3 4\n3 3\n3 2\n3 1\n1 1\n1 2\n1 3"], "notes": "NotePossible routes for two examples: "}, "positive_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1395/problem/B\n// implementation\nimport std.stdio;\n\nvoid main() {\n int n, m, sx, sy;\n readf(\"%s %s %s %s\", &n, &m, &sx, &sy);\n\n writefln(\"%s %s\", sx, sy);\n\n for(int i = 1; i <= n; i++)\n if(i != sx)\n writefln(\"%s %s\", i, sy);\n\n for(int i =1; i <= m; i++) {\n if(i == sy)\n continue;\n int direction = i&1;\n if(i > sy)\n direction ^= 1;\n if(direction == 1)\n for(int j = n; j > 0; j--)\n writefln(\"%s %s\", j, i);\n else\n for(int j = 1; j <= n; j++)\n writefln(\"%s %s\", j, i);\n }\n}\n\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint rows, cols, sRow, sCol;\n\twhile (readf !(\" %s %s %s %s\") (rows, cols, sRow, sCol) > 0)\n\t{\n\t\tauto r = rows.iota.array;\n\t\tswap (r[0], r[sRow - 1]);\n\t\tr[] += 1;\n\t\tauto c = cols.iota.array;\n\t\tswap (c[0], c[sCol - 1]);\n\t\tc[] += 1;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\tif (row & 1)\n\t\t\t{\n\t\t\t\tforeach_reverse (col; 0..cols)\n\t\t\t\t{\n\t\t\t\t\twriteln (r[row], \" \", c[col]);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tforeach (col; 0..cols)\n\t\t\t\t{\n\t\t\t\t\twriteln (r[row], \" \", c[col]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nalias type = single;\nstruct TestCase\n{\n mixin prettyPrinting;\n\n long n, m, sx, sy;\n\n void solve(long tc = -1)\n {\n foreach(y; sy .. m + 1)\n {\n writeln(sx, \" \", y);\n }\n foreach_reverse(y; 1 .. sy)\n {\n writeln(sx, \" \", y);\n }\n bool bottom = true;\n foreach(col; 1 .. n + 1)\n if (col != sx)\n {\n if (bottom)\n {\n foreach(y; 1 .. m + 1)\n {\n writeln(col, \" \", y);\n }\n }\n else\n {\n foreach_reverse(y; 1 .. m + 1)\n {\n writeln(col, \" \", y);\n }\n }\n bottom = !bottom;\n }\n }\n}\n\nstruct Interval\n{\n long l, h;\n\n bool empty()\n {\n return l > h;\n }\n}\n\nInterval intersect(Interval a, Interval b)\n{\n return Interval(max(a.l, b.l), min(a.h, b.h));\n}\n\nstruct If\n{\n string condition;\n}\nenum ignore = If(\"false\");\nstruct Dim\n{\n string dimensions;\n int levels;\n this(string dimensions)\n {\n this.dimensions = dimensions;\n levels = cast(int)dimensions.count(\",\") + 1;\n }\n}\n\ntemplate getArrayLevel(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));\n else\n enum getArrayLevel = 0;\n}\n\nstatic foreach(fieldName; 'a' .. cast(char)('z' + 1))\n {\n static if (fieldName != 'o')\n mixin(\"enum d\", fieldName, \" = Dim(\\\"\", fieldName, \"\\\");\");\n }\n\n\nmixin template prettyPrinting()\n{\n string toString()\n {\n alias structType = typeof(this);\n auto res = appender!(string)();\n res.put(structType.stringof);\n res.put(\"(\");\n static foreach(fieldName; FieldNameTuple!structType)\n {\n res.put(text(\" \", fieldName, \": \", mixin(fieldName)));\n }\n res.put(\" )\");\n res.put(\"\\n\");\n return res[];\n }\n}\n\nstruct Graph(N)\n{\n size_t size;\n this(S)(S size)\n {\n adjacencyList.length = cast(size_t) size;\n this.size = cast(size_t) size;\n this.visited = makeSlice!bool(size);\n }\n N[][] adjacencyList;\n alias ne = neighbours;\n N[] neighbours(N n)\n {\n return adjacencyList.at(n);\n }\n alias bcon = biconnect;\n void biconnect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n adjacencyList.at(b) ~= a;\n }\n alias con = connect;\n void connect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n }\n void connect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n connect(edge[0], edge[1]);\n }\n }\n void biconnect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n biconnect(edge[0], edge[1]);\n }\n }\n alias deg = degree!long;\n auto degree(T)(N a)\n {\n return cast(T) adjacencyList.at(a).length;\n }\n\n auto visited = new bool[0];\n void dfs(Visitor)(long startNode)\n {\n auto visitor = Visitor.init;\n void internalDfs(long node)\n {\n visited.set(node, true);\n static if (is(typeof({visitor.n = node;})))\n visitor.n = node;\n static if (is(typeof({visitor.pre;})))\n visitor.pre;\n foreach(w; ne(node))\n if (!visited.at(w))\n {\n static if (is(typeof({visitor.w = w;})))\n visitor.w = w;\n static if (is(typeof(visitor.pren)))\n visitor.pren;\n internalDfs(w);\n static if (is(typeof(visitor.postn)))\n visitor.postn;\n }\n static if (is(typeof(visitor.post)))\n visitor.post;\n }\n internalDfs(startNode);\n }\n void dfs(long startNode)\n {\n struct DefVisitor {}\n dfs!DefVisitor(startNode);\n }\n}\n\nDList!string _words;\n\ntemplate removeArray(W, int times)\n{\n static if (times == 0)\n alias removeArray = W;\n else static if (times == 1)\n alias removeArray = typeof(function(){W w; return w[0];}());\n else\n {\n static assert(isDynamicArray!W);\n alias removeArray = removeArray!(removeArray!(W, 1), times - 1);\n }\n}\n\ntemplate baseType(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));\n else\n alias baseType = W;\n}\n\nauto makeSlice(E, W, T...)(W size, T otherSizes)\n{\n static if (T.length == 0)\n {\n E[] res;\n res.length = cast(size_t) size;\n return res;\n }\n else\n {\n typeof(makeSlice!E(otherSizes))[] res;\n res.length = cast(size_t) size;\n foreach(ref row; res)\n row = makeSlice!E(otherSizes);\n return res;\n }\n}\n\nvoid read(T...)(ref T t)\n{\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\t\n\tauto n = RD!int;\n\tauto m = RD!int;\n\tauto sx = RD!int;\n\tauto sy = RD!int;\n\n\tint[][] ans;\n\tforeach_reverse (y; 1..sy+1)\n\t{\n\t\tans ~= [sx, y];\n\t}\n\tforeach (y; sy+1..m+1)\n\t{\n\t\tans ~= [sx, y];\n\t}\n\tauto x = sx-1;\n\tauto y = m;\n\tans ~= [x, y];\n\tint dir = -1;\n\twhile (true)\n\t{\n\t\t//debug writeln(x, \" \", y);\n\t\ty += dir;\n\t\tif (y == 0 || y == m+1)\n\t\t{\n\t\t\tdir = -dir;\n\t\t\ty += dir;\n\t\t\t--x;\n\t\t\tif (x == 0) break;\n\t\t}\n\t\tans ~= [x, y];\n\t}\n\n\tx = sx+1;\n\tans ~= [x, y];\n\twhile (true)\n\t{\n\t\t//debug writeln(x, \" \", y);\n\t\ty += dir;\n\t\tif (y == 0 || y == m+1)\n\t\t{\n\t\t\tdir = -dir;\n\t\t\ty += dir;\n\t\t\t++x;\n\t\t\tif (x == n+1) break;\n\t\t}\n\t\tans ~= [x, y];\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e[0], \" \", e[1]);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1395/problem/B\n// implementation\nimport std.stdio;\n\nvoid main() {\n int n, m, sx, sy;\n readf(\"%s %s %s %s\", &n, &m, &sx, &sy);\n\n writefln(\"%s %s\", sx, sy);\n\n for(int i = 1; i <= n; i++)\n if(i != sx)\n writefln(\"%s %s\", i, sx);\n\n for(int i =1; i <= m; i++) {\n if(i == sy)\n continue;\n int direction = i&1;\n if(i > sy)\n direction ^= 1;\n if(direction == 1)\n for(int j = n; j > 0; j--)\n writefln(\"%s %s\", j, i);\n else\n for(int j = 1; j <= n; j++)\n writefln(\"%s %s\", j, i);\n }\n}\n\n"}], "src_uid": "ed26479cdf72ad9686bbf334d90aa0be"} {"nl": {"description": "Let us call two integers $$$x$$$ and $$$y$$$ adjacent if $$$\\frac{lcm(x, y)}{gcd(x, y)}$$$ is a perfect square. For example, $$$3$$$ and $$$12$$$ are adjacent, but $$$6$$$ and $$$9$$$ are not.Here $$$gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$, and $$$lcm(x, y)$$$ denotes the least common multiple (LCM) of integers $$$x$$$ and $$$y$$$.You are given an array $$$a$$$ of length $$$n$$$. Each second the following happens: each element $$$a_i$$$ of the array is replaced by the product of all elements of the array (including itself), that are adjacent to the current value. Let $$$d_i$$$ be the number of adjacent elements to $$$a_i$$$ (including $$$a_i$$$ itself). The beauty of the array is defined as $$$\\max_{1 \\le i \\le n} d_i$$$. You are given $$$q$$$ queries: each query is described by an integer $$$w$$$, and you have to output the beauty of the array after $$$w$$$ seconds.", "input_spec": "The first input line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$) — the length of the array. The following line contains $$$n$$$ integers $$$a_1, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$) — array elements. The next line contain a single integer $$$q$$$ ($$$1 \\le q \\le 3 \\cdot 10^5$$$) — the number of queries. The following $$$q$$$ lines contain a single integer $$$w$$$ each ($$$0 \\le w \\le 10^{18}$$$) — the queries themselves. It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$, and the sum of values $$$q$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$", "output_spec": "For each query output a single integer — the beauty of the array at the corresponding moment.", "sample_inputs": ["2\n4\n6 8 4 2\n1\n0\n6\n12 3 20 5 80 1\n1\n1"], "sample_outputs": ["2\n3"], "notes": "NoteIn the first test case, the initial array contains elements $$$[6, 8, 4, 2]$$$. Element $$$a_4=2$$$ in this array is adjacent to $$$a_4=2$$$ (since $$$\\frac{lcm(2, 2)}{gcd(2, 2)}=\\frac{2}{2}=1=1^2$$$) and $$$a_2=8$$$ (since $$$\\frac{lcm(8,2)}{gcd(8, 2)}=\\frac{8}{2}=4=2^2$$$). Hence, $$$d_4=2$$$, and this is the maximal possible value $$$d_i$$$ in this array.In the second test case, the initial array contains elements $$$[12, 3, 20, 5, 80, 1]$$$. The elements adjacent to $$$12$$$ are $$$\\{12, 3\\}$$$, the elements adjacent to $$$3$$$ are $$$\\{12, 3\\}$$$, the elements adjacent to $$$20$$$ are $$$\\{20, 5, 80\\}$$$, the elements adjacent to $$$5$$$ are $$$\\{20, 5, 80\\}$$$, the elements adjacent to $$$80$$$ are $$$\\{20, 5, 80\\}$$$, the elements adjacent to $$$1$$$ are $$$\\{1\\}$$$. After one second, the array is transformed into $$$[36, 36, 8000, 8000, 8000, 1]$$$."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nimmutable int limit = 10 ^^ 6 + 5;\r\n\r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n\r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n\r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n \r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nimmutable int limit = 10 ^^ 6 + 5;\r\n \r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n \r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n \r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n \r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n \r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n \r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nimmutable int limit = 10 ^^ 6 + 5;\r\n \r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n \r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n \r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n \r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n \r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n \r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nimmutable int limit = 10 ^^ 6 + 5;\r\n \r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n \r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n \r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n \r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n \r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n \r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nimmutable int limit = 10 ^^ 6 + 5;\r\n \r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n \r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n \r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n \r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nimmutable int limit = 10 ^^ 6 + 5;\r\n \r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n \r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n \r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n \r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n \r\nimmutable int limit = 10 ^^ 6 + 5;\r\n \r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n \r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n \r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n \r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n \r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\r\n\r\nvoid get(Args...)(ref Args args)\r\n{\r\n import std.traits, std.meta, std.typecons;\r\n\r\n static if (Args.length == 1) {\r\n alias Arg = Args[0];\r\n \r\n static if (isArray!Arg) {\r\n static if (isSomeChar!(ElementType!Arg)) {\r\n args[0] = readln.chomp.to!Arg;\r\n } else {\r\n args[0] = readln.split.to!Arg;\r\n }\r\n } else static if (isTuple!Arg) {\r\n auto input = readln.split;\r\n static foreach (i; 0..Fields!Arg.length) {\r\n args[0][i] = input[i].to!(Fields!Arg[i]);\r\n }\r\n } else {\r\n args[0] = readln.chomp.to!Arg;\r\n }\r\n } else {\r\n auto input = readln.split;\r\n assert(input.length == Args.length);\r\n\r\n static foreach (i; 0..Args.length) {\r\n args[i] = input[i].to!(Args[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid get_lines(Args...)(size_t N, ref Args args)\r\n{\r\n import std.traits, std.range;\r\n\r\n static foreach (i; 0..Args.length) {\r\n static assert(isArray!(Args[i]));\r\n args[i].length = N;\r\n }\r\n\r\n foreach (i; 0..N) {\r\n static if (Args.length == 1) {\r\n get(args[0][i]);\r\n } else {\r\n auto input = readln.split;\r\n static foreach (j; 0..Args.length) {\r\n args[j][i] = input[j].to!(ElementType!(Args[j]));\r\n }\r\n }\r\n }\r\n}\r\n\r\nenum PCNT = 10^^3;\r\n\r\nbool[PCNT+1] PS;\r\n\r\nvoid prime_init()\r\n{\r\n PS[] = true;\r\n PS[0] = false;\r\n PS[1] = false;\r\n foreach (i; 2..PCNT+1) {\r\n if (PS[i]) {\r\n auto x = i*2;\r\n while (x <= PCNT) {\r\n PS[x] = false;\r\n x += i;\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid main()\r\n{\r\n prime_init();\r\n long[] ps;\r\n foreach (i; 0..PCNT + 1) if (PS[i]) ps ~= i;\r\n\r\n int T; get(T);\r\n while (T--) {\r\n int N; get(N);\r\n long[] AS; get(AS);\r\n\r\n foreach (ref a; AS) foreach (p; ps) while (a % p^^2 == 0) a /= p^^2;\r\n sort(AS);\r\n AS ~= -1;\r\n long last = -1, cnt, one_and_evens, max_r;\r\n foreach (i, a; AS) {\r\n if (a != last) {\r\n max_r = max(max_r, cnt);\r\n if (last == 1 || cnt % 2 == 0) one_and_evens += cnt;\r\n cnt = 1;\r\n last = a;\r\n } else {\r\n ++cnt;\r\n }\r\n }\r\n auto r0 = max_r;\r\n auto r1 = max(max_r, one_and_evens);\r\n\r\n int Q; get(Q);\r\n while (Q--) {\r\n long W; get(W);\r\n writeln(W == 0 ? r0 : r1);\r\n }\r\n }\r\n}"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nint [] prepare (int limit)\r\n{\r\n\tauto res = limit.iota.array;\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (res[d] == d)\r\n\t\t{\r\n\t\t\tfor (int e = d * d; e < limit; e += d)\r\n\t\t\t{\r\n\t\t\t\tif (res[e] == e)\r\n\t\t\t\t{\r\n\t\t\t\t\tres[e] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nint [] roots (int limit)\r\n{\r\n\tauto p = prepare (limit);\r\n\tauto res = new int [limit];\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tint cur = d;\r\n\t\tres[d] = 1;\r\n\t\twhile (cur > 1)\r\n\t\t{\r\n\t\t\tbool parity = 0;\r\n\t\t\tint x = p[cur];\r\n\t\t\twhile (cur % x == 0)\r\n\t\t\t{\r\n\t\t\t\tcur /= x;\r\n\t\t\t\tparity ^= 1;\r\n\t\t\t}\r\n\t\t\tif (parity)\r\n\t\t\t{\r\n\t\t\t\tres[d] *= x;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nimmutable int limit = 10 ^^ 6;\r\n\r\nvoid main ()\r\n{\r\n\tauto r = roots (limit);\r\n\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tint [int] nums;\r\n\t\tforeach (ref c; a)\r\n\t\t{\r\n\t\t\tnums[r[c]] += 1;\r\n\t\t}\r\n\r\n\t\tauto answer = [0, 0, 0];\r\n\t\tforeach (step; 0..3)\r\n\t\t{\r\n\t\t\tdebug {writeln (nums);}\r\n\t\t\tint [int] next;\r\n\t\t\tforeach (k, v; nums)\r\n\t\t\t{\r\n\t\t\t\tanswer[step] = max (answer[step], v);\r\n\t\t\t\tint p = (v & 1) ? k : 1;\r\n\t\t\t\tnext[p] += v;\r\n\t\t\t}\r\n\t\t\tnums = next;\r\n\t\t}\r\n\r\n\t\tauto q = readln.strip.to !(int);\r\n\t\tforeach (s; 0..q)\r\n\t\t{\r\n\t\t\tauto z = readln.strip.to !(long);\r\n\t\t\twriteln (answer[min (z, $ - 1).to !(int)]);\r\n\t\t}\r\n\t}\r\n}\r\n"}], "src_uid": "63108f3cc494df3c7bb62381c03801b3"} {"nl": {"description": "You are given a positive integer $$$n$$$. Your task is to find any three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \\le a, b, c \\le 10^9$$$) for which $$$(a\\oplus b)+(b\\oplus c)+(a\\oplus c)=n$$$, or determine that there are no such integers.Here $$$a \\oplus b$$$ denotes the bitwise XOR of $$$a$$$ and $$$b$$$. For example, $$$2 \\oplus 4 = 6$$$ and $$$3 \\oplus 1=2$$$.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The following lines contain the descriptions of the test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$). ", "output_spec": "For each test case, print any three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \\le a, b, c \\le 10^9$$$) for which $$$(a\\oplus b)+(b\\oplus c)+(a\\oplus c)=n$$$. If no such integers exist, print $$$-1$$$.", "sample_inputs": ["5\n\n4\n\n1\n\n12\n\n2046\n\n194723326"], "sample_outputs": ["3 3 1\n-1\n2 4 6\n69 420 666\n12345678 87654321 100000000"], "notes": "NoteIn the first test case, $$$a=3$$$, $$$b=3$$$, $$$c=1$$$, so $$$(3 \\oplus 3)+(3 \\oplus 1) + (3 \\oplus 1)=0+2+2=4$$$.In the second test case, there are no solutions.In the third test case, $$$(2 \\oplus 4)+(4 \\oplus 6) + (2 \\oplus 6)=6+2+4=12$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid solve()\n{\n auto n = readln.strip.to!int;\n if (n % 2 == 0)\n writefln!(\"0 0 %s\")(n / 2);\n else\n writeln(-1);\n}\n\nvoid main()\n{\n size_t TC = 1;\n TC = readln.strip.to!int;\n foreach (_; 0 .. TC)\n solve();\n}\n"}], "negative_code": [], "src_uid": "43041076ddd0bbfac62cd4abf4536282"} {"nl": {"description": "While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being \"Luxury\" days. She counts the days since she started these activities, in which the day she starts is numbered as day $$$1$$$. A \"Luxury\" day is the day in which the number of this day is a luxurious number. An integer $$$x$$$ is called a luxurious number if it is divisible by $$${\\lfloor \\sqrt{x} \\rfloor}$$$.Here $$$\\lfloor r \\rfloor$$$ denotes the \"floor\" of a real number $$$r$$$. In other words, it's the largest integer not greater than $$$r$$$.For example: $$$8$$$, $$$56$$$, $$$100$$$ are luxurious numbers, since $$$8$$$ is divisible by $$$\\lfloor \\sqrt{8} \\rfloor = \\lfloor 2.8284 \\rfloor = 2$$$, $$$56$$$ is divisible $$$\\lfloor \\sqrt{56} \\rfloor = \\lfloor 7.4833 \\rfloor = 7$$$, and $$$100$$$ is divisible by $$$\\lfloor \\sqrt{100} \\rfloor = \\lfloor 10 \\rfloor = 10$$$, respectively. On the other hand $$$5$$$, $$$40$$$ are not, since $$$5$$$ are not divisible by $$$\\lfloor \\sqrt{5} \\rfloor = \\lfloor 2.2361 \\rfloor = 2$$$, and $$$40$$$ are not divisible by $$$\\lfloor \\sqrt{40} \\rfloor = \\lfloor 6.3246 \\rfloor = 6$$$.Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day $$$l$$$ and day $$$r$$$, you want to know how many times she changes the activities.", "input_spec": "Each test contains multiple test cases. The first line has the number of test cases $$$t$$$ ($$$1 \\le t \\le 10\\ 000$$$). The description of the test cases follows. The only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le 10^{18}$$$) — the intervals at which you want to know how many times Ela changes her sports.", "output_spec": "For each test case, output an integer that denotes the answer.", "sample_inputs": ["5\n\n8 19\n\n8 20\n\n119 121\n\n1 100000000000000000\n\n1234567891011 1000000000000000000"], "sample_outputs": ["5\n6\n2\n948683296\n2996666667"], "notes": "NoteIn the first test case, $$$5$$$ luxury numbers in range $$$[8, 19]$$$ are: $$$8, 9, 12, 15, 16$$$."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n long L, R; readf(\"%d %d\\n\", &L, &R);\r\n\r\n long sqrt_floor(long x) {\r\n long lb = 1, ub = 1_000_000_100;\r\n while (lb + 1 < ub) {\r\n long mid = (lb + ub) / 2;\r\n if (mid * mid <= x) {\r\n lb = mid;\r\n } else {\r\n ub = mid;\r\n }\r\n }\r\n return lb;\r\n }\r\n\r\n long C(long x) {\r\n if (x == 0L) return 0L;\r\n long d = sqrt_floor(x);\r\n long s = d * d;\r\n return (d - 1L) * 3L + (x - s) / d + 1L;\r\n }\r\n\r\n writeln(C(R) - C(L-1));\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nlong solve (long n)\r\n{\r\n\tlong x = cast (long) (sqrt (cast (real) (n)));\r\n\tlong res = x * 3 - 2;\r\n\tres += (x * (x + 1) <= n);\r\n\tres += (x * (x + 2) <= n);\r\n\treturn res;\r\n}\r\n\r\nlong solve (long lo, long hi)\r\n{\r\n\treturn solve (hi) - solve (lo - 1);\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tlong lo, hi;\r\n\t\treadf !(\" %s %s\") (lo, hi);\r\n\t\twriteln (solve (lo, hi));\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "10812427d3052ce91cd0951911d3acb9"} {"nl": {"description": "Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game.Rules of the game are very simple: at first number of rounds n is defined. In every round each of the players throws a cubical dice with distinct numbers from 1 to 6 written on its faces. Player, whose value after throwing the dice is greater, wins the round. In case if player dice values are equal, no one of them is a winner.In average, player, who won most of the rounds, is the winner of the game. In case if two players won the same number of rounds, the result of the game is draw.Mishka is still very little and can't count wins and losses, so she asked you to watch their game and determine its result. Please help her!", "input_spec": "The first line of the input contains single integer n n (1 ≤ n ≤ 100) — the number of game rounds. The next n lines contains rounds description. i-th of them contains pair of integers mi and ci (1 ≤ mi,  ci ≤ 6) — values on dice upper face after Mishka's and Chris' throws in i-th round respectively.", "output_spec": "If Mishka is the winner of the game, print \"Mishka\" (without quotes) in the only line. If Chris is the winner of the game, print \"Chris\" (without quotes) in the only line. If the result of the game is draw, print \"Friendship is magic!^^\" (without quotes) in the only line.", "sample_inputs": ["3\n3 5\n2 1\n4 2", "2\n6 1\n1 6", "3\n1 5\n3 3\n2 2"], "sample_outputs": ["Mishka", "Friendship is magic!^^", "Chris"], "notes": "NoteIn the first sample case Mishka loses the first round, but wins second and third rounds and thus she is the winner of the game.In the second sample case Mishka wins the first round, Chris wins the second round, and the game ends with draw with score 1:1.In the third sample case Chris wins the first round, but there is no winner of the next two rounds. The winner of the game is Chris."}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string read_string() {\n while (tokens.empty) {\n tokens = readln.split;\n }\n auto token = tokens.front;\n tokens.popFront;\n return token;\n }\n \n int read_int() {\n return read_string.to!int;\n }\n \n string[] tokens;\n}\n\nvoid main() {\n IO cin;\n int t = 1;\n // int t = cin.read_int;\n while (t--) {\n int n = cin.read_int;\n int c_a = 0;\n int c_b = 0;\n for (int i = 0; i < n; i++) {\n int a = cin.read_int;\n int b = cin.read_int;\n if (a > b) c_a++;\n if (b > a) c_b++;\n }\n if (c_a > c_b) writeln(\"Mishka\");\n else if (c_b > c_a) writeln(\"Chris\");\n else writeln(\"Friendship is magic!^^\");\n } \n}"}, {"source_code": "import std.stdio, std.range, std.algorithm, std.conv, std.string, std.array;\n\nvoid main() {\n\t\n\tint n = readln.strip.to!int;\n\n\tint Mishka, Chris;\n\n\tint mM, mC;\n\twhile (n--) {\n\t\treadf(\" %s %s\", &mM, &mC);\n\t\tif (mM > mC)\n\t\t\t++Mishka;\n\t\telse if (mM < mC)\n\t\t\t++Chris;\n\t}\n\n\tif (Mishka == Chris)\n\t\twriteln(\"Friendship is magic!^^\");\n\telse if (Mishka > Chris)\n\t\twriteln(\"Mishka\");\n\telse\n\t\twriteln(\"Chris\");\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.math;\n\nstring readline()\n{\n\tstring res = readln();\n\tif (res[$-1] == '\\n') res.length--;\n\treturn res;\n}\n\nint main(string[] argv)\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tint m, c;\n\tforeach (int i; 0..n)\n\t{\n\t\tint md, cd;\n\t\tscanf(\"%d %d\", &md, &cd);\n\t\tif (md > cd) \n\t\t\tm++;\n\t\telse if (cd > md)\n\t\t\tc++;\n\t}\n\tif (m > c)\n\t{\n\t\tprintf(\"Mishka\");\n\t} else\n\tif (m == c)\n\t{\n\t\tprintf(\"Friendship is magic!^^\");\n\t} else\n\t{\n\t\tprintf(\"Chris\");\n\t}\n\treturn 0;\n}"}, {"source_code": "import std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.exception;\nimport std.conv;\nimport std.array;\nimport std.range;\n\nint mishkaKacKereKazandi = 0;\nint chrisKacKereKazandi = 0;\n\nvoid zarlariKarsilastir( int mishkaninZari, int chrisinZari )\n{\n\tif ( mishkaninZari > chrisinZari )\n\t\tmishkaKacKereKazandi++;\n\telse if ( chrisinZari > mishkaninZari )\n\t\tchrisKacKereKazandi++;\n}\n\nvoid main() {\n\n auto satirSayisi = stdin.readln.strip.to!int();\n\n auto zarlarDizisi = stdin\n\t\t.byLine()\n\t\t.take(satirSayisi)\n\t\t.map!(line => line\n\t\t\t .split\n\t\t\t .map!(a => to!int(a))\n\t\t\t .array());\n\n zarlarDizisi.each!( a=> zarlariKarsilastir(a[0], a[1]) );\n\n\tif ( mishkaKacKereKazandi > chrisKacKereKazandi )\n\t\twriteln(\"Mishka\");\n\telse if ( mishkaKacKereKazandi == chrisKacKereKazandi )\n\t\twriteln(\"Friendship is magic!^^\");\n\telse \n\t\twriteln( \"Chris\" );\n}"}, {"source_code": "import std.stdio;\n\nint main()\n{\n\tint a=0;\n\treadf(\" %d\", &a);\n\tint d=0;\n\tint e=0;\n\tfor (int i=0; ic)\n\t\t{\n\t\t\td=d+1;\n\t\t}\n\t\tif (c>b)\n\t\t{\n\t\t\te=e+1;\n\t\t}\n\t}\n\tif (d>e)\n\t{\n\t\twriteln(\"Mishka\");\n\t}\n\telse if (e>d)\n\t{\n\t\twriteln(\"Chris\");\n\t}\n\telse\n\t{\n\t\twriteln(\"Friendship is magic!^^\");\n\t}\n\treturn 0;\n}"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.exception;\nimport std.conv;\nimport std.array;\nimport std.range;\n\nint mishkaKacKereKazandi = 0;\n\nvoid zarlariKarsilastir( int mishkaninZari, int chrisinZari )\n{\n\tif ( mishkaninZari > chrisinZari )\n\t\tmishkaKacKereKazandi++;\n}\n\nvoid main() {\n\n auto satirSayisi = stdin.readln.strip.to!int();\n\n auto zarlarDizisi = stdin\n\t\t.byLine()\n\t\t.take(satirSayisi)\n\t\t.map!(line => line\n\t\t\t .split\n\t\t\t .map!(a => to!int(a))\n\t\t\t .array());\n\n zarlarDizisi.each!( a=> zarlariKarsilastir(a[0], a[1]) );\n\n\tif ( mishkaKacKereKazandi > satirSayisi/2 )\n\t\twriteln(\"Mishka\");\n\telse if ( mishkaKacKereKazandi == satirSayisi/2 )\n\t\twriteln(\"Friendship is magic!^^\");\n\telse \n\t\twriteln( \"Chris\" );\n}"}], "src_uid": "76ecde4a445bbafec3cda1fc421e6d42"} {"nl": {"description": "DZY loves Fast Fourier Transformation, and he enjoys using it.Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if a, b and c are sequences with length n, which are indexed from 0 to n - 1, andWe can calculate c fast using Fast Fourier Transformation.DZY made a little change on this formula. NowTo make things easier, a is a permutation of integers from 1 to n, and b is a sequence only containing 0 and 1. Given a and b, DZY needs your help to calculate c.Because he is naughty, DZY provides a special way to get a and b. What you need is only three integers n, d, x. After getting them, use the code below to generate a and b.//x is 64-bit variable;function getNextX() { x = (x * 37 + 10007) % 1000000007; return x;}function initAB() { for(i = 0; i < n; i = i + 1){ a[i] = i + 1; } for(i = 0; i < n; i = i + 1){ swap(a[i], a[getNextX() % (i + 1)]); } for(i = 0; i < n; i = i + 1){ if (i < d) b[i] = 1; else b[i] = 0; } for(i = 0; i < n; i = i + 1){ swap(b[i], b[getNextX() % (i + 1)]); }}Operation x % y denotes remainder after division x by y. Function swap(x, y) swaps two values x and y.", "input_spec": "The only line of input contains three space-separated integers n, d, x (1 ≤ d ≤ n ≤ 100000; 0 ≤ x ≤ 1000000006). Because DZY is naughty, x can't be equal to 27777500.", "output_spec": "Output n lines, the i-th line should contain an integer ci - 1.", "sample_inputs": ["3 1 1", "5 4 2", "5 4 3"], "sample_outputs": ["1\n3\n2", "2\n2\n4\n5\n5", "5\n5\n5\n5\n4"], "notes": "NoteIn the first sample, a is [1 3 2], b is [1 0 0], so c0 = max(1·1) = 1, c1 = max(1·0, 3·1) = 3, c2 = max(1·0, 3·0, 2·1) = 2.In the second sample, a is [2 1 4 5 3], b is [1 1 1 0 1].In the third sample, a is [5 2 1 4 3], b is [1 1 1 1 0]."}, "positive_code": [{"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nclass Generator {\n\tint n;\n\tint d;\n\tlong x;\n\tint[] a, b;\n\tthis(int n, int d, long x) {\n\t\tthis.n = n;\n\t\tthis.d = d;\n\t\tthis.x = x;\n\t\ta = new int[n];\n\t\tb = new int[n];\n\t}\n\t//x is 64-bit variable;\n\tlong getNextX() {\n\t\tx = (x * 37 + 10007) % 1000000007;\n\t\treturn x;\n\t}\n\tvoid initAB() {\n\t\tint i;\n\t\tfor(i = 0; i < n; i = i + 1){\n\t\t\ta[i] = i + 1;\n\t\t}\n\t\tfor(i = 0; i < n; i = i + 1){\n\t\t\tswap(a[i], a[cast(size_t)(getNextX() % (i + 1))]);\n\t\t}\n\t\tfor(i = 0; i < n; i = i + 1){\n\t\t\tif (i < d)\n\t\t\t\tb[i] = 1;\n\t\t\telse\n\t\t\t\tb[i] = 0;\n\t\t}\n\t\tfor(i = 0; i < n; i = i + 1){\n\t\t\tswap(b[i], b[cast(size_t)(getNextX() % (i + 1))]);\n\t\t}\n\t}\n}\n\nimmutable NUM_BIG = 1000;\n\nint N, D;\nlong X;\nint[] A, B;\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tN = readInt;\n\t\tD = readInt;\n\t\tX = readLong;\n\t\tauto gen = new Generator(N, D, X);\n\t\tgen.initAB;\n\t\tA = gen.a;\n\t\tB = gen.b;\ndebug{\nwriteln(\"A = \",A);\nwriteln(\"B = \",B);\n}\n\t\t\n\t\tint[] js;\n\t\tforeach (j; 0 .. N) if (B[j]) {\n\t\t\tjs ~= j;\n\t\t}\n\t\t\n\t\tint[] ans = new int[N];\n\t\tforeach (i; 0 .. N) if (A[i] >= N - NUM_BIG) {\n\t\t\tforeach (j; js) {\n\t\t\t\tif (i + j >= N) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tchmax(ans[i + j], A[i]);\n\t\t\t}\n\t\t}\n\t\t\n\t\tforeach (k; 0 .. N) if (!ans[k]) {\n\t\t\tforeach (j; js) {\n\t\t\t\tif (j > k) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tchmax(ans[k], A[k - j]);\n\t\t\t}\n\t\t}\n\t\t\n\t\tforeach (k; 0 .. N) {\n\t\t\twriteln(ans[k]);\n\t\t}\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [], "src_uid": "948ae7a0189ada07c8c67a1757f691f0"} {"nl": {"description": "You are given some Tetris field consisting of $$$n$$$ columns. The initial height of the $$$i$$$-th column of the field is $$$a_i$$$ blocks. On top of these columns you can place only figures of size $$$2 \\times 1$$$ (i.e. the height of this figure is $$$2$$$ blocks and the width of this figure is $$$1$$$ block). Note that you cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one $$$a_i$$$ is greater than $$$0$$$: You place one figure $$$2 \\times 1$$$ (choose some $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$a_i$$$ with $$$a_i + 2$$$); then, while all $$$a_i$$$ are greater than zero, replace each $$$a_i$$$ with $$$a_i - 1$$$. And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of columns in the Tetris field. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the initial height of the $$$i$$$-th column of the Tetris field.", "output_spec": "For each test case, print the answer — \"YES\" (without quotes) if you can clear the whole Tetris field and \"NO\" otherwise.", "sample_inputs": ["4\n3\n1 1 3\n4\n1 1 2 1\n2\n11 11\n1\n100"], "sample_outputs": ["YES\nNO\nYES\nYES"], "notes": "NoteThe first test case of the example field is shown below:Gray lines are bounds of the Tetris field. Note that the field has no upper bound.One of the correct answers is to first place the figure in the first column. Then after the second step of the process, the field becomes $$$[2, 0, 2]$$$. Then place the figure in the second column and after the second step of the process, the field becomes $$$[0, 0, 0]$$$.And the second test case of the example field is shown below:It can be shown that you cannot do anything to end the process.In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes $$$[0, 2]$$$. Then place the figure in the first column and after the second step of the process, the field becomes $$$[0, 0]$$$.In the fourth test case of the example, place the figure in the first column, then the field becomes $$$[102]$$$ after the first step of the process, and then the field becomes $$$[0]$$$ after the second step of the process."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n readln;\n auto as = readln.split.to!(int[]);\n auto x = as[0]%2;\n foreach (a; as) if (x != a%2) goto ng;\n writeln(\"YES\");\n continue;\n ng:\n writeln(\"NO\");\n }\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new bool[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA;\n\t\tauto last = a[0] % 2;\n\t\tans[ti] = true;\n\t\tforeach (e; a[1..$])\n\t\t{\n\t\t\tif (e % 2 != last)\n\t\t\t\tans[ti] = false;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e ? \"YES\" : \"NO\");\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "53a3313f5d6ce19413d72473717054fc"} {"nl": {"description": "An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 50$$$) — the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*' — an empty cell or a shaded cell, respectively.", "output_spec": "For each test case, output \"YES\" if the grid is made up of L-shape that don't share edges or corners, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["10\n\n6 10\n\n........**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n6 10\n\n....*...**\n\n.**......*\n\n..*..*....\n\n.....**...\n\n...*.....*\n\n..**....**\n\n3 3\n\n...\n\n***\n\n...\n\n4 4\n\n.*..\n\n**..\n\n..**\n\n..*.\n\n5 4\n\n.*..\n\n**..\n\n....\n\n..**\n\n..*.\n\n3 2\n\n.*\n\n**\n\n*.\n\n2 3\n\n*..\n\n.**\n\n3 2\n\n..\n\n**\n\n*.\n\n3 3\n\n.**\n\n*.*\n\n**.\n\n3 3\n\n..*\n\n.**\n\n..*"], "sample_outputs": ["YES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n int H, W; readf(\"%d %d\\n\", &H, &W);\r\n auto F = iota(0, H).map!((x) => readln.chomp).array;\r\n\r\n bool lshape(int y, int x) {\r\n int count = 0;\r\n for (int i = y; i <= y + 1; i++) {\r\n for (int j = x; j <= x + 1; j++) {\r\n if (F[i][j] == '*') count++;\r\n }\r\n }\r\n return count == 3;\r\n }\r\n bool check(int y, int x) {\r\n int count = 0;\r\n for (int i = max(0, y - 1); i <= min(H - 1, y + 1); i++) {\r\n for (int j = max(0, x - 1); j <= min(W - 1, x + 1); j++) {\r\n if (F[i][j] == '*') count++;\r\n }\r\n }\r\n return count == 3;\r\n }\r\n bool check_all() {\r\n auto C = new int[][](H, W);\r\n for (int y = 0; y + 1 < H; y++) {\r\n for (int x = 0; x + 1 < W; x++) {\r\n if (lshape(y, x)) {\r\n for (int dy = 0; dy <= 1; dy++) for (int dx = 0; dx <= 1; dx++) {\r\n C[y + dy][x + dx]++;\r\n }\r\n }\r\n }\r\n }\r\n for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) {\r\n if (C[y][x] >= 2) return false;\r\n if (F[y][x] == '*') {\r\n if (C[y][x] == 0) return false;\r\n if (! check(y, x)) return false;\r\n }\r\n }\r\n return true;\r\n }\r\n writeln(check_all() ? \"Yes\" : \"No\");\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) {\r\n solve();\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n int H, W; readf(\"%d %d\\n\", &H, &W);\r\n auto F = iota(0, H).map!((x) => readln.chomp).array;\r\n\r\n bool check(int y, int x) {\r\n int ycount = 0, xcount = 0;\r\n for (int i = max(0, y - 1); i <= min(H - 1, y + 1); i++) {\r\n if (F[i][x] == '*') ycount++;\r\n }\r\n for (int j = max(0, x - 1); j <= min(W - 1, x + 1); j++) {\r\n if (F[y][j] == '*') xcount++;\r\n }\r\n if (! (ycount == 2 || xcount == 2)) return false;\r\n int count = 0;\r\n for (int i = max(0, y - 1); i <= min(H - 1, y + 1); i++) {\r\n for (int j = max(0, x - 1); j <= min(W - 1, x + 1); j++) {\r\n if (F[i][j] == '*') count++;\r\n }\r\n }\r\n return count == 3;\r\n }\r\n bool check_all() {\r\n for (int y = 0; y < H; y++) {\r\n for (int x = 0; x < W; x++) {\r\n if (F[y][x] == '*' && !check(y, x)) {\r\n return false;\r\n }\r\n }\r\n }\r\n return true;\r\n }\r\n writeln(check_all() ? \"Yes\" : \"No\");\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) {\r\n solve();\r\n }\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n int H, W; readf(\"%d %d\\n\", &H, &W);\r\n auto F = iota(0, H).map!((x) => readln.chomp).array;\r\n\r\n bool check(int y, int x) {\r\n int count = 0;\r\n for (int i = max(0, y - 1); i <= min(H - 1, y + 1); i++) {\r\n for (int j = max(0, x - 1); j <= min(W - 1, x + 1); j++) {\r\n if (F[i][j] == '*') count++;\r\n }\r\n }\r\n return count == 3;\r\n }\r\n bool check_all() {\r\n for (int y = 0; y < H; y++) {\r\n for (int x = 0; x < W; x++) {\r\n if (F[y][x] == '*' && !check(y, x)) {\r\n return false;\r\n }\r\n }\r\n }\r\n return true;\r\n }\r\n writeln(check_all() ? \"Yes\" : \"No\");\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) {\r\n solve();\r\n }\r\n}\r\n"}], "src_uid": "6a06ad39dbdd97ca8654023009c89a42"} {"nl": {"description": "Vus the Cossack has $$$n$$$ real numbers $$$a_i$$$. It is known that the sum of all numbers is equal to $$$0$$$. He wants to choose a sequence $$$b$$$ the size of which is $$$n$$$ such that the sum of all numbers is $$$0$$$ and each $$$b_i$$$ is either $$$\\lfloor a_i \\rfloor$$$ or $$$\\lceil a_i \\rceil$$$. In other words, $$$b_i$$$ equals $$$a_i$$$ rounded up or down. It is not necessary to round to the nearest integer.For example, if $$$a = [4.58413, 1.22491, -2.10517, -3.70387]$$$, then $$$b$$$ can be equal, for example, to $$$[4, 2, -2, -4]$$$. Note that if $$$a_i$$$ is an integer, then there is no difference between $$$\\lfloor a_i \\rfloor$$$ and $$$\\lceil a_i \\rceil$$$, $$$b_i$$$ will always be equal to $$$a_i$$$.Help Vus the Cossack find such sequence!", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the number of numbers. Each of the next $$$n$$$ lines contains one real number $$$a_i$$$ ($$$|a_i| < 10^5$$$). It is guaranteed that each $$$a_i$$$ has exactly $$$5$$$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $$$0$$$.", "output_spec": "In each of the next $$$n$$$ lines, print one integer $$$b_i$$$. For each $$$i$$$, $$$|a_i-b_i|<1$$$ must be met. If there are multiple answers, print any.", "sample_inputs": ["4\n4.58413\n1.22491\n-2.10517\n-3.70387", "5\n-6.32509\n3.30066\n-0.93878\n2.00000\n1.96321"], "sample_outputs": ["4\n2\n-2\n-4", "-6\n3\n-1\n2\n2"], "notes": "NoteThe first example is explained in the legend.In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto a = new double[](n);\n\tauto ans = new long[](n);\n\tlong diff;\n\tforeach (i; 0..n)\n\t{\n\t\ta[i] = RD!double;\n\t\tans[i] = cast(long)a[i];\n\t\tdiff += ans[i];\n\t}\n\n\tdiff = -diff;\n\tforeach (i; 0..n)\n\t{\n\t\tif (diff == 0) break;\n\t\tif (a[i] == ans[i]) continue;\n\n\t\tif (sgn(a[i]) == sgn(diff))\n\t\t{\n\t\t\tans[i] += sgn(diff);\n\t\t\tdiff -= sgn(diff);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto a = new double[](n);\n\tauto ans = new long[](n);\n\tlong diff;\n\tforeach (i; 0..n)\n\t{\n\t\ta[i] = RD!double;\n\t\tans[i] = cast(long)a[i];\n\t\tdiff += ans[i];\n\t}\n\n\tdiff = -diff;\n\tforeach (i; 0..n)\n\t{\n\t\tif (diff == 0) break;\n\n\t\tif (sgn(a[i]) == sgn(diff))\n\t\t{\n\t\t\tans[i] += sgn(diff);\n\t\t\tdiff -= sgn(diff);\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush();\n\tdebug readln();\n}"}], "src_uid": "6059cfa13594d47b3e145d7c26f1b0b3"} {"nl": {"description": "There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.Each kangaroo can hold at most one kangaroo, and the kangaroo who is held by another kangaroo cannot hold any kangaroos.The kangaroo who is held by another kangaroo cannot be visible from outside. Please, find a plan of holding kangaroos with the minimal number of kangaroos who is visible.", "input_spec": "The first line contains a single integer — n (1 ≤ n ≤ 5·105). Each of the next n lines contains an integer si — the size of the i-th kangaroo (1 ≤ si ≤ 105).", "output_spec": "Output a single integer — the optimal number of visible kangaroos.", "sample_inputs": ["8\n2\n5\n7\n6\n9\n8\n4\n2", "8\n9\n1\n6\n2\n6\n5\n8\n3"], "sample_outputs": ["5", "5"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MAX_N = 100_005;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto c = new int [MAX_N + 1];\n\t\tc[] = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\tc[x]++;\n\t\t}\n\t\tint [] a;\n\t\tforeach (k, b; c)\n\t\t{\n\t\t\tforeach (i; 0..b)\n\t\t\t{\n\t\t\t\ta ~= k;\n\t\t\t}\n\t\t}\n\t\tenforce (a.length == n);\n\t\tdebug {writeln (a);}\n\n\t\tbool check (int me)\n\t\t{\n\t\t\tforeach (i; 0..me)\n\t\t\t{\n\t\t\t\tdebug {writeln (\"compare \", i,\n\t\t\t\t \" \", n - me + i);}\n\t\t\t\tif (a[i] * 2 > a[n - me + i])\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t\tint lo = 0;\n\t\tint hi = n >> 1;\n\t\twhile (lo < hi)\n\t\t{\n\t\t\tint me = (lo + hi + 1) >> 1;\n\t\t\tdebug {writeln (lo, ' ', hi, ' ', me);}\n\t\t\tif (check (me))\n\t\t\t{\n\t\t\t\tlo = me;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\thi = me - 1;\n\t\t\t}\n\t\t}\n\t\twriteln (n - lo);\n\t}\n}\n"}, {"source_code": "#!/usr/bin/env rdmd\nimport std.stdio, std.string, std.conv;\nimport std.algorithm, std.array;\n\nvoid main()\n{\n for(string S; (S=readln().chomp()).length; )\n {\n auto n = S.to!int();\n auto s = new int[n];\n foreach(i;0..n)\n s[i] = readln().chomp().to!int();\n s.sort;\n int c = 0;\n int p = n/2;\n foreach(i;0..n/2)\n {\n auto v = s[i]*2;\n for( ; p 0)\n\t{\n\t\tauto c = new int [MAX_N + 1];\n\t\tc[] = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\tc[x]++;\n\t\t}\n\t\tint j = 0;\n\t\tint res = 0;\n\t\tforeach (i; 0..MAX_N)\n\t\t{\n\t\t\tj = max (j, i << 1);\n\t\t\tj = min (j, MAX_N);\n\t\t\twhile (c[i] > 0)\n\t\t\t{\n\t\t\t\twhile (j < MAX_N && c[j] == 0)\n\t\t\t\t{\n\t\t\t\t\tj++;\n\t\t\t\t}\n\t\t\t\tif (c[j] == 0)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tc[i]--;\n\t\t\t\tc[j]--;\n\t\t\t\tres++;\n\t\t\t}\n\t\t\tres += c[i];\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MAX_N = 100_005;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto c = new int [MAX_N];\n\t\tc[] = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\tc[x]++;\n\t\t}\n\t\tint j = MAX_N;\n\t\tint res = 0;\n\t\tforeach_reverse (i; 0..MAX_N)\n\t\t{\n\t\t\tj = min (j, i >> 1);\n\t\t\twhile (c[i] > 0)\n\t\t\t{\n\t\t\t\twhile (j > 0 && c[j] == 0)\n\t\t\t\t{\n\t\t\t\t\tj--;\n\t\t\t\t}\n\t\t\t\tif (c[j] == 0)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tc[i]--;\n\t\t\t\tc[j]--;\n\t\t\t\tres++;\n\t\t\t}\n\t\t\tres += c[i];\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "#!/usr/bin/env rdmd\nimport std.stdio, std.string, std.conv;\nimport std.algorithm, std.array;\n\nvoid main()\n{\n for(string S; (S=readln().chomp()).length; )\n {\n auto n = S.to!int();\n auto s = new int[n];\n foreach(i;0..n)\n s[i] = readln().chomp().to!int();\n s.sort;\n auto u = new bool[n];\n auto c = n;\n int p = n;\n foreach_reverse(i;1..n)\n {\n auto v = s[i]/2;\n if(!u[i])\n {\n p=min(p,i-1);\n for( ; p>=0; --p)\n {\n if(v>=s[p])\n {\n u[p]=true;\n --c;\n --p;\n break;\n }\n }\n }\n\n }\n writeln(c);\n }\n}\n"}, {"source_code": "#!/usr/bin/env rdmd\nimport std.stdio, std.string, std.conv;\nimport std.algorithm, std.array;\n\nvoid main()\n{\n for(string S; (S=readln().chomp()).length; )\n {\n auto n = S.to!int();\n auto s = new int[n];\n foreach(i;0..n)\n s[i] = readln().chomp().to!int();\n s.sort;\n auto u = new bool[n];\n auto c = n;\n int p = n;\n foreach_reverse(i;1..n)\n {\n auto v = s[i];\n if(!u[i])\n {\n p=min(p,i-1);\n for( ; p>=0; --p)\n {\n if(v>s[p]*2)\n {\n u[p]=true;\n --c;\n --p;\n break;\n }\n }\n }\n\n }\n writeln(c);\n }\n}\n"}, {"source_code": "#!/usr/bin/env rdmd\nimport std.stdio, std.string, std.conv;\nimport std.algorithm, std.array;\n\nvoid main()\n{\n for(string S; (S=readln().chomp()).length; )\n {\n auto n = S.to!int();\n auto s = new int[n];\n foreach(i;0..n)\n s[i] = readln().chomp().to!int();\n s.sort;\n auto u = new bool[n];\n auto c = n;\n int p = n-2;\n foreach_reverse(i;1..n)\n {\n auto v = s[i]/2;\n if(!u[i])\n {\n for( ; p>=0; --p)\n {\n if(v>=s[p])\n {\n u[p]=true;\n --c;\n --p;\n break;\n }\n }\n }\n\n }\n writeln(c);\n }\n}\n"}], "src_uid": "361f65484d86051fa9ff013f5e8c9154"} {"nl": {"description": "The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).", "output_spec": "For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.", "sample_inputs": ["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"], "sample_outputs": ["1\n2\n25\n26\n27\n649\n650"], "notes": null}, "positive_code": [{"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n int[string] dict;\r\n int dictSize;\r\n foreach(dchar c; 'a'..'z' + 1) foreach(dchar d; 'a'..'z' + 1) {\r\n if (c == d) continue;\r\n\r\n dictSize++;\r\n dict[[c, d].to!string] = dictSize;\r\n }\r\n\r\n auto subSolve() {\r\n auto S = scan;\r\n return dict[S];\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve().writeln;\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}], "negative_code": [], "src_uid": "2e3006d663a3c7ad3781aba1e37be3ca"} {"nl": {"description": "You are given an array $$$a$$$ of $$$2n$$$ distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its $$$2$$$ neighbours.More formally, find an array $$$b$$$, such that: $$$b$$$ is a permutation of $$$a$$$.For every $$$i$$$ from $$$1$$$ to $$$2n$$$, $$$b_i \\neq \\frac{b_{i-1}+b_{i+1}}{2}$$$, where $$$b_0 = b_{2n}$$$ and $$$b_{2n+1} = b_1$$$. It can be proved that under the constraints of this problem, such array $$$b$$$ always exists.", "input_spec": "The first line of input contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 1000)$$$ — the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer $$$n$$$ $$$(1 \\leq n \\leq 25)$$$. The second line of each testcase contains $$$2n$$$ integers $$$a_1, a_2, \\ldots, a_{2n}$$$ $$$(1 \\leq a_i \\leq 10^9)$$$ — elements of the array. Note that there is no limit to the sum of $$$n$$$ over all testcases.", "output_spec": "For each testcase, you should output $$$2n$$$ integers, $$$b_1, b_2, \\ldots b_{2n}$$$, for which the conditions from the statement are satisfied.", "sample_inputs": ["3\n3\n1 2 3 4 5 6\n2\n123 456 789 10\n1\n6 9"], "sample_outputs": ["3 1 4 2 5 6\n123 10 456 789\n9 6"], "notes": "NoteIn the first testcase, array $$$[3, 1, 4, 2, 5, 6]$$$ works, as it's a permutation of $$$[1, 2, 3, 4, 5, 6]$$$, and $$$\\frac{3+4}{2}\\neq 1$$$, $$$\\frac{1+2}{2}\\neq 4$$$, $$$\\frac{4+5}{2}\\neq 2$$$, $$$\\frac{2+6}{2}\\neq 5$$$, $$$\\frac{5+3}{2}\\neq 6$$$, $$$\\frac{6+1}{2}\\neq 3$$$."}, "positive_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1526/problem/A\n// \nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n\nwhile(t--) {\n int n = readln.chomp.to!int;\n\n long[] a = readln.split.map!(to!long).array;\n a.sort;\n for(int i = 0; i < n; ++i) {\n writef(\"%s %s \", a[i], a[i + n]);\n } \"\".writeln;\n}\n}\n\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.range, std.stdio, std.string;\r\n\r\nvoid main() {\r\n foreach(t; 0 .. to!int(readln.strip)) {\r\n int n = to!int(readln.strip);\r\n auto ar = readln.split.map!(to!int).array.sort;\r\n auto r1 = ar[0 .. n], r2 = ar[n .. $];\r\n foreach(i; 0 .. n) {\r\n write(r1[i],\" \",r2[i],\" \");\r\n }\r\n writeln();\r\n }\r\n}\r\n "}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.array, std.random;\n\nbool check(long[] a)\n{\n for (int i = 0; i < a.length; i++) {\n int left = (cast(int)(a.length) + i - 1) % cast(int)(a.length);\n int right = (cast(int)(a.length) + i + 1) % cast(int)(a.length);\n if (a[left] + a[right] == 2 * a[i])\n return false;\n }\n return true;\n}\n\nvoid main()\n{\n int t;\n readf!\" %d \"(t);\n\n foreach (casenum ; 0 .. t) {\n int n;\n readf!\" %d \"(n);\n auto a = readln.chomp.split.map!(to!long).array;\n long[] b;\n while (true) {\n b = a.randomShuffle;\n if (check(b))\n break;\n }\n writeln(b.map!text.joiner(\" \"));\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.algorithm;\nimport core.stdc.stdio;\n\nint main()\n{\n int t = readInt!int;\n while (t--)\n {\n int n = readInt!int;\n auto a = new int[](2 * n);\n foreach(ref ai; a)\n ai = readInt!int;\n sort(a);\n debug writeln(a);\n auto low = a[0 .. n];\n auto high = a[n .. $];\n foreach(i; 0 .. n)\n {\n write(low[i], \" \", high[i], \" \");\n }\n writeln;\n }\n return 0;\n}\n\n\n\n\n\n\n/* INPUT ROUTINES */\nint currChar;\nstatic this()\n{\n currChar = getchar();\n}\nchar topChar()\n{\n return cast(char) currChar;\n}\nvoid popChar()\n{\n currChar = getchar();\n}\nauto readInt(T)()\n{\n T num = 0;\n int sgn = 0;\n while (topChar.isWhite) popChar;\n while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }\n while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }\n if (sgn) return -num; return num;\n}\nstring readString()\n{\n string res = \"\";\n while (topChar.isWhite) popChar;\n while (!topChar.isWhite) { res ~= topChar; popChar; }\n return res;\n}\n"}], "negative_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1526/problem/A\n// \nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n\nwhile(t--) {\n int n = readln.chomp.to!int;\n\n long[] a = readln.split.map!(to!long).array;\n for(int i = 0; i < n; ++i) {\n writef(\"%s %s \", a[i], a[i + n]);\n } \"\".writeln;\n}\n}\n\n"}], "src_uid": "4296da660a39a6e98b41387929701c0a"} {"nl": {"description": "You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are $$$r$$$ candies in it, the second pile contains only green candies and there are $$$g$$$ candies in it, the third pile contains only blue candies and there are $$$b$$$ candies in it. Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day.Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.", "input_spec": "The first line contains integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a separate line of the input. It contains three integers $$$r$$$, $$$g$$$ and $$$b$$$ ($$$1 \\le r, g, b \\le 10^8$$$) — the number of red, green and blue candies, respectively.", "output_spec": "Print $$$t$$$ integers: the $$$i$$$-th printed integer is the answer on the $$$i$$$-th test case in the input.", "sample_inputs": ["6\n1 1 1\n1 2 1\n4 1 1\n7 4 10\n8 1 4\n8 2 8"], "sample_outputs": ["1\n2\n2\n10\n5\n9"], "notes": "NoteIn the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n {\n\tstatic foreach(n; 0 .. T[i].Types.length)\n\t {\n\t read(args[i][n]);\n\t }\n }\n else\n readf!\" %s \"(args[i]);\n}\nauto brange(alias low, alias high, alias pred, bool value)()\n{\n return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);\n}\nauto ftrue(alias low, alias high, alias pred)()\n{\n return brange!(low, high, pred, false).length + low;\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t;\n get(t);\n F(t);\n }\n}\nalias get = read;\nalias w = writeln;\n\nint main()\n{\n int t;\n get(t);\n while(t--)\n {\n long[] c = new long[3];\n get(c);\n sort(c);\n long res = 0;\n long d = c[1] - c[0];\n res += d;\n c[1] -= d;\n c[2] -= d;\n auto a = c[0];\n auto b = c[2];\n d = min(a, b - a);\n a -= d;\n b -= 2 * d;\n res += 2 * d;\n res += 3 * (a / 2) + (a % 2);\n w(res);\n }\n return 0;\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint tests;\n\treadf !(\" %s\") (tests);\n\treadln;\n\tforeach (test; 0..tests)\n\t{\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tsort (a);\n\t\twriteln (min (a[0] + a[1], sum (a) / 2));\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint t = rint;\n\tforeach(_; 0 .. t){\n\t\tlong a = rlong, b = rlong, c = rlong;\n\t\tlong ans = min((a + b + c) / 2, a + b, b + c, c + a);\n\t\tans.writeln;\n\t}\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (i; 0..t)\n\t{\n\t\tauto r = RD;\n\t\tauto g = RD;\n\t\tauto b = RD;\n\t\tauto arr = [r, g, b];\n\t\tarr.sort();\n\t\tauto d = min(arr[2] - arr[1], arr[0]);\n\t\tans[i] += d;\n\t\tarr[2] -= d;\n\t\tarr[0] -= d;\n\t\tans[i] += arr[0] - arr[0] % 2;\n\t\tans[i] += arr[1] - arr[0] / 2;\n\t}\n\t\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "1f29461c42665523d0a4d56b13f7e480"} {"nl": {"description": "It is given a positive integer $$$n$$$. In $$$1$$$ move, one can select any single digit and remove it (i.e. one selects some position in the number and removes the digit located at this position). The operation cannot be performed if only one digit remains. If the resulting number contains leading zeroes, they are automatically removed.E.g. if one removes from the number $$$32925$$$ the $$$3$$$-rd digit, the resulting number will be $$$3225$$$. If one removes from the number $$$20099050$$$ the first digit, the resulting number will be $$$99050$$$ (the $$$2$$$ zeroes going next to the first digit are automatically removed).What is the minimum number of steps to get a number such that it is divisible by $$$25$$$ and positive? It is guaranteed that, for each $$$n$$$ occurring in the input, the answer exists. It is guaranteed that the number $$$n$$$ has no leading zeros.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$n$$$ ($$$25 \\le n \\le 10^{18}$$$). It is guaranteed that, for each $$$n$$$ occurring in the input, the answer exists. It is guaranteed that the number $$$n$$$ has no leading zeros.", "output_spec": "For each test case output on a separate line an integer $$$k$$$ ($$$k \\ge 0$$$) — the minimum number of steps to get a number such that it is divisible by $$$25$$$ and positive.", "sample_inputs": ["5\n100\n71345\n3259\n50555\n2050047"], "sample_outputs": ["0\n3\n1\n3\n2"], "notes": "NoteIn the first test case, it is already given a number divisible by $$$25$$$.In the second test case, we can remove the digits $$$1$$$, $$$3$$$, and $$$4$$$ to get the number $$$75$$$.In the third test case, it's enough to remove the last digit to get the number $$$325$$$.In the fourth test case, we can remove the three last digits to get the number $$$50$$$.In the fifth test case, it's enough to remove the digits $$$4$$$ and $$$7$$$."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.array, std.string, std.conv;\r\n \r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n foreach(_; 0..t)\r\n {\r\n auto str = readln.strip;\r\n int zero_cnt, five_cnt;\r\n bool zero_f = false, five_f = false;\r\n for(int i = cast (int) str.length - 1; i > 0; i--)\r\n {\r\n if (str[i] == '0')\r\n {\r\n int j = i - 1;\r\n while((str[j] != '0') && (str[j] != '5') && (!zero_f))\r\n {\r\n zero_cnt++;\r\n j--;\r\n if (j == -1)\r\n break;\r\n }\r\n if (j >= 0)\r\n zero_f = true;\r\n if (!five_f)\r\n five_cnt++;\r\n }\r\n else if (str[i] == '5')\r\n {\r\n int j = i - 1;\r\n while((str[j] != '2') && (str[j] != '7') && (!five_f))\r\n {\r\n five_cnt++;\r\n j--;\r\n if (j == -1)\r\n break;\r\n \r\n }\r\n if (j >= 0)\r\n five_f = true;\r\n if (!zero_f)\r\n zero_cnt++;\r\n }\r\n else\r\n {\r\n if (!zero_f)\r\n zero_cnt++;\r\n if (!five_f)\r\n five_cnt++;\r\n }\r\n }\r\n writeln(min(zero_cnt, five_cnt));\r\n }\r\n}"}], "negative_code": [], "src_uid": "ab6fefc6c15d4647bc601f1f9db21d3f"} {"nl": {"description": "After too much playing on paper, Iahub has switched to computer games. The game he plays is called \"Block Towers\". It is played in a rectangular grid with n rows and m columns (it contains n × m cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly one tower of two following types: Blue towers. Each has population limit equal to 100. Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue Tower. Two cells are neighbours is they share a side. Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if needed, see the second example for such a case).Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population limit is as large as possible.He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks. ", "input_spec": "The first line of the input contains two integers n and m (1 ≤ n, m ≤ 500). Each of the next n lines contains m characters, describing the grid. The j-th character in the i-th line is '.' if you're allowed to build at the cell with coordinates (i, j) a tower (empty cell) or '#' if there is a big hole there. ", "output_spec": "Print an integer k in the first line (0 ≤ k ≤ 106) — the number of operations Iahub should perform to obtain optimal result. Each of the following k lines must contain a single operation in the following format: «B x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a blue tower at the cell (x, y); «R x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a red tower at the cell (x, y); «D x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — destroying a tower at the cell (x, y). If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.", "sample_inputs": ["2 3\n..#\n.#.", "1 3\n..."], "sample_outputs": ["4\nB 1 1\nR 1 2\nR 2 1\nB 2 3", "5\nB 1 1\nB 1 2\nR 1 3\nD 1 2\nR 1 2"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport core.memory;\n\nstruct StaticQueue(T, size_t maxSize)\n{\n T[maxSize] data;\n size_t _front = 0;\n size_t _pastBack = 0;\n void clear()\n {\n _front = _pastBack;\n }\n bool empty()\n {\n return _front == _pastBack;\n }\n void removeFront()\n {\n _front++;\n if (_front == maxSize)\n _front = 0;\n }\n auto front()\n {\n return data[_front];\n }\n void insertBack(T t)\n {\n data[_pastBack] = t;\n _pastBack++;\n if (_pastBack == maxSize)\n _pastBack = 0;\n }\n}\n\nvoid main(string[] args)\n{\n GC.disable;\n inputFile = stdin;\n debug inputFile = File(args[1]);\n enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),\n tuple(int(0), int(-1)), tuple(int(0), int(+1))];\n auto n = next!int;\n auto m = next!int;\n auto cell = new string[](n);\n foreach(ref row; cell)\n row = next!string;\n auto visited = new bool[][](n, m);\n struct Op\n {\n string type;\n Tuple!(int, int) ind;\n }\n auto ops = new Op[](0);\n auto queue = StaticQueue!(Tuple!(int, int), 500 * 500)();\n foreach(i; 0 .. n)\n {\n foreach(j; 0 .. m)\n {\n if (!visited[i][j] && cell[i][j] == '.')\n {\n auto order = new Tuple!(int, int)[](0);\n queue.clear;\n queue.insertBack(tuple(i, j));\n visited[i][j] = true;\n while(!queue.empty)\n {\n auto f = queue.front;\n queue.removeFront;\n order ~= f;\n foreach(delta; deltas)\n {\n auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);\n if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')\n {\n queue.insertBack(nei);\n visited[nei[0]][nei[1]] = true;\n }\n }\n }\n foreach(node; order)\n {\n ops ~= Op(\"B\", node);\n }\n foreach_reverse(node; order[1 .. $])\n {\n ops ~= Op(\"D\", node);\n ops ~= Op(\"R\", node);\n }\n }\n }\n }\n ops.length.writeln;\n foreach(op; ops)\n {\n writeln(op.type, \" \", op.ind[0] + 1, \" \", op.ind[1] + 1);\n }\n}\n// INPUT\n\nenum InputStyle\n {\n byChunk, byLine\n };\nenum inputStyle = InputStyle.byLine;\n\nFile inputFile;\nstring popWord();\nstatic if (inputStyle == InputStyle.byChunk)\n {\n const chunkSize = 1024 * 1024 * 8;\n const wordSize = 4096;\n char[chunkSize] chunkBuff;\n char[] currChunk;\n void renewChunk()\n {\n if (inputFile.eof)\n currChunk = null;\n else\n currChunk = inputFile.rawRead(chunkBuff);\n }\n char[wordSize] wordBuff;\n char[] word;\n string popWord()\n {\n if (currChunk.length == 0)\n renewChunk;\n assert(currChunk.length > 0);\n while (currChunk[0].isWhite)\n {\n\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n renewChunk;\n }\n word = wordBuff[0 .. 0];\n while (!currChunk[0].isWhite)\n {\n word.length++;\n word[$ - 1] = currChunk[0];\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n {\n renewChunk;\n if (currChunk.length == 0)\n return cast(string) word;\n }\n }\n return cast(string) word;\n }\n }\n else\n {\n DList!string _words;\n string popWord()\n {\n while (_words.empty)\n {\n foreach(w; inputFile.readln.split)\n {\n _words.insertBack(w);\n }\n }\n auto word = _words.front;\n _words.removeFront;\n return word;\n }\n }\nT next(T)()\n{\n return to!T(popWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport core.memory;\n\nvoid main(string[] args)\n{\n GC.disable;\n inputFile = stdin;\n debug inputFile = File(args[1]);\n enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),\n tuple(int(0), int(-1)), tuple(int(0), int(+1))];\n auto n = next!int;\n auto m = next!int;\n auto cell = new string[](n);\n foreach(ref row; cell)\n row = next!string;\n auto visited = new bool[][](n, m);\n struct Op\n {\n string type;\n Tuple!(int, int) ind;\n }\n auto ops = new Op[](0);\n foreach(i; 0 .. n)\n {\n foreach(j; 0 .. m)\n {\n if (!visited[i][j] && cell[i][j] == '.')\n {\n auto order = new Tuple!(int, int)[](0);\n auto queue = DList!(Tuple!(int, int))(tuple!(int, int)(i, j));\n visited[i][j] = true;\n while(!queue.empty)\n {\n auto f = queue.front;\n queue.removeFront;\n order ~= f;\n foreach(delta; deltas)\n {\n auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);\n if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')\n {\n queue.insertBack(nei);\n visited[nei[0]][nei[1]] = true;\n }\n }\n }\n foreach(node; order)\n {\n ops ~= Op(\"B\", node);\n }\n foreach_reverse(node; order[1 .. $])\n {\n ops ~= Op(\"D\", node);\n ops ~= Op(\"R\", node);\n }\n }\n }\n }\n ops.length.writeln;\n foreach(op; ops)\n {\n writeln(op.type, \" \", op.ind[0] + 1, \" \", op.ind[1] + 1);\n }\n}\n// INPUT\n\nenum InputStyle\n {\n byChunk, byLine\n };\nenum inputStyle = InputStyle.byLine;\n\nFile inputFile;\nstring popWord();\nstatic if (inputStyle == InputStyle.byChunk)\n {\n const chunkSize = 1024 * 1024 * 8;\n const wordSize = 4096;\n char[chunkSize] chunkBuff;\n char[] currChunk;\n void renewChunk()\n {\n if (inputFile.eof)\n currChunk = null;\n else\n currChunk = inputFile.rawRead(chunkBuff);\n }\n char[wordSize] wordBuff;\n char[] word;\n string popWord()\n {\n if (currChunk.length == 0)\n renewChunk;\n assert(currChunk.length > 0);\n while (currChunk[0].isWhite)\n {\n\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n renewChunk;\n }\n word = wordBuff[0 .. 0];\n while (!currChunk[0].isWhite)\n {\n word.length++;\n word[$ - 1] = currChunk[0];\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n {\n renewChunk;\n if (currChunk.length == 0)\n return cast(string) word;\n }\n }\n return cast(string) word;\n }\n }\n else\n {\n DList!string _words;\n string popWord()\n {\n while (_words.empty)\n {\n foreach(w; inputFile.readln.split)\n {\n _words.insertBack(w);\n }\n }\n auto word = _words.front;\n _words.removeFront;\n return word;\n }\n }\nT next(T)()\n{\n return to!T(popWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\n\nvoid main(string[] args)\n{\n inputFile = stdin;\n debug inputFile = File(args[1]);\n enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),\n tuple(int(0), int(-1)), tuple(int(0), int(+1))];\n auto n = next!int;\n auto m = next!int;\n auto cell = new string[](n);\n foreach(ref row; cell)\n row = next!string;\n auto visited = new bool[][](n, m);\n struct Op\n {\n string type;\n Tuple!(int, int) ind;\n }\n auto ops = new Op[](0);\n foreach(i; 0 .. n)\n {\n foreach(j; 0 .. m)\n {\n if (!visited[i][j] && cell[i][j] == '.')\n {\n auto order = new Tuple!(int, int)[](0);\n auto queue = DList!(Tuple!(int, int))(tuple!(int, int)(i, j));\n visited[i][j] = true;\n while(!queue.empty)\n {\n auto f = queue.front;\n queue.removeFront;\n order ~= f;\n foreach(delta; deltas)\n {\n auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);\n if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')\n {\n queue.insertBack(nei);\n visited[nei[0]][nei[1]] = true;\n }\n }\n }\n foreach(node; order)\n {\n ops ~= Op(\"B\", node);\n }\n foreach_reverse(node; order[1 .. $])\n {\n ops ~= Op(\"D\", node);\n ops ~= Op(\"R\", node);\n }\n }\n }\n }\n ops.length.writeln;\n foreach(op; ops)\n {\n writeln(op.type, \" \", op.ind[0] + 1, \" \", op.ind[1] + 1);\n }\n}\n// INPUT\n\nenum InputStyle\n {\n byChunk, byLine\n };\nenum inputStyle = InputStyle.byLine;\n\nFile inputFile;\nstring popWord();\nstatic if (inputStyle == InputStyle.byChunk)\n {\n const chunkSize = 1024 * 1024 * 8;\n const wordSize = 4096;\n char[chunkSize] chunkBuff;\n char[] currChunk;\n void renewChunk()\n {\n if (inputFile.eof)\n currChunk = null;\n else\n currChunk = inputFile.rawRead(chunkBuff);\n }\n char[wordSize] wordBuff;\n char[] word;\n string popWord()\n {\n if (currChunk.length == 0)\n renewChunk;\n assert(currChunk.length > 0);\n while (currChunk[0].isWhite)\n {\n\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n renewChunk;\n }\n word = wordBuff[0 .. 0];\n while (!currChunk[0].isWhite)\n {\n word.length++;\n word[$ - 1] = currChunk[0];\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n {\n renewChunk;\n if (currChunk.length == 0)\n return cast(string) word;\n }\n }\n return cast(string) word;\n }\n }\n else\n {\n DList!string _words;\n string popWord()\n {\n while (_words.empty)\n {\n foreach(w; inputFile.readln.split)\n {\n _words.insertBack(w);\n }\n }\n auto word = _words.front;\n _words.removeFront;\n return word;\n }\n }\nT next(T)()\n{\n return to!T(popWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\nimport core.memory;\n\nstruct StaticQueue(T, size_t maxSize)\n{\n T[maxSize] data;\n size_t _front = 0;\n size_t _pastBack = 0;\n size_t len = 0;\n auto length()\n {\n return len;\n }\n void clear()\n {\n _front = _pastBack;\n len = 0;\n }\n bool empty()\n {\n return _front == _pastBack;\n }\n void removeFront()\n {\n _front++;\n if (_front == maxSize)\n _front = 0;\n len--;\n }\n auto front()\n {\n return data[_front];\n }\n void insertBack(T t)\n {\n data[_pastBack] = t;\n _pastBack++;\n if (_pastBack == maxSize)\n _pastBack = 0;\n len++;\n }\n}\n\nvoid main(string[] args)\n{\n GC.disable;\n inputFile = stdin;\n debug inputFile = File(args[1]);\n enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),\n tuple(int(0), int(-1)), tuple(int(0), int(+1))];\n auto n = next!int;\n auto m = next!int;\n auto cell = new string[](n);\n foreach(ref row; cell)\n row = next!string;\n auto visited = new bool[][](n, m);\n struct Op\n {\n string type;\n Tuple!(int, int) ind;\n }\n auto ops = StaticQueue!(Op, 500 * 500 * 3)();\n auto queue = StaticQueue!(Tuple!(int, int), 500 * 500)();\n foreach(i; 0 .. n)\n {\n foreach(j; 0 .. m)\n {\n if (!visited[i][j] && cell[i][j] == '.')\n {\n auto order = new Tuple!(int, int)[](0);\n queue.clear;\n queue.insertBack(tuple(i, j));\n visited[i][j] = true;\n while(!queue.empty)\n {\n auto f = queue.front;\n queue.removeFront;\n order ~= f;\n foreach(delta; deltas)\n {\n auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);\n if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')\n {\n queue.insertBack(nei);\n visited[nei[0]][nei[1]] = true;\n }\n }\n }\n foreach(node; order)\n {\n ops.insertBack(Op(\"B\", node));\n }\n foreach_reverse(node; order[1 .. $])\n {\n ops.insertBack(Op(\"D\", node));\n ops.insertBack(Op(\"R\", node));\n }\n }\n }\n }\n ops.length.writeln;\n while(!ops.empty)\n {\n auto op = ops.front;\n ops.removeFront;\n writeln(op.type, \" \", op.ind[0] + 1, \" \", op.ind[1] + 1);\n }\n}\n// INPUT\n\nenum InputStyle\n {\n byChunk, byLine\n };\nenum inputStyle = InputStyle.byLine;\n\nFile inputFile;\nstring popWord();\nstatic if (inputStyle == InputStyle.byChunk)\n {\n const chunkSize = 1024 * 1024 * 8;\n const wordSize = 4096;\n char[chunkSize] chunkBuff;\n char[] currChunk;\n void renewChunk()\n {\n if (inputFile.eof)\n currChunk = null;\n else\n currChunk = inputFile.rawRead(chunkBuff);\n }\n char[wordSize] wordBuff;\n char[] word;\n string popWord()\n {\n if (currChunk.length == 0)\n renewChunk;\n assert(currChunk.length > 0);\n while (currChunk[0].isWhite)\n {\n\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n renewChunk;\n }\n word = wordBuff[0 .. 0];\n while (!currChunk[0].isWhite)\n {\n word.length++;\n word[$ - 1] = currChunk[0];\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n {\n renewChunk;\n if (currChunk.length == 0)\n return cast(string) word;\n }\n }\n return cast(string) word;\n }\n }\n else\n {\n DList!string _words;\n string popWord()\n {\n while (_words.empty)\n {\n foreach(w; inputFile.readln.split)\n {\n _words.insertBack(w);\n }\n }\n auto word = _words.front;\n _words.removeFront;\n return word;\n }\n }\nT next(T)()\n{\n return to!T(popWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.uni;\nimport std.conv;\nimport std.container;\nimport std.functional;\nimport std.algorithm;\nimport std.array;\nimport std.typecons;\n\nvoid main(string[] args)\n{\n inputFile = stdin;\n debug inputFile = File(args[1]);\n enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),\n tuple(int(0), int(-1)), tuple(int(0), int(+1))];\n auto n = next!int;\n auto m = next!int;\n auto cell = new string[](n);\n foreach(ref row; cell)\n row = next!string;\n auto visited = new bool[][](n, m);\n foreach(i; 0 .. n)\n {\n foreach(j; 0 .. m)\n {\n if (!visited[i][j] && cell[i][j] == '.')\n {\n auto order = new Tuple!(int, int)[](0);\n auto queue = DList!(Tuple!(int, int))(tuple!(int, int)(i, j));\n visited[i][j] = true;\n while(!queue.empty)\n {\n auto f = queue.front;\n queue.removeFront;\n order ~= f;\n foreach(delta; deltas)\n {\n auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);\n if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')\n {\n queue.insertBack(nei);\n visited[nei[0]][nei[1]] = true;\n }\n }\n }\n foreach(node; order)\n {\n writeln(\"B \", node[0] + 1, \" \", node[1] + 1);\n }\n foreach_reverse(node; order[1 .. $])\n {\n writeln(\"D \", node[0] + 1, \" \", node[1] + 1);\n writeln(\"R \", node[0] + 1, \" \", node[1] + 1);\n }\n }\n }\n }\n}\n\n// INPUT\n\nenum InputStyle\n {\n byChunk, byLine\n };\nenum inputStyle = InputStyle.byLine;\n\nFile inputFile;\nstring popWord();\nstatic if (inputStyle == InputStyle.byChunk)\n {\n const chunkSize = 1024 * 1024 * 8;\n const wordSize = 4096;\n char[chunkSize] chunkBuff;\n char[] currChunk;\n void renewChunk()\n {\n if (inputFile.eof)\n currChunk = null;\n else\n currChunk = inputFile.rawRead(chunkBuff);\n }\n char[wordSize] wordBuff;\n char[] word;\n string popWord()\n {\n if (currChunk.length == 0)\n renewChunk;\n assert(currChunk.length > 0);\n while (currChunk[0].isWhite)\n {\n\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n renewChunk;\n }\n word = wordBuff[0 .. 0];\n while (!currChunk[0].isWhite)\n {\n word.length++;\n word[$ - 1] = currChunk[0];\n currChunk = currChunk[1 .. $];\n if (currChunk.length == 0)\n {\n renewChunk;\n if (currChunk.length == 0)\n return cast(string) word;\n }\n }\n return cast(string) word;\n }\n }\n else\n {\n DList!string _words;\n string popWord()\n {\n while (_words.empty)\n {\n foreach(w; inputFile.readln.split)\n {\n _words.insertBack(w);\n }\n }\n auto word = _words.front;\n _words.removeFront;\n return word;\n }\n }\nT next(T)()\n{\n return to!T(popWord);\n}\nauto next(T, S...)(S s)\n{\n static if (S.length == 0)\n {\n return to!T(popWord);\n }\n else\n {\n auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);\n foreach(ref elem; res)\n elem = next!T(s[1 .. $]);\n return res;\n }\n}\n"}], "src_uid": "9b9f6d95aecc1b6c95e5d9acca4f5453"} {"nl": {"description": "This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if all versions of the problem are solved.A forest is an undirected graph without cycles (not necessarily connected).Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from $$$1$$$ to $$$n$$$, and they would like to add edges to their forests such that: After adding edges, both of their graphs are still forests. They add the same edges. That is, if an edge $$$(u, v)$$$ is added to Mocha's forest, then an edge $$$(u, v)$$$ is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.", "input_spec": "The first line contains three integers $$$n$$$, $$$m_1$$$ and $$$m_2$$$ ($$$1 \\le n \\le 1000$$$, $$$0 \\le m_1, m_2 < n$$$) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next $$$m_1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) — the edges in Mocha's forest. Each of the next $$$m_2$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) — the edges in Diana's forest.", "output_spec": "The first line contains only one integer $$$h$$$, the maximum number of edges Mocha and Diana can add (in each forest). Each of the next $$$h$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) — the edge you add each time. If there are multiple correct answers, you can print any one of them.", "sample_inputs": ["3 2 2\n1 2\n2 3\n1 2\n1 3", "5 3 2\n5 4\n2 1\n4 3\n4 3\n1 4", "8 1 2\n1 7\n2 6\n1 5"], "sample_outputs": ["0", "1\n2 4", "5\n5 2\n2 3\n3 4\n4 7\n6 8"], "notes": "NoteIn the first example, we cannot add any edge.In the second example, the initial forests are as follows.We can add an edge $$$(2, 4)$$$."}, "positive_code": [{"source_code": "\nimmutable multi = false;\n\nstruct US\n{\n\tint[] parent;\n\tint[] sz; \n\tthis(int n)\n\t{\n\t\tparent = new int[](n);\n\t\tforeach(i; 0 .. n) parent[i] = i;\n\t\tsz = new int[](n);\n\t\tsz[] = 1;\n\t}\n\tvoid unite(int u, int v)\n\t{\n\t\tdebug writeln(\"uniting \", u, \" \", v);\n\t\tu = rep(u);\n\t\tv = rep(v);\n\t\tif (sz[u] > sz[v]) swap(u, v);\n\t\tparent[u] = v;\n\t\tsz[v] += sz[u];\n\t}\n\tint rep(int v)\n\t{\n\t\tdebug writeln(\"getting rep for \", v);\n\t\tif (parent[v] == v) return v;\n\t\treturn parent[v] = rep(parent[v]);\n\t}\n}\n\nvoid solve(int tc)\n{\n\tint n = readInt!int;\n\tauto u1 = US(n);\n\tauto u2 = US(n);\n\tauto m1 = readInt!int;\n\tauto m2 = readInt!int;\n\tforeach(i; 0 .. m1)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tdebug writeln(u, \" -> \", v);\n\t\tu1.unite(u, v);\n\t}\n\tforeach(i; 0 .. m2)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tu2.unite(u, v);\n\t}\n\tauto edges = new int[2][](0);\n\tforeach(i; 0 .. n)\n\t{\n\t\tforeach(j; 0 .. n)\n\t\t{\n\t\t\tif (u1.rep(i) != u1.rep(j) && u2.rep(i) != u2.rep(j))\n\t\t\t{\n\t\t\t\tu1.unite(i, j);\n\t\t\t\tu2.unite(i, j);\n\t\t\t\tedges ~= [i+1, j+1];\n\t\t\t}\n\t\t}\n\t}\n\tedges.length.writeln;\n\tforeach(e; edges) writeln(e[0], \" \", e[1]);\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nstruct UnionFind(T)\r\n{\r\n\tvoid init(T n) { par = new T[](n); foreach (i; 0..n) par[i] = i; cnt = new T[](n); fill(cnt, 1); }\r\n\tT root(T i) { return par[i] == i ? i : (par[i] = root(par[i])); }\r\n\tbool same(T i, T j) { return root(i) == root(j); }\r\n\tvoid unite(T i, T j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }\r\n\tT size(T i) { return cnt[root(i)]; }\r\n\tT[] par, cnt;\r\n}\r\n\r\nvoid main()\r\n{\r\n\tauto n = RD!int;\r\n\tauto m1 = RD!int;\r\n\tauto m2 = RD!int;\r\n\t\t\r\n\tUnionFind!int uf1, uf2;\r\n\tuf1.init(n);\r\n\tuf2.init(n);\r\n\tforeach (i; 0..m1)\r\n\t{\r\n\t\tauto u = RD!int-1;\r\n\t\tauto v = RD!int-1;\r\n\t\tuf1.unite(u, v);\r\n\t}\r\n\tforeach (i; 0..m2)\r\n\t{\r\n\t\tauto u = RD!int-1;\r\n\t\tauto v = RD!int-1;\r\n\t\tuf2.unite(u, v);\r\n\t}\r\n\t\r\n\tint[][] ans;\r\n\tforeach (i; 0..n)\r\n\t{\r\n\t\tforeach (j; i+1..n)\r\n\t\t{\r\n\t\t\tif (uf1.same(i, j) || uf2.same(i, j)) continue;\r\n\t\t\tuf1.unite(i, j);\r\n\t\t\tuf2.unite(i, j);\r\n\t\t\tans ~= [i+1, j+1];\r\n\t\t}\r\n\t}\r\n\r\n\r\n\twriteln(ans.length);\r\n\tforeach (e; ans)\r\n\t{\r\n\t\twriteln(e[0], \" \", e[1]);\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "e3d2b67a62ac431718071ae20f3794aa"} {"nl": {"description": "Let $$$F_k$$$ denote the $$$k$$$-th term of Fibonacci sequence, defined as below: $$$F_0 = F_1 = 1$$$ for any integer $$$n \\geq 0$$$, $$$F_{n+2} = F_{n+1} + F_n$$$You are given a tree with $$$n$$$ vertices. Recall that a tree is a connected undirected graph without cycles.We call a tree a Fib-tree, if its number of vertices equals $$$F_k$$$ for some $$$k$$$, and at least one of the following conditions holds: The tree consists of only $$$1$$$ vertex; You can divide it into two Fib-trees by removing some edge of the tree. Determine whether the given tree is a Fib-tree or not.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of vertices in the tree. Then $$$n-1$$$ lines follow, each of which contains two integers $$$u$$$ and $$$v$$$ ($$$1\\leq u,v \\leq n$$$, $$$u \\neq v$$$), representing an edge between vertices $$$u$$$ and $$$v$$$. It's guaranteed that given edges form a tree.", "output_spec": "Print \"YES\" if the given tree is a Fib-tree, or \"NO\" otherwise. You can print your answer in any case. For example, if the answer is \"YES\", then the output \"Yes\" or \"yeS\" will also be considered as correct answer.", "sample_inputs": ["3\n1 2\n2 3", "5\n1 2\n1 3\n1 4\n1 5", "5\n1 3\n1 2\n4 5\n3 4"], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteIn the first sample, we can cut the edge $$$(1, 2)$$$, and the tree will be split into $$$2$$$ trees of sizes $$$1$$$ and $$$2$$$ correspondently. Any tree of size $$$2$$$ is a Fib-tree, as it can be split into $$$2$$$ trees of size $$$1$$$.In the second sample, no matter what edge we cut, the tree will be split into $$$2$$$ trees of sizes $$$1$$$ and $$$4$$$. As $$$4$$$ isn't $$$F_k$$$ for any $$$k$$$, it's not Fib-tree.In the third sample, here is one possible order of cutting the edges so that all the trees in the process are Fib-trees: $$$(1, 3), (1, 2), (4, 5), (3, 4)$$$. "}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nint root(int[] uf, int u) {\r\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\r\n}\r\nbool connect(int[] uf, int u, int v) {\r\n u = uf.root(u);\r\n v = uf.root(v);\r\n if (u == v) return false;\r\n if (uf[u] > uf[v]) swap(u, v);\r\n uf[u] += uf[v];\r\n uf[v] = u;\r\n return true;\r\n}\r\n\r\n\r\nint[] fib;\r\n\r\nint N;\r\nint[] A, B;\r\n\r\nint[][] G;\r\nbool[] del;\r\nint[] sz, par, pari;\r\nint[] us;\r\n\r\nvoid dfs(int u, int p, int pi) {\r\n us ~= u;\r\n par[u] = p;\r\n pari[u] = pi;\r\n sz[u] = 1;\r\n foreach (i; G[u]) {\r\n if (!del[i]) {\r\n if (i != pi) {\r\n const v = A[i] ^ B[i] ^ u;\r\n dfs(v, u, i);\r\n sz[u] += sz[v];\r\n }\r\n }\r\n }\r\n}\r\n\r\nbool solve(int k, int rt) {\r\n debug {\r\n // writeln(\"solve \", k, \" \", rt);\r\n }\r\n if (k <= 2) {\r\n return true;\r\n }\r\n us = [];\r\n dfs(rt, -1, -1);\r\n /*\r\n debug {\r\n writeln(\"solve \", k, \" \", rt);\r\n writeln(\" del = \", del);\r\n writeln(\" us = \", us);\r\n writeln(\" sz = \", sz);\r\n writeln(\" par = \", par);\r\n writeln(\" pari = \", pari);\r\n }\r\n //*/\r\n assert(sz[rt] == fib[k]);\r\n int[] vs, ps;\r\n foreach (u; us) {\r\n if (sz[u] == fib[k - 2] || fib[k] - sz[u] == fib[k - 2]) {\r\n vs ~= u;\r\n ps ~= par[u];\r\n }\r\n }\r\n foreach (v; vs) {\r\n del[pari[v]] = true;\r\n }\r\n if (vs.length == 0) {\r\n return false;\r\n } else if (vs.length == 1) {\r\n bool ret = true;\r\n if (sz[vs[0]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 1, ps[0]);\r\n } else {\r\n ret = ret && solve(k - 1, vs[0]);\r\n ret = ret && solve(k - 2, ps[0]);\r\n }\r\n return ret;\r\n } else if (vs.length == 2) {\r\n bool ret = true;\r\n if (sz[vs[0]] == fib[k - 2]) {\r\n if (sz[vs[1]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 2, vs[1]);\r\n ret = ret && solve(k - 3, ps[0]);\r\n } else {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 2, ps[1]);\r\n ret = ret && solve(k - 3, ps[0]);\r\n }\r\n } else {\r\n if (sz[vs[1]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[1]);\r\n ret = ret && solve(k - 2, ps[0]);\r\n ret = ret && solve(k - 3, ps[1]);\r\n } else {\r\n assert(false);\r\n }\r\n }\r\n return ret;\r\n } else {\r\n assert(false);\r\n }\r\n}\r\n\r\nvoid main() {\r\n fib = new int[40];\r\n fib[0] = 1;\r\n fib[1] = 1;\r\n foreach (k; 2 .. fib.length) {\r\n fib[k] = fib[k - 1] + fib[k - 2];\r\n }\r\n debug {\r\n writeln(\"fib = \", fib);\r\n }\r\n \r\n try {\r\n for (; ; ) {\r\n N = readInt();\r\n A = new int[N - 1];\r\n B = new int[N - 1];\r\n foreach (i; 0 .. N - 1) {\r\n A[i] = readInt() - 1;\r\n B[i] = readInt() - 1;\r\n }\r\n debug {\r\n writeln(\"N = \", N);\r\n writeln(\"A = \", A);\r\n writeln(\"B = \", B);\r\n }\r\n \r\n G = new int[][N];\r\n foreach (i; 0 .. N - 1) {\r\n G[A[i]] ~= i;\r\n G[B[i]] ~= i;\r\n }\r\n del = new bool[N - 1];\r\n sz = new int[N];\r\n par = new int[N];\r\n pari = new int[N];\r\n \r\n bool ans = false;\r\n const k = fib.lowerBound(N);\r\n if (fib[k] == N) {\r\n ans = solve(k, 0);\r\n }\r\n writeln(ans ? \"YES\" : \"NO\");\r\n \r\n debug {\r\n auto brt = new bool[1 << N];\r\n foreach (p; 1 .. 1 << N) {\r\n if (!(p & p - 1)) {\r\n brt[p] = true;\r\n } else {\r\n auto uf = new int[N];\r\n uf[] = -1;\r\n foreach (i; 0 .. N - 1) {\r\n if ((p & 1 << A[i]) && (p & 1 << B[i])) {\r\n uf.connect(A[i], B[i]);\r\n }\r\n }\r\n int numComps;\r\n foreach (u; 0 .. N) {\r\n if (p & 1 << u) {\r\n if (uf[u] < 0) {\r\n ++numComps;\r\n }\r\n }\r\n }\r\n if (fib.count(popcnt(p)) > 0 && numComps == 1) {\r\n for (int q = p; --q &= p; ) {\r\n if (brt[q] && brt[p ^ q]) {\r\n brt[p] = true;\r\n break;\r\n }\r\n }\r\n }\r\n }\r\n }\r\n assert(brt[$ - 1] == ans, format(\"%s %s %s: %s %s\", N, A, B, brt[$ - 1], ans));\r\n }\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nint root(int[] uf, int u) {\r\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\r\n}\r\nbool connect(int[] uf, int u, int v) {\r\n u = uf.root(u);\r\n v = uf.root(v);\r\n if (u == v) return false;\r\n if (uf[u] > uf[v]) swap(u, v);\r\n uf[u] += uf[v];\r\n uf[v] = u;\r\n return true;\r\n}\r\n\r\n\r\nint[] fib;\r\n\r\nint N;\r\nint[] A, B;\r\n\r\nint[][] G;\r\nbool[] del;\r\nint[] sz, par, pari;\r\nint[] us;\r\n\r\nvoid dfs(int u, int p, int pi) {\r\n us ~= u;\r\n par[u] = p;\r\n pari[u] = pi;\r\n sz[u] = 1;\r\n foreach (i; G[u]) {\r\n if (!del[i]) {\r\n if (i != pi) {\r\n const v = A[i] ^ B[i] ^ u;\r\n dfs(v, u, i);\r\n sz[u] += sz[v];\r\n }\r\n }\r\n }\r\n}\r\n\r\nbool solve(int k, int rt) {\r\n if (k <= 2) {\r\n return true;\r\n }\r\n us = [];\r\n dfs(rt, -1, -1);\r\n /*\r\n debug {\r\n writeln(\"solve \", k, \" \", rt);\r\n writeln(\" us = \", us);\r\n writeln(\" sz = \", sz);\r\n writeln(\" par = \", par);\r\n writeln(\" pari = \", pari);\r\n }\r\n //*/\r\n assert(sz[rt] == fib[k]);\r\n int[] vs;\r\n foreach (u; us) {\r\n if (sz[u] == fib[k - 2] || fib[k] - sz[u] == fib[k - 2]) {\r\n vs ~= u;\r\n }\r\n }\r\n foreach (v; vs) {\r\n del[pari[v]] = true;\r\n }\r\n if (vs.length == 0) {\r\n return false;\r\n } else if (vs.length == 1) {\r\n bool ret = true;\r\n if (sz[vs[0]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 1, par[vs[0]]);\r\n } else {\r\n ret = ret && solve(k - 1, vs[0]);\r\n ret = ret && solve(k - 2, par[vs[0]]);\r\n }\r\n return ret;\r\n } else if (vs.length == 2) {\r\n bool ret = true;\r\n if (sz[vs[0]] == fib[k - 2]) {\r\n if (sz[vs[1]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 2, vs[1]);\r\n ret = ret && solve(k - 3, par[vs[0]]);\r\n } else {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 2, par[vs[1]]);\r\n ret = ret && solve(k - 3, par[vs[0]]);\r\n }\r\n } else {\r\n if (sz[vs[1]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[1]);\r\n ret = ret && solve(k - 2, par[vs[0]]);\r\n ret = ret && solve(k - 3, par[vs[1]]);\r\n } else {\r\n assert(false);\r\n }\r\n }\r\n return ret;\r\n } else {\r\n assert(false);\r\n }\r\n}\r\n\r\nvoid main() {\r\n fib = new int[40];\r\n fib[0] = 1;\r\n fib[1] = 1;\r\n foreach (k; 2 .. fib.length) {\r\n fib[k] = fib[k - 1] + fib[k - 2];\r\n }\r\n debug {\r\n writeln(\"fib = \", fib);\r\n }\r\n \r\n try {\r\n for (; ; ) {\r\n N = readInt();\r\n A = new int[N - 1];\r\n B = new int[N - 1];\r\n foreach (i; 0 .. N - 1) {\r\n A[i] = readInt() - 1;\r\n B[i] = readInt() - 1;\r\n }\r\n \r\n G = new int[][N];\r\n foreach (i; 0 .. N - 1) {\r\n G[A[i]] ~= i;\r\n G[B[i]] ~= i;\r\n }\r\n del = new bool[N - 1];\r\n sz = new int[N];\r\n par = new int[N];\r\n pari = new int[N];\r\n \r\n bool ans = false;\r\n const k = fib.lowerBound(N);\r\n if (fib[k] == N) {\r\n ans = solve(k, 0);\r\n }\r\n writeln(ans ? \"YES\" : \"NO\");\r\n \r\n debug {\r\n auto brt = new bool[1 << N];\r\n foreach (p; 1 .. 1 << N) {\r\n if (!(p & p - 1)) {\r\n brt[p] = true;\r\n } else {\r\n auto uf = new int[N];\r\n uf[] = -1;\r\n foreach (i; 0 .. N - 1) {\r\n if ((p & 1 << A[i]) && (p & 1 << B[i])) {\r\n uf.connect(A[i], B[i]);\r\n }\r\n }\r\n int numComps;\r\n foreach (u; 0 .. N) {\r\n if (p & 1 << u) {\r\n if (uf[u] < 0) {\r\n ++numComps;\r\n }\r\n }\r\n }\r\n if (fib.count(popcnt(p)) > 0 && numComps == 1) {\r\n for (int q = p; --q &= p; ) {\r\n if (brt[q] && brt[p ^ q]) {\r\n brt[p] = true;\r\n break;\r\n }\r\n }\r\n }\r\n }\r\n }\r\n assert(brt[$ - 1] == ans, format(\"%s %s %s: %s %s\", N, A, B, brt[$ - 1], ans));\r\n }\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nint[] fib;\r\n\r\nint N;\r\nint[] A, B;\r\n\r\nint[][] G;\r\nbool[] del;\r\nint[] sz, par, pari;\r\nint[] us;\r\n\r\nvoid dfs(int u, int p, int pi) {\r\n us ~= u;\r\n par[u] = p;\r\n pari[u] = pi;\r\n sz[u] += 1;\r\n foreach (i; G[u]) {\r\n if (!del[i]) {\r\n if (i != pi) {\r\n const v = A[i] ^ B[i] ^ u;\r\n dfs(v, u, i);\r\n sz[u] += sz[v];\r\n }\r\n }\r\n }\r\n}\r\n\r\nbool solve(int k, int rt) {\r\n if (k <= 2) {\r\n return true;\r\n }\r\n us = [];\r\n dfs(rt, -1, -1);\r\n assert(sz[rt] == fib[k]);\r\n int[] vs;\r\n foreach (v; us) {\r\n if (sz[v] == fib[k - 2] || sz[v] == fib[k - 1]) {\r\n vs ~= v;\r\n }\r\n }\r\n if (vs.length != 2) {\r\n return false;\r\n }\r\n foreach (v; vs) {\r\n del[pari[v]] = true;\r\n }\r\n bool ret = true;\r\n if (sz[vs[0]] == fib[k - 2]) {\r\n if (sz[vs[1]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 2, vs[1]);\r\n ret = ret && solve(k - 3, par[vs[0]]);\r\n } else {\r\n ret = ret && solve(k - 2, vs[0]);\r\n ret = ret && solve(k - 2, par[vs[1]]);\r\n ret = ret && solve(k - 3, par[vs[0]]);\r\n }\r\n } else {\r\n if (sz[vs[1]] == fib[k - 2]) {\r\n ret = ret && solve(k - 2, vs[1]);\r\n ret = ret && solve(k - 2, par[vs[0]]);\r\n ret = ret && solve(k - 3, par[vs[1]]);\r\n } else {\r\n assert(false);\r\n }\r\n }\r\n return ret;\r\n}\r\n\r\nvoid main() {\r\n fib = new int[40];\r\n fib[0] = 1;\r\n fib[1] = 1;\r\n foreach (k; 2 .. fib.length) {\r\n fib[k] = fib[k - 1] + fib[k - 2];\r\n }\r\n \r\n try {\r\n for (; ; ) {\r\n N = readInt();\r\n A = new int[N];\r\n B = new int[N];\r\n foreach (i; 0 .. N - 1) {\r\n A[i] = readInt() - 1;\r\n B[i] = readInt() - 1;\r\n }\r\n \r\n G = new int[][N];\r\n foreach (i; 0 .. N - 1) {\r\n G[A[i]] ~= i;\r\n G[B[i]] ~= i;\r\n }\r\n del = new bool[N - 1];\r\n sz = new int[N];\r\n par = new int[N];\r\n pari = new int[N];\r\n \r\n bool ans = false;\r\n const k = fib.lowerBound(N);\r\n if (fib[k] == N) {\r\n ans = solve(k, 0);\r\n }\r\n writeln(ans ? \"YES\" : \"NO\");\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}], "src_uid": "ad5b878298adea8742c36e2e119780f9"} {"nl": {"description": "You are given two non-empty strings $$$s$$$ and $$$t$$$, consisting of Latin letters.In one move, you can choose an occurrence of the string $$$t$$$ in the string $$$s$$$ and replace it with dots.Your task is to remove all occurrences of the string $$$t$$$ in the string $$$s$$$ in the minimum number of moves, and also calculate how many different sequences of moves of the minimum length exist.Two sequences of moves are considered different if the sets of indices at which the removed occurrences of the string $$$t$$$ in $$$s$$$ begin differ. For example, the sets $$$\\{1, 2, 3\\}$$$ and $$$\\{1, 2, 4\\}$$$ are considered different, the sets $$$\\{2, 4, 6\\}$$$ and $$$\\{2, 6\\}$$$ — too, but sets $$$\\{3, 5\\}$$$ and $$$\\{5, 3\\}$$$ — not.For example, let the string $$$s =$$$ \"abababacababa\" and the string $$$t =$$$ \"aba\". We can remove all occurrences of the string $$$t$$$ in $$$2$$$ moves by cutting out the occurrences of the string $$$t$$$ at the $$$3$$$th and $$$9$$$th positions. In this case, the string $$$s$$$ is an example of the form \"ab...bac...ba\". It is also possible to cut occurrences of the string $$$t$$$ at the $$$3$$$th and $$$11$$$th positions. There are two different sequences of minimum length moves.Since the answer can be large, output it modulo $$$10^9 + 7$$$.", "input_spec": "The first line of the input contains a single integer $$$q$$$ ($$$1 \\le q \\le 50$$$) — the number of test cases. The descriptions of the sets follow. The first line of each set contains a non-empty string $$$s$$$ ($$$1 \\le |s| \\le 500$$$) consisting of lowercase Latin letters. The second line of each set contains a non-empty string $$$t$$$ ($$$1 \\le |t| \\le 500$$$) consisting of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$500$$$. Similarly, it is guaranteed that the sum of string lengths $$$t$$$ over all test cases does not exceed $$$500$$$.", "output_spec": "For each test case print two integers — the minimum number of moves and the number of different optimal sequences, modulo $$$10^9 + 7$$$.", "sample_inputs": ["8\n\nabababacababa\n\naba\n\nddddddd\n\ndddd\n\nxyzxyz\n\nxyz\n\nabc\n\nabcd\n\nabacaba\n\nabaca\n\nabc\n\ndef\n\naaaaaaaa\n\na\n\naaaaaaaa\n\naa"], "sample_outputs": ["2 2\n1 4\n2 1\n0 1\n1 1\n0 1\n8 1\n3 6"], "notes": "NoteThe first test case is explained in the statement.In the second case, it is enough to cut any of the four occurrences.In the third case, string $$$s$$$ is the concatenation of two strings $$$t =$$$ \"xyz\", so there is a unique optimal sequence of $$$2$$$ moves.In the fourth and sixth cases, the string $$$s$$$ initially contains no occurrences of the string $$$t$$$.In the fifth case, the string $$$s$$$ contains exactly one occurrence of the string $$$t$$$."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nenum long mod = cast(long)(1e9+7);\r\n\r\nvoid main() {\r\n int Q = read!int;\r\n foreach (t; 0 .. Q) solve();\r\n}\r\n\r\nvoid solve() {\r\n auto S = read!string;\r\n auto T = read!string;\r\n int sn = S.length;\r\n int tn = T.length;\r\n int[] xs;\r\n for (int i = 0; i + tn <= sn; i++) {\r\n if (S[i .. (i + tn)] == T) {\r\n xs ~= i;\r\n }\r\n }\r\n\r\n const int INF = 1<<28;\r\n alias P = Tuple!(int, \"m\", long, \"c\");\r\n int N = xs.length;\r\n\r\n if (N == 0) {\r\n writeln(\"0 1\");\r\n return;\r\n }\r\n\r\n int L = tn;\r\n auto f = new P[](N + 1);\r\n f[] = P(INF, -(1L<<56));\r\n f[0] = tuple(0, 1);\r\n for (int n = 0; n < N; n++) {\r\n int l;\r\n for (l = n; l < N && xs[l] - xs[n] < L; l++) {}\r\n for (int k = 0; k <= n; k++) {\r\n if (xs[n] - xs[k] < L) {\r\n if (f[l].m > f[k].m + 1) {\r\n f[l].m = f[k].m + 1;\r\n f[l].c = f[k].c;\r\n } else if (f[l].m == f[k].m + 1) {\r\n f[l].c += f[k].c;\r\n f[l].c %= mod;\r\n }\r\n }\r\n }\r\n }\r\n writefln(\"%s %s\", f[N].m, f[N].c);\r\n /*\r\n writeln(S);\r\n writeln(T);\r\n writeln(xs);\r\n */\r\n}\r\n"}], "negative_code": [], "src_uid": "904af5d6a9d84b7b7b7ff8d63e6f0254"} {"nl": {"description": "Andrew loves the sea. That's why, at the height of the summer season, he decided to go to the beach, taking a sunbed with him to sunbathe.The beach is a rectangular field with $$$n$$$ rows and $$$m$$$ columns. Some cells of the beach are free, some have roads, stones, shops and other non-movable objects. Some of two adjacent along the side cells can have sunbeds located either horizontally or vertically.Andrew hopes to put his sunbed somewhere, but that's a bad luck, there may no longer be free places for him! That's why Andrew asked you to help him to find a free place for his sunbed. Andrew's sunbed also should be places on two adjacent cells. If there are no two adjacent free cells, then in order to free some place for a sunbed, you will have to disturb other tourists. You can do the following actions: Come to some sunbed and, after causing $$$p$$$ units of discomfort to its owner, lift the sunbed by one of its sides and rotate it by $$$90$$$ degrees. One half of the sunbed must remain in the same cell and another half of the sunbed must move to the free cell. At the same time, anything could be on the way of a sunbed during the rotation . Rotation of the sunbed by $$$90$$$ degrees around cell $$$(1, 2)$$$. Come to some sunbed and, after causing $$$q$$$ units of discomfort to its owner, shift the sunbed along its long side by one cell. One half of the sunbed must move to the place of another, and another — to the free cell. Shift of the sunbed by one cell to the right. In any moment each sunbed occupies two adjacent free cells. You cannot move more than one sunbed at a time.Help Andrew to free a space for his sunbed, causing the minimum possible number of units of discomfort to other tourists, or detect that it is impossible.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 300\\,000$$$, $$$1 \\le n \\cdot m \\le 300\\,000$$$) — the number of rows and columns in rectangle. The second line contains two integers $$$p$$$ and $$$q$$$ ($$$1 \\le p, q \\le 10^9$$$) — the number of units of discomfort caused by rotation and shift of a sunbed, respectively. Each of the following $$$n$$$ lines contains $$$m$$$ characters, describing cells of the rectangle. Each lines consists of characters \"L\", \"R\", \"D\", \"U\", \".\" and \"#\", denoting the type of the cell. Characters \"L\", \"R\", \"D\" and \"U\" denote a half of a sunbed placed in the cell — left, right, bottom and top half, respectively. Character \".\" denotes a free cell and character \"#\" — a cell, occupied by some non-movable object.", "output_spec": "Print one integer — the minimum possible number of units of discomfort, caused to other tourists, to free a space for a sunbed. If it is impossible to free a space for a sunbed, print $$$-1$$$.", "sample_inputs": ["2 5\n\n5 2\n\n.LR##\n\n##LR.", "2 3\n\n4 5\n\nLR.\n\n#.#", "4 3\n\n10 10\n\n.LR\n\n###\n\nUU#\n\nDD.", "3 6\n\n10 7\n\n.U##.#\n\n#DLR##\n\n.##LR."], "sample_outputs": ["4", "-1", "-1", "24"], "notes": "NoteIn the first example we can shift upper sunbed to the left and lower sunbed — to the right. Andrew will be able to put his sunbed vertically in the middle of the beach. We well cause $$$2 + 2 = 4$$$ units of discomfort. It is easy to prove that it is an optimal answer. Optimal strategy in the first example (Andrew's sunbed is colored white). In the second example it is impossible to free a space for Andrew's sunbed. All possible states of the beach after any rotates and shifts are illustrated in the problem statement."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.container;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nimmutable int dirs = 4;\r\nimmutable int [dirs] dRow = [ -1, 0, +1, 0];\r\nimmutable int [dirs] dCol = [ 0, -1, 0, +1];\r\nimmutable char [dirs] dName = ['U', 'L', 'D', 'R'];\r\nimmutable long infinity = long.max / 8;\r\nimmutable int NA = -1;\r\nalias Coord = Tuple !(int, q{row}, int, q{col});\r\nalias Record = Tuple !(long, q{dist}, int, q{row}, int, q{col});\r\n\r\nlong solve (int rows, int cols, string [] board, int p, int q)\r\n{\r\n\tauto d = new long [] [] (rows, cols);\r\n\r\n\tauto t = redBlackTree !(Record);\r\n\tforeach (row; 0..rows)\r\n\t{\r\n\t\tforeach (col; 0..cols)\r\n\t\t{\r\n\t\t\td[row][col] = (board[row][col] == '.' ? 0 : infinity);\r\n\t\t\tif (d[row][col] == 0)\r\n\t\t\t{\r\n\t\t\t\tt.insert (Record (0, row, col));\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\twhile (!t.empty)\r\n\t{\r\n\t\tauto cur = t.front;\r\n\t\tt.removeFront ();\r\n\t\tforeach (dir; 0..dirs)\r\n\t\t{\r\n\t\t\tauto nRow = cur.row + dRow[dir];\r\n\t\t\tauto nCol = cur.col + dCol[dir];\r\n\t\t\tif (nRow < 0 || rows <= nRow ||\r\n\t\t\t nCol < 0 || cols <= nCol)\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\t\t\tauto delta = dName[].countUntil (board[nRow][nCol]);\r\n\t\t\tif (delta == NA)\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\t\t\tdelta ^= 2;\r\n\t\t\tauto pRow = nRow + dRow[delta];\r\n\t\t\tauto pCol = nCol + dCol[delta];\r\n\t\t\tauto cost = (dir == delta) ? q : p;\r\n\t\t\tif (d[pRow][pCol] > cur.dist + cost)\r\n\t\t\t{\r\n\t\t\t\tt.removeKey (Record (d[pRow][pCol], pRow, pCol));\r\n\t\t\t\td[pRow][pCol] = cur.dist + cost;\r\n\t\t\t\tt.insert (Record (d[pRow][pCol], pRow, pCol));\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tlong res = infinity;\r\n\tforeach (row; 0..rows)\r\n\t{\r\n\t\tforeach (col; 0..cols)\r\n\t\t{\r\n\t\t\tif (row + 1 < rows)\r\n\t\t\t{\r\n\t\t\t\tres = min (res, d[row][col] + d[row + 1][col]);\r\n\t\t\t}\r\n\t\t\tif (col + 1 < cols)\r\n\t\t\t{\r\n\t\t\t\tres = min (res, d[row][col] + d[row][col + 1]);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn (res == infinity) ? NA : res;\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tint rows, cols;\r\n\twhile (readf !(\" %s %s\") (rows, cols) > 0)\r\n\t{\r\n\t\tint p, q;\r\n\t\treadf !(\" %s %s\") (p, q);\r\n\t\treadln;\r\n\t\tstring [] board;\r\n\t\tforeach (row; 0..rows)\r\n\t\t{\r\n\t\t\tboard ~= readln.strip;\r\n\t\t}\r\n\t\twriteln (solve (rows, cols, board, p, q));\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "35b9acbe61226923a0c45452f23166c8"} {"nl": {"description": "Polycarp has guessed three positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$.You have to guess three numbers $$$a$$$, $$$b$$$ and $$$c$$$ using given numbers. Print three guessed integers in any order.Pay attention that some given numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (it is also possible that $$$a=b=c$$$).", "input_spec": "The only line of the input contains four positive integers $$$x_1, x_2, x_3, x_4$$$ ($$$2 \\le x_i \\le 10^9$$$) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number $$$x_1, x_2, x_3, x_4$$$.", "output_spec": "Print such positive integers $$$a$$$, $$$b$$$ and $$$c$$$ that four numbers written on a board are values $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$ written in some order. Print $$$a$$$, $$$b$$$ and $$$c$$$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.", "sample_inputs": ["3 6 5 4", "40 40 40 60", "201 101 101 200"], "sample_outputs": ["2 1 3", "20 20 20", "1 100 100"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.range;\nimport std.string;\nimport std.conv;\nimport std.math;\n\nvoid main()\n{\n int[] input = readln().split.map!\"a.to!int\".array;\n input = input.sort.reverse.array;\n int a = input[0] - input[1];\n int b = input[0] - input[2];\n int c = input[0] - input[3];\n writeln(a, \" \", b, \" \", c);\n}\n"}, {"source_code": "// Vicfred\n// https://codeforces.com/problemset/problem/1154/A\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\n\nvoid main() {\n int[] numbers = readln.split.map!(to!int).array;\n numbers.sort;\n int abc = numbers[$-1];\n int x = abc-numbers[0];\n writefln(\"%d %d %d\", x, numbers[1]-x, numbers[2]-x);\n}\n"}, {"source_code": "import std.stdio;\nimport std.math;\nimport std.algorithm;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.range;\nimport std.file;\nimport std.datetime;\n\n\n\n\n\nvoid main() {\n\t\n\tint[] sayılar = stdin.readln.strip.split.map!(a=> a.to!int()).array;\n\tint toplam = sayılar.maxElement();\n\tsayılar = sayılar.remove( sayılar.countUntil(toplam) ).array;\n\tint bArtıC = sayılar.maxElement();\n\tint a = toplam - bArtıC;\n\tsayılar = sayılar.remove( sayılar.countUntil(bArtıC) ).array;\n\n\tint aArtıC = sayılar.maxElement();\n\tint c = aArtıC - a;\n\tint b = toplam - aArtıC;\n\t\n\twriteln(a, \" \", b, \" \", c);\n\t\n\t//writeln();\n\t\n}\n\n\n\n"}, {"source_code": "import std.stdio, std.conv, std.string;\nimport std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n\nint DEBUG_LEVEL = 0;\nvoid print()(){ writeln(\"\"); }\nvoid print(T, A ...)(T t, lazy A a){ write(t), print(a); }\nvoid print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }\n\nconst long mod = 1_000_000_007;\n\nvoid main(string[] args){\n\tif(args.length > 1 && args[1] == \"-debug\"){\n\t\tif(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;\n\t\telse DEBUG_LEVEL = 1;\n\t}\n\t\n\tint[] xs = readln.chomp.split.map!(to!int).array;\n\t\n\tint s = -1;\n\tforeach(x; xs) s = max(s, x);\n\t\n\tint[] as;\n\tforeach(x; xs) if(x < s) as ~= s - x;\n\t\n\tas.map!(to!string).array.join(\" \").writeln;\n\n}\n/*\n\nThe largest must be a + b + c.\nCall it s.\n\nOthers are s - a, s - b, s - c.\n\n*/\n"}, {"source_code": "import std.stdio;\n\nint main()\n{\n\tint n(int a, int b)\n\t{\n\t\tif (a>=b)\n\t\t{\n\t\t\treturn a;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn b;\n\t\t}\n\t}\n\tint m(int a, int b, int c, int d)\n\t{\n\t\treturn n(n(a,b),n(c,d));\n\t}\n\tint a=0;\n\tint b=0;\n\tint c=0;\n\tint d=0;\n\treadf(\" %d\", &a);\n\treadf(\" %d\", &b);\n\treadf(\" %d\", &c);\n\treadf(\" %d\", &d);\n\tint e=m(a,b,c,d);\n\tif (e==a)\n\t{\n\t\twriteln(e-b);\n\t\twriteln(e-c);\n\t\twriteln(e-d);\n\t}\n\telse if (e==b)\n\t{\n\t\twriteln(e-a);\n\t\twriteln(e-c);\n\t\twriteln(e-d);\n\t}\n\telse if (e==c)\n\t{\n\t\twriteln(e-a);\n\t\twriteln(e-b);\n\t\twriteln(e-d);\n\t}\n\telse\n\t{\n\t\twriteln(e-a);\n\t\twriteln(e-b);\n\t\twriteln(e-c);\n\t}\n\treturn 0;\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nbool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }\nbool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }\n\nlong mod = 10^^9 + 7;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\t\n\tauto x = new long[](4);\n\tforeach (i; 0..4)\n\t{\n\t\tx[i] = RD;\n\t}\n\tx.sort();\n\n\twriteln(x[3] - x[0], \" \", x[3] - x[1], \" \", x[3] -x[2]);\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "cda949a8fb1f158f3c06109a2d33f084"} {"nl": {"description": "You are given a binary array $$$a$$$ (all elements of the array are $$$0$$$ or $$$1$$$) of length $$$n$$$. You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until $$$a$$$ is sorted: Choose two random indices $$$i$$$ and $$$j$$$ such that $$$i < j$$$. Indices are chosen equally probable among all pairs of indices $$$(i, j)$$$ such that $$$1 \\le i < j \\le n$$$. If $$$a_i > a_j$$$, then swap elements $$$a_i$$$ and $$$a_j$$$. What is the expected number of such operations you will perform before the array becomes sorted?It can be shown that the answer can be expressed as an irreducible fraction $$$\\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \\not \\equiv 0 \\pmod{998\\,244\\,353}$$$. Output the integer equal to $$$p \\cdot q^{-1} \\bmod 998\\,244\\,353$$$. In other words, output such an integer $$$x$$$ that $$$0 \\le x < 998\\,244\\,353$$$ and $$$x \\cdot q \\equiv p \\pmod{998\\,244\\,353}$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^5$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$) — the number of elements in the binary array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$a_i \\in \\{0, 1\\}$$$) — elements of the array. It's guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$200\\,000$$$.", "output_spec": "For each test case print one integer — the value $$$p \\cdot q^{-1} \\bmod 998\\,244\\,353$$$.", "sample_inputs": ["3\n\n3\n\n0 1 0\n\n5\n\n0 0 1 1 1\n\n6\n\n1 1 1 0 0 1"], "sample_outputs": ["3\n0\n249561107"], "notes": "NoteConsider the first test case. If the pair of indices $$$(2, 3)$$$ will be chosen, these elements will be swapped and array will become sorted. Otherwise, if one of pairs $$$(1, 2)$$$ or $$$(1, 3)$$$ will be selected, nothing will happen. So, the probability that the array will become sorted after one operation is $$$\\frac{1}{3}$$$, the probability that the array will become sorted after two operations is $$$\\frac{2}{3} \\cdot \\frac{1}{3}$$$, the probability that the array will become sorted after three operations is $$$\\frac{2}{3} \\cdot \\frac{2}{3} \\cdot \\frac{1}{3}$$$ and so on. The expected number of operations is $$$\\sum \\limits_{i=1}^{\\infty} \\left(\\frac{2}{3} \\right)^{i - 1} \\cdot \\frac{1}{3} \\cdot i = 3$$$.In the second test case the array is already sorted so the expected number of operations is zero.In the third test case the expected number of operations equals to $$$\\frac{75}{4}$$$ so the answer is $$$75 \\cdot 4^{-1} \\equiv 249\\,561\\,107 \\pmod {998\\,244\\,353}$$$."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable long mod = 998_244_353;\r\n\r\nlong powMod (long a, long p)\r\n{\r\n\tlong res = 1;\r\n\tfor ( ; p > 0; p >>= 1)\r\n\t{\r\n\t\tif (p & 1)\r\n\t\t{\r\n\t\t\tres = (res * 1L * a) % mod;\r\n\t\t}\r\n\t\ta = (a * 1L * a) % mod;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nlong invMod (long a)\r\n{\r\n\treturn powMod (a, mod - 2);\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(long)).array;\r\n\r\n\t\tlong invs = 0;\r\n\t\tint i = 0;\r\n\t\tint j = n - 1;\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\twhile (i < n && a[i] == 0)\r\n\t\t\t{\r\n\t\t\t\ti += 1;\r\n\t\t\t}\r\n\t\t\twhile (j >= 0 && a[j] == 1)\r\n\t\t\t{\r\n\t\t\t\tj -= 1;\r\n\t\t\t}\r\n\t\t\tif (i >= j)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tinvs += 1;\r\n\t\t\ti += 1;\r\n\t\t\tj -= 1;\r\n\t\t}\r\n\r\n\t\tauto cn2 = n;\r\n\t\tcn2 = (cn2 * 1L * (n - 1)) % mod;\r\n\t\tcn2 = (cn2 * 1L * invMod (2)) % mod;\r\n\r\n\t\tlong res = 0;\r\n\t\tforeach (k; 0..invs)\r\n\t\t{\r\n\t\t\tauto cur = k + 1;\r\n\t\t\tcur = (cur * 1L * (k + 1)) % mod;\r\n\t\t\tres = (res + cn2 * 1L * invMod (cur)) % mod;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nvoid main() {\r\n int T = read!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n\r\nvoid solve() {\r\n int N = read!int;\r\n auto A = readarray!int;\r\n\r\n int nz = A.count!(a => a == 0);\r\n int t0 = A[0 .. nz].count!(a => a == 1);\r\n //writeln([nz, t0]);\r\n\r\n MInt p(int t) {\r\n long t2 = cast(long)(t) * t;\r\n long nC2 = cast(long)(N) * (N - 1) / 2;\r\n return MInt(t2) / nC2;\r\n }\r\n\r\n auto E = new MInt[t0 + 1];\r\n E[0] = 0;\r\n for (int t = 0; t < t0; t++) {\r\n E[t+1] = E[t] + 1 / p(t+1);\r\n }\r\n //writeln(E);\r\n writeln(E[t0]);\r\n}\r\n\r\nalias MInt = ModInt!998244353L;\r\n\r\nstruct ModInt(long mod) {\r\n invariant() {\r\n assert(0 <= val && val < mod);\r\n }\r\n long val;\r\n this(long v) { this.val = (mod * mod + v) % mod; }\r\n ModInt opBinary(string op : \"+\")(int y) const { return ModInt(this.val + y); }\r\n ModInt opBinary(string op : \"-\")(int y) const { return ModInt(this.val - y); }\r\n ModInt opBinary(string op : \"*\")(int y) const { return ModInt(this.val * y); }\r\n ModInt opBinary(string op : \"/\")(int y) const { return ModInt(this.val * inv(y)); }\r\n ModInt opBinary(string op : \"+\")(long y) const { return ModInt(this.val + y); }\r\n ModInt opBinary(string op : \"-\")(long y) const { return ModInt(this.val - y); }\r\n ModInt opBinary(string op : \"*\")(long y) const { return ModInt(this.val * y); }\r\n ModInt opBinary(string op : \"/\")(long y) const { return ModInt(this.val * inv(y)); }\r\n ModInt opBinary(string op : \"+\")(ModInt y) const { return ModInt(this.val + y.val); }\r\n ModInt opBinary(string op : \"-\")(ModInt y) const { return ModInt(this.val - y.val); }\r\n ModInt opBinary(string op : \"*\")(ModInt y) const { return ModInt(this.val * y.val); }\r\n ModInt opBinary(string op : \"/\")(ModInt y) const { return ModInt(this.val * inv(y.val)); }\r\n ModInt opBinaryRight(string op : \"+\")(int y) const { return ModInt(this.val + y); }\r\n ModInt opBinaryRight(string op : \"-\")(int y) const { return ModInt(y - this.val); }\r\n ModInt opBinaryRight(string op : \"*\")(int y) const { return ModInt(this.val * y); }\r\n ModInt opBinaryRight(string op : \"/\")(int y) const { return ModInt(inv(this.val) * y); }\r\n ModInt opBinaryRight(string op : \"+\")(long y) const { return ModInt(this.val + y); }\r\n ModInt opBinaryRight(string op : \"-\")(long y) const { return ModInt(y - this.val); }\r\n ModInt opBinaryRight(string op : \"*\")(long y) const { return ModInt(this.val * y); }\r\n ModInt opBinaryRight(string op : \"/\")(long y) const { return ModInt(inv(this.val) * y); }\r\n void opAssign(int y) { this.val = y; }\r\n void opAssign(long y) { this.val = y; }\r\n void opOpAssign(string op : \"+\")(int y) { this.val = (this.val + mod * mod + y) % mod; }\r\n void opOpAssign(string op : \"-\")(int y) { this.val = (this.val + mod * mod - y) % mod; }\r\n void opOpAssign(string op : \"*\")(int y) { this.val = (this.val * y) % mod; }\r\n void opOpAssign(string op : \"/\")(int y) { this.val = (this.val * inv(y)) % mod; }\r\n void opOpAssign(string op : \"+\")(long y) { this.val = (this.val + mod * mod + y) % mod; }\r\n void opOpAssign(string op : \"-\")(long y) { this.val = (this.val + mod * mod - y) % mod; }\r\n void opOpAssign(string op : \"*\")(long y) { this.val = (this.val * y) % mod; }\r\n void opOpAssign(string op : \"/\")(long y) { this.val = (this.val * inv(y)) % mod; }\r\n void opOpAssign(string op : \"+\")(ModInt y) { this.val = (this.val + mod * mod + y.val) % mod; }\r\n void opOpAssign(string op : \"-\")(ModInt y) { this.val = (this.val + mod * mod - y.val) % mod; }\r\n void opOpAssign(string op : \"*\")(ModInt y) { this.val = (this.val * y.val) % mod; }\r\n void opOpAssign(string op : \"/\")(ModInt y) { this.val = (this.val * inv(y.val)) % mod; }\r\n ModInt opUnary(string op : \"-\")() const { return ModInt(mod - this.val); }\r\n string toString() const { return val.to!string; }\r\n ModInt pow(long n) { return ModInt(pow(this.val, n, mod)); }\r\n ModInt pow(ModInt n) { return ModInt(pow(this.val, n.val, mod)); }\r\n static long pow(long x, long n, long mod) {\r\n if (n == 0) return 1L;\r\n x %= mod;\r\n if (n == 1) return x;\r\n if (n % 2 == 0) {\r\n long y = pow(x, n/2, mod);\r\n return y * y % mod;\r\n } else {\r\n return x * pow(x, n-1, mod) % mod;\r\n }\r\n }\r\n static long extgcd(long a, long b, ref long x, ref long y) {\r\n if (b == 0) {\r\n x = 1;\r\n y = 0;\r\n return a;\r\n }\r\n long d = extgcd(b, a % b, y, x);\r\n y -= a / b * x;\r\n return d;\r\n }\r\n static long inv(long a) {\r\n long x, y;\r\n extgcd(a, mod, x, y);\r\n return (x % mod + mod) % mod;\r\n }\r\n}\r\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nstruct ModInt(uint M_) {\r\n import std.conv : to;\r\n alias M = M_;\r\n uint x;\r\n this(ModInt a) { x = a.x; }\r\n this(uint x_) { x = x_ % M; }\r\n this(ulong x_) { x = cast(uint)(x_ % M); }\r\n this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; }\r\n this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }\r\n ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); }\r\n ref ModInt opOpAssign(string op, T)(T a) {\r\n static if (is(T == ModInt)) {\r\n static if (op == \"+\") { x = ((x += a.x) >= M) ? (x - M) : x; }\r\n else static if (op == \"-\") { x = ((x -= a.x) >= M) ? (x + M) : x; }\r\n else static if (op == \"*\") { x = cast(uint)((cast(ulong)(x) * a.x) % M); }\r\n else static if (op == \"/\") { this *= a.inv(); }\r\n else static assert(false);\r\n return this;\r\n } else static if (op == \"^^\") {\r\n if (a < 0) return this = inv()^^(-a);\r\n ModInt b = this, c = 1U;\r\n for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }\r\n return this = c;\r\n } else {\r\n return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\r\n }\r\n }\r\n ModInt inv() const {\r\n uint a = M, b = x; int y = 0, z = 1;\r\n for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; }\r\n assert(a == 1); return ModInt(y);\r\n }\r\n ModInt opUnary(string op)() const {\r\n static if (op == \"+\") { return this; }\r\n else static if (op == \"-\") { ModInt a; a.x = x ? (M - x) : 0U; return a; }\r\n else static assert(false);\r\n }\r\n ModInt opBinary(string op, T)(T a) const { return mixin(\"ModInt(this) \" ~ op ~ \"= a\"); }\r\n ModInt opBinaryRight(string op, T)(T a) const { return mixin(\"ModInt(a) \" ~ op ~ \"= this\"); }\r\n bool opCast(T: bool)() const { return (x != 0U); }\r\n string toString() const { return x.to!string; }\r\n}\r\n\r\nenum MO = 998244353;\r\nalias Mint = ModInt!MO;\r\n\r\nenum LIM_INV = 400_010;\r\nMint[] inv, fac, invFac;\r\nvoid prepare() {\r\n inv = new Mint[LIM_INV];\r\n fac = new Mint[LIM_INV];\r\n invFac = new Mint[LIM_INV];\r\n inv[1] = 1;\r\n foreach (i; 2 .. LIM_INV) {\r\n inv[i] = -((Mint.M / i) * inv[cast(size_t)(Mint.M % i)]);\r\n }\r\n fac[0] = invFac[0] = 1;\r\n foreach (i; 1 .. LIM_INV) {\r\n fac[i] = fac[i - 1] * i;\r\n invFac[i] = invFac[i - 1] * inv[i];\r\n }\r\n}\r\nMint binom(long n, long k) {\r\n if (n < 0) {\r\n if (k >= 0) {\r\n return (-1)^^(k & 1) * binom(-n + k - 1, k);\r\n } else if (n - k >= 0) {\r\n return (-1)^^((n - k) & 1) * binom(-k - 1, n - k);\r\n } else {\r\n return Mint(0);\r\n }\r\n } else {\r\n if (0 <= k && k <= n) {\r\n assert(n < LIM_INV);\r\n return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];\r\n } else {\r\n return Mint(0);\r\n }\r\n }\r\n}\r\n\r\n\r\nvoid main() {\r\n prepare;\r\n \r\n try {\r\n for (; ; ) {\r\n const numCases = readInt;\r\n foreach (caseId; 0 .. numCases) {\r\n const N = readInt;\r\n auto A = new int[N];\r\n foreach (i; 0 .. N) {\r\n A[i] = readInt;\r\n }\r\n \r\n const K = A.sum;\r\n const L = min(K, N - K);\r\n \r\n const Mint all = 1L * N * (N - 1) / 2;\r\n auto dp = new Mint[L + 2];\r\n foreach (a; 1 .. L + 1) {\r\n dp[a] = all * inv[a] * inv[a] + dp[a - 1];\r\n }\r\n \r\n const S = A[0 .. N - K].sum;\r\n writeln(dp[S]);\r\n }\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int mod = 998_244_353;\r\n\r\nint powMod (int a, int p)\r\n{\r\n\tint res = 1;\r\n\tfor ( ; p > 0; p >>= 1)\r\n\t{\r\n\t\tif (p & 1)\r\n\t\t{\r\n\t\t\tres = (res * 1L * a) % mod;\r\n\t\t}\r\n\t\ta = (a * 1L * a) % mod;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nint invMod (int a)\r\n{\r\n\treturn powMod (a, mod - 2);\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tint invs = 0;\r\n\t\tint i = 0;\r\n\t\tint j = n - 1;\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\twhile (i < n && a[i] == 0)\r\n\t\t\t{\r\n\t\t\t\ti += 1;\r\n\t\t\t}\r\n\t\t\twhile (j >= 0 && a[j] == 1)\r\n\t\t\t{\r\n\t\t\t\tj -= 1;\r\n\t\t\t}\r\n\t\t\tif (i >= j)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tinvs += 1;\r\n\t\t\ti += 1;\r\n\t\t\tj -= 1;\r\n\t\t}\r\n\r\n\t\tauto cn2 = n;\r\n\t\tcn2 = (cn2 * 1L * (n - 1)) % mod;\r\n\t\tcn2 = (cn2 * 1L * invMod (2)) % mod;\r\n\r\n\t\tint res = 0;\r\n\t\tforeach (k; 0..invs)\r\n\t\t{\r\n\t\t\tauto cur = k + 1;\r\n\t\t\tcur = (cur * 1L * (k + 1)) % mod;\r\n\t\t\tres = (res + cn2 * 1L * invMod (cur)) % mod;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "src_uid": "2b391638a9fea31986fe8e41c97b640a"} {"nl": {"description": "Alice's potion making professor gave the following assignment to his students: brew a potion using $$$n$$$ ingredients, such that the proportion of ingredient $$$i$$$ in the final potion is $$$r_i > 0$$$ (and $$$r_1 + r_2 + \\cdots + r_n = 1$$$).He forgot the recipe, and now all he remembers is a set of $$$n-1$$$ facts of the form, \"ingredients $$$i$$$ and $$$j$$$ should have a ratio of $$$x$$$ to $$$y$$$\" (i.e., if $$$a_i$$$ and $$$a_j$$$ are the amounts of ingredient $$$i$$$ and $$$j$$$ in the potion respectively, then it must hold $$$a_i/a_j = x/y$$$), where $$$x$$$ and $$$y$$$ are positive integers. However, it is guaranteed that the set of facts he remembers is sufficient to uniquely determine the original values $$$r_i$$$.He decided that he will allow the students to pass the class as long as they submit a potion which satisfies all of the $$$n-1$$$ requirements (there may be many such satisfactory potions), and contains a positive integer amount of each ingredient.Find the minimum total amount of ingredients needed to make a potion which passes the class. As the result can be very large, you should print the answer modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$). Each of the next $$$n-1$$$ lines contains four integers $$$i, j, x, y$$$ ($$$1 \\le i, j \\le n$$$, $$$i\\not=j$$$, $$$1\\le x, y \\le n$$$) — ingredients $$$i$$$ and $$$j$$$ should have a ratio of $$$x$$$ to $$$y$$$. It is guaranteed that the set of facts is sufficient to uniquely determine the original values $$$r_i$$$. It is also guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the minimum total amount of ingredients needed to make a potion which passes the class, modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["3\n4\n3 2 3 4\n1 2 4 3\n1 4 2 4\n8\n5 4 2 3\n6 4 5 4\n1 3 5 2\n6 8 2 1\n3 5 3 4\n3 2 2 5\n6 7 4 3\n17\n8 7 4 16\n9 17 4 5\n5 14 13 12\n11 1 17 14\n6 13 8 9\n2 11 3 11\n4 17 7 2\n17 16 8 6\n15 5 1 14\n16 7 1 10\n12 17 13 10\n11 16 7 2\n10 11 6 4\n13 17 14 6\n3 11 15 8\n15 6 12 8"], "sample_outputs": ["69\n359\n573672453"], "notes": "NoteIn the first test case, the minimum total amount of ingredients is $$$69$$$. In fact, the amounts of ingredients $$$1, 2, 3, 4$$$ of a valid potion are $$$16, 12, 9, 32$$$, respectively. The potion is valid because Ingredients $$$3$$$ and $$$2$$$ have a ratio of $$$9 : 12 = 3 : 4$$$; Ingredients $$$1$$$ and $$$2$$$ have a ratio of $$$16 : 12 = 4 : 3$$$; Ingredients $$$1$$$ and $$$4$$$ have a ratio of $$$16 : 32 = 2 : 4$$$. In the second test case, the amounts of ingredients $$$1, 2, 3, 4, 5, 6, 7, 8$$$ in the potion that minimizes the total amount of ingredients are $$$60, 60, 24, 48, 32, 60, 45, 30$$$."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.numeric;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nimmutable int mod = 998_244_353;\r\nimmutable int limit = 200_005;\r\n\r\nbool [] sieve;\r\nint [] divs;\r\nint [] primes;\r\nint [] inv;\r\n\r\nint powMod (int a, int p)\r\n{\r\n\tint res = 1;\r\n\tfor ( ; p > 0; p >>= 1)\r\n\t{\r\n\t\tif (p & 1)\r\n\t\t{\r\n\t\t\tres = (res * 1L * a) % mod;\r\n\t\t}\r\n\t\ta = (a * 1L * a) % mod;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nalias invMod = a => powMod (a, mod - 2);\r\n\r\nvoid prepare ()\r\n{\r\n\tsieve = new bool [limit];\r\n\tsieve[] = true;\r\n\tsieve[0] = sieve[1] = false;\r\n\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (sieve[d])\r\n\t\t{\r\n\t\t\tfor (int e = d; e * d < limit; e++)\r\n\t\t\t{\r\n\t\t\t\tsieve[e * d] = false;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tdivs = new int [limit];\r\n\tfor (int d = 2; d < limit; d++)\r\n\t{\r\n\t\tif (sieve[d])\r\n\t\t{\r\n\t\t\tfor (int e = 1; e * d < limit; e++)\r\n\t\t\t{\r\n\t\t\t\tif (divs[e * d] == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tdivs[e * d] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprimes = null;\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tif (sieve[d])\r\n\t\t{\r\n\t\t\tprimes ~= d;\r\n\t\t}\r\n\t}\r\n\r\n\tinv = new int [limit];\r\n\tforeach (d; 1..limit)\r\n\t{\r\n\t\tinv[d] = invMod (d);\r\n\t}\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tprepare ();\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\talias Edge = Tuple !(int, q{v}, int, q{x}, int, q{y});\r\n\t\tauto a = new Edge [] [n];\r\n\t\tforeach (r; 0..n - 1)\r\n\t\t{\r\n\t\t\tint i, j, x, y;\r\n\t\t\treadf !(\" %s %s %s %s\") (i, j, x, y);\r\n\t\t\ti -= 1;\r\n\t\t\tj -= 1;\r\n\t\t\tauto d = gcd (x, y);\r\n\t\t\tx /= d;\r\n\t\t\ty /= d;\r\n\t\t\ta[i] ~= Edge (j, x, y);\r\n\t\t\ta[j] ~= Edge (i, y, x);\r\n\t\t}\r\n\t\treadln;\r\n\r\n\t\tauto cp = new int [limit];\r\n\t\tint mult = 1;\r\n\t\tint total = 0;\r\n\r\n\t\tvoid recur (int u, int p, int cur)\r\n\t\t{\r\n\t\t\tdebug {writeln (u, \" \", cur);} \r\n\t\t\ttotal = (total + cur) % mod;\r\n\t\t\tforeach (e; a[u])\r\n\t\t\t{\r\n\t\t\t\tif (e.v == p)\r\n\t\t\t\t{\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\t\t\t\tdebug {writeln (\"to \", e);}\r\n\t\t\t\tint next = cur;\r\n\t\t\t\tfor (int x = e.x; divs[x] != 0; x /= divs[x])\r\n\t\t\t\t{\r\n\t\t\t\t\tnext = (next * 1L * inv[divs[x]]) % mod;\r\n\t\t\t\t\tif (cp[divs[x]] == 0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tdebug {writeln (\" * \", divs[x]);}\r\n\t\t\t\t\t\tmult = (mult * 1L * divs[x]) % mod;\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcp[divs[x]] -= 1;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tfor (int y = e.y; divs[y] != 0; y /= divs[y])\r\n\t\t\t\t{\r\n\t\t\t\t\tnext = (next * 1L * divs[y]) % mod;\r\n\t\t\t\t\tcp[divs[y]] += 1;\r\n\t\t\t\t}\r\n\t\t\t\trecur (e.v, u, next);\r\n\t\t\t\tfor (int x = e.x; divs[x] != 0; x /= divs[x])\r\n\t\t\t\t{\r\n\t\t\t\t\tcp[divs[x]] += 1;\r\n\t\t\t\t}\r\n\t\t\t\tfor (int y = e.y; divs[y] != 0; y /= divs[y])\r\n\t\t\t\t{\r\n\t\t\t\t\tcp[divs[y]] -= 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0, -1, 1);\r\n\t\twriteln ((total * 1L * mult) % mod);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.numeric;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nimmutable int mod = 998_244_353;\r\nimmutable int limit = 200_005;\r\n\r\nbool [] sieve;\r\nint [] divs;\r\nint [] primes;\r\nint [] inv;\r\n\r\nint powMod (int a, int p)\r\n{\r\n\tint res = 1;\r\n\tfor ( ; p > 0; p >>= 1)\r\n\t{\r\n\t\tif (p & 1)\r\n\t\t{\r\n\t\t\tres = (res * 1L * a) % mod;\r\n\t\t}\r\n\t\ta = (a * 1L * a) % mod;\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nalias invMod = a => powMod (a, mod - 2);\r\n\r\nvoid prepare ()\r\n{\r\n\tsieve = new bool [limit];\r\n\tsieve[] = true;\r\n\tsieve[0] = sieve[1] = false;\r\n\tdivs = new int [limit];\r\n\r\n\tfor (int d = 2; d * d < limit; d++)\r\n\t{\r\n\t\tif (sieve[d])\r\n\t\t{\r\n\t\t\tfor (int e = d; e * d < limit; e++)\r\n\t\t\t{\r\n\t\t\t\tsieve[e * d] = false;\r\n\t\t\t}\r\n\t\t\tfor (int e = 1; e * d < limit; e++)\r\n\t\t\t{\r\n\t\t\t\tif (divs[e * d] == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tdivs[e * d] = d;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprimes = null;\r\n\tforeach (d; 0..limit)\r\n\t{\r\n\t\tif (sieve[d])\r\n\t\t{\r\n\t\t\tprimes ~= d;\r\n\t\t}\r\n\t}\r\n\r\n\tinv = new int [limit];\r\n\tforeach (d; 1..limit)\r\n\t{\r\n\t\tinv[d] = invMod (d);\r\n\t}\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tprepare ();\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\talias Edge = Tuple !(int, q{v}, int, q{x}, int, q{y});\r\n\t\tauto a = new Edge [] [n];\r\n\t\tforeach (r; 0..n - 1)\r\n\t\t{\r\n\t\t\tint i, j, x, y;\r\n\t\t\treadf !(\" %s %s %s %s\") (i, j, x, y);\r\n\t\t\ti -= 1;\r\n\t\t\tj -= 1;\r\n\t\t\tauto d = gcd (x, y);\r\n\t\t\tx /= d;\r\n\t\t\ty /= d;\r\n\t\t\ta[i] ~= Edge (j, x, y);\r\n\t\t\ta[j] ~= Edge (i, y, x);\r\n\t\t}\r\n\t\treadln;\r\n\r\n\t\tauto cp = new int [limit];\r\n\t\tint mult = 1;\r\n\t\tint total = 0;\r\n\r\n\t\tvoid recur (int u, int p, int cur)\r\n\t\t{\r\n\t\t\tdebug {writeln (u, \" \", cur);} \r\n\t\t\ttotal = (total + cur) % mod;\r\n\t\t\tforeach (e; a[u])\r\n\t\t\t{\r\n\t\t\t\tif (e.v == p)\r\n\t\t\t\t{\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\t\t\t\tdebug {writeln (\"to \", e);}\r\n\t\t\t\tint next = cur;\r\n\t\t\t\tfor (int x = e.x; divs[x] != 0; x /= divs[x])\r\n\t\t\t\t{\r\n\t\t\t\t\tnext = (next * 1L * inv[divs[x]]) % mod;\r\n\t\t\t\t\tif (cp[divs[x]] == 0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tdebug {writeln (\" * \", divs[x]);}\r\n\t\t\t\t\t\tmult = (mult * 1L * divs[x]) % mod;\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcp[divs[x]] -= 1;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tfor (int y = e.y; divs[y] != 0; y /= divs[y])\r\n\t\t\t\t{\r\n\t\t\t\t\tnext = (next * 1L * divs[y]) % mod;\r\n\t\t\t\t\tcp[divs[y]] += 1;\r\n\t\t\t\t}\r\n\t\t\t\trecur (e.v, u, next);\r\n\t\t\t\tfor (int x = e.x; divs[x] != 0; x /= divs[x])\r\n\t\t\t\t{\r\n\t\t\t\t\tcp[divs[x]] += 1;\r\n\t\t\t\t}\r\n\t\t\t\tfor (int y = e.y; divs[y] != 0; y /= divs[y])\r\n\t\t\t\t{\r\n\t\t\t\t\tcp[divs[y]] -= 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0, -1, 1);\r\n\t\twriteln ((total * 1L * mult) % mod);\r\n\t}\r\n}\r\n"}], "src_uid": "178222a468f37615ee260fc9d2944aec"} {"nl": {"description": "New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below. He lifts all the books above book x. He pushes book x out of the stack. He puts down the lifted books without changing their order. After reading book x, he puts book x on the top of the stack. He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?", "input_spec": "The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books. The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book. The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.", "output_spec": "Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.", "sample_inputs": ["3 5\n1 2 3\n1 3 2 3 1"], "sample_outputs": ["12"], "notes": "NoteHere's a picture depicting the example. Each vertical column presents the stacked books. "}, "positive_code": [{"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n int N, M; scanf(\"%d %d\\n\", &N, &M);\n auto W = readln.chomp.split(\" \").map!(to!int).array;\n auto B = readln.chomp.split(\" \").map!(to!int).array;\n\n foreach (ref b; B) b--;\n\n int[] ans;\n auto used = new bool[N];\n foreach (int i, b; B) {\n if (used[b]) continue;\n used[b] = true;\n ans ~= b;\n }\n foreach (int i; 0 .. N) {\n if (!used[i]) {\n ans ~= i;\n }\n }\n\n int total = 0;\n int[] prev = ans.dup;\n foreach (int day; 0 .. M) {\n auto key = B[day];\n auto split = -1;\n foreach (int i, p; prev) {\n if (key == p) {\n split = i;\n break;\n }\n total += W[p];\n }\n assert(split >= 0);\n prev = [key] ~ prev[0 .. split] ~ prev[split + 1 .. $];\n }\n writeln(total);\n}\n"}], "negative_code": [], "src_uid": "a18edcadb31f76e69968b0a3e1cb7e3e"} {"nl": {"description": "You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \\dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \\leq i \\leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$.Find out the non-negative remainder of division of $$$\\sum \\limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$.Note that the modulo is unusual!", "input_spec": "The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \\leq n \\leq 10^{9}, 1 \\leq a, b \\leq 10^{9}, 1 \\leq k \\leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.", "output_spec": "Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.", "sample_inputs": ["2 2 3 3\n+-+", "4 1 5 1\n-"], "sample_outputs": ["7", "999999228"], "notes": "NoteIn the first example:$$$(\\sum \\limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7In the second example:$$$(\\sum \\limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \\equiv 999999228 \\pmod{10^{9} + 9}$$$."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op)() const if (op == \"-\") { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const { return mixin(\"ModInt(this) \" ~ op ~ \"= a\"); }\n ModInt opBinaryRight(string op)(long a) const { return mixin(\"ModInt(a) \" ~ op ~ \"= this\"); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 9;\nalias Mint = ModInt!MO;\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readLong();\n const A = readLong();\n const B = readLong();\n const K = readInt();\n const S = readToken();\n \n assert((N + 1) % K == 0);\n const q = (N + 1) / K;\n const r = Mint(B) / Mint(A);\n const rK = r^^K;\n const rKSum = (rK.x == 1) ? Mint(q) : ((rK^^q - 1) / (rK - 1));\n \n Mint ans;\n Mint c = Mint(A)^^N;\n foreach (i; 0 .. K) {\n ans += ((S[i] == '-') ? -1 : +1) * c * rKSum;\n c *= r;\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nconst mod = 10^^9+9;\nalias mint = FactorRing!mod;\n\nvoid main()\n{\n int n, a, b, k; readV(n, a, b, k);\n string s; readV(s);\n\n auto t0 = mint(0);\n foreach (i; 0..k)\n t0 += mint(a).repeatedSquare(n-i) * mint(b).repeatedSquare(i) * (s[i] == '+' ? 1 : -1);\n\n auto c = (mint(b)/mint(a)).repeatedSquare(k), m = (n+1)/k;\n if (c == 1) {\n writeln(t0 * m);\n } else {\n writeln(t0 * (c.repeatedSquare(m)-1) / (c-1));\n }\n}\n\npure T repeatedSquare(alias pred = \"a * b\", T, U)(T a, U n)\n{\n return repeatedSquare!(pred, T, U)(a, n, T(1));\n}\n\npure T repeatedSquare(alias pred = \"a * b\", T, U)(T a, U n, T init)\n{\n import std.functional;\n alias predFun = binaryFun!pred;\n\n if (n == 0) return init;\n\n auto r = init;\n while (n > 0) {\n if (n&1) r = predFun(r, a);\n a = predFun(a, a);\n n >>= 1;\n }\n\n return r;\n}\n\nstruct FactorRing(int m, bool pos = false)\n{\n version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }\n alias FR = FactorRing!(m, pos);\n @property static init() { return FR(0); }\n @property int value() { return vi; }\n @property void value(int v) { vi = mod(v); }\n alias value this;\n\n this(int v) { vi = v; }\n this(int v, bool runMod) { vi = runMod ? mod(v) : v; }\n this(long v) { vi = mod(v); }\n\n ref auto opAssign(int v) { vi = v; return this; }\n\n pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }\n pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }\n\n static if (!pos) pure ref auto opUnary(string op: \"-\")() { return FR(mod(-vi)); }\n\n static if (m < int.max / 2) {\n pure ref auto opBinary(string op)(int r) if (op == \"+\" || op == \"-\") { return FR(mod(mixin(\"vi\"~op~\"r\"))); }\n ref auto opOpAssign(string op)(int r) if (op == \"+\" || op == \"-\") { vi = mod(mixin(\"vi\"~op~\"r\")); return this; }\n } else {\n pure ref auto opBinary(string op)(int r) if (op == \"+\" || op == \"-\") { return FR(mod(mixin(\"vl\"~op~\"r\"))); }\n ref auto opOpAssign(string op)(int r) if (op == \"+\" || op == \"-\") { vi = mod(mixin(\"vl\"~op~\"r\")); return this; }\n }\n pure ref auto opBinary(string op: \"*\")(int r) { return FR(mod(vl*r)); }\n ref auto opOpAssign(string op: \"*\")(int r) { vi = mod(vl*r); return this; }\n\n pure ref auto opBinary(string op)(ref FR r) if (op == \"+\" || op == \"-\" || op == \"*\") { return opBinary!op(r.vi); }\n ref auto opOpAssign(string op)(ref FR r) if (op == \"+\" || op == \"-\" || op == \"*\") { return opOpAssign!op(r.vi); }\n\n pure auto opBinary(string op: \"/\")(FR r) { return FR(mod(vl*r.inv.vi)); }\n pure auto opBinary(string op: \"/\")(int r) { return opBinary!op(FR(r)); }\n ref auto opOpAssign(string op: \"/\")(ref FR r) { vi = mod(vl*r.inv.vi); return this; }\n ref auto opOpAssign(string op: \"/\")(int r) { return opOpAssign!op(FR(r)); }\n\n pure auto inv()\n {\n int x = vi, a, b;\n exEuclid(x, m, a, b);\n return FR(mod(a));\n }\n}\n\npure T exEuclid(T)(T a, T b, ref T x, ref T y)\n{\n auto g = a;\n x = 1;\n y = 0;\n if (b) {\n g = exEuclid(b, a%b, y, x);\n y -= a/b*x;\n }\n return g;\n}\n"}], "negative_code": [{"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nconst mod = 10^^9+9;\nalias mint = FactorRing!mod;\n\nvoid main()\n{\n int n, a, b, k; readV(n, a, b, k);\n string s; readV(s);\n\n auto t0 = mint(0);\n foreach (i; 0..k)\n t0 += mint(a).repeatedSquare(n-i) * mint(b).repeatedSquare(i) * (s[i] == '+' ? 1 : -1);\n\n auto c = mint(b)/mint(a);\n if (c == 1) {\n writeln(t0 * (n+1) / k);\n } else {\n writeln(t0 * (c.repeatedSquare(n+1)-1) / (c.repeatedSquare(k)-1));\n }\n}\n\npure T repeatedSquare(alias pred = \"a * b\", T, U)(T a, U n)\n{\n return repeatedSquare!(pred, T, U)(a, n, T(1));\n}\n\npure T repeatedSquare(alias pred = \"a * b\", T, U)(T a, U n, T init)\n{\n import std.functional;\n alias predFun = binaryFun!pred;\n\n if (n == 0) return init;\n\n auto r = init;\n while (n > 0) {\n if (n&1) r = predFun(r, a);\n a = predFun(a, a);\n n >>= 1;\n }\n\n return r;\n}\n\nstruct FactorRing(int m, bool pos = false)\n{\n version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }\n alias FR = FactorRing!(m, pos);\n @property static init() { return FR(0); }\n @property int value() { return vi; }\n @property void value(int v) { vi = mod(v); }\n alias value this;\n\n this(int v) { vi = v; }\n this(int v, bool runMod) { vi = runMod ? mod(v) : v; }\n this(long v) { vi = mod(v); }\n\n ref auto opAssign(int v) { vi = v; return this; }\n\n pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }\n pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }\n\n static if (!pos) pure ref auto opUnary(string op: \"-\")() { return FR(mod(-vi)); }\n\n static if (m < int.max / 2) {\n pure ref auto opBinary(string op)(int r) if (op == \"+\" || op == \"-\") { return FR(mod(mixin(\"vi\"~op~\"r\"))); }\n ref auto opOpAssign(string op)(int r) if (op == \"+\" || op == \"-\") { vi = mod(mixin(\"vi\"~op~\"r\")); return this; }\n } else {\n pure ref auto opBinary(string op)(int r) if (op == \"+\" || op == \"-\") { return FR(mod(mixin(\"vl\"~op~\"r\"))); }\n ref auto opOpAssign(string op)(int r) if (op == \"+\" || op == \"-\") { vi = mod(mixin(\"vl\"~op~\"r\")); return this; }\n }\n pure ref auto opBinary(string op: \"*\")(int r) { return FR(mod(vl*r)); }\n ref auto opOpAssign(string op: \"*\")(int r) { vi = mod(vl*r); return this; }\n\n pure ref auto opBinary(string op)(ref FR r) if (op == \"+\" || op == \"-\" || op == \"*\") { return opBinary!op(r.vi); }\n ref auto opOpAssign(string op)(ref FR r) if (op == \"+\" || op == \"-\" || op == \"*\") { return opOpAssign!op(r.vi); }\n\n pure auto opBinary(string op: \"/\")(FR r) { return FR(mod(vl*r.inv.vi)); }\n pure auto opBinary(string op: \"/\")(int r) { return opBinary!op(FR(r)); }\n ref auto opOpAssign(string op: \"/\")(ref FR r) { vi = mod(vl*r.inv.vi); return this; }\n ref auto opOpAssign(string op: \"/\")(int r) { return opOpAssign!op(FR(r)); }\n\n pure auto inv()\n {\n int x = vi, a, b;\n exEuclid(x, m, a, b);\n return FR(mod(a));\n }\n}\n\npure T exEuclid(T)(T a, T b, ref T x, ref T y)\n{\n auto g = a;\n x = 1;\n y = 0;\n if (b) {\n g = exEuclid(b, a%b, y, x);\n y -= a/b*x;\n }\n return g;\n}\n"}, {"source_code": "import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;\n\nauto rdsp(){return readln.splitter;}\nvoid pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}\nvoid readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}\n\nconst mod = 10^^9+9;\nalias mint = FactorRing!mod;\n\nvoid main()\n{\n int n, a, b, k; readV(n, a, b, k);\n string s; readV(s);\n\n auto t0 = mint(0);\n foreach (i; 0..k)\n t0 += mint(a).repeatedSquare(n-i) * mint(b).repeatedSquare(i) * (s[i] == '+' ? 1 : -1);\n\n if (a == 16665164) {\n writeln(t0);\n }\n\n auto c = mint(b)/mint(a);\n if (c == 1) {\n writeln(t0 * (n+1) / k);\n } else {\n writeln(t0 * (c.repeatedSquare(n+1)-1) / (c.repeatedSquare(k)-1));\n }\n}\n\npure T repeatedSquare(alias pred = \"a * b\", T, U)(T a, U n)\n{\n return repeatedSquare!(pred, T, U)(a, n, T(1));\n}\n\npure T repeatedSquare(alias pred = \"a * b\", T, U)(T a, U n, T init)\n{\n import std.functional;\n alias predFun = binaryFun!pred;\n\n if (n == 0) return init;\n\n auto r = init;\n while (n > 0) {\n if (n&1) r = predFun(r, a);\n a = predFun(a, a);\n n >>= 1;\n }\n\n return r;\n}\n\nstruct FactorRing(int m, bool pos = false)\n{\n version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }\n alias FR = FactorRing!(m, pos);\n @property static init() { return FR(0); }\n @property int value() { return vi; }\n @property void value(int v) { vi = mod(v); }\n alias value this;\n\n this(int v) { vi = v; }\n this(int v, bool runMod) { vi = runMod ? mod(v) : v; }\n this(long v) { vi = mod(v); }\n\n ref auto opAssign(int v) { vi = v; return this; }\n\n pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }\n pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }\n\n static if (!pos) pure ref auto opUnary(string op: \"-\")() { return FR(mod(-vi)); }\n\n static if (m < int.max / 2) {\n pure ref auto opBinary(string op)(int r) if (op == \"+\" || op == \"-\") { return FR(mod(mixin(\"vi\"~op~\"r\"))); }\n ref auto opOpAssign(string op)(int r) if (op == \"+\" || op == \"-\") { vi = mod(mixin(\"vi\"~op~\"r\")); return this; }\n } else {\n pure ref auto opBinary(string op)(int r) if (op == \"+\" || op == \"-\") { return FR(mod(mixin(\"vl\"~op~\"r\"))); }\n ref auto opOpAssign(string op)(int r) if (op == \"+\" || op == \"-\") { vi = mod(mixin(\"vl\"~op~\"r\")); return this; }\n }\n pure ref auto opBinary(string op: \"*\")(int r) { return FR(mod(vl*r)); }\n ref auto opOpAssign(string op: \"*\")(int r) { vi = mod(vl*r); return this; }\n\n pure ref auto opBinary(string op)(ref FR r) if (op == \"+\" || op == \"-\" || op == \"*\") { return opBinary!op(r.vi); }\n ref auto opOpAssign(string op)(ref FR r) if (op == \"+\" || op == \"-\" || op == \"*\") { return opOpAssign!op(r.vi); }\n\n pure auto opBinary(string op: \"/\")(FR r) { return FR(mod(vl*r.inv.vi)); }\n pure auto opBinary(string op: \"/\")(int r) { return opBinary!op(FR(r)); }\n ref auto opOpAssign(string op: \"/\")(ref FR r) { vi = mod(vl*r.inv.vi); return this; }\n ref auto opOpAssign(string op: \"/\")(int r) { return opOpAssign!op(FR(r)); }\n\n pure auto inv()\n {\n int x = vi, a, b;\n exEuclid(x, m, a, b);\n return FR(mod(a));\n }\n}\n\npure T exEuclid(T)(T a, T b, ref T x, ref T y)\n{\n auto g = a;\n x = 1;\n y = 0;\n if (b) {\n g = exEuclid(b, a%b, y, x);\n y -= a/b*x;\n }\n return g;\n}\n"}], "src_uid": "607e670403a40e4fddf389caba79607e"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers.Let instability of the array be the following value: $$$\\max\\limits_{i = 1}^{n} a_i - \\min\\limits_{i = 1}^{n} a_i$$$.You have to remove exactly one element from this array to minimize instability of the resulting $$$(n-1)$$$-elements array. Your task is to calculate the minimum possible instability.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$) — elements of the array $$$a$$$.", "output_spec": "Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array $$$a$$$.", "sample_inputs": ["4\n1 3 3 7", "2\n1 100000"], "sample_outputs": ["2", "0"], "notes": "NoteIn the first example you can remove $$$7$$$ then instability of the remaining array will be $$$3 - 1 = 2$$$.In the second example you can remove either $$$1$$$ or $$$100000$$$ then instability of the remaining array will be $$$100000 - 100000 = 0$$$ and $$$1 - 1 = 0$$$ correspondingly."}, "positive_code": [{"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n int n;\n readf!\"%d\"(n);\n readln;\n auto a=readln.splitter\n .map!(to!int)\n .array;\n sort(a);\n writeln(min(a[$-2]-a[0],a[$-1]-a[1]));\n}\n\n"}], "negative_code": [{"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n int n;\n readf!\"%d\"(n);\n readln;\n auto a=readln.splitter\n .map!(to!int)\n .array;\n sort(a);\n writeln(min(a[$-2]-a[0],a[$-1]-a[2]));\n}\n\n"}, {"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n int n;\n readf!\"%d\"(n);\n readln;\n auto a=readln.splitter\n .map!(to!int)\n .array;\n sort(a);\n writeln(a[$-2]-a[0]);\n}\n\n"}], "src_uid": "2eb7234904b28b4793b7c482a2370092"} {"nl": {"description": "You are given an array $$$a_{1}, a_{2}, \\ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$) and delete integers $$$a_l, a_{l+1}, \\ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \\ldots, a_{n}$$$ ($$$1 \\le a_{i} \\le 10^{9}$$$) — the elements of the array. ", "output_spec": "Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.", "sample_inputs": ["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"], "sample_outputs": ["0", "2", "2"], "notes": "NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\tlong[] as;\n\tforeach(i; 0 .. n) as ~= read.to!long;\n\t\n\tint[][long] am;\n\tforeach(int i, a; as){\n\t\tif(a !in am) am[a] = [];\n\t\tam[a] ~= i;\n\t}\n\t\n\tlong[] amks = am.keys;\n\t\n\tforeach(a; amks) am[a].sort();\n\tlog(\"am:\", am);\n\t\n\tbool f(int x){\n\t\tforeach(l; 0 .. n - x + 1){\n\t\t\tint r = l + x - 1;\n\t\t\tbool isOK = 1;\n\t\t\tforeach(a; amks){\n\t\t\t\tif(am[a].length <= 1) continue;\n\t\t\t\tif(am[a][0] < l && am[a][$ - 1] > r) isOK = 0;\n\t\t\t\tif(am[a][1] < l || am[a][$ - 2] > r) isOK = 0;\n\t\t\t}\n\t\t\tif(isOK) return 0;\n\t\t}\n\t\treturn 1;\n\t}\n\t\n\tint d = uplimit(0, n - 1, &f);\n\t\n\t(d + 1).writeln;\n\treturn;\n\t\n}\n\n\n// a <= x <= c の中でfをみたす最大(二分探索; binary search) ※テンプレート版\nT uplimit(T)(T a, T c, bool delegate(T) f){\n\tif(f(c)) return c;\n\tif(! f(a)) return a - 1;\n\twhile(a + 1 < c){\n\t\tT b = (a + c) / 2;\n\t\tif(f(b)) a = b;\n\t\telse c = b;\n\t}\n\treturn a;\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nint[] A;\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n int ans = N - 1;\n foreach (l; 0 .. N + 1) {\n auto set = new RedBlackTree!int;\n bool ok = true;\n foreach (i; 0 .. l) {\n ok = ok && set.insert(A[i]);\n }\n if (ok) {\n chmin(ans, N - l);\n foreach_reverse (i; l .. N) {\n if (set.insert(A[i])) {\n chmin(ans, i - l);\n } else {\n break;\n }\n }\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\tlong[] as;\n\tforeach(i; 0 .. n) as ~= read.to!long;\n\t\n\tint[][long] am;\n\tforeach(int i, a; as){\n\t\tif(a !in am) am[a] = [];\n\t\tam[a] ~= i;\n\t}\n\t\n\tlong[] amks = am.keys;\n\t\n\tforeach(a; amks) am[a].sort();\n\tlog(\"am:\", am);\n\t\n\tbool f(int x){\n\t\tforeach(l; 0 .. n - x){\n\t\t\tint r = l + x - 1;\n\t\t\tbool isOK = 1;\n\t\t\tforeach(a; amks){\n\t\t\t\tif(am[a].length <= 1) continue;\n\t\t\t\tif(am[a][0] < l && am[a][$ - 1] > r) isOK = 0;\n\t\t\t\tif(am[a][1] < l || am[a][$ - 2] > r) isOK = 0;\n\t\t\t}\n\t\t\tif(isOK) return 0;\n\t\t}\n\t\treturn 1;\n\t}\n\t\n\tint d = uplimit(0, n - 1, &f);\n\t\n\t(d + 1).writeln;\n\treturn;\n\t\n}\n\n\n// a <= x <= c の中でfをみたす最大(二分探索; binary search) ※テンプレート版\nT uplimit(T)(T a, T c, bool delegate(T) f){\n\tif(f(c)) return c;\n\tif(! f(a)) return a - 1;\n\twhile(a + 1 < c){\n\t\tT b = (a + c) / 2;\n\t\tif(f(b)) a = b;\n\t\telse c = b;\n\t}\n\treturn a;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\tlong[] as;\n\tforeach(i; 0 .. n) as ~= read.to!long;\n\t\n\tint[][long] am;\n\tforeach(int i, a; as){\n\t\tif(a !in am) am[a] = [];\n\t\tam[a] ~= i;\n\t}\n\t\n\tlong[] amks = am.keys;\n\tif(amks.length == 0){\n\t\t\"0\".writeln;\n\t\treturn;\n\t}\n\tif(amks.length == 1){\n\t\t\"1\".writeln;\n\t\treturn;\n\t}\n\t\n\tforeach(a; amks) am[a].sort();\n\t\n\tbool f(int x){\n\t\tforeach(l; 0 .. n - x){\n\t\t\tint r = l + x - 1;\n\t\t\tbool isOK = 1;\n\t\t\tforeach(a; amks){\n\t\t\t\tif(am[a].length <= 1) continue;\n\t\t\t\tif(am[a][0] < l && am[a][$ - 1] > r) isOK = 0;\n\t\t\t\tif(am[a][1] < l || am[a][$ - 2] > r) isOK = 0;\n\t\t\t}\n\t\t\tif(isOK) return 0;\n\t\t}\n\t\treturn 1;\n\t}\n\t\n\tint d = uplimit(0, n - 1, &f);\n\t\n\t(d + 1).writeln;\n\treturn;\n\t\n}\n\n\n// a <= x <= c の中でfをみたす最大(二分探索; binary search) ※テンプレート版\nT uplimit(T)(T a, T c, bool delegate(T) f){\n\tif(f(c)) return c;\n\tif(! f(a)) return a - 1;\n\twhile(a + 1 < c){\n\t\tT b = (a + c) / 2;\n\t\tif(f(b)) a = b;\n\t\telse c = b;\n\t}\n\treturn a;\n}\n"}], "src_uid": "9873a576d00630fa6e5fd4448a1a65ad"} {"nl": {"description": "There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.", "input_spec": "The first line contains single integer n (2 ≤ n ≤ 2·105) — the number of groups. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 2), where ai is the number of people in group i.", "output_spec": "Print the maximum number of teams of three people the coach can form.", "sample_inputs": ["4\n1 1 2 1", "2\n2 2", "7\n2 2 2 1 1 1 1", "3\n1 1 1"], "sample_outputs": ["1", "0", "3", "1"], "notes": "NoteIn the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.In the second example he can't make a single team.In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person). "}, "positive_code": [{"source_code": "import std.stdio, std.string, std.conv, std.algorithm, std.numeric;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\n\nvoid main() {\n int n;\n scan(n);\n auto a = readln.split.to!(int[]);\n\n int o, t;\n foreach (ai ; a) {\n if (ai == 1) o++;\n else t++;\n }\n\n int ans;\n\n if (t <= o) {\n ans += t;\n o -= t;\n\n ans += o / 3;\n }\n else {\n ans += o;\n }\n\n writeln(ans);\n}\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}"}], "negative_code": [], "src_uid": "6c9cbe714f8f594654ebc59b6059b30a"} {"nl": {"description": "In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i + 1) if the type of this bumper is '>', or one position to the left (to i - 1) if the type of the bumper at position i is '<'. If there is no such position, in other words if i - 1 < 1 or i + 1 > n, the ball falls from the game field.Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.", "output_spec": "Print one integer — the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.", "sample_inputs": ["4\n<<><", "5\n>>>>>", "4\n>><<"], "sample_outputs": ["2", "5", "0"], "notes": "NoteIn the first sample, the ball will fall from the field if starts at position 1 or position 2.In the second sample, any starting position will result in the ball falling from the field."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto s = readln.strip;\n\t\tlong res = 0;\n\t\twhile (!s.empty && s[0] == '<')\n\t\t{\n\t\t\ts = s[1..$];\n\t\t\tres += 1;\n\t\t}\n\t\twhile (!s.empty && s[$ - 1] == '>')\n\t\t{\n\t\t\ts = s[0..$ - 1];\n\t\t\tres += 1;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "6b4242ae9a52d36548dda79d93fe0aef"} {"nl": {"description": "In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$.A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel.You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le k \\le \\min(2 \\cdot 10^5, \\frac{n \\cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \\dots, r_n$$$ $$$(1 \\le r_i \\le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \\le x, y \\le n$$$, $$$x \\ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.", "output_spec": "Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.", "sample_inputs": ["4 2\n10 4 10 15\n1 2\n4 3", "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5"], "sample_outputs": ["0 0 1 2", "5 4 0 5 3 3 9 0 2 5"], "notes": "NoteIn the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.range;\nimport std.stdio;\n\nvoid main ()\n{\n\tint n, k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\treadln;\n\t\tauto r = readln.splitter.map !(to !(int)).array;\n\t\tauto p = n.iota.array;\n\t\tp.schwartzSort !(i => r[i]);\n\t\tauto q = new int [n];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tq[p[i]] = i;\n\t\t}\n\n\t\tauto a = new int [] [n];\n\t\tforeach (j; 0..k)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf (\" %s %s\", &u, &v);\n\t\t\tu -= 1;\n\t\t\tv -= 1;\n\t\t\ta[u] ~= v;\n\t\t\ta[v] ~= u;\n\t\t}\n\n\t\tauto ans = new int [n];\n\t\tint x = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint v = p[i];\n\t\t\twhile (r[p[x]] < r[v])\n\t\t\t{\n\t\t\t\tx += 1;\n\t\t\t}\n\t\t\tdebug {writeln (i, \" \", x);}\n\t\t\tint res = x;\n\t\t\tforeach (u; a[v])\n\t\t\t{\n\t\t\t\tif (q[u] < x)\n\t\t\t\t{\n\t\t\t\t\tres -= 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans[v] = res;\n\t\t}\n\t\twritefln (\"%(%s %)\", ans);\n\t}\n}\n"}, {"source_code": "void main(){\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int n, m; rd(n, m);\n auto a=readln.split.to!(int[]);\n auto g=new int[][](n);\n foreach(_; 0..m){\n int u, v; rd(u, v);\n g[u-1]~=(v-1);\n g[v-1]~=(u-1);\n }\n\n int[long] freq;\n\n struct T{int val, idx;}\n auto data=new T[](n);\n foreach(i; 0..n) data[i]=T(a[i], i);\n sort!\"a.val s.to!int - 1).array;\r\n auto L = new long[N], R = new long[N];\r\n foreach (i; 0 .. N) {\r\n readf(\"%d %d\\n\", &L[i], &R[i]);\r\n }\r\n\r\n auto G = new int[][N];\r\n for (int i = 0; i < N; i++) {\r\n if (P[i] >= 0) {\r\n G[P[i]] ~= i;\r\n }\r\n }\r\n\r\n T f(int v) {\r\n if (G[v].empty) {\r\n return T(R[v], 1);\r\n } else {\r\n T s = T(0, 0);\r\n foreach (c; G[v]) {\r\n auto r = f(c);\r\n s[0] += r[0];\r\n s[1] += r[1];\r\n }\r\n if (s[0] >= L[v]) {\r\n s[0] = min(R[v], s[0]);\r\n } else {\r\n s[0] = R[v];\r\n s[1] += 1;\r\n }\r\n return s;\r\n }\r\n }\r\n\r\n writeln(f(0)[1]);\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto p = readln.splitter.map !(to !(int)).array;\r\n\t\tauto lo = new int [n];\r\n\t\tauto hi = new int [n];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\treadf !(\" %s %s\") (lo[i], hi[i]);\r\n\t\t}\r\n\t\treadln;\r\n\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..n - 1)\r\n\t\t{\r\n\t\t\tadj[p[j] - 1] ~= j + 1;\r\n\t\t}\r\n\r\n\t\tauto sat = new long [n];\r\n\t\tint res = 0;\r\n\r\n\t\tvoid recur (int v)\r\n\t\t{\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u);\r\n\t\t\t\tsat[v] += sat[u];\r\n\t\t\t}\r\n\t\t\tsat[v] = min (sat[v], hi[v]);\r\n\t\t\tif (sat[v] < lo[v])\r\n\t\t\t{\r\n\t\t\t\tsat[v] = hi[v];\r\n\t\t\t\tres += 1;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0);\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nint[] P;\nlong[] L, R;\n\nint[][] graph;\n\nalias Result = Tuple!(long, \"val\", int, \"cost\");\n\nResult solve(int u) {\n long sum;\n int cost;\n foreach (v; graph[u]) {\n const res = solve(v);\n sum += res.val;\n cost += res.cost;\n }\n Result ret;\n if (L[u] <= sum) {\n ret.val = min(sum, R[u]);\n ret.cost = cost;\n } else {\n ret.val = R[u];\n ret.cost = cost + 1;\n }\n debug {\n writeln(u, \": \", ret);\n }\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n N = readInt;\n P = new int[N];\n P[0] = -1;\n foreach (u; 1 .. N) {\n P[u] = readInt - 1;\n }\n L = new long[N];\n R = new long[N];\n foreach (u; 0 .. N) {\n L[u] = readLong;\n R[u] = readLong;\n }\n \n graph = new int[][N];\n foreach (u; 1 .. N) {\n graph[P[u]] ~= u;\n }\n \n const ans = solve(0);\n writeln(ans.cost);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nalias T = Tuple!(long, int);\r\n\r\nvoid solve() {\r\n int N = readln.chomp.to!int;\r\n auto P = [-1] ~ readln.chomp.split(\" \").map!(s => s.to!int - 1).array;\r\n auto L = new long[N], R = new long[N];\r\n foreach (i; 0 .. N) {\r\n readf(\"%d %d\\n\", &L[i], &R[i]);\r\n }\r\n\r\n auto G = new int[][N];\r\n for (int i = 0; i < N; i++) {\r\n if (P[i] >= 0) {\r\n G[P[i]] ~= i;\r\n }\r\n }\r\n\r\n T f(int v) {\r\n if (G[v].empty) {\r\n return T(R[v], 1);\r\n } else {\r\n T s = T(0, 0);\r\n foreach (c; G[v]) {\r\n auto r = f(c);\r\n s[0] += r[0];\r\n s[1] += r[1];\r\n }\r\n if (s[0] >= L[v]) {\r\n s[0] = max(R[v], s[0]);\r\n } else {\r\n s[0] = R[v];\r\n s[1] += 1;\r\n }\r\n return s;\r\n }\r\n }\r\n\r\n writeln(f(0)[1]);\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto p = readln.splitter.map !(to !(int)).array;\r\n\t\tauto lo = new int [n];\r\n\t\tauto hi = new int [n];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\treadf !(\" %s %s\") (lo[i], hi[i]);\r\n\t\t}\r\n\t\treadln;\r\n\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..n - 1)\r\n\t\t{\r\n\t\t\tadj[p[j] - 1] ~= j + 1;\r\n\t\t}\r\n\r\n\t\tauto sat = new long [n];\r\n\t\tint res = 0;\r\n\r\n\t\tvoid recur (int v)\r\n\t\t{\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u);\r\n\t\t\t\tsat[v] += sat[u];\r\n\t\t\t}\r\n\t\t\tif (sat[v] < lo[v])\r\n\t\t\t{\r\n\t\t\t\tsat[v] = hi[v];\r\n\t\t\t\tres += 1;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0);\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "src_uid": "130fdf010c228564611a380b6dd37a34"} {"nl": {"description": "This is the harder version of the problem. In this version, $$$1 \\le n, m \\le 2\\cdot10^5$$$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems.You are given a sequence of integers $$$a=[a_1,a_2,\\dots,a_n]$$$ of length $$$n$$$. Its subsequence is obtained by removing zero or more elements from the sequence $$$a$$$ (they do not necessarily go consecutively). For example, for the sequence $$$a=[11,20,11,33,11,20,11]$$$: $$$[11,20,11,33,11,20,11]$$$, $$$[11,20,11,33,11,20]$$$, $$$[11,11,11,11]$$$, $$$[20]$$$, $$$[33,20]$$$ are subsequences (these are just some of the long list); $$$[40]$$$, $$$[33,33]$$$, $$$[33,20,20]$$$, $$$[20,20,11,11]$$$ are not subsequences. Suppose that an additional non-negative integer $$$k$$$ ($$$1 \\le k \\le n$$$) is given, then the subsequence is called optimal if: it has a length of $$$k$$$ and the sum of its elements is the maximum possible among all subsequences of length $$$k$$$; and among all subsequences of length $$$k$$$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $$$b=[b_1, b_2, \\dots, b_k]$$$ is lexicographically smaller than the sequence $$$c=[c_1, c_2, \\dots, c_k]$$$ if the first element (from the left) in which they differ less in the sequence $$$b$$$ than in $$$c$$$. Formally: there exists $$$t$$$ ($$$1 \\le t \\le k$$$) such that $$$b_1=c_1$$$, $$$b_2=c_2$$$, ..., $$$b_{t-1}=c_{t-1}$$$ and at the same time $$$b_t<c_t$$$. For example: $$$[10, 20, 20]$$$ lexicographically less than $$$[10, 21, 1]$$$, $$$[7, 99, 99]$$$ is lexicographically less than $$$[10, 21, 1]$$$, $$$[10, 21, 0]$$$ is lexicographically less than $$$[10, 21, 1]$$$. You are given a sequence of $$$a=[a_1,a_2,\\dots,a_n]$$$ and $$$m$$$ requests, each consisting of two numbers $$$k_j$$$ and $$$pos_j$$$ ($$$1 \\le k \\le n$$$, $$$1 \\le pos_j \\le k_j$$$). For each query, print the value that is in the index $$$pos_j$$$ of the optimal subsequence of the given sequence $$$a$$$ for $$$k=k_j$$$.For example, if $$$n=4$$$, $$$a=[10,20,30,20]$$$, $$$k_j=2$$$, then the optimal subsequence is $$$[20,30]$$$ — it is the minimum lexicographically among all subsequences of length $$$2$$$ with the maximum total sum of items. Thus, the answer to the request $$$k_j=2$$$, $$$pos_j=1$$$ is the number $$$20$$$, and the answer to the request $$$k_j=2$$$, $$$pos_j=2$$$ is the number $$$30$$$.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 2\\cdot10^5$$$) — the length of the sequence $$$a$$$. The second line contains elements of the sequence $$$a$$$: integer numbers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). The third line contains an integer $$$m$$$ ($$$1 \\le m \\le 2\\cdot10^5$$$) — the number of requests. The following $$$m$$$ lines contain pairs of integers $$$k_j$$$ and $$$pos_j$$$ ($$$1 \\le k \\le n$$$, $$$1 \\le pos_j \\le k_j$$$) — the requests.", "output_spec": "Print $$$m$$$ integers $$$r_1, r_2, \\dots, r_m$$$ ($$$1 \\le r_j \\le 10^9$$$) one per line: answers to the requests in the order they appear in the input. The value of $$$r_j$$$ should be equal to the value contained in the position $$$pos_j$$$ of the optimal subsequence for $$$k=k_j$$$.", "sample_inputs": ["3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3", "7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4"], "sample_outputs": ["20\n10\n20\n10\n20\n10", "2\n3\n2\n3\n2\n3\n1\n1\n3"], "notes": "NoteIn the first example, for $$$a=[10,20,10]$$$ the optimal subsequences are: for $$$k=1$$$: $$$[20]$$$, for $$$k=2$$$: $$$[10,20]$$$, for $$$k=3$$$: $$$[10,20,10]$$$. "}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nvoid bAdd(T)(T[] bit, int pos, T val)\nin {\n assert(0 <= pos && pos < bit.length, \"bAdd: 0 <= pos < |bit| must hold\");\n}\ndo {\n for (int x = pos; x < bit.length; x |= x + 1) {\n bit[x] += val;\n }\n}\n\n// sum of [0, pos)\nT bSum(T)(T[] bit, int pos)\nin {\n assert(0 <= pos && pos <= bit.length, \"bSum: 0 <= pos <= |bit| must hold\");\n}\ndo {\n T sum = 0;\n for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {\n sum += bit[x];\n }\n return sum;\n}\n\n// min pos s.t. pred(sum of [0, pos))\n// assume pred(sum of [0, pos)) is non-decreasing\nint bBinarySearch(alias pred, T)(T[] bit) {\n import core.bitop : bsr;\n import std.functional : unaryFun;\n alias predFun = unaryFun!pred;\n if (predFun(0)) return 0;\n int pos = 0;\n T sum = 0;\n foreach_reverse (e; 0 .. bsr(bit.length) + 1) {\n const x = (pos | 1 << e) - 1;\n if (x < bit.length && !predFun(sum + bit[x])) {\n pos |= 1 << e;\n sum += bit[x];\n }\n }\n return pos + 1;\n}\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n const M = readInt();\n auto K = new int[M];\n auto P = new int[M];\n foreach (m; 0 .. M) {\n K[m] = readInt();\n P[m] = readInt();\n }\n \n auto queries = new int[][N + 1];\n foreach (m; 0 .. M) {\n queries[K[m]] ~= m;\n }\n auto ans = new int[M];\n \n auto as = new Tuple!(int, int)[N];\n foreach (i; 0 .. N) {\n as[i] = tuple(A[i], i);\n }\n as.sort!((a, b) => ((a[0] != b[0]) ? (a[0] > b[0]) : (a[1] < b[1])));\n \n auto bit = new int[N];\n foreach (i; 0 .. N) {\n bit.bAdd(as[i][1], 1);\n foreach (m; queries[i + 1]) {\n const res = bit.bBinarySearch!(s => (s >= P[m]));\n ans[m] = A[res - 1];\n }\n }\n \n foreach (m; 0 .. M) {\n writeln(ans[m]);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "082eec813f870357dbe3c5abec6a2b52"} {"nl": {"description": "Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.", "input_spec": "The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000). Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell. It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.", "output_spec": "Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109 + 7.", "sample_inputs": ["3 4 2\n2 2\n2 3", "100 100 3\n15 16\n16 15\n99 88"], "sample_outputs": ["2", "545732279"], "notes": null}, "positive_code": [{"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int MOD = 1_000_000_007;\n\nint powmod (int a, int b)\n{\n\tint res = 1;\n\tfor ( ; b; b >>= 1)\n\t{\n\t\tif (b & 1)\n\t\t{\n\t\t\tres = (res * 1L * a) % MOD;\n\t\t}\n\t\ta = (a * 1L * a) % MOD;\n\t}\n\treturn res;\n}\n\nvoid main ()\n{\n\timmutable int MAX_N = 200_009;\n\n\tauto fact = new int [MAX_N];\n\tfact[0] = 1;\n\tforeach (i; 1..MAX_N)\n\t{\n\t\tfact[i] = (fact[i - 1] * 1L * i) % MOD;\n\t}\n\n\tauto invf = new int [MAX_N];\n\tforeach (i; 0..MAX_N)\n\t{\n\t\tinvf[i] = powmod (fact[i], MOD - 2);\n\t}\n\n\tint ways (int r, int c)\n\t{\n\t\tint res = fact[r + c];\n\t\tres = (res * 1L * invf[r]) % MOD;\n\t\tres = (res * 1L * invf[c]) % MOD;\n\t\treturn res;\n\t}\n\n\tint h, w, n;\n\twhile (readf (\" %s %s %s\", &h, &w, &n) > 0)\n\t{\n\t\talias Cell = Tuple !(int, \"r\", int, \"c\");\n\t\tauto a = new Cell [n];\n\t\tforeach (ref e; a)\n\t\t{\n\t\t\treadf (\" %s %s\", &e.r, &e.c);\n\t\t}\n\t\ta ~= Cell (1, 1);\n\t\ta ~= Cell (h, w);\n\t\tsort !(q{a < b}, SwapStrategy.stable) (a);\n\t\tassert (a[0] == Cell (1, 1));\n\t\tassert (a[n + 1] == Cell (h, w));\n\n\t\tauto f = new int [2] [n + 2];\n\t\tf[0][1] = 1;\n\t\tforeach (i; 1..n + 2)\n\t\t{\n\t\t\tforeach (j; 0..i)\n\t\t\t{\n\t\t\t\tint dr = a[i].r - a[j].r;\n\t\t\t\tint dc = a[i].c - a[j].c;\n\t\t\t\tif (dr < 0 || dc < 0)\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tforeach (k; 0..2)\n\t\t\t\t{\n\t\t\t\t\tf[i][k] = (f[i][k] + f[j][!k] * 1L *\n\t\t\t\t\t ways (dr, dc)) % MOD;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\twriteln ((f[n + 1][0] - f[n + 1][1] + MOD) % MOD);\n\t}\n}\n"}], "negative_code": [], "src_uid": "91749edcc396819d4172d06e2744b20b"} {"nl": {"description": "You are given an array $$$A$$$ of length $$$N$$$ weights of masses $$$A_1$$$, $$$A_2$$$...$$$A_N$$$. No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order $$$A_1$$$,...,$$$A_N$$$. There is also a string $$$S$$$ consisting of characters \"L\" and \"R\", meaning that after putting the $$$i-th$$$ weight (not $$$A_i$$$, but $$$i-th$$$ weight of your choice) left or right side of the balance should be heavier. Find the order of putting the weights on the balance such that rules of string $$$S$$$ are satisfied. ", "input_spec": "The first line contains one integer $$$N$$$ ($$$1 \\leq N \\leq 2*10^5$$$) - the length of the array $$$A$$$ The second line contains $$$N$$$ distinct integers: $$$A_1$$$, $$$A_2$$$,...,$$$A_N$$$ ($$$1 \\leq A_i \\leq 10^9$$$) - the weights given The third line contains string $$$S$$$ of length $$$N$$$ consisting only of letters \"L\" and \"R\" - string determining which side of the balance should be heavier after putting the $$$i-th$$$ weight of your choice", "output_spec": "The output contains $$$N$$$ lines. In every line, you should print one integer and one letter - integer representing the weight you are putting on the balance in that move and the letter representing the side of the balance where you are putting the weight. If there is no solution, print $$$-1$$$.", "sample_inputs": ["5\n3 8 2 13 7\nLLRLL"], "sample_outputs": ["3 L\n2 R\n8 R\n13 L\n7 L"], "notes": "NoteExplanation for the test case:  after the 1st weight: 3 L (left side is heavier)after the 2nd weight: 2 R (left side is heavier)after the 3rd weight: 8 R (right side is heavier)after the 4th weight: 13 L (left side is heavier)after the 5th weight: 7 L (left side is heavier)So, the rules given by string $$$S$$$ are fulfilled and our order of putting the weights is correct."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n A.sort;\n const S = readToken();\n \n auto cs = new char[N];\n cs[N - 1] = S[N - 1];\n foreach_reverse (i; 0 .. N - 1) {\n cs[i] = 'L' ^ 'R' ^ cs[i + 1];\n }\n \n int cnt;\n char last = '@';\n foreach (s; S) {\n if (last != s) {\n ++cnt;\n }\n last = s;\n }\n \n int l = N - cnt, r = N - cnt;\n last = '@';\n foreach (s; S) {\n int i;\n if (last != s) {\n i = r++;\n } else {\n i = --l;\n }\n writeln(A[i], \" \", cs[i]);\n last = s;\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "c03face2b6e5736a401ebf61339fac3c"} {"nl": {"description": "You have a set of $$$n$$$ discs, the $$$i$$$-th disc has radius $$$i$$$. Initially, these discs are split among $$$m$$$ towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top.You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $$$i$$$ and $$$j$$$ (each containing at least one disc), take several (possibly all) top discs from the tower $$$i$$$ and put them on top of the tower $$$j$$$ in the same order, as long as the top disc of tower $$$j$$$ is bigger than each of the discs you move. You may perform this operation any number of times.For example, if you have two towers containing discs $$$[6, 4, 2, 1]$$$ and $$$[8, 7, 5, 3]$$$ (in order from bottom to top), there are only two possible operations: move disc $$$1$$$ from the first tower to the second tower, so the towers are $$$[6, 4, 2]$$$ and $$$[8, 7, 5, 3, 1]$$$; move discs $$$[2, 1]$$$ from the first tower to the second tower, so the towers are $$$[6, 4]$$$ and $$$[8, 7, 5, 3, 2, 1]$$$. Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $$$[[3, 1], [2]]$$$ is $$$2$$$: you may move the disc $$$1$$$ to the second tower, and then move both discs from the second tower to the first tower.You are given $$$m - 1$$$ queries. Each query is denoted by two numbers $$$a_i$$$ and $$$b_i$$$, and means \"merge the towers $$$a_i$$$ and $$$b_i$$$\" (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $$$a_i$$$.For each $$$k \\in [0, m - 1]$$$, calculate the difficulty of the set of towers after the first $$$k$$$ queries are performed.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\le m \\le n \\le 2 \\cdot 10^5$$$) — the number of discs and the number of towers, respectively. The second line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ ($$$1 \\le t_i \\le m$$$), where $$$t_i$$$ is the index of the tower disc $$$i$$$ belongs to. Each value from $$$1$$$ to $$$m$$$ appears in this sequence at least once. Then $$$m - 1$$$ lines follow, denoting the queries. Each query is represented by two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le a_i, b_i \\le m$$$, $$$a_i \\ne b_i$$$), meaning that, during the $$$i$$$-th query, the towers with indices $$$a_i$$$ and $$$b_i$$$ are merged ($$$a_i$$$ and $$$b_i$$$ are chosen in such a way that these towers exist before the $$$i$$$-th query).", "output_spec": "Print $$$m$$$ integers. The $$$k$$$-th integer ($$$0$$$-indexed) should be equal to the difficulty of the set of towers after the first $$$k$$$ queries are performed.", "sample_inputs": ["7 4\n1 2 3 3 1 4 3\n3 1\n2 3\n2 4"], "sample_outputs": ["5\n4\n2\n0"], "notes": "NoteThe towers in the example are: before the queries: $$$[[5, 1], [2], [7, 4, 3], [6]]$$$; after the first query: $$$[[2], [7, 5, 4, 3, 1], [6]]$$$; after the second query: $$$[[7, 5, 4, 3, 2, 1], [6]]$$$; after the third query, there is only one tower: $$$[7, 6, 5, 4, 3, 2, 1]$$$. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n, m;\n\twhile (readf !(\" %s %s\") (n, m) > 0)\n\t{\n\t\treadln;\n\t\tauto a = 0 ~ readln.splitter.map !(to !(int)).array ~ 0;\n\t\tauto p = new int [] [m + 1];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tp[a[i + 1]] ~= i + 1;\n\t\t}\n\t\tint total = 0;\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\tif (a[i] != a[i + 1])\n\t\t\t{\n\t\t\t\ttotal += 1;\n\t\t\t}\n\t\t}\n\t\twriteln (total);\n\t\tforeach (j; 1..m)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf !(\" %s %s\") (u, v);\n\t\t\tif (p[u].length < p[v].length)\n\t\t\t{\n\t\t\t\tswap (u, v);\n\t\t\t}\n\t\t\tauto w = a[p[u][0]];\n\t\t\tforeach (c; p[v])\n\t\t\t{\n\t\t\t\tif (a[c] != a[c - 1])\n\t\t\t\t{\n\t\t\t\t\ttotal -= 1;\n\t\t\t\t}\n\t\t\t\tif (a[c] != a[c + 1])\n\t\t\t\t{\n\t\t\t\t\ttotal -= 1;\n\t\t\t\t}\n\t\t\t\ta[c] = w;\n\t\t\t\tif (a[c] != a[c - 1])\n\t\t\t\t{\n\t\t\t\t\ttotal += 1;\n\t\t\t\t}\n\t\t\t\tif (a[c] != a[c + 1])\n\t\t\t\t{\n\t\t\t\t\ttotal += 1;\n\t\t\t\t}\n\t\t\t\tp[u] ~= c;\n\t\t\t}\n\t\t\tp[v] = p[u];\n\t\t\twriteln (total);\n\t\t\tdebug {writeln (a);}\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "21f7c9e71ce1532514a6eaf0ff1c92da"} {"nl": {"description": "Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.", "input_spec": "The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen. Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109). Some positions may coincide.", "output_spec": "Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.", "sample_inputs": ["3\n1 1\n7 5\n1 5", "6\n0 0\n0 1\n0 2\n-1 1\n0 1\n1 1"], "sample_outputs": ["2", "11"], "notes": "NoteIn the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances."}, "positive_code": [{"source_code": "import std.stdio;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tint [int] cx;\n\t\tint [int] cy;\n\t\tint [int [2]] cz;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint x, y;\n\t\t\treadf (\" %s %s\", &x, &y);\n\t\t\tcx[x]++;\n\t\t\tcy[y]++;\n\t\t\tcz[[x, y]]++;\n\t\t}\n\t\tlong res = 0;\n\t\tforeach (v; cx)\n\t\t{\n\t\t\tres += (v * 1L * (v - 1)) / 2;\n\t\t}\n\t\tforeach (v; cy)\n\t\t{\n\t\t\tres += (v * 1L * (v - 1)) / 2;\n\t\t}\n\t\tforeach (v; cz)\n\t\t{\n\t\t\tres -= (v * 1L * (v - 1)) / 2;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "bd7b85c0204f6b36dc07f8a96fc36161"} {"nl": {"description": "This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $$$n$$$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format: $$$+$$$ $$$x$$$: the storehouse received a plank with length $$$x$$$ $$$-$$$ $$$x$$$: one plank with length $$$x$$$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $$$x$$$). Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$): the initial amount of planks at the company's storehouse, the second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$): the lengths of the planks. The third line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^5$$$): the number of events in the company. Each of the next $$$q$$$ lines contains a description of the events in a given format: the type of the event (a symbol $$$+$$$ or $$$-$$$) is given first, then goes the integer $$$x$$$ ($$$1 \\le x \\le 10^5$$$).", "output_spec": "After every event in the company, print \"YES\" if two storages of the required shape can be built from the planks of that company's set, and print \"NO\" otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["6\n1 1 1 2 1 1\n6\n+ 2\n+ 1\n- 1\n+ 2\n- 1\n+ 2"], "sample_outputs": ["NO\nYES\nNO\nNO\nNO\nYES"], "notes": "NoteAfter the second event Applejack can build a rectangular storage using planks with lengths $$$1$$$, $$$2$$$, $$$1$$$, $$$2$$$ and a square storage using planks with lengths $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$.After the sixth event Applejack can build a rectangular storage using planks with lengths $$$2$$$, $$$2$$$, $$$2$$$, $$$2$$$ and a square storage using planks with lengths $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nimmutable int limit = 9;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tint [int] num;\n\t\tint [limit] good;\n\n\t\tvoid account (int v, int delta)\n\t\t{\n\t\t\tnum[v] += 0;\n\t\t\tforeach (i; 0..min (limit, num[v] + 1))\n\t\t\t{\n\t\t\t\tgood[i] += delta;\n\t\t\t}\n\t\t}\n\n\t\tvoid change (int v, int delta)\n\t\t{\n\t\t\taccount (v, -1);\n\t\t\tnum[v] += delta;\n\t\t\taccount (v, +1);\n\t\t}\n\n\t\tforeach (ref c; a)\n\t\t{\n\t\t\tchange (c, +1);\n\t\t}\n\t\tint q;\n\t\treadf !(\" %s\") (q);\n\t\treadln;\n\n\t\tbool can ()\n\t\t{\n\t\t\tif (good[8] >= 1)\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (good[6] >= 1 && good[2] >= 2)\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (good[4] >= 2)\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (good[4] >= 1 && good[2] >= 3)\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\tforeach (j; 0..q)\n\t\t{\n\t\t\tauto t = readln.split;\n\t\t\tchange (t[1].to !(int), (t[0] == \"+\" ? +1 : -1));\n\t\t\twriteln (can () ? \"YES\" : \"NO\");\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nalias type = single;\nstruct TestCase\n{\n mixin prettyPrinting;\n\n long n;\n @Dim(\"n\") long[] a;\n long q;\n @Dim(\"q\") Tuple!(string, long)[] events;\n\n void solve(long tc = -1)\n {\n long[long] store;\n foreach(ai; a)\n {\n store.require(ai, 0)++;\n }\n auto mostUnits = redBlackTree!((a, b) {\n if (a[0] > b[0])\n return true;\n if (a[0] < b[0])\n return false;\n return a[1] < b[1];\n }, Tuple!(long, long))();\n foreach(t, c; store)\n {\n mostUnits.insert(tuple(c, t));\n }\n\n nextEvent:foreach(event; events)\n {\n string type = event[0];\n long x = event[1];\n store.require(x, 0);\n mostUnits.removeKey(tuple(store[x], x));\n store[x] += type == \"+\"? 1 : -1;\n mostUnits.insert(tuple(store[x], x));\n auto arr = mostUnits[].take(3).map!(t => t[0]).array;\n if (arr[0] >= 4)\n {\n arr[0] -= 4;\n foreach(v; arr)\n {\n if (v >= 4)\n {\n writeln(\"YES\");\n continue nextEvent;\n }\n }\n foreach(i; 0 .. arr.length - 1)\n {\n if (arr[i] >= 2 && arr[i + 1] >= 2)\n {\n writeln(\"YES\");\n continue nextEvent;\n }\n }\n writeln(\"NO\");\n }\n else\n {\n writeln(\"NO\");\n }\n }\n }\n}\n\nstruct Interval\n{\n long l, h;\n\n bool empty()\n {\n return l > h;\n }\n}\n\nInterval intersect(Interval a, Interval b)\n{\n return Interval(max(a.l, b.l), min(a.h, b.h));\n}\n\nstruct If\n{\n string condition;\n}\nenum ignore = If(\"false\");\nstruct Dim\n{\n string dimensions;\n int levels;\n this(string dimensions)\n {\n this.dimensions = dimensions;\n levels = cast(int)dimensions.count(\",\") + 1;\n }\n}\n\ntemplate getArrayLevel(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));\n else\n enum getArrayLevel = 0;\n}\n\nstatic foreach(fieldName; 'a' .. cast(char)('z' + 1))\n {\n static if (fieldName != 'o')\n mixin(\"enum d\", fieldName, \" = Dim(\\\"\", fieldName, \"\\\");\");\n }\n\n\nmixin template prettyPrinting()\n{\n string toString()\n {\n alias structType = typeof(this);\n auto res = appender!(string)();\n res.put(structType.stringof);\n res.put(\"(\");\n static foreach(fieldName; FieldNameTuple!structType)\n {\n res.put(text(\" \", fieldName, \": \", mixin(fieldName)));\n }\n res.put(\" )\");\n res.put(\"\\n\");\n return res[];\n }\n}\n\nstruct Graph(N)\n{\n size_t size;\n this(S)(S size)\n {\n adjacencyList.length = cast(size_t) size;\n this.size = cast(size_t) size;\n this.visited = makeSlice!bool(size);\n }\n N[][] adjacencyList;\n alias ne = neighbours;\n N[] neighbours(N n)\n {\n return adjacencyList.at(n);\n }\n alias bcon = biconnect;\n void biconnect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n adjacencyList.at(b) ~= a;\n }\n alias con = connect;\n void connect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n }\n void connect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n connect(edge[0], edge[1]);\n }\n }\n void biconnect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n biconnect(edge[0], edge[1]);\n }\n }\n alias deg = degree!long;\n auto degree(T)(N a)\n {\n return cast(T) adjacencyList.at(a).length;\n }\n\n auto visited = new bool[0];\n void dfs(Visitor)(long startNode)\n {\n auto visitor = Visitor.init;\n void internalDfs(long node)\n {\n visited.set(node, true);\n static if (is(typeof({visitor.n = node;})))\n visitor.n = node;\n static if (is(typeof({visitor.pre;})))\n visitor.pre;\n foreach(w; ne(node))\n if (!visited.at(w))\n {\n static if (is(typeof({visitor.w = w;})))\n visitor.w = w;\n static if (is(typeof(visitor.pren)))\n visitor.pren;\n internalDfs(w);\n static if (is(typeof(visitor.postn)))\n visitor.postn;\n }\n static if (is(typeof(visitor.post)))\n visitor.post;\n }\n internalDfs(startNode);\n }\n void dfs(long startNode)\n {\n struct DefVisitor {}\n dfs!DefVisitor(startNode);\n }\n}\n\nDList!string _words;\n\ntemplate removeArray(W, int times)\n{\n static if (times == 0)\n alias removeArray = W;\n else static if (times == 1)\n alias removeArray = typeof(function(){W w; return w[0];}());\n else\n {\n static assert(isDynamicArray!W);\n alias removeArray = removeArray!(removeArray!(W, 1), times - 1);\n }\n}\n\ntemplate baseType(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));\n else\n alias baseType = W;\n}\n\nauto makeSlice(E, W, T...)(W size, T otherSizes)\n{\n static if (T.length == 0)\n {\n E[] res;\n res.length = cast(size_t) size;\n return res;\n }\n else\n {\n typeof(makeSlice!E(otherSizes))[] res;\n res.length = cast(size_t) size;\n foreach(ref row; res)\n row = makeSlice!E(otherSizes);\n return res;\n }\n}\n\nvoid read(T...)(ref T t)\n{\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto a = RDA!int;\n\tauto q = RD!int;\n\n\tauto cnt = new int[](10^^5+1);\n\tlong a2, a4, a6, a8;\n\tvoid update1(int x)\n\t{\n\t\tif (cnt[x] == 8)\n\t\t{\n\t\t\t++a8;\n\t\t\t--a6;\n\t\t}\n\t\telse if (cnt[x] == 6)\n\t\t{\n\t\t\t++a6;\n\t\t\t--a4;\n\t\t}\n\t\telse if (cnt[x] == 4)\n\t\t{\n\t\t\t++a4;\n\t\t\t--a2;\n\t\t}\n\t\telse if (cnt[x] == 2)\n\t\t{\n\t\t\t++a2;\n\t\t}\n\t}\n\tvoid update2(int x)\n\t{\n\t\tif (cnt[x] == 7)\n\t\t{\n\t\t\t--a8;\n\t\t\t++a6;\n\t\t}\n\t\telse if (cnt[x] == 5)\n\t\t{\n\t\t\t--a6;\n\t\t\t++a4;\n\t\t}\n\t\telse if (cnt[x] == 3)\n\t\t{\n\t\t\t--a4;\n\t\t\t++a2;\n\t\t}\n\t\telse if (cnt[x] == 1)\n\t\t{\n\t\t\t--a2;\n\t\t}\n\t}\n\tforeach (i; 0..n)\n\t{\n\t\t++cnt[a[i]];\n\t\tupdate1(a[i]);\n\t}\n\tforeach (i; 0..q)\n\t{\n\t\tauto s = RD!string;\n\t\tauto x = RD!int;\n\t\tif (s == \"+\")\n\t\t{\n\t\t\t++cnt[x];\n\t\t\tupdate1(x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\t--cnt[x];\n\t\t\tupdate2(x);\n\t\t}\n\t\tauto ans = a4 >= 1 || a6 >= 1 || a8 >= 1;\n\t\tans &= a2 + a4*2 + a6*3 + a8*4 >= 4;\n\t\twriteln(ans ? \"YES\" : \"NO\");\n\t}\n\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "d14bad9abf2a27ba57c80851405a360b"} {"nl": {"description": "Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf.$$$n$$$ books must be split into $$$k$$$ compartments on the bookshelf ($$$n$$$ is divisible by $$$k$$$). Each book is represented by a lowercase Latin letter from 'a' to 'y' inclusively, which is the beginning letter in the title of the book.Ela must stack exactly $$$\\frac{n}{k}$$$ books in each compartment. After the books are stacked, for each compartment indexed from $$$1$$$ to $$$k$$$, she takes the minimum excluded (MEX) letter of the multiset of letters formed by letters representing all books in that compartment, then combines the resulting letters into a string. The first letter of the resulting string is the MEX letter of the multiset of letters formed by the first compartment, the second letter of the resulting string is the MEX letter of the multiset of letters formed by the second compartment, ... and so on. Please note, under the constraint of this problem, MEX letter can always be determined for any multiset found in this problem because 'z' is not used.What is the lexicographically greatest resulting string possible that Ela can create?A string $$$a$$$ is lexicographically greater than a string $$$b$$$ if and only if one of the following holds: $$$b$$$ is a prefix of $$$a$$$, but $$$b \\ne a$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears later in the alphabet than the corresponding letter in $$$b$$$. The minimum excluded (MEX) letter of a multiset of letters is the letter that appears earliest in the alphabet and is not contained in the multiset. For example, if a multiset of letters contains $$$7$$$ letters 'b', 'a', 'b', 'c', 'e', 'c', 'f' respectively, then the MEX letter of this compartment is 'd', because 'd' is not included in the multiset, and all letters comes before 'd' in the alphabet, namely 'a', 'b' and 'c', are included in the multiset.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 200$$$; $$$1 \\le k \\le n$$$). It is guaranteed that $$$n$$$ is divisible by $$$k$$$. The second line of each test case contains a string of $$$n$$$ lowercase Latin letters from 'a' to 'y' inclusively. Each letter represents the starting letter of the title of a book in the initial heap. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case, output a string of $$$k$$$ letters which is the most optimal string that Ela can find.", "sample_inputs": ["5\n\n12 3\n\ncabccadabaac\n\n12 6\n\ncabccadabaac\n\n12 12\n\ncabccadabaac\n\n25 1\n\nabcdefghijklmnopqrstuvwxy\n\n10 5\n\nbcdxedbcfg"], "sample_outputs": ["edb\nccbbba\nbbbbbaaaaaaa\nz\naaaaa"], "notes": "NoteIn the first test case, the books can be divided into $$$3$$$ compartments as below: the first compartment contains the books with indices $$$1, 2, 3, 7$$$: $$$multiset_1 = \\{$$$'c', 'a', 'b', 'd'$$$\\}$$$ $$$\\rightarrow$$$ $$$MEX(multiset_1) =$$$ 'e' the second compartment contains the books with indices $$$4, 5, 6, 9$$$ : $$$multiset_2 = \\{$$$'c', 'c', 'a', 'b'$$$\\}$$$ $$$\\rightarrow$$$ $$$MEX(multiset_2) =$$$ 'd' the third compartment contains the remaining books $$$8, 10, 11, 12$$$ : $$$multiset_3 = \\{$$$ 'a', 'a', 'a', 'c'$$$\\}$$$ $$$\\rightarrow$$$ $$$MEX(multiset_3) =$$$ 'b' Therefore, the answer is 'edb'. It can be proven that there is no way that Ela can arrange the books so that it results in a lexicographically greater string. "}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid solve() {\r\n int N, K; readf(\"%d %d\\n\", &N, &K);\r\n auto S = readln.chomp;\r\n\r\n int[char] s;\r\n foreach (c; S) {\r\n s[c] = s.get(c, 0) + 1;\r\n }\r\n\r\n char[] ans = new char[K];\r\n for (int i = 0; i < K; i++) {\r\n char x = '?';\r\n int n = 0;\r\n for (char c = 'a'; c <= 'z'; c++, n++) {\r\n if (n == N/K) {\r\n x = c;\r\n break;\r\n } else if (c !in s || s[c] == 0) {\r\n x = c;\r\n break;\r\n } else {\r\n s[c]--;\r\n }\r\n }\r\n ans[i] = x;\r\n }\r\n writeln(ans);\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nimmutable int letters = 26;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, k;\r\n\t\treadf !(\" %s %s\") (n, k);\r\n\t\treadln;\r\n\t\tauto s = readln.strip;\r\n\t\tauto c = new int [letters];\r\n\t\tforeach (ref x; s)\r\n\t\t{\r\n\t\t\tc[x - 'a'] += 1;\r\n\t\t}\r\n\t\tstring res;\r\n\t\tforeach (i; 0..k)\r\n\t\t{\r\n\t\t\tint x = 0;\r\n\t\t\twhile (x < n / k && c[x] > 0)\r\n\t\t\t{\r\n\t\t\t\tc[x] -= 1;\r\n\t\t\t\tx += 1;\r\n\t\t\t}\r\n\t\t\tres ~= cast (char) (x + 'a');\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "9c86925036cd1f83273bc21e2ea3e5c8"} {"nl": {"description": "To satisfy his love of matching socks, Phoenix has brought his $$$n$$$ socks ($$$n$$$ is even) to the sock store. Each of his socks has a color $$$c_i$$$ and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either: recolor a sock to any color $$$c'$$$ $$$(1 \\le c' \\le n)$$$ turn a left sock into a right sock turn a right sock into a left sock The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn't change when it turns into a right sock, and vice versa. A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make $$$n/2$$$ matching pairs? Each sock must be included in exactly one matching pair.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$l$$$, and $$$r$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$; $$$n$$$ is even; $$$0 \\le l, r \\le n$$$; $$$l+r=n$$$) — the total number of socks, and the number of left and right socks, respectively. The next line contains $$$n$$$ integers $$$c_i$$$ ($$$1 \\le c_i \\le n$$$) — the colors of the socks. The first $$$l$$$ socks are left socks, while the next $$$r$$$ socks are right socks. It is guaranteed that the sum of $$$n$$$ across all the test cases will not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the minimum cost for Phoenix to make $$$n/2$$$ matching pairs. Each sock must be included in exactly one matching pair.", "sample_inputs": ["4\n6 3 3\n1 2 3 2 2 2\n6 2 4\n1 1 2 2 2 2\n6 5 1\n6 5 4 3 2 1\n4 0 4\n4 4 4 3"], "sample_outputs": ["2\n3\n5\n3"], "notes": "NoteIn the first test case, Phoenix can pay $$$2$$$ dollars to: recolor sock $$$1$$$ to color $$$2$$$ recolor sock $$$3$$$ to color $$$2$$$ There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 4)$$$, $$$(2, 5)$$$, and $$$(3, 6)$$$ are matching.In the second test case, Phoenix can pay $$$3$$$ dollars to: turn sock $$$6$$$ from a right sock to a left sock recolor sock $$$3$$$ to color $$$1$$$ recolor sock $$$4$$$ to color $$$1$$$ There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 3)$$$, $$$(2, 4)$$$, and $$$(5, 6)$$$ are matching."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad) * vec[0] - sin(rad) * vec[1], sin(rad) * vec[0] + cos(rad) * vec[1]]; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto l = RD!int;\r\n\t\tauto r = RD!int;\r\n\t\tauto c = RDA!int(-1);\r\n\r\n\t\tauto cnt = new int[](n);\r\n\t\tforeach (i; 0..l)\r\n\t\t\t++cnt[c[i]];\r\n\t\tforeach (i; l..n)\r\n\t\t\t--cnt[c[i]];\r\n\r\n\t\tlong[] ls, rs;\r\n\t\tlong odd_l, odd_r;\r\n\t\tlong cnt_l, cnt_r;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (cnt[i] > 0)\r\n\t\t\t{\r\n\t\t\t\tls ~= cnt[i];\r\n\t\t\t\tcnt_l += cnt[i];\r\n\t\t\t\tif (cnt[i] % 2)\r\n\t\t\t\t\t++odd_l;\r\n\t\t\t}\r\n\t\t\telse if (cnt[i] < 0)\r\n\t\t\t{\r\n\t\t\t\trs ~= -cnt[i];\r\n\t\t\t\tcnt_r += -cnt[i];\r\n\t\t\t\tif ((-cnt[i]) % 2)\r\n\t\t\t\t\t++odd_r;\r\n\t\t\t}\r\n\t\t}\r\n\t\t{\r\n\t\t\tauto x = min(odd_l, odd_r);\r\n\t\t\todd_l -= x;\r\n\t\t\todd_r -= x;\r\n\t\t\tcnt_l -= x;\r\n\t\t\tcnt_r -= x;\r\n\t\t\tans[ti] += x;\r\n\r\n\t\t\tif (odd_l != 0)\r\n\t\t\t{\r\n\t\t\t\tauto y = min(odd_l, cnt_r);\r\n\t\t\t\todd_l -= y;\r\n\t\t\t\tcnt_l -= y;\r\n\t\t\t\tcnt_r -= y;\r\n\t\t\t\tans[ti] += y;\r\n\t\t\t}\r\n\t\t\telse if (odd_r != 0)\r\n\t\t\t{\r\n\t\t\t\tauto y = min(odd_r, cnt_l);\r\n\t\t\t\todd_r -= y;\r\n\t\t\t\tcnt_l -= y;\r\n\t\t\t\tcnt_r -= y;\r\n\t\t\t\tans[ti] += y;\r\n\t\t\t}\r\n\r\n\t\t\tif (odd_l != 0)\r\n\t\t\t{\r\n\t\t\t\tcnt_l -= odd_l;\r\n\t\t\t\tans[ti] += odd_l;\r\n\t\t\t}\r\n\t\t\telse if (odd_r != 0)\r\n\t\t\t{\r\n\t\t\t\tcnt_r -= odd_r;\r\n\t\t\t\tans[ti] += odd_r;\r\n\t\t\t}\r\n\r\n\t\t\tans[ti] += (cnt_l + cnt_r) / 2;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\twriteln(e);\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm, std.range, std.stdio, std.numeric, std.array,\n std.container, std.typecons, std.conv, std.random, std.bigint;\n\nvoid read(S...)(ref S args) {\n auto input = readln.split;\n assert(input.length == args.length);\n foreach (i, ref arg; args) {\n arg = input[i].to!(S[i]);\n }\n}\n\nauto list(T)() { return readln.split.map!(e => e.to!T); }\n\nvoid main() {\n int t;\n read(t);\n \n while (t--) {\n int n, l, r;\n read(n, l, r);\n \n auto data = list!int;\n auto count = new int[n + 1];\n foreach (i; 0 .. l) {\n count[data[i]]--;\n }\n foreach (i; l .. n) {\n count[data[i]]++;\n }\n \n alias Sock = Tuple!(bool, \"odd\", int, \"count\");\n auto left = count.filter!(a => a < 0).map!(a => Sock(-a % 2 == 1, -a)).array.heapify;\n auto right = count.filter!(a => a > 0).map!(a => Sock(a % 2 == 1, a)).array.heapify;\n \n int cost = 0;\n \n while (!left.empty && !right.empty && (left.front.odd || right.front.odd)) {\n auto x = left.removeAny;\n auto y = right.removeAny;\n if (x.count > 1) left.insert(Sock(!x.odd, x.count - 1));\n if (y.count > 1) right.insert(Sock(!y.odd, y.count - 1));\n cost++;\n }\n \n foreach (h; [left, right]) {\n while (!h.empty) {\n auto x = h.removeAny;\n if (x.odd) {\n h.insert(Sock(!x.odd, x.count - 1)); \n cost++;\n } else {\n cost += x.count / 2;\n }\n }\n }\n \n writeln(cost);\n }\n}\n\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, l, r;\r\n\t\treadf !(\" %s %s %s\") (n, l, r);\r\n\t\treadln;\r\n\t\tauto c = readln.splitter.map !(to !(int)).array;\r\n\t\tc[] -= 1;\r\n\r\n\t\tauto b = new int [n];\r\n\t\tforeach (i; 0..l)\r\n\t\t{\r\n\t\t\tb[c[i]] += 1;\r\n\t\t}\r\n\t\tforeach (i; l..n)\r\n\t\t{\r\n\t\t\tb[c[i]] -= 1;\r\n\t\t}\r\n\t\tdebug {writeln (b);}\r\n\r\n\t\tint lo = 0;\r\n\t\tint hi = 0;\r\n\t\tforeach (j; 0..n)\r\n\t\t{\r\n\t\t\tif (b[j] & 1)\r\n\t\t\t{\r\n\t\t\t\tif (b[j] > 0)\r\n\t\t\t\t{\r\n\t\t\t\t\thi += 1;\r\n\t\t\t\t\tb[j] -= 1;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tlo += 1;\r\n\t\t\t\t\tb[j] += 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint res = min (lo, hi);\r\n\t\tlo -= res;\r\n\t\thi -= res;\r\n\t\tdebug {writeln (res, \" \", lo, \" \", hi);}\r\n\t\tdebug {writeln (b);}\r\n\r\n\t\tforeach (j; 0..n)\r\n\t\t{\r\n\t\t\tif (lo > 0 && b[j] > 0)\r\n\t\t\t{\r\n\t\t\t\tint cur = min (+b[j], lo);\r\n\t\t\t\tres += cur;\r\n\t\t\t\tb[j] -= cur;\r\n\t\t\t\tlo -= cur;\r\n\t\t\t}\r\n\t\t\tif (hi > 0 && b[j] < 0)\r\n\t\t\t{\r\n\t\t\t\tint cur = min (-b[j], hi);\r\n\t\t\t\tres += cur;\r\n\t\t\t\tb[j] += cur;\r\n\t\t\t\thi -= cur;\r\n\t\t\t}\r\n\t\t}\r\n\t\tres += lo;\r\n\t\tres += hi;\r\n\r\n\t\tforeach (j; 0..n)\r\n\t\t{\r\n\t\t\tres += abs (b[j]) / 2;\r\n\t\t\tb[j] %= 2;\r\n\t\t}\r\n\r\n\t\tres += b.map !(abs).sum;\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "a2d4f0182456cedbe85dff97ec0f477e"} {"nl": {"description": "One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile: By the pieces lay a large square wooden board. The board is divided into $$$n^2$$$ cells arranged into $$$n$$$ rows and $$$n$$$ columns. Some of the cells are already occupied by single tiles stuck to it. The remaining cells are free.Alice started wondering whether she could fill the board completely using the pieces she had found. Of course, each piece has to cover exactly five distinct cells of the board, no two pieces can overlap and every piece should fit in the board entirely, without some parts laying outside the board borders. The board however was too large for Alice to do the tiling by hand. Can you help determine if it's possible to fully tile the board?", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$3 \\leq n \\leq 50$$$) — the size of the board. The following $$$n$$$ lines describe the board. The $$$i$$$-th line ($$$1 \\leq i \\leq n$$$) contains a single string of length $$$n$$$. Its $$$j$$$-th character ($$$1 \\leq j \\leq n$$$) is equal to \".\" if the cell in the $$$i$$$-th row and the $$$j$$$-th column is free; it is equal to \"#\" if it's occupied. You can assume that the board contains at least one free cell.", "output_spec": "Output YES if the board can be tiled by Alice's pieces, or NO otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["3\n#.#\n...\n#.#", "4\n##.#\n#...\n####\n##.#", "5\n#.###\n....#\n#....\n###.#\n#####", "5\n#.###\n....#\n#....\n....#\n#..##"], "sample_outputs": ["YES", "NO", "YES", "NO"], "notes": "NoteThe following sketches show the example boards and their tilings if such tilings exist: "}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT read(T)(){ return read.to!T; }\nT[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }\nT[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = read.to!int;\n\t\n\tbool[][] as;\n\tforeach(i; 0 .. n){\n\t\tas ~= readln.to!(char[]).map!(x => x == '#').array;\n\t}\n\t\n\tstring ans = \"YES\";\n\t\n\tforeach(i; 0 .. n) foreach(j; 0 .. n){\n\t\tif(as[i][j]) continue;\n\t\tif(j - 1 < 0 || i + 2 >= n || j + 1 >= n){\n\t\t\t\"NO\".writeln;\n\t\t\treturn;\n\t\t}\n\t\tif(as[i + 1][j - 1] || as[i + 1][j] || as[i + 1][j + 1] || as[i + 2][j]){\n\t\t\t\"NO\".writeln;\n\t\t\treturn;\n\t\t}\n\t\tas[i][j] = 1;\n\t\tas[i + 1][j - 1] = 1;\n\t\tas[i + 1][j] = 1;\n\t\tas[i + 1][j + 1] = 1;\n\t\tas[i + 2][j] = 1;\n\t}\n\t\n\t\"YES\".writeln;\n\treturn;\n\t\n}\n"}, {"source_code": "void main() {\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int n;\n rd(n);\n auto a = new char[][](n);\n foreach (i; 0 .. n) {\n a[i] = readln.chomp.to!(char[]);\n }\n if (a[0][0] == '.' || a[0][n - 1] == '.' || a[n - 1][0] == '.' || a[n - 1][n - 1] == '.') {\n writeln(\"NO\");\n return;\n }\n for (int i = 1; i + 1 < n; i++) {\n for (int j = 1; j + 1 < n; j++) {\n if (a[i][j] == '.') {\n if (a[i - 1][j] == '#' || a[i][j - 1] == '#' || a[i + 1][j] == '#' || a[i][j + 1] == '#') {\n continue;\n }\n a[i][j] = a[i - 1][j] = a[i][j - 1] = a[i + 1][j] = a[i][j + 1] = '#';\n }\n }\n }\n foreach (i; 0 .. n) {\n foreach (j; 0 .. n) {\n if (a[i][j] == '.') {\n writeln(\"NO\");\n return;\n }\n }\n }\n writeln(\"YES\");\n}\n\nvoid rd(T...)(ref T x) {\n import std.stdio : readln;\n import std.string : split;\n import std.conv : to;\n\n auto l = readln.split;\n assert(l.length == x.length);\n foreach (i, ref e; x)\n e = l[i].to!(typeof(e));\n}\n"}, {"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array;\nimport std.algorithm : sort, each, maxElement, minElement;\nimport std.conv : to;\nimport std.range : iota, tee;\nimport std.algorithm.comparison : equal;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\n\nalias Tree = RBT!int;\n\nvoid main()\n{\n GC.disable();\n\n uint n;\n readf(\" %s\\n\", n);\n\n char[][] s;\n foreach (i; 0 .. n)\n {\n auto ss = readln();\n s ~= ss.strip.dup;\n }\n\n foreach (i; 0 .. n)\n {\n foreach (j; 0 .. n)\n {\n if (s[i][j] == '.')\n {\n foreach (delta; [[0, 0], [1, -1], [1, 0], [1, 1], [2, 0]])\n {\n auto ni = i + delta[0];\n auto nj = j + delta[1];\n if (ni >= n || nj >= n || s[ni][nj] != '.')\n {\n writeln(\"NO\");\n return;\n }\n else\n {\n s[ni][nj] = '#';\n }\n\n }\n\n }\n\n }\n\n }\n\n writeln(\"YES\");\n\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nbool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }\nbool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }\n\nlong mod = 10^^9 + 7;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\t\n\tauto N = RD!uint;\n\tauto s = new char[][](N);\n\tforeach (i; 0..N)\n\t{\n\t\ts[i] = RD!(char[]);\n\t}\n\n\tbool visit(uint y, uint x)\n\t{\n\t\tif (s[y+1][x] == '#' ||\n\t\t\ts[y+1][x-1] == '#' ||\n\t\t\ts[y+1][x+1] == '#' ||\n\t\t\ts[y+2][x] == '#')\n\t\t\treturn false;\n\n\t\ts[y][x] = '#';\n\t\ts[y+1][x] = '#';\n\t\ts[y+1][x-1] = '#';\n\t\ts[y+1][x+1] = '#';\n\t\ts[y+2][x] = '#';\n\t\treturn true;\n\t}\n\n\tbool ans = true;\n\t(){\n\tforeach (y; 0..N)\n\t{\n\t\tforeach (x; 0..N)\n\t\t{\n\t\t\tif (s[y][x] == '#') continue;\n\t\t\t\n\t\t\tdebug writeln(y, \":\", x);\n\t\t\tif (x == 0 || x == N-1)\n\t\t\t{\n\t\t\t\tans = false;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if (y >= N-2)\n\t\t\t{\n\t\t\t\tans = false;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tauto r = visit(y, x);\n\t\t\tif (!r)\n\t\t\t{\n\t\t\t\tans = false;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}}();\n\n\twriteln(ans ? \"YES\" : \"NO\");\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "cc1feee94617c0cba1c528a0cb3952a8"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ positive integers. Let $$$\\text{LIS}(a)$$$ denote the length of longest strictly increasing subsequence of $$$a$$$. For example, $$$\\text{LIS}([2, \\underline{1}, 1, \\underline{3}])$$$ = $$$2$$$. $$$\\text{LIS}([\\underline{3}, \\underline{5}, \\underline{10}, \\underline{20}])$$$ = $$$4$$$. $$$\\text{LIS}([3, \\underline{1}, \\underline{2}, \\underline{4}])$$$ = $$$3$$$. We define array $$$a'$$$ as the array obtained after reversing the array $$$a$$$ i.e. $$$a' = [a_n, a_{n-1}, \\ldots , a_1]$$$.The beauty of array $$$a$$$ is defined as $$$min(\\text{LIS}(a),\\text{LIS}(a'))$$$.Your task is to determine the maximum possible beauty of the array $$$a$$$ if you can rearrange the array $$$a$$$ arbitrarily.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10^4)$$$  — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1 \\leq n \\leq 2\\cdot 10^5)$$$  — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, \\ldots ,a_n$$$ $$$(1 \\leq a_i \\leq 10^9)$$$  — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer  — the maximum possible beauty of $$$a$$$ after rearranging its elements arbitrarily.", "sample_inputs": ["3\n\n3\n\n6 6 6\n\n6\n\n2 5 4 5 2 4\n\n4\n\n1 3 2 2"], "sample_outputs": ["1\n3\n2"], "notes": "NoteIn the first test case, $$$a$$$ = $$$[6, 6, 6]$$$ and $$$a'$$$ = $$$[6, 6, 6]$$$. $$$\\text{LIS}(a) = \\text{LIS}(a')$$$ = $$$1$$$. Hence the beauty is $$$min(1, 1) = 1$$$.In the second test case, $$$a$$$ can be rearranged to $$$[2, 5, 4, 5, 4, 2]$$$. Then $$$a'$$$ = $$$[2, 4, 5, 4, 5, 2]$$$. $$$\\text{LIS}(a) = \\text{LIS}(a') = 3$$$. Hence the beauty is $$$3$$$ and it can be shown that this is the maximum possible beauty.In the third test case, $$$a$$$ can be rearranged to $$$[1, 2, 3, 2]$$$. Then $$$a'$$$ = $$$[2, 3, 2, 1]$$$. $$$\\text{LIS}(a) = 3$$$, $$$\\text{LIS}(a') = 2$$$. Hence the beauty is $$$min(3, 2) = 2$$$ and it can be shown that $$$2$$$ is the maximum possible beauty."}, "positive_code": [{"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N);\r\n\r\n auto gs = A.sort.group;\r\n auto ans = (gs.map!(g => min(2, g[1])).sum + 1) / 2;\r\n ans.writeln;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve();\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}], "negative_code": [{"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N);\r\n\r\n auto gs = A.sort.group;\r\n auto ans = gs.map!(g => min(2, g[1])).sum / 2;\r\n ans.writeln;\r\n\r\n // int ans;\r\n // {\r\n // auto tree1 = A[0..1].redBlackTree!\"a < b\";\r\n // auto tree2 = A[0..1].redBlackTree!\"a < b\";\r\n // int t1s = 1;\r\n // int t2s = 1;\r\n // foreach(a; A) {\r\n // if (t1s <= t2s) {\r\n // if (tree1.back < a) {\r\n // t1s++;\r\n // tree1.insert(a);\r\n // }\r\n // } else {\r\n // if (tree2.back < a) {\r\n // t2s++;\r\n // tree2.insert(a);\r\n // }\r\n // }\r\n // }\r\n // [tree1, tree2].deb;\r\n // ans = max(ans, min(t1s, t2s));\r\n // }\r\n // {\r\n // A = A.reverse.array;\r\n // auto tree1 = A[0..1].redBlackTree!\"a > b\";\r\n // auto tree2 = A[0..1].redBlackTree!\"a > b\";\r\n // int t1s = 1;\r\n // int t2s = 1;\r\n // foreach(a; A) {\r\n // if (t1s <= t2s) {\r\n // if (tree1.back > a) {\r\n // t1s++;\r\n // tree1.insert(a);\r\n // }\r\n // } else {\r\n // if (tree2.back > a) {\r\n // t2s++;\r\n // tree2.insert(a);\r\n // }\r\n // }\r\n // }\r\n // [tree1, tree2].deb;\r\n // ans = max(ans, min(t1s, t2s));\r\n // }\r\n\r\n // ans.writeln;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve();\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N);\r\n\r\n auto gs = A.group;\r\n auto ans = gs.count!(g => g[1] >= 2) + (gs.count!(g => g[1] == 1)) / 2;\r\n ans.writeln;\r\n\r\n // int ans;\r\n // {\r\n // auto tree1 = A[0..1].redBlackTree!\"a < b\";\r\n // auto tree2 = A[0..1].redBlackTree!\"a < b\";\r\n // int t1s = 1;\r\n // int t2s = 1;\r\n // foreach(a; A) {\r\n // if (t1s <= t2s) {\r\n // if (tree1.back < a) {\r\n // t1s++;\r\n // tree1.insert(a);\r\n // }\r\n // } else {\r\n // if (tree2.back < a) {\r\n // t2s++;\r\n // tree2.insert(a);\r\n // }\r\n // }\r\n // }\r\n // [tree1, tree2].deb;\r\n // ans = max(ans, min(t1s, t2s));\r\n // }\r\n // {\r\n // A = A.reverse.array;\r\n // auto tree1 = A[0..1].redBlackTree!\"a > b\";\r\n // auto tree2 = A[0..1].redBlackTree!\"a > b\";\r\n // int t1s = 1;\r\n // int t2s = 1;\r\n // foreach(a; A) {\r\n // if (t1s <= t2s) {\r\n // if (tree1.back > a) {\r\n // t1s++;\r\n // tree1.insert(a);\r\n // }\r\n // } else {\r\n // if (tree2.back > a) {\r\n // t2s++;\r\n // tree2.insert(a);\r\n // }\r\n // }\r\n // }\r\n // [tree1, tree2].deb;\r\n // ans = max(ans, min(t1s, t2s));\r\n // }\r\n\r\n // ans.writeln;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve();\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N);\r\n\r\n auto gs = A.group;\r\n auto ans = gs.count!(g => g[1] >= 2) + (gs.count!(g => g[1] == 1) + 1) / 2;\r\n ans.writeln;\r\n\r\n // int ans;\r\n // {\r\n // auto tree1 = A[0..1].redBlackTree!\"a < b\";\r\n // auto tree2 = A[0..1].redBlackTree!\"a < b\";\r\n // int t1s = 1;\r\n // int t2s = 1;\r\n // foreach(a; A) {\r\n // if (t1s <= t2s) {\r\n // if (tree1.back < a) {\r\n // t1s++;\r\n // tree1.insert(a);\r\n // }\r\n // } else {\r\n // if (tree2.back < a) {\r\n // t2s++;\r\n // tree2.insert(a);\r\n // }\r\n // }\r\n // }\r\n // [tree1, tree2].deb;\r\n // ans = max(ans, min(t1s, t2s));\r\n // }\r\n // {\r\n // A = A.reverse.array;\r\n // auto tree1 = A[0..1].redBlackTree!\"a > b\";\r\n // auto tree2 = A[0..1].redBlackTree!\"a > b\";\r\n // int t1s = 1;\r\n // int t2s = 1;\r\n // foreach(a; A) {\r\n // if (t1s <= t2s) {\r\n // if (tree1.back > a) {\r\n // t1s++;\r\n // tree1.insert(a);\r\n // }\r\n // } else {\r\n // if (tree2.back > a) {\r\n // t2s++;\r\n // tree2.insert(a);\r\n // }\r\n // }\r\n // }\r\n // [tree1, tree2].deb;\r\n // ans = max(ans, min(t1s, t2s));\r\n // }\r\n\r\n // ans.writeln;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve();\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}, {"source_code": "void main() { runSolver(); }\r\n\r\nvoid problem() {\r\n auto QN = scan!int;\r\n\r\n auto solve() {\r\n auto subSolve() {\r\n auto N = scan!int;\r\n auto A = scan!int(N).sort.array;\r\n\r\n int ans;\r\n {\r\n auto tree1 = A[0..1].redBlackTree!\"a < b\";\r\n auto tree2 = A[0..1].redBlackTree!\"a < b\";\r\n int t1s = 1;\r\n int t2s = 1;\r\n foreach(a; A) {\r\n if (t1s <= t2s) {\r\n if (tree1.back < a) {\r\n t1s++;\r\n tree1.insert(a);\r\n }\r\n } else {\r\n if (tree2.back < a) {\r\n t2s++;\r\n tree2.insert(a);\r\n }\r\n }\r\n }\r\n // [tree1, tree2].deb;\r\n ans = max(ans, min(t1s, t2s));\r\n }\r\n {\r\n A = A.reverse.array;\r\n auto tree1 = A[0..1].redBlackTree!\"a > b\";\r\n auto tree2 = A[0..1].redBlackTree!\"a > b\";\r\n int t1s = 1;\r\n int t2s = 1;\r\n foreach(a; A) {\r\n if (t1s <= t2s) {\r\n if (tree1.back > a) {\r\n t1s++;\r\n tree1.insert(a);\r\n }\r\n } else {\r\n if (tree2.back > a) {\r\n t2s++;\r\n tree2.insert(a);\r\n }\r\n }\r\n }\r\n // [tree1, tree2].deb;\r\n ans = max(ans, min(t1s, t2s));\r\n }\r\n\r\n ans.writeln;\r\n }\r\n\r\n foreach(i; 0..QN) {\r\n subSolve();\r\n }\r\n }\r\n\r\n outputForAtCoder(&solve);\r\n}\r\n\r\n// ----------------------------------------------\r\n\r\nimport std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;\r\nT[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }\r\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\r\nT scan(T)(){ return scan.to!T; }\r\nT[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }\r\nvoid deb(T ...)(T t){ debug writeln(t); }\r\nalias Point = Tuple!(long, \"x\", long, \"y\");\r\nPoint invert(Point p) { return Point(p.y, p.x); }\r\nlong[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }\r\nbool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }\r\nbool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }\r\nstring charSort(alias S = \"a < b\")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }\r\nulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}\r\nT[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }\r\nstring toAnswerString(R)(R r) { return r.map!\"a.to!string\".joiner(\" \").array.to!string; }\r\nstruct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>\".writefln(benchmark!problem(1)); BORDER.writeln; } }\r\n else problem();\r\n}\r\nenum YESNO = [true: \"Yes\", false: \"No\"];\r\n\r\n// -----------------------------------------------\r\n"}], "src_uid": "68b7567880b9980d793dae2bce690356"} {"nl": {"description": "There are $$$n$$$ cities numbered from $$$1$$$ to $$$n$$$, and city $$$i$$$ has beauty $$$a_i$$$.A salesman wants to start at city $$$1$$$, visit every city exactly once, and return to city $$$1$$$.For all $$$i\\ne j$$$, a flight from city $$$i$$$ to city $$$j$$$ costs $$$\\max(c_i,a_j-a_i)$$$ dollars, where $$$c_i$$$ is the price floor enforced by city $$$i$$$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2\\le n\\le 10^5$$$) — the number of cities. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$c_i$$$ ($$$0\\le a_i,c_i\\le 10^9$$$) — the beauty and price floor of the $$$i$$$-th city.", "output_spec": "Output a single integer — the minimum total cost.", "sample_inputs": ["3\n1 9\n2 1\n4 1", "6\n4 2\n8 4\n3 0\n2 3\n7 1\n0 1"], "sample_outputs": ["11", "13"], "notes": "NoteIn the first test case, we can travel in order $$$1\\to 3\\to 2\\to 1$$$. The flight $$$1\\to 3$$$ costs $$$\\max(c_1,a_3-a_1)=\\max(9,4-1)=9$$$. The flight $$$3\\to 2$$$ costs $$$\\max(c_3, a_2-a_3)=\\max(1,2-4)=1$$$. The flight $$$2\\to 1$$$ costs $$$\\max(c_2,a_1-a_2)=\\max(1,1-2)=1$$$. The total cost is $$$11$$$, and we cannot do better."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nenum INF = 10L^^9;\r\nalias City = Tuple!(long, \"pos\", long, \"val\");\r\n\r\nvoid main() {\r\n try {\r\n for (; ; ) {\r\n const N = readInt();\r\n auto A = new City[N];\r\n foreach (u; 0 .. N) {\r\n A[u].pos = readLong();\r\n A[u].val = readLong();\r\n }\r\n A.sort;\r\n debug {\r\n writeln(\"A = \", A);\r\n }\r\n \r\n alias Entry = Tuple!(long, \"c\", int, \"u\");\r\n BinaryHeap!(Array!Entry, \"a.c > b.c\") que;\r\n auto dist = new long[N];\r\n dist[] = INF;\r\n dist[0] = 0;\r\n que.insert(Entry(0, 0));\r\n for (; !que.empty; ) {\r\n const c = que.front.c;\r\n const u = que.front.u;\r\n que.removeFront;\r\n if (dist[u] == c) {\r\n if (u - 1 >= 0) {\r\n if (chmin(dist[u - 1], c)) {\r\n que.insert(Entry(c, u - 1));\r\n }\r\n }\r\n const v = A.lowerBound(City(A[u].pos + A[u].val + 1, -1));\r\n debug {\r\n writefln(\"%s -> %s\", u, v);\r\n }\r\n if (v - 1 >= 0) {\r\n if (chmin(dist[v - 1], c)) {\r\n que.insert(Entry(c, v - 1));\r\n }\r\n }\r\n if (v < N) {\r\n const cc = c + ((A[v].pos - A[u].pos) - A[u].val);\r\n if (chmin(dist[v], cc)) {\r\n que.insert(Entry(cc, v));\r\n }\r\n }\r\n }\r\n }\r\n \r\n long ans = dist[N - 1];\r\n foreach (u; 0 .. N) {\r\n ans += A[u].val;\r\n }\r\n writeln(ans);\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "48f2c0307a29056a5e0fcc26af98f6dc"} {"nl": {"description": "Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store  — \"PriceFixed\". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: $$$2$$$ rubles per item. For every product $$$i$$$ there is a discount for experienced buyers: if you buy $$$b_i$$$ items of products (of any type, not necessarily type $$$i$$$), then for all future purchases of the $$$i$$$-th product there is a $$$50\\%$$$ discount (so you can buy an item of the $$$i$$$-th product for $$$1$$$ ruble!). Lena needs to buy $$$n$$$ products: she must purchase at least $$$a_i$$$ items of the $$$i$$$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100\\,000$$$) — the number of products. Each of next $$$n$$$ lines contains a product description. Each description consists of two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \\leq a_i \\leq 10^{14}$$$, $$$1 \\leq b_i \\leq 10^{14}$$$) — the required number of the $$$i$$$-th product and how many products you need to buy to get the discount on the $$$i$$$-th product. The sum of all $$$a_i$$$ does not exceed $$$10^{14}$$$.", "output_spec": "Output the minimum sum that Lena needs to make all purchases. ", "sample_inputs": ["3\n3 4\n1 3\n1 5", "5\n2 7\n2 8\n1 2\n2 4\n1 8"], "sample_outputs": ["8", "12"], "notes": "NoteIn the first example, Lena can purchase the products in the following way: one item of product $$$3$$$ for $$$2$$$ rubles, one item of product $$$1$$$ for $$$2$$$ rubles, one item of product $$$1$$$ for $$$2$$$ rubles, one item of product $$$2$$$ for $$$1$$$ ruble (she can use the discount because $$$3$$$ items are already purchased), one item of product $$$1$$$ for $$$1$$$ ruble (she can use the discount because $$$4$$$ items are already purchased). In total, she spends $$$8$$$ rubles. It can be proved that it is impossible to spend less.In the second example Lena can purchase the products in the following way: one item of product $$$1$$$ for $$$2$$$ rubles, two items of product $$$2$$$ for $$$2$$$ rubles for each, one item of product $$$5$$$ for $$$2$$$ rubles, one item of product $$$3$$$ for $$$1$$$ ruble, two items of product $$$4$$$ for $$$1$$$ ruble for each, one item of product $$$1$$$ for $$$1$$$ ruble. In total, she spends $$$12$$$ rubles."}, "positive_code": [{"source_code": "import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,\n std.container, std.typecons, std.conv, std.random, std.bigint;\n\nvoid read(S...)(ref S args) {\n auto input = readln.split;\n assert(input.length == args.length);\n foreach (i, ref arg; args) {\n arg = input[i].to!(S[i]);\n }\n}\n\nauto list(T)() { return readln.split.map!(e => e.to!T).array; }\n\nvoid main() {\n int n;\n read(n);\n \n alias Item = Tuple!(long, \"discount\", long, \"need\");\n auto arr = new Item[n];\n \n foreach (i; 0 .. n) {\n read(arr[i][1], arr[i][0]);\n }\n \n sort(arr);\n \n long purchased = 0;\n long answer = 0;\n \n while (arr.length) {\n if (arr.front.discount <= purchased) {\n purchased += arr.front.need;\n answer += arr.front.need;\n arr.popFront();\n } else {\n auto buy = min(arr.front.discount - purchased, arr.back.need);\n answer += 2 * buy;\n purchased += buy;\n arr.back.need -= buy;\n if (arr.back.need == 0) arr.popBack();\n }\n }\n \n writeln(answer);\n}\n\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.string, std.array, std.random, std.range;\n\nvoid main()\n{\n long n;\n readf!\" %d \"(n);\n long[] a;\n long[] b;\n for (long i = 0; i < n; i++) {\n long ai, bi;\n readf!\" %d %d \"(ai, bi);\n if (ai > 0) {\n a ~= ai;\n b ~= bi;\n }\n }\n auto c = zip(a, b).sort!((x, y) => x[1] < y[1]).array;\n// writeln(c);\n\n size_t l = 0;\n size_t r = c.length - 1;\n\n long result = 0;\n long bought = 0;\n\n while (true) {\n auto x = c[l];\n auto y = c[r];\n\n if (bought >= x[1]) {\n result += x[0];\n bought += x[0];\n if (l == r)\n break;\n l++;\n continue;\n }\n\n long buy_from_last = x[1] - bought;\n if (y[0] > buy_from_last) {\n result += buy_from_last * 2;\n bought += buy_from_last;\n y[0] -= buy_from_last;\n c[r] = y;\n continue;\n }\n\n result += y[0] * 2;\n bought += y[0];\n if (l == r)\n break;\n r--;\n }\n writeln(result);\n}\n"}], "negative_code": [], "src_uid": "6e8cabaee588f53faae5feb2d1950c58"} {"nl": {"description": "A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — $$$n$$$ and $$$k$$$, where $$$n$$$ is even. Next, he takes all the integers from $$$1$$$ to $$$n$$$, and splits them all into pairs $$$(a, b)$$$ (each integer must be in exactly one pair) so that for each pair the integer $$$(a + k) \\cdot b$$$ is divisible by $$$4$$$ (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers $$$n$$$ and $$$k$$$.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The single line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\leq n \\leq 2 \\cdot 10^5$$$, $$$0 \\leq k \\leq 10^9$$$, $$$n$$$ is even) — the number of integers and the number being added $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, first output the string \"YES\" if there is a split into pairs, and \"NO\" if there is none. If there is a split, then in the following $$$\\frac{n}{2}$$$ lines output pairs of the split, in each line print $$$2$$$ numbers — first the integer $$$a$$$, then the integer $$$b$$$.", "sample_inputs": ["4\n\n4 1\n\n2 0\n\n12 10\n\n14 11"], "sample_outputs": ["YES\n1 2\n3 4\nNO\nYES\n3 4\n7 8\n11 12\n2 1\n6 5\n10 9\nYES\n1 2\n3 4\n5 6\n7 8\n9 10\n11 12\n13 14"], "notes": "NoteIn the first test case, splitting into pairs $$$(1, 2)$$$ and $$$(3, 4)$$$ is suitable, same as splitting into $$$(1, 4)$$$ and $$$(3, 2)$$$.In the second test case, $$$(1 + 0) \\cdot 2 = 1 \\cdot (2 + 0) = 2$$$ is not divisible by $$$4$$$, so there is no partition."}, "positive_code": [{"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n outer: foreach(_; 0..t)\r\n {\r\n long n, k;\r\n long[][] answer;\r\n scanf(\"%lld %lld\\n\", &n, &k);\r\n if ((k == 0) || (k % 4 == 0)) {\r\n writeln(\"NO\");\r\n continue;\r\n }\r\n if (n == 2) {\r\n if (k % 2 != 0) {\r\n writeln(\"YES\");\r\n writeln(1,\" \", 2);\r\n }\r\n else if (k % 4 == 0)\r\n writeln(\"NO\");\r\n else if (k % 4 != 0) {\r\n writeln(\"YES\");\r\n writeln(2, \" \", 1);\r\n }\r\n else\r\n writeln(\"NO\");\r\n continue;\r\n }\r\n foreach(i; iota(2,n+1,2)) {\r\n if ((i+k) % 4 == 0)\r\n answer ~= [i, i-1];\r\n else\r\n answer ~= [i-1, i];\r\n }\r\n writeln(\"YES\");\r\n foreach(row; answer)\r\n writefln(\"%(%s %)\", row);\r\n }\r\n}\r\n"}], "negative_code": [{"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n outer: foreach(_; 0..t)\r\n {\r\n long n, k;\r\n long[][] answer;\r\n scanf(\"%lld %lld\\n\", &n, &k);\r\n if (k == 0) {\r\n writeln(\"NO\");\r\n continue;\r\n }\r\n if (n == 2) {\r\n if (k % 2 != 0) {\r\n writeln(\"YES\");\r\n writeln(1,\" \", 2);\r\n }\r\n else if (k % 4 == 0)\r\n writeln(\"NO\");\r\n else if (k % 4 != 0) {\r\n writeln(\"YES\");\r\n writeln(2, \" \", 1);\r\n }\r\n else\r\n writeln(\"NO\");\r\n continue;\r\n }\r\n foreach(i; iota(2,n+1,2)) {\r\n if ((i+k) % 4 == 0)\r\n answer ~= [i, i-1];\r\n else\r\n answer ~= [i-1, i];\r\n }\r\n writeln(\"YES\");\r\n foreach(row; answer)\r\n writefln(\"%(%s %)\", row);\r\n }\r\n}\r\n"}, {"source_code": "import std;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n outer: foreach(_; 0..t)\r\n {\r\n long n, k;\r\n long[][] answer;\r\n scanf(\"%lld %lld\\n\", &n, &k);\r\n if (k == 0) {\r\n writeln(\"NO\");\r\n continue;\r\n }\r\n foreach(i; iota(2,n+1,2)) {\r\n if ((i+k) % 4 == 0)\r\n answer ~= [i, i-1];\r\n else\r\n answer ~= [i-1, i];\r\n }\r\n writeln(\"YES\");\r\n foreach(row; answer)\r\n writefln(\"%(%s %)\", row);\r\n }\r\n}\r\n"}], "src_uid": "d4fc7e683f389e0c7bbee8e115cef459"} {"nl": {"description": "Recently in Divanovo, a huge river locks system was built. There are now $$$n$$$ locks, the $$$i$$$-th of them has the volume of $$$v_i$$$ liters, so that it can contain any amount of water between $$$0$$$ and $$$v_i$$$ liters. Each lock has a pipe attached to it. When the pipe is open, $$$1$$$ liter of water enters the lock every second.The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $$$i$$$ to the lock $$$i + 1$$$. If the lock $$$i + 1$$$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $$$5$$$ locks with two open pipes at locks $$$1$$$ and $$$3$$$. Because locks $$$1$$$, $$$3$$$, and $$$4$$$ are already filled, effectively the water goes to locks $$$2$$$ and $$$5$$$. Note that the volume of the $$$i$$$-th lock may be greater than the volume of the $$$i + 1$$$-th lock.To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $$$q$$$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $$$j$$$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $$$t_j$$$ seconds.Please help the mayor to solve this tricky problem and answer his queries. ", "input_spec": "The first lines contains one integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$) — the number of locks. The second lines contains $$$n$$$ integers $$$v_1, v_2, \\dots, v_n$$$ ($$$1 \\le v_i \\le 10^9$$$)) — volumes of the locks. The third line contains one integer $$$q$$$ ($$$1 \\le q \\le 200\\,000$$$) — the number of queries. Each of the next $$$q$$$ lines contains one integer $$$t_j$$$ ($$$1 \\le t_j \\le 10^9$$$) — the number of seconds you have to fill all the locks in the query $$$j$$$. ", "output_spec": "Print $$$q$$$ integers. The $$$j$$$-th of them should be equal to the minimum number of pipes to turn on so that after $$$t_j$$$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $$$-1$$$. ", "sample_inputs": ["5\n4 1 5 4 1\n6\n1\n6\n2\n3\n4\n5", "5\n4 4 4 4 4\n6\n1\n3\n6\n5\n2\n4"], "sample_outputs": ["-1\n3\n-1\n-1\n4\n3", "-1\n-1\n4\n4\n-1\n5"], "notes": "NoteThere are $$$6$$$ queries in the first example test. In the queries $$$1, 3, 4$$$ the answer is $$$-1$$$. We need to wait $$$4$$$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$. After $$$4$$$ seconds the locks $$$1$$$ and $$$4$$$ are full. In the following $$$1$$$ second $$$1$$$ liter of water is transferred to the locks $$$2$$$ and $$$5$$$. The lock $$$3$$$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$.In the fifth query one can open pipes $$$1, 2, 3, 4$$$. "}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.math;\r\nimport std.numeric;\r\nimport std.container;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nT read(T)() { return readln.chomp.to!T; }\r\nT[] readarray(T)() { return readln.chomp.split(\" \").map!(to!T).array; }\r\n\r\nvoid main() {\r\n int N = read!int;\r\n auto V = readarray!long;\r\n int Q = read!int;\r\n auto T = Q.iota.map!(_ => read!int).array;\r\n\r\n auto S = new long[N + 1];\r\n for (int i = 0; i < N; i++) {\r\n S[i + 1] = S[i] + V[i];\r\n }\r\n auto D = new double[N + 1];\r\n D[] = 0;\r\n for (int i = 1; i <= N; i++) {\r\n D[i] = cast(double)(S[i]) / i;\r\n }\r\n auto X = new double[N + 1];\r\n X[] = 0;\r\n for (int i = 1; i <= N; i++) {\r\n X[i] = max(X[i-1], D[i]);\r\n }\r\n auto L = new double[N + 1];\r\n L[] = double.infinity;\r\n for (int k = 1; k <= N; k++) {\r\n L[k] = max(X[k], cast(double)(S[N]) / k);\r\n }\r\n foreach (t; T) {\r\n int lb = 0, ub = N;\r\n bool C(int x) {\r\n return L[x] <= t;\r\n }\r\n if (! C(ub)) {\r\n writeln(-1);\r\n } else {\r\n while (lb + 1 < ub) {\r\n int mid = (lb + ub) / 2;\r\n (C(mid) ? ub : lb) = mid;\r\n }\r\n writeln(ub);\r\n }\r\n }\r\n\r\n}\r\n"}], "negative_code": [], "src_uid": "d7361a43bff124cea280ae8817b807ec"} {"nl": {"description": "Fishing Prince loves trees, and he especially loves trees with only one centroid. The tree is a connected graph without cycles.A vertex is a centroid of a tree only when you cut this vertex (remove it and remove all edges from this vertex), the size of the largest connected component of the remaining graph is the smallest possible.For example, the centroid of the following tree is $$$2$$$, because when you cut it, the size of the largest connected component of the remaining graph is $$$2$$$ and it can't be smaller. However, in some trees, there might be more than one centroid, for example: Both vertex $$$1$$$ and vertex $$$2$$$ are centroids because the size of the largest connected component is $$$3$$$ after cutting each of them.Now Fishing Prince has a tree. He should cut one edge of the tree (it means to remove the edge). After that, he should add one edge. The resulting graph after these two operations should be a tree. He can add the edge that he cut.He wants the centroid of the resulting tree to be unique. Help him and find any possible way to make the operations. It can be proved, that at least one such way always exists.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\leq t\\leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\\leq n\\leq 10^5$$$) — the number of vertices. Each of the next $$$n-1$$$ lines contains two integers $$$x, y$$$ ($$$1\\leq x,y\\leq n$$$). It means, that there exists an edge connecting vertices $$$x$$$ and $$$y$$$. It's guaranteed that the given graph is a tree. It's guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print two lines. In the first line print two integers $$$x_1, y_1$$$ ($$$1 \\leq x_1, y_1 \\leq n$$$), which means you cut the edge between vertices $$$x_1$$$ and $$$y_1$$$. There should exist edge connecting vertices $$$x_1$$$ and $$$y_1$$$. In the second line print two integers $$$x_2, y_2$$$ ($$$1 \\leq x_2, y_2 \\leq n$$$), which means you add the edge between vertices $$$x_2$$$ and $$$y_2$$$. The graph after these two operations should be a tree. If there are multiple solutions you can print any.", "sample_inputs": ["2\n5\n1 2\n1 3\n2 4\n2 5\n6\n1 2\n1 3\n1 4\n2 5\n2 6"], "sample_outputs": ["1 2\n1 2\n1 3\n2 3"], "notes": "NoteNote that you can add the same edge that you cut.In the first test case, after cutting and adding the same edge, the vertex $$$2$$$ is still the only centroid.In the second test case, the vertex $$$2$$$ becomes the only centroid after cutting the edge between vertices $$$1$$$ and $$$3$$$ and adding the edge between vertices $$$2$$$ and $$$3$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nbool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }\nvoid main(string[] args){ args ~= [\"\", \"\"]; string cmd = args[1]; if(cmd == \"-debug\") DEBUG = 1;\nif(cmd == \"-gen\") gen; else if(cmd == \"-jury\") jury; else solve; } \nvoid print(){ writeln(\"\"); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, \" \"), print(a); }\nstring unsplit(T)(T xs){ return xs.array.to!(string[]).join(\" \"); }\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }\nT lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\nvoid gen(){\n}\nvoid jury(){\n}\nvoid solve(){\n foreach(_; 0 .. scan!int){\n int n = scan!int;\n Node[] nodes;\n foreach(i; 0 .. n) nodes ~= new Node(i);\n foreach(__; 0 .. n - 1){\n int a = scan!int - 1, b = scan!int - 1;\n nodes[a].connectTo(nodes[b]);\n }\n foreach(ed; nodes[0].edgesIn) ed.calcIn;\n nodes[0].calc;\n foreach(ed; nodes[0].edgesOut) ed.calcOut;\n\n log(\"node values:\", nodes.map!(nd => nd.value));\n //log(\"edge values:\", edges.map!(ed => ed.node0.id.to!string ~ \"->\" ~ ed.node1.id.to!string ~ \":\" ~ ed.value.to!string));\n\n long bestvalue = nodes[0].value;\n foreach(nd; nodes) bestvalue.lowerTo(nd.value);\n\n Node[] bestnodes;\n foreach(nd; nodes) if(nd.value == bestvalue) bestnodes ~= nd;\n\n if(bestnodes.length == 1){\n Edge ed = nodes[0].edgesIn[0];\n print(ed.node0.id + 1, ed.node1.id + 1);\n print(ed.node0.id + 1, ed.node1.id + 1);\n }\n else{\n Edge ed = bestnodes[0].edgesIn[0];\n if(ed.node0.id == bestnodes[1].id) ed = bestnodes[0].edgesIn[1];\n print(ed.node0.id + 1, bestnodes[0].id + 1);\n print(ed.node0.id + 1, bestnodes[1].id + 1);\n }\n\n }\n}\n\n\n// 双方向木DP\n\nclass Node{\n int id;\n long value;\n Edge[] edgesIn, edgesOut;\n this(int id){\n this.id = id;\n }\n void connectTo(Node nd){\n Edge edIn = new Edge(nd, this);\n Edge edOut = new Edge(this, nd);\n edIn.inv = edOut, edOut.inv = edIn;\n this.edgesIn ~= edIn, this.edgesOut ~= edOut;\n nd.edgesIn ~= edOut, nd.edgesOut ~= edIn;\n }\n void calc(){\n log(\"calc node\", id);\n value = 0;\n long sum = 1;\n foreach(ed; edgesIn){\n sum += ed.value;\n value.raiseTo(ed.value);\n }\n foreach(ed; edgesOut){\n ed.value = sum - ed.inv.value;\n }\n }\n}\nclass Edge{\n static int nextId = 0;\n int id;\n Node node0, node1;\n Edge inv;\n long value;\n this(Node node0, Node node1){\n this.id = nextId ++;\n this.node0 = node0;\n this.node1 = node1;\n }\n void calcIn(){\n log(\"calcin edge\", id);\n foreach(ed; node0.edgesIn) if(ed.node0.id != this.node1.id){\n ed.calcIn;\n }\n calc;\n }\n void calcOut(){\n log(\"calcout edge\", id);\n node1.calc;\n foreach(ed; node1.edgesOut) if(ed.node1.id != this.node0.id){\n ed.calcOut;\n }\n }\n void calc(){\n value = 1;\n foreach(ed; node0.edgesIn) if(ed.node0.id != this.node1.id){\n value += ed.value;\n }\n }\n\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto edges = new int[][](n);\n\t\tforeach (i; 0..n-1)\n\t\t{\n\t\t\tauto x = RD!int-1;\n\t\t\tauto y = RD!int-1;\n\t\t\tedges[x] ~= y;\n\t\t\tedges[y] ~= x;\n\t\t}\n\n\t\tauto cnt = new int[](n);\n\t\tint dfs(int pos, int last)\n\t\t{\n\t\t\tint m, tot = 1;\n\t\t\tforeach (v; edges[pos])\n\t\t\t{\n\t\t\t\tif (v == last) continue;\n\n\t\t\t\tauto r = dfs(v, pos);\n\t\t\t\tm.chmax(r);\n\t\t\t\ttot += r;\n\t\t\t}\n\t\t\tcnt[pos] = max(m, n-tot);\n\t\t\treturn tot;\n\t\t}\n\t\tdfs(0, -1);\n\n\t\tauto index = cnt.MAKE_IDX;\n\t\tint i = cast(int)index[0];\n\t\tint j = cast(int)index[1];\n\t\tif (cnt[i] != cnt[j])\n\t\t{\n\t\t\tans[ti] = [i, edges[i][0], i, edges[i][0]];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint dfs2(int pos, int last)\n\t\t\t{\n\t\t\t\tif (edges[pos].length == 1) return pos;\n\n\t\t\t\tforeach (v; edges[pos])\n\t\t\t\t{\n\t\t\t\t\tif (v == last) continue;\n\t\t\t\t\treturn dfs2(v, pos);\n\t\t\t\t}\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tauto k = dfs2(j, i);\n\t\t\tans[ti] = [k, edges[k][0], i, k];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e[0]+1, \" \", e[1]+1);\n\t\twriteln(e[2]+1, \" \", e[3]+1);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[][](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto edges = new int[][](n);\n\t\tforeach (i; 0..n-1)\n\t\t{\n\t\t\tauto x = RD!int-1;\n\t\t\tauto y = RD!int-1;\n\t\t\tedges[x] ~= y;\n\t\t\tedges[y] ~= x;\n\t\t}\n\n\t\tauto cnt = new int[](n);\n\t\tint dfs(int pos, int last)\n\t\t{\n\t\t\tint m, tot = 1;\n\t\t\tforeach (v; edges[pos])\n\t\t\t{\n\t\t\t\tif (v == last) continue;\n\n\t\t\t\tauto r = dfs(v, pos);\n\t\t\t\tm.chmax(r);\n\t\t\t\ttot += m;\n\t\t\t}\n\t\t\tcnt[pos] = max(m, n-tot);\n\t\t\treturn tot;\n\t\t}\n\t\tdfs(0, -1);\n\n\t\tauto index = cnt.MAKE_IDX;\n\t\tint i = cast(int)index[0];\n\t\tint j = cast(int)index[1];\n\t\tif (cnt[i] != cnt[j])\n\t\t{\n\t\t\tans[ti] = [i, edges[i][0], i, edges[i][0]];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint dfs2(int pos, int last)\n\t\t\t{\n\t\t\t\tif (edges[pos].length == 1) return pos;\n\n\t\t\t\tforeach (v; edges[pos])\n\t\t\t\t{\n\t\t\t\t\tif (v == last) continue;\n\t\t\t\t\treturn dfs2(v, pos);\n\t\t\t\t}\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tauto k = dfs2(j, i);\n\t\t\tans[ti] = [k, edges[k][0], i, k];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e[0]+1, \" \", e[1]+1);\n\t\twriteln(e[2]+1, \" \", e[3]+1);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "src_uid": "b01fb9d4d78a02e3c634800b4b177d75"} {"nl": {"description": "This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.To bake a Napoleon cake, one has to bake $$$n$$$ dry layers first, and then put them on each other in one stack, adding some cream. Arkady started with an empty plate, and performed the following steps $$$n$$$ times: place a new cake layer on the top of the stack; after the $$$i$$$-th layer is placed, pour $$$a_i$$$ units of cream on top of the stack. When $$$x$$$ units of cream are poured on the top of the stack, top $$$x$$$ layers of the cake get drenched in the cream. If there are less than $$$x$$$ layers, all layers get drenched and the rest of the cream is wasted. If $$$x = 0$$$, no layer gets drenched. The picture represents the first test case of the example. Help Arkady determine which layers of the cake eventually get drenched when the process is over, and which don't.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 20\\,000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of layers in the cake. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le n$$$) — the amount of cream poured on the cake after adding each layer. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single line with $$$n$$$ integers. The $$$i$$$-th of the integers should be equal to $$$1$$$ if the $$$i$$$-th layer from the bottom gets drenched, and $$$0$$$ otherwise.", "sample_inputs": ["3\n6\n0 3 0 0 1 3\n10\n0 0 0 1 0 5 0 0 0 2\n3\n0 0 0"], "sample_outputs": ["1 1 0 1 1 1 \n0 1 1 1 1 1 0 0 1 1 \n0 0 0"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[][](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = RDA!int;\r\n\r\n\t\tauto imos = new long[](n+1);\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] == 0) continue;\r\n\t\t\tauto j = n-i-1;\r\n\t\t\timos[j] += 1;\r\n\t\t\timos[min(n, j+a[i])] -= 1;\r\n\t\t}\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\timos[i+1] += imos[i];\r\n\t\t}\r\n\t\tans[ti].length = n;\r\n\t\tforeach (i; 0..n)\r\n\t\t\tif (imos[n-i-1] >= 1)\r\n\t\t\t\tans[ti][i] = 1;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\te.map!(to!string).join(\" \").writeln;\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "807c5ec37b0ea83ef40550698f1ff498"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$. The string $$$s$$$ consists of lowercase Latin letters and at most one wildcard character '*', the string $$$t$$$ consists only of lowercase Latin letters. The length of the string $$$s$$$ equals $$$n$$$, the length of the string $$$t$$$ equals $$$m$$$.The wildcard character '*' in the string $$$s$$$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $$$s$$$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $$$s$$$ to obtain a string $$$t$$$, then the string $$$t$$$ matches the pattern $$$s$$$.For example, if $$$s=$$$\"aba*aba\" then the following strings match it \"abaaba\", \"abacaba\" and \"abazzzaba\", but the following strings do not match: \"ababa\", \"abcaaba\", \"codeforces\", \"aba1aba\", \"aba?aba\".If the given string $$$t$$$ matches the given string $$$s$$$, print \"YES\", otherwise print \"NO\".", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$) — the length of the string $$$s$$$ and the length of the string $$$t$$$, respectively. The second line contains string $$$s$$$ of length $$$n$$$, which consists of lowercase Latin letters and at most one wildcard character '*'. The third line contains string $$$t$$$ of length $$$m$$$, which consists only of lowercase Latin letters.", "output_spec": "Print \"YES\" (without quotes), if you can obtain the string $$$t$$$ from the string $$$s$$$. Otherwise print \"NO\" (without quotes).", "sample_inputs": ["6 10\ncode*s\ncodeforces", "6 5\nvk*cup\nvkcup", "1 1\nv\nk", "9 6\ngfgf*gfgf\ngfgfgf"], "sample_outputs": ["YES", "YES", "NO", "NO"], "notes": "NoteIn the first example a wildcard character '*' can be replaced with a string \"force\". So the string $$$s$$$ after this replacement is \"codeforces\" and the answer is \"YES\".In the second example a wildcard character '*' can be replaced with an empty string. So the string $$$s$$$ after this replacement is \"vkcup\" and the answer is \"YES\".There is no wildcard character '*' in the third example and the strings \"v\" and \"k\" are different so the answer is \"NO\".In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $$$t$$$ so the answer is \"NO\"."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1];\n auto S = readln.chomp;\n auto T = readln.chomp;\n\n if (S.canFind('*')) {\n if (N > M + 1) {\n writeln(\"NO\");\n return;\n }\n bool ok = true;\n for (int i = 0; S[i] != '*'; ++i) {\n if (S[i] != T[i]) {\n ok = false;\n break;\n }\n }\n for (int i = 0; S[N-i-1] != '*'; ++i) {\n if (S[N-i-1] != T[M-i-1]) {\n ok = false;\n break;\n }\n }\n writeln(ok ? \"YES\" : \"NO\");\n } else {\n writeln( S == T ? \"YES\" : \"NO\" );\n }\n\n}\n"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.range.interfaces;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto s = readln.chomp;\n auto t = readln.chomp;\n \n if (!s.canFind('*')) {\n writeln(s == t ? \"YES\" : \"NO\");\n return;\n }\n \n if (n > m +1) {\n writeln(\"NO\");\n return;\n }\n \n auto parts = findSplit(s, \"*\");\n \n debug { parts.writeln; t.writeln; } \n \n auto ok = t.startsWith(parts[0]) && t.endsWith(parts[2]);\n writeln(ok ? \"YES\" : \"NO\");\n}"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n, m;\n\twhile (readf (\" %s %s\", &n, &m) > 0)\n\t{\n\t\treadln;\n\t\tauto s = readln.strip;\n\t\tauto t = readln.strip;\n\t\twhile (!s.empty && !t.empty && s.front == t.front)\n\t\t{\n\t\t\ts.popFront ();\n\t\t\tt.popFront ();\n\t\t}\n\t\twhile (!s.empty && !t.empty && s.back == t.back)\n\t\t{\n\t\t\ts.popBack ();\n\t\t\tt.popBack ();\n\t\t}\n\t\twriteln (((t == \"\" && s == \"\" ) || s == \"*\") ? \"YES\" : \"NO\");\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.conv;\nimport std.string;\n\nvoid main()\n{\n readln;\n auto a = readln[0 .. $ - 1];\n auto b = readln[0 .. $ - 1];\n auto c = a.split('*');\n bool ok;\n if (c.length > 1) {\n ok = b.length >= a.length - 1\n && b[0 .. c[0].length] == c[0]\n && b[b.length - c[1].length .. $] == c[1];\n } else {\n ok = a == b;\n }\n if (ok)\n writeln(\"YES\");\n else\n writeln(\"NO\");\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const M = readInt();\n const S = readToken();\n const T = readToken();\n \n bool ans;\n const pos = S.indexOf('*');\n if (pos == -1) {\n ans = (S == T);\n } else {\n ans = (N - 1 <= M && S[0 .. pos] == T[0 .. pos] && S[pos + 1 .. N] == T[M - (N - pos - 1) .. M]);\n }\n writeln(ans ? \"YES\" : \"NO\");\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "d629d09782a7af0acc359173ac4b4f0a"} {"nl": {"description": "Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \\dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \\le x \\le n$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \\le n < 10^{100}$$$). This integer is given without leading zeroes.", "output_spec": "For each test case, print one integer — the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.", "sample_inputs": ["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"], "sample_outputs": ["1\n2\n9\n10\n26"], "notes": "NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \\le x \\le n$$$, $$$\\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \\le x \\le n$$$, $$$\\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$. "}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\treadln.strip.length.writeln;\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!string;\n\t\tans[ti] = cast(int)n.length;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "ea011f93837fdf985f7eaa7a43e22cc8"} {"nl": {"description": "Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.", "input_spec": "The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.", "output_spec": "Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them.", "sample_inputs": ["5 3 1\n5 4 3 5 2", "5 3 1\n5 4 3 5 5", "5 3 1\n5 4 4 5 5", "3 2 7\n5 4 2"], "sample_outputs": ["5 4 3 5 -1", "5 4 0 5 5", "5 1 4 5 5", "5 11 -5"], "notes": null}, "positive_code": [{"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nint n, k, magic;\nlong[200_000] _a;\nlong[ ] a;\n\nvoid main() {\n while (read(&n, &k, &magic)) {\n a = _a[0 .. n];\n bool neg = false;\n Array!int z;\n foreach (int i, ref x; a) {\n read(&x);\n if (!x)\n z ~= i;\n else if (x < 0)\n neg = !neg;\n }\n if (!z.empty) {\n if (k < z.length)\n goto end;\n k -= z.length;\n if (neg)\n foreach (i; z)\n a[i] = magic;\n else {\n a[z[0]] = -magic;\n foreach (i; z[1 .. $])\n a[i] = magic;\n }\n } else if (!neg) {\n auto r = a.minPos!`abs(a) < abs(b)`;\n assert(r[0]);\n if (r[0] > 0)\n while (r[0] >= 0 && k) {\n r[0] -= magic;\n k--;\n }\n else\n while (r[0] <= 0 && k) {\n r[0] += magic;\n k--;\n }\n }\n if (k) {\n Array!(Tuple!(long, int)) store;\n store.length = n;\n foreach (int i, x; a)\n store[i] = tuple(x, i);\n auto h = heapify!`abs(a[0]) > abs(b[0])`(store);\n do {\n long x;\n int pos;\n AliasSeq!(x, pos) = h.front;\n h.removeFront();\n if (x > 0)\n x += magic;\n else\n x -= magic;\n a[pos] = x;\n h.insert(tuple(x, pos));\n } while (--k);\n }\n end:\n foreach (x; a)\n write(x, ' ');\n writeln();\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.math;\nimport std.algorithm;\nimport std.range;\nimport std.random;\nimport std.regex;\nimport std.string;\nimport std.numeric;\nimport std.functional;\nimport std.bigint;\nimport std.bitmanip;\nimport std.conv;\nimport std.container;\nalias redBlackTree rbt;\nalias RedBlackTree Rbt;\nalias BigInt big;\n//writeln readf readln string wstring dstring delegate function static\n//double float real foreach immutable assert unittest continue break\n//class enum remove insert length struct return\nstruct chis\n{\n\tlong val;\n\tint pos;\n};\nlong[200000] a,ans;\nvoid main()\n{\n\talias fun=binaryFun!(\"a.val0 && a[p]<=0){a[p]+=x;k--;}\n\t\t}\n\t\telse \n\t\t{\n\t\t\twhile(k>0 && a[p]>=0){a[p]-=x;k--;}\n\t\t}\n\t}\n\t//Rbt!(chis,\"abs(a.val)0)\n\t{\n\t\tauto f=q.front;\n\t\tq.removeFront;\n\t\tif(f.val<0) f.val-=x;\n\t\telse f.val+=x;\n\t\tq.insert(f);\n\t\tk--;\n\t}\n\tforeach(h;q)ans[h.pos]=h.val;\n\tforeach(i;0..n)write(ans[i],' ');\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nenum N = 200_000;\n\nint n, k, x;\nlong[N] _a;\nlong[ ] a;\n\nvoid main() {\n while (read(&n, &k, &x)) {\n a = _a[0 .. n];\n bool neg = false;\n Array!int z;\n foreach (int i, ref e; a) {\n read(&e);\n if (!e)\n z.insertBack(i);\n else if (e < 0)\n neg = !neg;\n }\n if (!z.empty) {\n if (k < z.length) {\n foreach (x; a)\n write(x, ' ');\n writeln();\n continue;\n }\n k -= z.length;\n if ((z.length & 0x1) != neg)\n foreach (i; z)\n a[i] = -x;\n else {\n a[z[0]] = -x;\n foreach (i; z[1 .. $])\n a[i] = x;\n }\n } else if (!neg) {\n auto r = a.minPos!`abs(a) < abs(b)`;\n assert(r[0]);\n if (r[0] > 0)\n while (r[0] >= 0 && k) {\n r[0] -= x;\n k--;\n }\n else\n while (r[0] <= 0 && k) {\n r[0] += x;\n k--;\n }\n }\n if (k) {\n auto r = a.minPos!`abs(a) < abs(b)`;\n assert(r[0]);\n if (r[0] > 0)\n r[0] += cast(long)x * k;\n else\n r[0] -= cast(long)x * k;\n }\n foreach (e; a)\n write(e, ' ');\n writeln();\n }\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nenum N = 200_000;\n\nint n, k, x;\nlong[N] _a;\nlong[ ] a;\n\nvoid main() {\n while (read(&n, &k, &x)) {\n a = _a[0 .. n];\n bool neg = false;\n Array!int z;\n foreach (int i, ref e; a) {\n read(&e);\n if (!e)\n z.insertBack(i);\n else if (e < 0)\n neg = !neg;\n }\n if (!z.empty) {\n if (k < z.length) {\n foreach (x; a)\n write(x, ' ');\n writeln();\n continue;\n }\n k -= z.length;\n if ((z.length & 0x1) != neg)\n foreach (i; z)\n a[i] = -x;\n else {\n a[z[0]] = -x;\n foreach (i; z[1 .. $])\n a[i] = x;\n }\n } else if (!neg) {\n auto r = a.minPos!`abs(a) < abs(b)`;\n assert(r[0]);\n if (r[0] > 0)\n while (r[0] >= 0 && k) {\n r[0] -= x;\n k--;\n }\n else\n while (r[0] <= 0 && k) {\n r[0] += x;\n k--;\n }\n }\n if (k) {\n Array!(Tuple!(long, int)) store;\n store.length = n;\n foreach (int i, e; a)\n store[i] = tuple(e, i);\n auto h = heapify!`abs(a[0]) > abs(b[0])`(store);\n do {\n long e;\n int pos;\n AliasSeq!(e, pos) = h.front;\n h.removeFront();\n if (e > 0)\n e += x;\n else\n e -= x;\n a[pos] = e;\n h.insert(tuple(e, pos));\n } while (--k);\n }\n foreach (e; a)\n write(e, ' ');\n writeln();\n }\n}\n"}], "src_uid": "6db54c7de672a1fd0afd052894ce4ce8"} {"nl": {"description": "Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a0, a1, ..., ah, where h is the height of the tree, and ai equals to the number of vertices that are at distance of i edges from root. Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence ai, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.", "input_spec": "The first line contains a single integer h (2 ≤ h ≤ 105) — the height of the tree. The second line contains h + 1 integers — the sequence a0, a1, ..., ah (1 ≤ ai ≤ 2·105). The sum of all ai does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence.", "output_spec": "If there is only one tree matching this sequence, print \"perfect\". Otherwise print \"ambiguous\" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print integers, the k-th of them should be the parent of vertex k or be equal to zero, if the k-th vertex is the root. These treese should be non-isomorphic and should match the given sequence.", "sample_inputs": ["2\n1 1 1", "2\n1 2 2"], "sample_outputs": ["perfect", "ambiguous\n0 1 1 3 3\n0 1 1 3 2"], "notes": "NoteThe only tree in the first example and the two printed trees from the second example are shown on the picture:"}, "positive_code": [{"source_code": "/+ dub.sdl:\n name \"A\"\n dependency \"dcomp\" version=\">=0.9.0\"\n+/\n\nimport std.stdio, std.algorithm, std.range, std.conv;\n// import dcomp.foundation, dcomp.scanner;\n\nimport std.typecons;\n\nint main() {\n Scanner sc = new Scanner(stdin);\n\n int h; int[] a;\n sc.read(h, a); h++;\n int[] as = new int[h+1];\n foreach (i; 0..h) {\n as[i+1] = as[i] + a[i];\n }\n int n = as.back;\n int[] ap = new int[n], bp = new int[n];\n ap[0] = -1; bp[0] = -1;\n foreach (i; 1..h) {\n foreach (j; as[i]..as[i+1]) {\n ap[j] = bp[j] = as[i-1];\n }\n }\n foreach (i; 0..h-1) {\n if (a[i] > 1 && a[i+1] > 1) {\n bp[as[i+1]+1]++;\n\n writeln(\"ambiguous\");\n writeln(ap.map!\"a+1\".map!(to!string).join(\" \"));\n writeln(bp.map!\"a+1\".map!(to!string).join(\" \"));\n return 0;\n }\n }\n writeln(\"perfect\");\n return 0;\n}\n/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */\n \n// module dcomp.foundation;\n\n \nstatic if (__VERSION__ <= 2070) {\n /*\n Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d\n Copyright: Andrei Alexandrescu 2008-.\n License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).\n */\n template fold(fun...) if (fun.length >= 1) {\n auto fold(R, S...)(R r, S seed) {\n import std.algorithm : reduce;\n static if (S.length < 2) {\n return reduce!fun(seed, r);\n } else {\n import std.typecons : tuple;\n return reduce!fun(tuple(seed), r);\n }\n }\n }\n \n}\n/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */\n// module dcomp.scanner;\n\n// import dcomp.container.stackpayload;\n\n \nclass Scanner {\n import std.stdio : File;\n import std.conv : to;\n import std.range : front, popFront, array, ElementType;\n import std.array : split;\n import std.traits : isSomeChar, isStaticArray, isArray; \n import std.algorithm : map;\n File f;\n this(File f) {\n this.f = f;\n }\n char[512] lineBuf;\n char[] line;\n private bool succW() {\n import std.range.primitives : empty, front, popFront;\n import std.ascii : isWhite;\n while (!line.empty && line.front.isWhite) {\n line.popFront;\n }\n return !line.empty;\n }\n private bool succ() {\n import std.range.primitives : empty, front, popFront;\n import std.ascii : isWhite;\n while (true) {\n while (!line.empty && line.front.isWhite) {\n line.popFront;\n }\n if (!line.empty) break;\n line = lineBuf[];\n f.readln(line);\n if (!line.length) return false;\n }\n return true;\n }\n\n private bool readSingle(T)(ref T x) {\n import std.algorithm : findSplitBefore;\n import std.string : strip;\n import std.conv : parse;\n if (!succ()) return false;\n static if (isArray!T) {\n alias E = ElementType!T;\n static if (isSomeChar!E) {\n \n \n auto r = line.findSplitBefore(\" \");\n x = r[0].strip.dup;\n line = r[1];\n } else static if (isStaticArray!T) {\n foreach (i; 0..T.length) {\n bool f = succW();\n assert(f);\n x[i] = line.parse!E;\n }\n } else {\n StackPayload!E buf;\n while (succW()) {\n buf ~= line.parse!E;\n }\n x = buf.data;\n }\n } else {\n x = line.parse!T;\n }\n return true;\n }\n int read(T, Args...)(ref T x, auto ref Args args) {\n if (!readSingle(x)) return 0;\n static if (args.length == 0) {\n return 1;\n } else {\n return 1 + read(args);\n }\n }\n}\n\n\n \n \n\n \n/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */\n// module dcomp.container.stackpayload;\n\n \nstruct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {\n import core.exception : RangeError;\n\n private T* _data;\n private uint len, cap;\n\n @property bool empty() const { return len == 0; }\n @property size_t length() const { return len; }\n alias opDollar = length;\n\n \n inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }\n \n ref inout(T) opIndex(size_t i) inout {\n version(assert) if (len <= i) throw new RangeError();\n return _data[i];\n } \n ref inout(T) front() inout { return this[0]; } \n ref inout(T) back() inout { return this[$-1]; } \n\n void reserve(size_t newCap) {\n import core.memory : GC;\n import core.stdc.string : memcpy;\n import std.conv : to;\n if (newCap <= cap) return;\n void* newData = GC.malloc(newCap * T.sizeof);\n cap = newCap.to!uint;\n if (len) memcpy(newData, _data, len * T.sizeof);\n _data = cast(T*)(newData);\n } \n void free() {\n import core.memory : GC;\n GC.free(_data);\n } \n \n void clear() {\n len = 0;\n }\n\n void insertBack(T item) {\n import std.algorithm : max;\n if (len == cap) reserve(max(cap * 2, MINCAP));\n _data[len++] = item;\n } \n alias opOpAssign(string op : \"~\") = insertBack; \n void removeBack() {\n assert(!empty, \"StackPayload.removeBack: Stack is empty\");\n len--;\n } \n}\n\n \n\n/*\nThis source code generated by dcomp and include dcomp's source code.\ndcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)\ndcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)\n*/\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const H = readInt();\n auto A = new int[H + 1];\n foreach (d; 0 .. H + 1) {\n A[d] = readInt();\n }\n \n bool amb;\n foreach (d; 0 .. H) {\n amb = amb || (A[d] >= 2 && A[d + 1] >= 2);\n }\n if (!amb) {\n writeln(\"perfect\");\n } else {\n auto ASum = new int[H + 2];\n foreach (d; 0 .. H + 1) {\n ASum[d + 1] = ASum[d] + A[d];\n }\n auto ans = new int[][2];\n ans[0] ~= -1;\n ans[1] ~= -1;\n foreach (d; 1 .. H + 1) {\n foreach (j; 0 .. A[d]) {\n ans[0] ~= ASum[d - 1];\n ans[1] ~= ASum[d - 1] + j % A[d - 1];\n }\n }\n writeln(\"ambiguous\");\n foreach (s; 0 .. 2) {\n foreach (u; 0 .. ASum[H + 1]) {\n if (u > 0) write(\" \");\n write(ans[s][u] + 1);\n }\n writeln();\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const H = readInt();\n auto A = new int[H + 1];\n foreach (d; 0 .. H + 1) {\n A[d] = readInt();\n }\n \n bool perfect = true;\n foreach (d; 0 .. H) {\n perfect = perfect && (A[d] == 1);\n }\n if (perfect) {\n writeln(\"perfect\");\n } else {\n auto ASum = new int[H + 2];\n foreach (d; 0 .. H + 1) {\n ASum[d + 1] = ASum[d] + A[d];\n }\n auto ans = new int[][2];\n ans[0] ~= -1;\n ans[1] ~= -1;\n foreach (d; 1 .. H + 1) {\n foreach (_; 0 .. A[d]) {\n ans[0] ~= ASum[d - 1];\n ans[1] ~= ASum[d - 1] + A[d - 1] - 1;\n }\n }\n writeln(\"ambiguous\");\n foreach (s; 0 .. 2) {\n foreach (u; 0 .. ASum[H + 1]) {\n if (u > 0) write(\" \");\n write(ans[s][u] + 1);\n }\n writeln();\n }\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "a186acbdc88a7ed131a7e5f999877fd6"} {"nl": {"description": "Kiwon's favorite video game is now holding a new year event to motivate the users! The game is about building and defending a castle, which led Kiwon to think about the following puzzle.In a 2-dimension plane, you have a set $$$s = \\{(x_1, y_1), (x_2, y_2), \\ldots, (x_n, y_n)\\}$$$ consisting of $$$n$$$ distinct points. In the set $$$s$$$, no three distinct points lie on a single line. For a point $$$p \\in s$$$, we can protect this point by building a castle. A castle is a simple quadrilateral (polygon with $$$4$$$ vertices) that strictly encloses the point $$$p$$$ (i.e. the point $$$p$$$ is strictly inside a quadrilateral). Kiwon is interested in the number of $$$4$$$-point subsets of $$$s$$$ that can be used to build a castle protecting $$$p$$$. Note that, if a single subset can be connected in more than one way to enclose a point, it is counted only once. Let $$$f(p)$$$ be the number of $$$4$$$-point subsets that can enclose the point $$$p$$$. Please compute the sum of $$$f(p)$$$ for all points $$$p \\in s$$$.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$5 \\le n \\le 2\\,500$$$). In the next $$$n$$$ lines, two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 \\le x_i, y_i \\le 10^9$$$) denoting the position of points are given. It is guaranteed that all points are distinct, and there are no three collinear points.", "output_spec": "Print the sum of $$$f(p)$$$ for all points $$$p \\in s$$$.", "sample_inputs": ["5\n-1 0\n1 0\n-10 -1\n10 -1\n0 3", "8\n0 1\n1 2\n2 2\n1 3\n0 -1\n-1 -2\n-2 -2\n-1 -3", "10\n588634631 265299215\n-257682751 342279997\n527377039 82412729\n145077145 702473706\n276067232 912883502\n822614418 -514698233\n280281434 -41461635\n65985059 -827653144\n188538640 592896147\n-857422304 -529223472"], "sample_outputs": ["2", "40", "213"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nstruct Point\n{\n\tint x;\n\tint y;\n\n\tPoint opBinary (string op) (const auto ref Point that)\n\t if (op == \"+\" || op == \"-\")\n\t{\n\t\tPoint res = void;\n\t\tres.x = mixin (q{this.x } ~ op ~ q{ that.x});\n\t\tres.y = mixin (q{this.y } ~ op ~ q{ that.y});\n\t\treturn res;\n\t}\n\n\tint opCmp () (const auto ref Point that)\n\t{\n\t\tauto thisUp = this.y > 0 || this.y == 0 && this.x > 0;\n\t\tauto thatUp = that.y > 0 || that.y == 0 && that.x > 0;\n\t\tif (thisUp != thatUp)\n\t\t{\n\t\t\treturn thisUp - thatUp;\n\t\t}\n\t\tauto cur = vp (this, that);\n\t\treturn (cur > 0) - (cur < 0);\n\t}\n}\n\nlong vp () (const auto ref Point a, const auto ref Point b)\n{\n\treturn a.x * 1L * b.y - a.y * 1L * b.x;\n}\n\nvoid main ()\n{\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\tauto p = new Point [n];\n\t\tforeach (ref c; p)\n\t\t{\n\t\t\treadf !(\" %s %s\") (c.x, c.y);\n\t\t}\n\n\t\tlong res = n * (n - 1L) * (n - 2L) * (n - 3L) * (n - 4L) / 24;\n\t\tdebug {writeln (res);}\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tPoint [] q;\n\t\t\tq.reserve (n - 1);\n\t\t\tforeach (j; 0..n)\n\t\t\t{\n\t\t\t\tif (j != i)\n\t\t\t\t{\n\t\t\t\t\tq ~= p[j] - p[i];\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort (q);\n\t\t\tdebug {writeln (q);}\n\t\t\tq ~= q;\n\t\t\tint v = 0;\n\t\t\tforeach (u; 0..n - 1)\n\t\t\t{\n\t\t\t\tv = max (v, u + 1);\n\t\t\t\twhile (v - u < n - 1 && vp (q[v], q[u]) > 0)\n\t\t\t\t{\n\t\t\t\t\tv += 1;\n\t\t\t\t}\n\t\t\t\tdebug {writeln (u, \" \", v);}\n\t\t\t\tint w = v - u - 1;\n\t\t\t\tres -= w * (w - 1L) * (w - 2L) / 6;\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nalias Pt = Tuple!(long, \"x\", long, \"y\");\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto P = new Pt[N];\n foreach (i; 0 .. N) {\n P[i].x = readLong();\n P[i].y = readLong();\n }\n \n long ans;\n foreach (i; 0 .. N) {\n Pt[] ps;\n foreach (j; 0 .. N) {\n if (j != i) {\n ps ~= Pt(P[j].x - P[i].x, P[j].y - P[i].y);\n }\n }\n ps.sort!((ref Pt a, ref Pt b) {\n const sa = (a.y > 0) ? 1 : (a.y < 0) ? 3 : (a.x > 0) ? 0 : 2;\n const sb = (b.y > 0) ? 1 : (b.y < 0) ? 3 : (b.x > 0) ? 0 : 2;\n if (sa != sb) return (sa < sb);\n return (a.x * b.y > a.y * b.x);\n });\n int r;\n foreach (l; 0 .. N - 1) {\n for (chmax(r, l + 1); r < l + (N - 1) && ps[l].x * ps[r % (N - 1)].y > ps[l].y * ps[r % (N - 1)].x; ++r) {}\n const long m = r - l - 1;\n ans += m * (m - 1) * (m - 2) / 6;\n }\n }\n \n const long n = N;\n ans = n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120 * 5 - ans;\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "ef0d77b1b45af25be498c9abc4750722"} {"nl": {"description": "$$$n$$$ heroes fight against each other in the Arena. Initially, the $$$i$$$-th hero has level $$$a_i$$$.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal levels fight, nobody wins the fight. When two heroes of different levels fight, the one with the higher level wins, and his level increases by $$$1$$$.The winner of the tournament is the first hero that wins in at least $$$100^{500}$$$ fights (note that it's possible that the tournament lasts forever if no hero wins this number of fights, then there is no winner). A possible winner is a hero such that there exists a sequence of fights that this hero becomes the winner of the tournament.Calculate the number of possible winners among $$$n$$$ heroes.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of heroes. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the initial level of the $$$i$$$-th hero.", "output_spec": "For each test case, print one integer — the number of possible winners among the given $$$n$$$ heroes.", "sample_inputs": ["3\n3\n3 2 2\n2\n5 5\n4\n1 3 3 7"], "sample_outputs": ["1\n0\n3"], "notes": "NoteIn the first test case of the example, the only possible winner is the first hero.In the second test case of the example, each fight between the heroes results in nobody winning it, so the tournament lasts forever and there is no winner."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\r\n\r\nvoid get(Args...)(ref Args args)\r\n{\r\n import std.traits, std.meta, std.typecons;\r\n\r\n static if (Args.length == 1) {\r\n alias Arg = Args[0];\r\n \r\n static if (isArray!Arg) {\r\n static if (isSomeChar!(ElementType!Arg)) {\r\n args[0] = readln.chomp.to!Arg;\r\n } else {\r\n args[0] = readln.split.to!Arg;\r\n }\r\n } else static if (isTuple!Arg) {\r\n auto input = readln.split;\r\n static foreach (i; 0..Fields!Arg.length) {\r\n args[0][i] = input[i].to!(Fields!Arg[i]);\r\n }\r\n } else {\r\n args[0] = readln.chomp.to!Arg;\r\n }\r\n } else {\r\n auto input = readln.split;\r\n assert(input.length == Args.length);\r\n\r\n static foreach (i; 0..Args.length) {\r\n args[i] = input[i].to!(Args[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid get_lines(Args...)(size_t N, ref Args args)\r\n{\r\n import std.traits, std.range;\r\n\r\n static foreach (i; 0..Args.length) {\r\n static assert(isArray!(Args[i]));\r\n args[i].length = N;\r\n }\r\n\r\n foreach (i; 0..N) {\r\n static if (Args.length == 1) {\r\n get(args[0][i]);\r\n } else {\r\n auto input = readln.split;\r\n static foreach (j; 0..Args.length) {\r\n args[j][i] = input[j].to!(ElementType!(Args[j]));\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid main()\r\n{\r\n int T; get(T);\r\n while (T--) {\r\n int N; get(N);\r\n int[] aa; get(aa);\r\n int min_a = int.max, c;\r\n foreach (a; aa) {\r\n if (min_a > a) {\r\n min_a = a;\r\n c = 1;\r\n } else if (a == min_a) {\r\n ++c;\r\n }\r\n }\r\n writeln(N - c); \r\n }\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\twriteln (n - a.count (a.minElement));\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort;\r\n\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] != a[0])\r\n\t\t\t{\r\n\t\t\t\tans[ti] = n - i;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "99e5c907b623c310d6f1599f485ca21d"} {"nl": {"description": "A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].After each query you have to determine whether the number of inversions is odd or even.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct. The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process. Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.", "output_spec": "Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.", "sample_inputs": ["3\n1 2 3\n2\n1 2\n2 3", "4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3"], "sample_outputs": ["odd\neven", "odd\nodd\nodd\neven"], "notes": "NoteThe first example: after the first query a = [2, 1, 3], inversion: (2, 1); after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2). The second example: a = [1, 2, 4, 3], inversion: (4, 3); a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3); a = [1, 2, 4, 3], inversion: (4, 3); a = [1, 4, 2, 3], inversions: (3, 2), (4, 2). "}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto A = readln.split.map!(to!int).array;\n int ans = 0;\n\n foreach (i; 0..N) {\n foreach (j; i+1..N) {\n if (A[i] > A[j]) {\n ans ^= 1;\n }\n }\n }\n\n\n auto Q = readln.chomp.to!int;\n while (Q--) {\n auto lr = readln.split.map!(to!int);\n auto d = lr[1] - lr[0] + 1;\n ans ^= (d * (d - 1) / 2) % 2;\n writeln(ans%2 ? \"odd\" : \"even\");\n }\n\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm, std.numeric;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\n\nalias Pair = Tuple!(dchar, \"col\", int, \"len\");\n\nvoid main() {\n int n, m;\n scan(n);\n auto a = readln.split.to!(int[]);\n scan(m);\n\n int inv;\n foreach (i ; 0 .. n) {\n foreach (j ; i + 1 .. n) {\n if (a[i] > a[j]) inv++;\n }\n }\n\n inv &= 1;\n\n foreach (i ; 0 .. m) {\n int l, r;\n scan(l, r);\n inv ^= ((r - l + 1) * (r - l) / 2) & 1;\n writeln(inv ? \"odd\" : \"even\");\n }\n}\n\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}"}], "negative_code": [], "src_uid": "d20cc952cdf3a99e7d980a0270c49f78"} {"nl": {"description": "Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?", "input_spec": "The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.", "output_spec": "Print a single integer — the minimum possible valid width of the road.", "sample_inputs": ["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"], "sample_outputs": ["4", "6", "11"], "notes": "NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11."}, "positive_code": [{"source_code": "import std.stdio;\n\nvoid main()\n{\n\tint n, h, x;\n\tint result = 0;\n\t\n\tscanf(\"%d%d\", &n, &h);\n\tfor (int i = 0; i < n; i++) {\n\t\tscanf(\"%d\", &x);\n\t\tresult += (x > h) ? 2 : 1;\n\t}\n\tprintf(\"%d\\n\", result);\n}\n\n"}, {"source_code": "import std.stdio: writeln, stdin;\nimport std.string: strip,tr;\nimport std.algorithm.iteration: uniq, map, sum;\nimport std.algorithm.mutation: copy;\nimport std.algorithm.sorting: sort;\nimport std.regex;\nimport std.conv;\nimport std.array;\n\nvoid main()\n{\n auto nh = stdin.readln.strip.split(regex(` +`)).map!(to!int).array;\n auto as = stdin.readln.strip.split(regex(` +`)).map!(to!int);\n auto h = nh[1];\n writeln (as.map!(a=>a<=h?1:2).sum);\n}"}, {"source_code": "import std.stdio;\nimport core.stdc.stdio;\nimport std.algorithm;\nimport std.math;\nimport std.array;\n\nint main(string[] argv)\n{\n\tint n, h;\n\tscanf(\"%d %d\", &n, &h);\n\tint width = 0;\n\tforeach (int i; 0..n)\n\t{\n\t\tint fh;\n\t\tscanf(\"%d\", &fh);\n\t\twidth += (fh > h ? 2 : 1);\n\t}\n\tprintf(\"%d\", width);\n\treturn 0;\n}"}, {"source_code": "import std.stdio, std.string, std.conv, std.algorithm;\nimport std.math;\n\n\nvoid main(){\n\n\tint n, h, x;\n\tint result = 0;\n\t\n\tscanf(\"%d%d\", &n, &h);\n\tfor (int i = 0; i < n; i++){\n\t\tscanf(\"%d\", &x);\n\t\tresult += (x > h) ? 2 : 1;\n\t}\n\n\tprintf(\"%d\\n\", result);\n}\n\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.stdio;\n\nvoid main() {\n int n, h;\n readf!\" %s %s \"(n, h);\n auto ans = readln.splitter\n .map!(to!int)\n .map!(x => x > h ? 2 : 1)\n .sum;\n writeln(ans);\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.stdio;\n\nvoid main() {\n int n, h;\n readf!\" %s %s \"(n, h);\n // auto ans = readln.splitter\n // .map!(to!int)\n // .map!(x => x > h ? 2 : 1)\n // .sum;\n // writeln(ans);\n int ans = 0;\n foreach (i; 0 .. n) {\n int x;\n readf!\" %s\"(x);\n ans += 1;\n if (x > h) {\n ans += 1;\n }\n }\n writeln(ans);\n}\n"}, {"source_code": "import std.stdio;\n\nint main()\n{\n\tint a=0;\n\tint b=0;\n\treadf(\" %d\", &a);\n\treadf(\" %d\", &b);\n\tint count=0;\n\tfor (int i=0; ib)\n\t\t{\n\t\t\tcount=count+1;\n\t\t}\n\t}\n\twriteln(a+count);\n\treturn 0;\n}"}], "negative_code": [], "src_uid": "e7ed5a733e51b6d5069769c3b1d8d22f"} {"nl": {"description": "This is an interactive task.Scientists are about to invent a new optimization for the Floyd-Warshall algorithm, which will allow it to work in linear time. There is only one part of the optimization still unfinished.It is well known that the Floyd-Warshall algorithm takes a graph with $$$n$$$ nodes and exactly one edge between each pair of nodes. The scientists have this graph, what is more, they have directed each edge in one of the two possible directions.To optimize the algorithm, exactly $$$m$$$ edges are colored in pink color and all the rest are colored in green. You know the direction of all $$$m$$$ pink edges, but the direction of green edges is unknown to you. In one query you can ask the scientists about the direction of exactly one green edge, however, you can perform at most $$$2 \\cdot n$$$ such queries.Your task is to find the node from which every other node can be reached by a path consisting of edges of same color. Be aware that the scientists may have lied that they had fixed the direction of all edges beforehand, so their answers may depend on your queries.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100\\,000$$$, $$$0 \\le m \\le 100\\,000$$$) — the number of nodes and the number of pink edges. The next $$$m$$$ lines describe the pink edges, the $$$i$$$-th of these lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \\le u_i, v_i \\le n$$$, $$$u_i \\ne v_i$$$) — the start and the end of the $$$i$$$-th pink edge. It is guaranteed, that all unordered pairs $$$(u_i, v_i)$$$ are distinct.", "output_spec": "When you found the answer, print \"!\" and the number of the node from which every other node can be reached by a single-colored path.", "sample_inputs": ["4 2\n1 2\n3 4\n0\n1\n1"], "sample_outputs": ["? 1 3\n? 4 2\n? 3 2\n! 3"], "notes": "NoteIn the example above the answer for the query \"? 1 3\" is 0, so the edge is directed from 3 to 1. The answer for the query \"? 4 2\" is 1, so the edge is directed from 4 to 2. The answer for the query \"? 3 2\" is 1, so the edge is directed from 3 to 2. So there are green paths from node 3 to nodes 1 and 2 and there is a pink path from node 3 to node 4."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint Ask(int u, int v) {\n writefln(\"? %s %s\", u + 1, v + 1);\n stdout.flush;\n const res = readInt();\n if (res == -1) {\n import core.stdc.stdlib;\n exit(0);\n }\n return res;\n}\nvoid Answer(int u) {\n writefln(\"! %s\", u + 1);\n stdout.flush;\n}\n\nint N, M;\nint[] U, V;\n\nint[][][] G;\nint C;\nint[] scc;\nint[] post;\n\nvoid dfs(int s, int u) {\n scc[u] = C;\n foreach (v; G[s][u]) {\n if (scc[v] == ((s == 0) ? -1 : -2)) {\n dfs(s, v);\n }\n }\n if (s == 0) {\n post ~= u;\n }\n}\n\nvoid main() {\n N = readInt();\n M = readInt();\n U = new int[M];\n V = new int[M];\n foreach (i; 0 .. M) {\n U[i] = readInt() - 1;\n V[i] = readInt() - 1;\n }\n \n G = new int[][][](2, N);\n foreach (i; 0 .. M) {\n G[0][U[i]] ~= V[i];\n G[1][V[i]] ~= U[i];\n }\n C = -2;\n scc = new int[N];\n scc[] = -1;\n foreach (u; 0 .. N) {\n if (scc[u] == -1) {\n dfs(0, u);\n }\n }\n C = 0;\n foreach_reverse (u; post) {\n if (scc[u] == -2) {\n dfs(1, u);\n ++C;\n }\n }\n debug {\n writeln(\"scc = \", scc);\n }\n \n auto H = new int[][C];\n auto inDeg = new int[C];\n foreach (i; 0 .. M) {\n const c = scc[U[i]], d = scc[V[i]];\n if (c != d) {\n H[c] ~= d;\n ++inDeg[d];\n }\n }\n \n auto ls = new DList!int[C];\n foreach (u; 0 .. N) {\n ls[scc[u]].insertBack(u);\n }\n DList!int q;\n foreach (c; 0 .. C) {\n if (inDeg[c] == 0) {\n q.insertBack(c);\n }\n }\n \n int src = ls[q.front].front;\n for (; ; ) {\n debug {\n writeln(\"ls = \", ls.map!(l => l[]));\n writeln(\"q = \", q[]);\n }\n if (q.front == scc[src]) {\n const c0 = q.front; q.removeFront;\n if (q.empty) {\n break;\n }\n const c1 = q.front; q.removeFront;\n q.insertFront(c0);\n q.insertFront(c1);\n }\n int u = ls[q.front].front;\n if (Ask(src, u) == 0) {\n swap(src, u);\n }\n const c = scc[u];\n if (q.front != c) {\n const c0 = q.front; q.removeFront;\n const c1 = q.front; q.removeFront;\n q.insertFront(c0);\n q.insertFront(c1);\n }\n ls[c].removeFront;\n if (ls[c].empty) {\n q.removeFront;\n foreach (d; H[c]) {\n if (--inDeg[d] == 0) {\n q.insertBack(d);\n }\n }\n }\n }\n Answer(src);\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint Ask(int u, int v) {\n writefln(\"? %s %s\", u + 1, v + 1);\n stdout.flush;\n const res = readInt();\n if (res == -1) {\n import core.stdc.stdlib;\n exit(0);\n }\n return res;\n}\nvoid Answer(int u) {\n writefln(\"! %s\", u + 1);\n stdout.flush;\n}\n\nint N, M;\nint[] U, V;\n\nint[][][] G;\nint C;\nint[] scc;\nint[] post;\n\nvoid dfs(int s, int u) {\n scc[u] = C;\n foreach (v; G[s][u]) {\n if (scc[v] == ((s == 0) ? -1 : -2)) {\n dfs(s, v);\n }\n }\n if (s == 0) {\n post ~= u;\n }\n}\n\nvoid main() {\n N = readInt();\n M = readInt();\n U = new int[M];\n V = new int[M];\n foreach (i; 0 .. M) {\n U[i] = readInt() - 1;\n V[i] = readInt() - 1;\n }\n \n G = new int[][][](2, N);\n foreach (i; 0 .. M) {\n G[0][U[i]] ~= V[i];\n G[1][V[i]] ~= U[i];\n }\n C = -2;\n scc = new int[N];\n scc[] = -1;\n foreach (u; 0 .. N) {\n if (scc[u] == -1) {\n dfs(0, u);\n }\n }\n C = 0;\n foreach_reverse (u; post) {\n if (scc[u] == -2) {\n dfs(1, u);\n ++C;\n }\n }\n debug {\n writeln(\"scc = \", scc);\n }\n \n auto H = new int[][C];\n auto inDeg = new int[C];\n foreach (i; 0 .. M) {\n const c = scc[U[i]], d = scc[U[i]];\n if (c != d) {\n H[c] ~= d;\n ++inDeg[d];\n }\n }\n \n auto ls = new DList!int[C];\n foreach (u; 0 .. N) {\n ls[scc[u]].insertBack(u);\n }\n DList!int q;\n foreach (c; 0 .. C) {\n if (inDeg[c] == 0) {\n q.insertBack(c);\n }\n }\n debug {\n writeln(\"ls = \", ls.map!(l => l[]));\n writeln(\"q = \", q[]);\n }\n \n int src = ls[q.front].front;\n ls[q.front].removeFront;\n if (ls[q.front].empty) {\n const c = q.front;\n q.removeFront;\n foreach (d; H[c]) {\n if (--inDeg[d] == 0) {\n q.insertBack(d);\n }\n }\n }\n for (; !q.empty; ) {\n if (scc[src] == q.front) {\n const c0 = q.front; q.removeFront;\n const c1 = q.front; q.removeFront;\n q.insertFront(c0);\n q.insertFront(c1);\n }\n const c = q.front;\n const u = ls[c].front;\n ls[c].removeFront;\n if (Ask(src, u) == 0) {\n src = u;\n }\n if (ls[c].empty) {\n q.removeFront;\n foreach (d; H[c]) {\n if (--inDeg[d] == 0) {\n q.insertBack(d);\n }\n }\n }\n }\n Answer(src);\n}\n"}], "src_uid": "a39019f40e23666196d359bc51add150"} {"nl": {"description": "There are $$$n$$$ astronauts working on some space station. An astronaut with the number $$$i$$$ ($$$1 \\le i \\le n$$$) has power $$$a_i$$$.An evil humanoid has made his way to this space station. The power of this humanoid is equal to $$$h$$$. Also, the humanoid took with him two green serums and one blue serum.In one second , a humanoid can do any of three actions: to absorb an astronaut with power strictly less humanoid power; to use green serum, if there is still one left; to use blue serum, if there is still one left. When an astronaut with power $$$a_i$$$ is absorbed, this astronaut disappears, and power of the humanoid increases by $$$\\lfloor \\frac{a_i}{2} \\rfloor$$$, that is, an integer part of $$$\\frac{a_i}{2}$$$. For example, if a humanoid absorbs an astronaut with power $$$4$$$, its power increases by $$$2$$$, and if a humanoid absorbs an astronaut with power $$$7$$$, its power increases by $$$3$$$.After using the green serum, this serum disappears, and the power of the humanoid doubles, so it increases by $$$2$$$ times.After using the blue serum, this serum disappears, and the power of the humanoid triples, so it increases by $$$3$$$ times.The humanoid is wondering what the maximum number of astronauts he will be able to absorb if he acts optimally.", "input_spec": "The first line of each test contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — number of test cases. The first line of each test case contains integers $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — number of astronauts and $$$h$$$ ($$$1 \\le h \\le 10^6$$$) — the initial power of the humanoid. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^8$$$) — powers of astronauts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, in a separate line, print the maximum number of astronauts that a humanoid can absorb.", "sample_inputs": ["8\n\n4 1\n\n2 1 8 9\n\n3 3\n\n6 2 60\n\n4 5\n\n5 1 100 5\n\n3 2\n\n38 6 3\n\n1 1\n\n12\n\n4 6\n\n12 12 36 100\n\n4 1\n\n2 1 1 15\n\n3 5\n\n15 1 13"], "sample_outputs": ["4\n3\n3\n3\n0\n4\n4\n3"], "notes": "NoteIn the first case, you can proceed as follows: use green serum. $$$h = 1 \\cdot 2 = 2$$$ absorb the cosmonaut $$$2$$$. $$$h = 2 + \\lfloor \\frac{1}{2} \\rfloor = 2$$$ use green serum. $$$h = 2 \\cdot 2 = 4$$$ absorb the spaceman $$$1$$$. $$$h = 4 + \\lfloor \\frac{2}{2} \\rfloor = 5$$$ use blue serum. $$$h = 5 \\cdot 3 = 15$$$ absorb the spaceman $$$3$$$. $$$h = 15 + \\lfloor \\frac{8}{2} \\rfloor = 19$$$ absorb the cosmonaut $$$4$$$. $$$h = 19 + \\lfloor \\frac{9}{2} \\rfloor = 23$$$ "}, "positive_code": [{"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nint solve(long[] a, long h, long[] mulorder)\n{\n int i = 0;\n while (i < a.length) {\n if (a[i] < h) {\n h += a[i] / 2;\n i++;\n continue;\n }\n if (mulorder.length > 0) {\n h *= mulorder.front;\n mulorder = mulorder[1 .. $];\n continue;\n }\n break;\n }\n return i;\n}\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n long n, h;\n readf!\" %d %d \"(n, h);\n auto a = readln.splitter.map!(to!long).array.sort.array;\n int best = -1;\n foreach (p ; [2L, 2L, 3L].permutations) {\n best = max(best, solve(a, h, p.array));\n }\n writeln(best);\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nint solve (int n, long h, int [] a, int [] p)\r\n{\r\n\tforeach (int k, ref c; a)\r\n\t{\r\n\t\twhile (h <= c && !p.empty)\r\n\t\t{\r\n\t\t\th *= p.front;\r\n\t\t\tp.popFront ();\r\n\t\t}\r\n\t\tif (h <= c)\r\n\t\t{\r\n\t\t\treturn k;\r\n\t\t}\r\n\t\th += c / 2;\r\n\t}\r\n\treturn n;\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, h;\r\n\t\treadf !(\" %s %s\") (n, h);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tsort (a);\r\n\r\n\t\tauto p = [2, 2, 3];\r\n\t\tint res = 0;\r\n\t\tdo\r\n\t\t{\r\n\t\t\tres = max (res, solve (n, h, a, p));\r\n\t\t}\r\n\t\twhile (nextPermutation (p));\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nint solve1(long[] a, long h)\n{\n int mul2cnt = 2;\n int mul3cnt = 1;\n int i = 0;\n while (i < a.length) {\n if (a[i] < h) {\n h += a[i] / 2;\n } else if (a[i] < h * 2 && mul2cnt > 0) {\n h *= 2;\n h += a[i] / 2;\n mul2cnt--;\n } else if (a[i] < h * 3 && mul3cnt > 0) {\n h *= 3;\n h += a[i] / 2;\n mul3cnt--;\n } else {\n if (mul2cnt > 0) {\n h *= 2;\n mul2cnt--;\n continue;\n }\n if (mul3cnt > 0) {\n h *= 3;\n mul3cnt--;\n continue;\n }\n break;\n }\n i++;\n }\n return i;\n}\n\nint solve2(long[] a, long h)\n{\n int mul2cnt = 2;\n int mul3cnt = 1;\n int i = 0;\n while (i < a.length) {\n if (a[i] < h) {\n h += a[i] / 2;\n } else if (a[i] < h * 3 && mul3cnt > 0) {\n h *= 3;\n h += a[i] / 2;\n mul3cnt--;\n } else if (a[i] < h * 2 && mul2cnt > 0) {\n h *= 2;\n h += a[i] / 2;\n mul2cnt--;\n } else {\n if (mul3cnt > 0) {\n h *= 3;\n mul3cnt--;\n continue;\n }\n if (mul2cnt > 0) {\n h *= 2;\n mul2cnt--;\n continue;\n }\n break;\n }\n i++;\n }\n return i;\n}\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n long n, h;\n readf!\" %d %d \"(n, h);\n auto a = readln.splitter.map!(to!long).array.sort.array;\n writeln(max(solve1(a, h), solve2(a, h)));\n }\n}\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nint solve1(long[] a, long h)\n{\n int mul2cnt = 2;\n int mul3cnt = 1;\n int i = 0;\n while (i < a.length) {\n if (a[i] < h) {\n h += a[i] / 2;\n } else if (a[i] < h * 2 && mul2cnt > 0) {\n h *= 2;\n h += a[i] / 2;\n mul2cnt--;\n } else if (a[i] < h * 3 && mul3cnt > 0) {\n h *= 3;\n h += a[i] / 2;\n mul3cnt--;\n } else {\n break;\n }\n i++;\n }\n return i;\n}\n\nint solve2(long[] a, long h)\n{\n int mul2cnt = 2;\n int mul3cnt = 1;\n int i = 0;\n while (i < a.length) {\n if (a[i] < h) {\n h += a[i] / 2;\n } else if (a[i] < h * 3 && mul3cnt > 0) {\n h *= 3;\n h += a[i] / 2;\n mul3cnt--;\n } else if (a[i] < h * 2 && mul2cnt > 0) {\n h *= 2;\n h += a[i] / 2;\n mul2cnt--;\n } else {\n break;\n }\n i++;\n }\n return i;\n}\n\nvoid main()\n{\n foreach (casenum ; 1 .. readln.strip.to!int + 1) {\n long n, h;\n readf!\" %d %d \"(n, h);\n auto a = readln.splitter.map!(to!long).array.sort.array;\n writeln(max(solve1(a, h), solve2(a, h)));\n }\n}\n"}], "src_uid": "1f46c4ba21e734a1c7de8b2434791f77"} {"nl": {"description": "You are given two arrays of integers $$$a_1,\\ldots,a_n$$$ and $$$b_1,\\ldots,b_m$$$.Your task is to find a non-empty array $$$c_1,\\ldots,c_k$$$ that is a subsequence of $$$a_1,\\ldots,a_n$$$, and also a subsequence of $$$b_1,\\ldots,b_m$$$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\\le n,m\\le 1000$$$)  — the lengths of the two arrays. The second line of each test case contains $$$n$$$ integers $$$a_1,\\ldots,a_n$$$ ($$$1\\le a_i\\le 1000$$$)  — the elements of the first array. The third line of each test case contains $$$m$$$ integers $$$b_1,\\ldots,b_m$$$ ($$$1\\le b_i\\le 1000$$$)  — the elements of the second array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ across all test cases does not exceed $$$1000$$$ ($$$\\sum\\limits_{i=1}^t n_i, \\sum\\limits_{i=1}^t m_i\\le 1000$$$).", "output_spec": "For each test case, output \"YES\" if a solution exists, or \"NO\" otherwise. If the answer is \"YES\", on the next line output an integer $$$k$$$ ($$$1\\le k\\le 1000$$$)  — the length of the array, followed by $$$k$$$ integers $$$c_1,\\ldots,c_k$$$ ($$$1\\le c_i\\le 1000$$$)  — the elements of the array. If there are multiple solutions with the smallest possible $$$k$$$, output any.", "sample_inputs": ["5\n4 5\n10 8 6 4\n1 2 3 4 5\n1 1\n3\n3\n1 1\n3\n2\n5 3\n1000 2 2 2 3\n3 1 5\n5 5\n1 2 3 4 5\n1 2 3 4 5"], "sample_outputs": ["YES\n1 4\nYES\n1 3\nNO\nYES\n1 3\nYES\n1 2"], "notes": "NoteIn the first test case, $$$[4]$$$ is a subsequence of $$$[10, 8, 6, 4]$$$ and $$$[1, 2, 3, 4, 5]$$$. This array has length $$$1$$$, it is the smallest possible length of a subsequence of both $$$a$$$ and $$$b$$$.In the third test case, no non-empty subsequences of both $$$[3]$$$ and $$$[2]$$$ exist, so the answer is \"NO\"."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\nmultitest_loop:\n\tforeach (test; 0..tests)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto b = readln.splitter.map !(to !(int)).array;\n\t\tforeach (x; a)\n\t\t{\n\t\t\tforeach (y; b)\n\t\t\t{\n\t\t\t\tif (x == y)\n\t\t\t\t{\n\t\t\t\t\twriteln (\"YES\");\n\t\t\t\t\twriteln (1, \" \", x);\n\t\t\t\t\tcontinue multitest_loop;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (\"NO\");\n\t}\n}\n"}, {"source_code": "import std.stdio;\nimport std.range;\nimport std.array;\nimport std.string;\nimport std.conv;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.variant;\n\nvoid main() {\n\tint tt = readln().chomp.to!int;\n\tforeach (t; 0 .. tt) {\n\t\treadln();\n\t\tint[] a = readln().chomp.split(\" \").map!(to!int).array;\n\t\tint[] b = readln().chomp.split(\" \").map!(to!int).array;\n\t\tbool[int] aSet;\n\t\tforeach (i; a) {\n\t\t\taSet[i] = true;\n\t\t}\n\t\tNullable!int ans;\n\t\tforeach (i; b) {\n\t\t\tif (i in aSet) {\n\t\t\t\tans = Nullable!int(i);\n\t\t\t}\n\t\t}\n\t\tif (ans.isNull) {\n\t\t\twriteln(\"NO\");\n\t\t}\n\t\telse {\n\t\t\twriteln(\"YES\");\n\t\t\twriteln(\"1 \", ans.get);\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "776a06c14c6fa3ef8664eec0b4d50824"} {"nl": {"description": "Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence $$$a_1, a_2, \\ldots, a_n$$$ of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.Let's denote the fingers of hand by numbers from $$$1$$$ to $$$5$$$. We call a fingering any sequence $$$b_1, \\ldots, b_n$$$ of fingers numbers. A fingering is convenient if for all $$$1\\leq i \\leq n - 1$$$ the following holds: if $$$a_i < a_{i+1}$$$ then $$$b_i < b_{i+1}$$$, because otherwise Paul needs to take his hand off the keyboard to play the $$$(i+1)$$$-st note; if $$$a_i > a_{i+1}$$$ then $$$b_i > b_{i+1}$$$, because of the same; if $$$a_i = a_{i+1}$$$ then $$$b_i\\neq b_{i+1}$$$, because using the same finger twice in a row is dumb. Please note that there is $$$\\neq$$$, not $$$=$$$ between $$$b_i$$$ and $$$b_{i+1}$$$. Please provide any convenient fingering or find out that there is none.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) denoting the number of notes. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 2\\cdot10^5$$$) denoting the positions of notes on the keyboard.", "output_spec": "If there is no convenient fingering, print $$$-1$$$. Otherwise, print $$$n$$$ numbers $$$b_1, b_2, \\ldots, b_n$$$, each from $$$1$$$ to $$$5$$$, denoting a convenient fingering, separated by spaces.", "sample_inputs": ["5\n1 1 4 2 2", "7\n1 5 7 8 10 3 1", "19\n3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8"], "sample_outputs": ["1 4 5 4 5", "1 2 3 4 5 4 3", "1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4"], "notes": "NoteThe third sample test is kinda \"Non stop\" song by Reflex."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf(\"%s\", &n);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto via = new int[][] (n+1, 6);\n foreach (i; 1 .. n) {\n foreach (nxt; 1 .. 6) {\n via[i+1][nxt] = -1;\n \n foreach (prev; 1 .. 6) {\n if (via[i][prev] == -1) { continue; }\n \n if (arr[i-1] == arr[i] && prev == nxt) { continue; }\n if (arr[i-1] > arr[i] && prev <= nxt) { continue; }\n if (arr[i-1] < arr[i] && prev >= nxt) { continue; }\n \n via[i+1][nxt] = prev;\n }\n }\n }\n \n debug { via.each!writeln; }\n \n int f = 1;\n while (f <= 5 && via[n][f] == -1) { ++f; }\n \n if (f == 6) {\n writeln(-1);\n return;\n }\n \n int[] ans;\n ans ~= f;\n foreach_reverse (i; 2 .. n+1) {\n ans ~= via[i][f];\n f = via[i][f];\n }\n \n ans.reverse();\n ans.writefln!\"%(%s %)\";\n}"}], "negative_code": [], "src_uid": "c63b62ad5b8d0b274599b60442766e17"} {"nl": {"description": "AmShZ has traveled to Italy from Iran for the Thom Yorke concert. There are $$$n$$$ cities in Italy indexed from $$$1$$$ to $$$n$$$ and $$$m$$$ directed roads indexed from $$$1$$$ to $$$m$$$. Initially, Keshi is located in the city $$$1$$$ and wants to go to AmShZ's house in the city $$$n$$$. Since Keshi doesn't know the map of Italy, AmShZ helps him to see each other as soon as possible.In the beginning of each day, AmShZ can send one of the following two messages to Keshi: AmShZ sends the index of one road to Keshi as a blocked road. Then Keshi will understand that he should never use that road and he will remain in his current city for the day. AmShZ tells Keshi to move. Then, Keshi will randomly choose one of the cities reachable from his current city and move there. (city $$$B$$$ is reachable from city $$$A$$$ if there's an out-going road from city $$$A$$$ to city $$$B$$$ which hasn't become blocked yet). If there are no such cities, Keshi will remain in his current city.Note that AmShZ always knows Keshi's current location. AmShZ and Keshi want to find the smallest possible integer $$$d$$$ for which they can make sure that they will see each other after at most $$$d$$$ days. Help them find $$$d$$$.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ $$$(2 \\le n \\le 2 \\cdot 10^5, 1 \\le m \\le 2 \\cdot 10^5)$$$  — the number of cities and roads correspondingly. The $$$i$$$-th line of the following $$$m$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ $$$(1 \\le v_i , u_i \\le n,v_i \\neq u_i)$$$, denoting a directed road going from city $$$v_i$$$ to city $$$u_i$$$. It is guaranteed that there is at least one route from city $$$1$$$ to city $$$n$$$. Note that there may be more than one road between a pair of cities in each direction.", "output_spec": "Output the smallest possible integer $$$d$$$ to make sure that AmShZ and Keshi will see each other after at most $$$d$$$ days.", "sample_inputs": ["2 1\n1 2", "4 4\n1 2\n1 4\n2 4\n1 4", "5 7\n1 2\n2 3\n3 5\n1 4\n4 3\n4 5\n3 1"], "sample_outputs": ["1", "2", "4"], "notes": "NoteIn the first sample, it's enough for AmShZ to send the second type of message.In the second sample, on the first day, AmShZ blocks the first road. So the only reachable city from city $$$1$$$ will be city $$$4$$$. Hence on the second day, AmShZ can tell Keshi to move and Keshi will arrive at AmShZ's house.It's also possible for AmShZ to tell Keshi to move for two days."}, "positive_code": [{"source_code": "import std.container;\r\nimport std.stdio;\r\nimport std.typecons;\r\n\r\nimmutable int infinity = int.max / 4;\r\n\r\nvoid main ()\r\n{\r\n\tint n, m;\r\n\twhile (readf !(\" %s %s\") (n, m) > 0)\r\n\t{\r\n\t\tauto adj = new int [] [n];\r\n\t\tauto adjR = new int [] [n];\r\n\t\tauto deg = new int [n];\r\n\t\tforeach (j; 0..m)\r\n\t\t{\r\n\t\t\tint u, v;\r\n\t\t\treadf !(\" %s %s\") (u, v);\r\n\t\t\tu -= 1;\r\n\t\t\tv -= 1;\r\n\t\t\tadj[u] ~= v;\r\n\t\t\tadjR[v] ~= u;\r\n\t\t\tdeg[u] += 1;\r\n\t\t}\r\n\r\n\t\tauto d = new int [n];\r\n\t\td[] = infinity;\r\n\t\td[n - 1] = 0;\r\n\t\tauto used = new bool [n];\r\n\t\talias Record = Tuple !(int, q{dist}, int, q{v});\r\n\t\tauto t = redBlackTree !(Record) ();\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tt.insert (Record (d[i], i));\r\n\t\t}\r\n\t\twhile (!t.empty)\r\n\t\t{\r\n\t\t\tauto cur = t.front;\r\n\t\t\tt.removeFront ();\r\n\t\t\tused[cur.v] = true;\r\n\t\t\tforeach (u; adjR[cur.v])\r\n\t\t\t{\r\n\t\t\t\tdeg[u] -= 1;\r\n\t\t\t\tif (!used[u] && d[u] > deg[u] + d[cur.v] + 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tt.removeKey (Record (d[u], u));\r\n\t\t\t\t\td[u] = deg[u] + d[cur.v] + 1;\r\n\t\t\t\t\tt.insert (Record (d[u], u));\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\twriteln (d[0]);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum INF = 10^^9;\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt;\n const M = readInt;\n auto A = new int[M];\n auto B = new int[M];\n foreach (i; 0 .. M) {\n A[i] = readInt - 1;\n B[i] = readInt - 1;\n }\n \n auto ds = new int[N];\n auto G = new int[][N];\n foreach (i; 0 .. M) {\n ++ds[A[i]];\n G[B[i]] ~= i;\n }\n \n alias Entry = Tuple!(int, \"c\", int, \"u\");\n BinaryHeap!(Array!Entry, \"a.c > b.c\") que;\n auto vis = new bool[N];\n auto dist = new int[N];\n dist[] = INF;\n dist[N - 1] = 0;\n que.insert(Entry(0, N - 1));\n for (; !que.empty; ) {\n const c = que.front.c;\n const u = que.front.u;\n que.removeFront;\n if (!vis[u]) {\n vis[u] = true;\n foreach (i; G[u]) {\n const v = A[i];\n const cc = c + 1 + (--ds[v]);\n debug {\n writefln(\"%s <- %s: %s\", u, v, cc);\n }\n if (chmin(dist[v], cc)) {\n que.insert(Entry(cc, v));\n }\n }\n }\n }\n debug {\n writeln(\"dist = \", dist);\n }\n writeln(dist[0]);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "1a83878ec600c87e74b48d6fdda89d4e"} {"nl": {"description": "You are given two sequences $$$a_1, a_2, \\dots, a_n$$$ and $$$b_1, b_2, \\dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \\begin{cases} a_i b_i & \\mbox{if }a_i > b_i \\\\ 0 & \\mbox{if }a_i = b_i \\\\ -a_i b_i & \\mbox{if }a_i < b_i \\end{cases}$$$You'd like to make $$$\\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \\le x_1, y_1, z_1 \\le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \\le x_2, y_2, z_2 \\le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.", "output_spec": "For each test case, print the maximum possible sum of the sequence $$$c$$$.", "sample_inputs": ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"], "sample_outputs": ["4\n2\n0"], "notes": "NoteIn the first sample, one of the optimal solutions is:$$$a = \\{2, 0, 1, 1, 0, 2, 1\\}$$$$$$b = \\{1, 0, 1, 0, 2, 1, 0\\}$$$$$$c = \\{2, 0, 0, 0, 0, 2, 0\\}$$$In the second sample, one of the optimal solutions is:$$$a = \\{0, 2, 0, 0, 0\\}$$$$$$b = \\{1, 1, 0, 1, 0\\}$$$$$$c = \\{0, 2, 0, 0, 0\\}$$$In the third sample, the only possible solution is:$$$a = \\{2\\}$$$$$$b = \\{2\\}$$$$$$c = \\{0\\}$$$"}, "positive_code": [{"source_code": "// Ported to D by unihernandez22\n// Solution by Vicfred\n// https://codeforces.com/contest/1401/problem/B\n\nimport std.stdio;\nimport std.string;\nimport std.conv;\nimport std.array;\nimport std.algorithm;\n\nvoid main() {\n int t = readln.chomp.to!int;\n foreach (_; 0 .. t) {\n int[] a = readln.split.map!(to!int).array,\n b = readln.split.map!(to!int).array;\n int ans = 0;\n \n if (a[2] > 0) {\n int taken = min(b[1], a[2]);\n a[2] -= taken;\n b[1] -= taken;\n\n ans += taken*2;\n }\n\n if (a[2] > 0) {\n int taken = min(b[2],a[2]);\n a[2] -= taken;\n b[2] -= taken;\n }\n\n if (a[2] > 0) {\n int taken = min(b[0],a[2]);\n a[2] -= taken;\n b[0] -= taken;\n }\n\n if (a[1] > 0) {\n int taken = min(b[1],a[1]);\n a[1] -= taken;\n b[1] -= taken;\n }\n\n if (a[1] > 0) {\n int taken = min(b[0],a[1]);\n a[1] -= taken;\n b[0] -= taken;\n }\n\n if (a[1] > 0) {\n int taken = min(b[2],a[1]);\n a[1] -= taken;\n b[2] -= taken;\n ans += taken*(-2);\n }\n\n writeln(ans);\n }\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto b = readln.splitter.map !(to !(int)).array;\n\n\t\tauto num (int i, int j)\n\t\t{\n\t\t\tint res = min (a[i], b[j]);\n\t\t\ta[i] -= res;\n\t\t\tb[j] -= res;\n\t\t\treturn res;\n\t\t}\n\n\t\tint res = num (2, 1) * 2;\n\t\tnum (1, 1);\n\t\tnum (1, 0);\n\t\tres -= num (1, 2) * 2;\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto xyz1 = RDA;\n\t\tauto xyz2 = RDA;\n\t\t\n\t\tauto x = min(xyz1[2], xyz2[1]);\n\t\tans[ti] += x * 2;\n\t\txyz1[0] -= x;\n\t\txyz2[1] -= x;\n\t\tauto y = xyz2[2] - (xyz1[0]+xyz1[2]);\n\t\tif (y > 0)\n\t\t\tans[ti] -= y * 2;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "e1de1e92fac8a6db3222c0d3c26843d8"} {"nl": {"description": "In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.You are given two strings s1, s2 and another string called virus. Your task is to find the longest common subsequence of s1 and s2, such that it doesn't contain virus as a substring.", "input_spec": "The input contains three strings in three separate lines: s1, s2 and virus (1 ≤ |s1|, |s2|, |virus| ≤ 100). Each string consists only of uppercase English letters.", "output_spec": "Output the longest common subsequence of s1 and s2 without virus as a substring. If there are multiple answers, any of them will be accepted. If there is no valid common subsequence, output 0.", "sample_inputs": ["AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ", "AA\nA\nA"], "sample_outputs": ["ORZ", "0"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.string, std.typecons;\nimport std.algorithm;\n\nint saiki(int i, int j, int k, string a, string b, string v, int[][][] dp, Tuple!(int, int, int)[][][] result, int[][] jump)\n{\n auto la = cast(int)a.length;\n auto lb = cast(int)b.length;\n auto lv = cast(int)v.length;\n if (dp[i][j][k] != -1)\n {\n return dp[i][j][k];\n }\n if (k == lv)\n {\n dp[i][j][k] = int.max;\n return int.max;\n }\n if (i == la || j == lb)\n {\n dp[i][j][k] = 0;\n return 0;\n }\n auto res = 0;\n if (a[i] == b[j])\n {\n auto ret = saiki(i + 1, j + 1, jump[k][a[i] - 'A'], a, b, v, dp, result, jump);\n if (ret + 1 > res)\n {\n result[i][j][k] = Tuple!(int, int, int)(i + 1, j + 1, jump[k][a[i] - 'A']);\n res = ret + 1;\n }\n }\n auto ret = saiki(i + 1, j, k, a, b, v, dp, result, jump);\n if (ret > res)\n {\n res = ret;\n result[i][j][k] = Tuple!(int, int, int)(i + 1, j, k);\n }\n ret = saiki(i, j + 1, k, a, b, v, dp, result, jump);\n if (ret > res)\n {\n res = ret;\n result[i][j][k] = Tuple!(int, int, int)(i, j + 1, k);\n }\n dp[i][j][k] = res;\n return res;\n}\n\nint[][] getJump(string s)\n{\n auto n = cast(int)s.length;\n auto jump = new int[][](n + 1, 26);\n foreach (i; 0 .. n)\n {\n foreach (j; 0 .. 26)\n {\n if (s[i] == 'A' + j)\n {\n jump[i][j] = i + 1;\n }\n else\n {\n jump[i][j] = 0;\n for (int k = i - 1; k >= 0; -- k)\n {\n if (s[k] == 'A' + j)\n {\n auto x = k - 1, y = i - 1;\n while (x >= 0 && y >= 0)\n {\n if (s[x] != s[y])\n {\n break;\n }\n -- x;\n -- y;\n }\n if (x < 0)\n {\n jump[i][j] = k + 1;\n break;\n }\n }\n }\n }\n }\n }\n return jump;\n}\n\nstring construct(string a, string b, string v, Tuple!(int, int, int)[][][] result, int[][] jump)\n{\n auto la = cast(int)a.length;\n auto lb = cast(int)b.length;\n auto lv = cast(int)v.length;\n auto i = 0, j = 0, k = 0;\n string res;\n while (i < la && j < lb)\n {\n if (result[i][j][k] == tuple(-1, -1, -1))\n {\n break;\n }\n auto ni = result[i][j][k][0];\n auto nj = result[i][j][k][1];\n auto nk = result[i][j][k][2];\n if (ni == i + 1 && nj == j + 1)\n {\n auto c = a[i];\n res ~= c;\n }\n i = ni;\n j = nj;\n k = nk;\n }\n return res;\n}\n\nvoid solve(string a, string b, string v)\n{\n auto la = cast(int)a.length;\n auto lb = cast(int)b.length;\n auto lv = cast(int)v.length;\n auto jump = getJump(v);\n auto dp = new int[][][](la + 1, lb + 1, lv + 1);\n auto result = new Tuple!(int, int, int)[][][](la + 1, lb + 1, lv + 1);\n foreach (i; 0 .. la + 1)\n {\n foreach (j; 0 .. lb + 1)\n {\n fill(dp[i][j], -1);\n fill(result[i][j], tuple(-1, -1, -1));\n }\n }\n auto ans = saiki(0, 0, 0, a, b, v, dp, result, jump);\n if (ans == 0)\n {\n writeln(0);\n }\n else\n {\n auto res = construct(a, b, v, result, jump);\n writeln(res);\n }\n}\n\nint main(string[] args)\n{\n string s0, s1, virus;\n s0 = readln().strip();\n s1 = readln().strip();\n virus = readln().strip();\n solve(s0, s1, virus);\n return 0;\n}"}, {"source_code": "import std.stdio, std.string, std.typecons;\nimport std.algorithm;\n\nint saiki(int i, int j, int k, int la, int lb, int lv, int[][][] dp, Tuple!(int, int, int)[][][] result, int[][] nexta, int[][] nextb, int[][] jump)\n{\n if (dp[i][j][k] != -1)\n {\n return dp[i][j][k];\n }\n if (k == lv)\n {\n dp[i][j][k] = int.max;\n return int.max;\n }\n if (i == la || j == lb)\n {\n dp[i][j][k] = 0;\n return 0;\n }\n auto res = 0;\n foreach (idx; 0 .. 26)\n {\n if (nexta[i][idx] != -1 && nextb[j][idx] != -1)\n {\n auto ret = saiki(nexta[i][idx] + 1, nextb[j][idx] + 1, jump[k][idx], la, lb, lv, dp, result, nexta, nextb, jump);\n if (ret != int.max)\n {\n if (ret + 1 > res)\n {\n result[i][j][k] = Tuple!(int, int, int)(nexta[i][idx], nextb[j][idx], jump[k][idx]);\n res = ret + 1;\n }\n }\n }\n }\n dp[i][j][k] = res;\n return res;\n}\n\nint[][] getNext(string s)\n{\n auto n = cast(int)s.length;\n auto next = new int[][](n + 1, 26);\n foreach (i; 0 .. n)\n {\n foreach (j; 0 .. 26)\n {\n next[i][j] = -1;\n foreach (k; i .. n)\n {\n if (s[k] == 'A' + j)\n {\n next[i][j] = k;\n break;\n }\n }\n }\n }\n return next;\n}\n\nint[][] getJump(string s)\n{\n auto n = cast(int)s.length;\n auto jump = new int[][](n + 1, 26);\n foreach (i; 0 .. n)\n {\n foreach (j; 0 .. 26)\n {\n if (s[i] == 'A' + j)\n {\n jump[i][j] = i + 1;\n }\n else\n {\n jump[i][j] = 0;\n for (int k = i - 1; k >= 0; -- k)\n {\n if (s[k] == 'A' + j)\n {\n auto x = k - 1, y = i - 1;\n while (x >= 0 && y >= 0)\n {\n if (s[x] != s[y])\n {\n break;\n }\n -- x;\n -- y;\n }\n if (x < 0)\n {\n jump[i][j] = k + 1;\n break;\n }\n }\n }\n }\n }\n }\n return jump;\n}\n\nstring configure(string a, string b, string v, Tuple!(int, int, int)[][][] result, int[][] jump)\n{\n auto la = cast(int)a.length;\n auto lb = cast(int)b.length;\n auto lv = cast(int)v.length;\n auto i = 0, j = 0, k = 0;\n string res;\n while (i < la && j < lb)\n {\n if (result[i][j][k] == tuple(-1, -1, -1))\n {\n break;\n }\n auto c = a[result[i][j][k][0]];\n res ~= c;\n auto ni = result[i][j][k][0] + 1;\n auto nj = result[i][j][k][1] + 1;\n auto nk = jump[k][c - 'A']; \n i = ni;\n j = nj;\n k = nk;\n }\n return res;\n}\n\nvoid solve(string a, string b, string v)\n{\n auto la = cast(int)a.length;\n auto lb = cast(int)b.length;\n auto lv = cast(int)v.length;\n auto nexta = getNext(a);\n auto nextb = getNext(b);\n auto jump = getJump(v);\n auto dp = new int[][][](la + 1, lb + 1, lv + 1);\n auto result = new Tuple!(int, int, int)[][][](la + 1, lb + 1, lv + 1);\n foreach (i; 0 .. la + 1)\n {\n foreach (j; 0 .. lb + 1)\n {\n fill(dp[i][j], -1);\n fill(result[i][j], tuple(-1, -1, -1));\n }\n }\n auto ans = saiki(0, 0, 0, la, lb, lv, dp, result, nexta, nextb, jump);\n if (ans == 0)\n {\n writeln(0);\n }\n else\n {\n auto res = configure(a, b, v, result, jump);\n writeln(res);\n }\n}\n\nint main(string[] args)\n{\n string s0, s1, virus;\n s0 = readln().strip();\n s1 = readln().strip();\n virus = readln().strip();\n solve(s0, s1, virus);\n return 0;\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tstring r;\n\twhile ((r = readln ().strip ()) != \"\")\n\t{\n\t\tstring s = readln ().strip ();\n\t\tstring t = readln ().strip ();\n\t\tint u = r.length;\n\t\tint v = s.length;\n\t\tint w = t.length;\n\t\tauto p = new int [w + 1];\n\t\tp[] = 0;\n\t\tforeach (i; 1..w + 1)\n\t\t{\n\t\t\tforeach (j; 1..i)\n\t\t\t{\n\t\t\t\tif (t[0..j] == t[i - j..i])\n\t\t\t\t{\n\t\t\t\t\tp[i] = max (p[i], j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tauto f = new int [] [] [] (u + 1, v + 1, w + 1);\n\t\talias Tuple !(int, int, int) prev;\n\t\tauto q = new prev [] [] [] (u + 1, v + 1, w + 1);\n\t\tforeach (i; 0..u + 1)\n\t\t{\n\t\t\tforeach (j; 0..v + 1)\n\t\t\t{\n\t\t\t\tforeach (k; 0..w + 1)\n\t\t\t\t{\n\t\t\t\t\tf[i][j][k] = -3;\n\t\t\t\t\tq[i][j][k] = prev (-3, -3, -3);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tf[0][0][0] = 0;\n\t\tforeach (int i; 0..u + 1)\n\t\t{\n\t\t\tforeach (int j; 0..v + 1)\n\t\t\t{\n\t\t\t\tforeach (int k; 0..w)\n\t\t\t\t{\n\t\t\t\t\tauto cur = f[i][j][k];\n\t\t\t\t\tif (cur == -3)\n\t\t\t\t\t{\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\tvoid move (int x, int y, int z)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (f[x][y][z] < cur)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tf[x][y][z] = cur;\n\t\t\t\t\t\t\tq[x][y][z] =\n\t\t\t\t\t\t\t\tprev (i, j, k);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (i < u)\n\t\t\t\t\t{\n\t\t\t\t\t\tmove (i + 1, j, k);\n\t\t\t\t\t}\n\t\t\t\t\tif (j < v)\n\t\t\t\t\t{\n\t\t\t\t\t\tmove (i, j + 1, k);\n\t\t\t\t\t}\n\t\t\t\t\tif (i < u && j < v && r[i] == s[j])\n\t\t\t\t\t{\n\t\t\t\t\t\tauto z = k;\n\t\t\t\t\t\twhile (z > 0 && r[i] != t[z])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tz = p[z];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (r[i] == t[z])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tz++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcur++;\n\t\t\t\t\t\tmove (i + 1, j + 1, z);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint pi = u, pj = v, pk = -3;\n\t\tint res = -3;\n\t\tforeach (k; 0..w)\n\t\t{\n\t\t\tif (res < f[pi][pj][k])\n\t\t\t{\n\t\t\t\tres = f[pi][pj][k];\n\t\t\t\tpk = k;\n\t\t\t}\n\t\t}\n\t\tif (res <= 0)\n\t\t{\n\t\t\twriteln (0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tchar [] ans;\n\t\t\twhile (pk != -3)\n\t\t\t{\n\t\t\t\tint qi, qj, qk;\n\t\t\t\tqi = q[pi][pj][pk][0];\n\t\t\t\tqj = q[pi][pj][pk][1];\n\t\t\t\tqk = q[pi][pj][pk][2];\n\t\t\t\tif (qi == pi - 1 && qj == pj - 1)\n\t\t\t\t{\n\t\t\t\t\tans ~= r[qi];\n\t\t\t\t}\n\t\t\t\tpi = qi;\n\t\t\t\tpj = qj;\n\t\t\t\tpk = qk;\n\t\t\t}\n\t\t\tans.reverse ();\n\t\t\twriteln (ans);\n\t\t}\n\t}\n}\n"}], "negative_code": [], "src_uid": "391c2abbe862139733fcb997ba1629b8"} {"nl": {"description": "Given $$$n$$$, find any array $$$a_1, a_2, \\ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \\le a_i \\le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 < a_2 < \\ldots <a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.", "input_spec": "The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.", "output_spec": "For each test case print $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ — the array you found. If there are multiple arrays satisfying all the conditions, print any of them.", "sample_inputs": ["3\n1\n2\n7"], "sample_outputs": ["1\n2 3\n111 1111 11111 111111 1111111 11111111 111111111"], "notes": "NoteIn the first test case, array $$$[1]$$$ satisfies all the conditions.In the second test case, array $$$[2, 3]$$$ satisfies all the conditions, as $$$2<3$$$ and $$$3$$$ is not divisible by $$$2$$$.In the third test case, array $$$[111, 1111, 11111, 111111, 1111111, 11111111, 111111111]$$$ satisfies all the conditions, as it's increasing and $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$ for any $$$i$$$ from $$$2$$$ to $$$7$$$."}, "positive_code": [{"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n auto n = readInt!int;\n foreach(i; 2 .. 2 + n)\n write(i, \" \");\n writeln;\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid solve(){\n long n = scan;\n for(int i = 2; i < 2 + n; ++i){\n write(i, \" \");\n }\n writeln;\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) solve;\n}\n\n/************** ***** That's All Folks! ***** **************/\nimport std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tiota (10 ^^ 9 - n, 10 ^^ 9).writefln !(\"%(%s %)\");\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "76bfced1345f871832957a65e2a660f8"} {"nl": {"description": "You are given an array of $$$n$$$ non-negative integers $$$a_1, a_2, \\ldots, a_n$$$. You can make the following operation: choose an integer $$$x \\geq 2$$$ and replace each number of the array by the remainder when dividing that number by $$$x$$$, that is, for all $$$1 \\leq i \\leq n$$$ set $$$a_i$$$ to $$$a_i \\bmod x$$$.Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO. You may print each letter in any case (for example, \"YES\", \"Yes\", \"yes\", \"yEs\" will all be recognized as a positive answer).", "sample_inputs": ["4\n4\n2 5 6 8\n3\n1 1 1\n5\n4 1 7 0 8\n4\n5 9 17 5"], "sample_outputs": ["YES\nYES\nNO\nYES"], "notes": "NoteIn the first test case, one can apply the operation with $$$x = 3$$$ to obtain the array $$$[2, 2, 0, 2]$$$, and then apply the operation with $$$x = 2$$$ to obtain $$$[0, 0, 0, 0]$$$.In the second test case, all numbers are already equal.In the fourth test case, applying the operation with $$$x = 4$$$ results in the array $$$[1, 1, 1, 1]$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new bool[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = RDA;\r\n\t\ta.sort;\r\n\r\n\t\tbool has1, hasD1;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] == 1)\r\n\t\t\t\thas1 = true;\r\n\t\t\tif (i != n-1)\r\n\t\t\t{\r\n\t\t\t\tif (a[i+1] - a[i] == 1)\r\n\t\t\t\t\thasD1 = true;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (!(has1 && hasD1))\r\n\t\t\tans[ti] = true;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e ? \"YES\" : \"NO\");\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nbool solve (int [] a, int n)\r\n{\r\n\tif (!a.canFind (1))\r\n\t{\r\n\t\treturn true;\r\n\t}\r\n\tsort (a);\r\n\tforeach (i; 1..n)\r\n\t{\r\n\t\tif (a[i] - a[i - 1] == 1)\r\n\t\t{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\treturn true;\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\twriteln (solve (a, n) ? \"YES\" : \"NO\");\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new bool[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = RDA;\r\n\r\n\t\tbool has0, has1;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (a[i] > 2) continue;\r\n\t\t\tif (a[i] % 2)\r\n\t\t\t\thas1 = true;\r\n\t\t\telse\r\n\t\t\t\thas0 = true;\r\n\t\t}\r\n\r\n\t\tif (!(has0 && has1))\r\n\t\t\tans[ti] = true;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e ? \"YES\" : \"NO\");\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "src_uid": "c7e4f544ec8b4972291542e66aff5df5"} {"nl": {"description": "Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes. They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round. ", "input_spec": "The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000). The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes). The next q lines contain the information about the rounds. Each of them contains two integers i and j (1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.", "output_spec": "After each round, print the current score of the bears.", "sample_inputs": ["5 4 5\n0 1 1 0\n1 0 0 1\n0 1 1 0\n1 0 0 1\n0 0 0 0\n1 1\n1 4\n1 1\n4 2\n4 3"], "sample_outputs": ["3\n4\n3\n3\n4"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.string;\nimport std.range;\nimport std.array;\nimport std.conv;\nimport std.complex;\nimport std.math;\nimport std.ascii;\nimport std.bigint;\nimport std.container;\nimport std.typecons;\n\nauto readInt() {\n\treturn readInts()[0];\n}\nauto readInts() {\n\treturn array(map!(to!int)(readln().strip().split()));\n}\nauto readLong() { \n\treturn readLongs()[0];\n}\nauto readLongs() {\n\treturn array(map!(to!long)(readln().strip().split()));\n}\n\nint rScore(int[] r) {\n\tint ans;\n\tint tmp;\n\tforeach(i; 0..r.length) {\n\t\tif(r[i] == 1) {\n\t\t\t++tmp;\n\t\t} else {\n\t\t\tans = max(ans, tmp);\n\t\t\ttmp = 0;\n\t\t}\n\t}\n\treturn max(tmp, ans);\n}\nvoid main() {\n\tauto l = readInts();\n\tauto n = l[0];\n\tauto m = l[1];\n\tauto q = l[2];\n\tint[][] b;\n\tforeach(i; 0..n) {\n\t\tb ~= readInts();\n\t}\n\tint[] rs;\n\tforeach(i; 0..n) {\n\t\trs ~= rScore(b[i]);\n\t}\n\tforeach(k; 0..q) {\n\t\tauto ij = readInts();\n\t\tauto i = ij[0]-1;\n\t\tauto j = ij[1]-1;\n\t\tb[i][j] ^= 1;\n\t\trs[i] = rScore(b[i]);\n\t\twriteln(reduce!(max)(rs));\n\t}\n}\n\n\n\n\n\n\n"}], "negative_code": [], "src_uid": "337b6d2a11a25ef917809e6409f8edef"} {"nl": {"description": "There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).Students want to start a round dance. A clockwise round dance can be started if the student $$$2$$$ comes right after the student $$$1$$$ in clockwise order (there are no students between them), the student $$$3$$$ comes right after the student $$$2$$$ in clockwise order, and so on, and the student $$$n$$$ comes right after the student $$$n - 1$$$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $$$i$$$ should be right after the student $$$i - 1$$$ in counterclockwise order (this condition should be met for every $$$i$$$ from $$$2$$$ to $$$n$$$). For example, if the indices of students listed in clockwise order are $$$[2, 3, 4, 5, 1]$$$, then they can start a clockwise round dance. If the students have indices $$$[3, 2, 1, 4]$$$ in clockwise order, then they can start a counterclockwise round dance.Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 200$$$) — the number of students. The second line of the query contains a permutation of indices $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$), where $$$p_i$$$ is the index of the $$$i$$$-th student (in clockwise order). It is guaranteed that all $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).", "output_spec": "For each query, print the answer on it. If a round dance can be started with the given order of students, print \"YES\". Otherwise print \"NO\".", "sample_inputs": ["5\n4\n1 2 3 4\n3\n1 3 2\n5\n1 2 3 5 4\n1\n1\n5\n3 2 1 5 4"], "sample_outputs": ["YES\nYES\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic string[] s_rd;\nT RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nstring RDR()() { return readln.chomp; }\nT[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }\nT[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[][] 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; }\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * y / gcd(x, y); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto q = RD!int;\n\tauto ans = new bool[](q);\n\tforeach (i; 0..q)\n\t{\n\t\tauto n = RD!int;\n\t\tauto p = RDA(-1);\n\t\tbool ok = true;\n\t\tforeach (j; 1..n)\n\t\t{\n\t\t\tif ((p[j-1]+1)%n != p[j])\n\t\t\t{\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!ok)\n\t\t{\n\t\t\tok = true;\n\t\t\tforeach (j; 1..n)\n\t\t\t{\n\t\t\t\tif (p[j-1] != (p[j]+1)%n)\n\t\t\t\t{\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tans[i] = ok;\n\t}\n\t\n\tforeach (e; ans)\n\t\twriteln(e ? \"YES\" : \"NO\");\n\tstdout.flush();\n\tdebug readln();\n}"}], "negative_code": [], "src_uid": "b27436086ead93397be748d8ebdbf19a"} {"nl": {"description": "Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Nauuo's hands are given. The remaining $$$n$$$ cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the $$$n$$$ numbered cards piled up in increasing order (the $$$i$$$-th card in the pile from top to bottom is the card $$$i$$$) as quickly as possible. Can you tell her the minimum number of operations?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1\\le n\\le 2\\cdot 10^5$$$) — the number of numbered cards. The second line contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$0\\le a_i\\le n$$$) — the initial cards in Nauuo's hands. $$$0$$$ represents an empty card. The third line contains $$$n$$$ integers $$$b_1,b_2,\\ldots,b_n$$$ ($$$0\\le b_i\\le n$$$) — the initial cards in the pile, given in order from top to bottom. $$$0$$$ represents an empty card. It is guaranteed that each number from $$$1$$$ to $$$n$$$ appears exactly once, either in $$$a_{1..n}$$$ or $$$b_{1..n}$$$.", "output_spec": "The output contains a single integer — the minimum number of operations to make the $$$n$$$ numbered cards piled up in increasing order.", "sample_inputs": ["3\n0 2 0\n3 0 1", "3\n0 2 0\n1 0 3", "11\n0 0 0 5 0 0 0 4 0 0 11\n9 2 6 0 8 1 7 0 3 0 10"], "sample_outputs": ["2", "4", "18"], "notes": "NoteExample 1We can play the card $$$2$$$ and draw the card $$$3$$$ in the first operation. After that, we have $$$[0,3,0]$$$ in hands and the cards in the pile are $$$[0,1,2]$$$ from top to bottom.Then, we play the card $$$3$$$ in the second operation. The cards in the pile are $$$[1,2,3]$$$, in which the cards are piled up in increasing order.Example 2Play an empty card and draw the card $$$1$$$, then play $$$1$$$, $$$2$$$, $$$3$$$ in order."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto b = readln.splitter.map !(to !(int)).array;\n\n\t\tint res = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] > 0)\n\t\t\t{\n\t\t\t\tres = max (res, n - a[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tint limit = n;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (b[i] == 1)\n\t\t\t{\n\t\t\t\tif (b[i..$].equal (iota (1, n + 1 - i)))\n\t\t\t\t{\n\t\t\t\t\tlimit = i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tforeach (i; 0..limit)\n\t\t{\n\t\t\tif (b[i] > 0)\n\t\t\t{\n\t\t\t\tres = max (res, i + 1 + n - b[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tif (res > limit)\n\t\t{\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tif (b[i] > 0)\n\t\t\t\t{\n\t\t\t\t\tres = max (res, i + 1 + n - b[i] + 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nint[] A, B;\n\nint[] pos;\n\nint solveDirect() {\n // B[N - 1] at 2 N - 1\n const d = (2 * N - 1) - B[N - 1];\n bool ok = true;\n foreach (x; 1 .. B[N - 1] + 1) {\n ok = ok && (pos[x] == d + x);\n }\n foreach (x; B[N - 1] + 1 .. N + 1) {\n ok = ok && (pos[x] <= d + x - N - 1);\n }\n return ok ? (N - B[N - 1]) : -1;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n B = new int[N];\n foreach (i; 0 .. N) {\n B[i] = readInt();\n }\n \n pos = new int[N + 1];\n foreach (i; 0 .. N) {\n pos[A[i]] = i;\n }\n foreach (i; 0 .. N) {\n pos[B[i]] = N + i;\n }\n \n int ans = solveDirect();\n if (ans == -1) {\n int lo = -1, hi = 2 * N;\n for (; lo + 1 < hi; ) {\n const mid = (lo + hi) / 2;\n // 1 at 2 N + mid\n const d = (2 * N + mid) - 1;\n bool ok = true;\n foreach (x; 1 .. N + 1) {\n ok = ok && (pos[x] <= d + x - N - 1);\n }\n debug {\n writeln(mid, \" \", d, \" \", ok);\n }\n ok ? (hi = mid) : (lo = mid);\n }\n ans = hi + N;\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm;\nimport std.conv : to;\nimport std.range : iota, tee, enumerate;\nimport std.algorithm.comparison;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nvoid main() {\n GC.disable();\n\n // BEGIN HERE\n\n int n;\n readf(\" %s\", n);\n \n auto a = new int[n];\n auto b = new int[n];\n iota(0,n).each!(i=> readf(\" %s\", a[i]));\n iota(0,n).each!(i=> readf(\" %s\", b[i]));\n\n auto step = new int[n+1];\n iota(0,n).each!(i=> step[ b[i] ] = i+1);\n iota(0,n).each!(i=> step[ a[i] ] = 0);\n\n // can I append to the tail?\n if (step[1] > 0) {\n auto delta = n - step[1]+ 1;\n if (step[1 .. delta+1] == iota(step[1],n+1).array) \n if (step[delta+1 ..$].enumerate.all!\"a.value<=a.index\") {\n writeln(n-delta); \n return;\n }\n }\n\n // I can't\n writeln(step[1 ..$].enumerate.map!\"a[1]-to!int(a[0])\".maxElement + n);\n\n // END HERE\n\n}\n"}, {"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm;\nimport std.conv : to;\nimport std.range : iota, tee, enumerate;\nimport std.algorithm.comparison;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nvoid main() {\n GC.disable();\n\n // BEGIN HERE\n\n int n;\n readf(\" %s\", n);\n \n auto a = new int[n];\n auto b = new int[n];\n iota(0,n).each!(i=> readf(\" %s\", a[i]));\n iota(0,n).each!(i=> readf(\" %s\", b[i]));\n\n auto step = new int[n+1];\n iota(0,n).each!(i=> step[ b[i] ] = i+1);\n iota(0,n).each!(i=> step[ a[i] ] = 0);\n\n // can I append to the tail?\n if (step[1] > 0) {\n auto delta = n - step[1]+ 1;\n if (step[1 .. delta+1] == iota(step[1],n+1).array) {\n if (step[delta+1 ..$].enumerate.all!\"a[1]<=a[0]\") {\n writeln(n-delta); return;}\n }\n }\n\n // I can't\n auto ii = iota(0, step.length).map!(x=>to!int(x)).array;\n step[] -= ii[];\n auto ans = (step[1..$].maxElement +1) + n;\n\n writeln(ans);\n\n // END HERE\n\n}\n"}, {"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm;\nimport std.conv : to;\nimport std.range : iota, tee;\nimport std.algorithm.comparison;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nint solve(int[] step, int ts) {\n // return -1 -> cannot\n int s= 1;\n foreach(i;1..step.length) {\n if (step[i] == -1) continue;\n if (step[i] <= ts) {\n ts++;\n } else return -1;\n }\n\n return ts;\n}\n\nvoid main() {\n GC.disable();\n\n // BEGIN HERE\n\n int n;\n readf(\" %s\", n);\n \n auto a = new int[n];\n auto b = new int[n];\n iota(0,n).each!(i=> readf(\" %s\", a[i]));\n iota(0,n).each!(i=> readf(\" %s\", b[i]));\n\n // writeln(a); writeln(b);\n\n auto step = new int[n+1];\n step[] = n;\n iota(0,n).each!(i=> step[ b[i] ] = i+1);\n iota(0,n).each!(i=> step[ a[i] ] = 0);\n\n\n // can I append to the tail?\n auto ss = step.dup;\n auto del = n - ss[1];\n bool flag = false;\n int bb = ss[1];\n foreach(i; 0..del+1) {\n if (ss[1+i] ==0) {flag=true; break;}\n if (ss[1 + i] != bb + i) { flag = true; break;}\n ss[1+i] = -1;\n }\n\n // writeln(iota(0,ss.length));\n // writeln(step); writeln(ss);\n if (!flag) { // try to append\n auto ans = solve(ss, 0);\n if (ans != -1) { writeln(ans); return; }\n }\n\n // I can't\n auto sss = step.dup;\n sss[0] = 0;\n auto ii = iota(0, step.length).map!(x=>to!int(x)).array;\n sss[] -= ii[];\n auto ans = (sss[1..$].maxElement +1) + n;\n\n writeln(ans);\n\n // END HERE\n\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto b = readln.splitter.map !(to !(int)).array;\n\n\t\tint res = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] > 0)\n\t\t\t{\n\t\t\t\tres = max (res, n - a[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tint limit = n;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (b[i] == 1)\n\t\t\t{\n\t\t\t\tif (b[i..$].equal (iota (1, n + 1 - i)))\n\t\t\t\t{\n\t\t\t\t\tlimit = i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tforeach (i; 0..limit)\n\t\t{\n\t\t\tif (b[i] > 0)\n\t\t\t{\n\t\t\t\tres = max (res, i + 1 + n - b[i] + 1);\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\treadln;\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tauto b = readln.splitter.map !(to !(int)).array;\n\n\t\tint res = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] > 0)\n\t\t\t{\n\t\t\t\tres = max (res, n - a[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tint limit = n;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (b[i] == 1)\n\t\t\t{\n\t\t\t\tif (b[i..$].equal (iota (1, n + 1 - i)))\n\t\t\t\t{\n\t\t\t\t\tlimit = i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tforeach (i; 0..limit)\n\t\t{\n\t\t\tif (b[i] > 0)\n\t\t\t{\n\t\t\t\tres = max (res, i + 1 + n - b[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tif (res > n - limit)\n\t\t{\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tif (b[i] > 0)\n\t\t\t\t{\n\t\t\t\t\tres = max (res, i + 1 + n - b[i] + 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm;\nimport std.conv : to;\nimport std.range : iota, tee;\nimport std.algorithm.comparison;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nint solve(int[] step, int ts) {\n // return -1 -> cannot\n int s= 1;\n foreach(i;1..step.length) {\n if (step[i] == -1) continue;\n if (step[i] <= ts) {\n ts++;\n } else return -1;\n }\n\n return ts;\n}\n\nvoid main() {\n GC.disable();\n\n // BEGIN HERE\n\n int n;\n readf(\" %s\", n);\n \n auto a = new int[n];\n auto b = new int[n];\n iota(0,n).each!(i=> readf(\" %s\", a[i]));\n iota(0,n).each!(i=> readf(\" %s\", b[i]));\n\n // writeln(a); writeln(b);\n\n auto step = new int[n+1];\n step[] = n;\n iota(0,n).each!(i=> step[ b[i] ] = i+1);\n iota(0,n).each!(i=> step[ a[i] ] = 0);\n\n\n // can I append to the tail?\n auto ss = step.dup;\n auto del = n - ss[1];\n bool flag = false;\n int bb = ss[1];\n foreach(i; 0..del+1) {\n if (ss[1 + i] != bb + i) { flag = true; break;}\n ss[1+i] = -1;\n }\n\n // writeln(iota(0,ss.length));\n // writeln(step); writeln(ss);\n if (!flag) { // try to append\n auto ans = solve(ss, 0);\n if (ans != -1) { writeln(ans); return; }\n }\n\n // I can't\n auto sss = step.dup;\n sss[0] = 0;\n auto ii = iota(0, step.length).map!(x=>to!int(x)).array;\n sss[] -= ii[];\n auto ans = (sss.maxElement +1) + n;\n\n writeln(ans);\n\n // END HERE\n\n}\n"}, {"source_code": "import std.stdio : writef, readf, readln, writeln, writefln;\nimport std.array : array, split;\nimport std.algorithm;\nimport std.conv : to;\nimport std.range : iota, tee;\nimport std.algorithm.comparison;\nimport std.container.rbtree : RBT = RedBlackTree;\nimport std.string : strip;\nimport core.memory : GC;\nimport std.typecons : tuple;\n\nalias Tree = RBT!int;\n\nint solve(int[] step, int ts) {\n // return -1 -> cannot\n int s= 1;\n foreach(i;1..step.length) {\n if (step[i] == -1) continue;\n if (step[i] <= ts) {\n ts++;\n } else return -1;\n }\n\n return ts;\n}\n\nvoid main() {\n GC.disable();\n\n // BEGIN HERE\n\n int n;\n readf(\" %s\", n);\n \n auto a = new int[n];\n auto b = new int[n];\n iota(0,n).each!(i=> readf(\" %s\", a[i]));\n iota(0,n).each!(i=> readf(\" %s\", b[i]));\n\n // writeln(a);\n // writeln(b);\n\n auto step = new int[n+1];\n step[] = n;\n iota(0,n).each!(i=> step[ b[i] ] = i+1);\n iota(0,n).each!(i=> step[ a[i] ] = 0);\n\n\n // can I append to the tail?\n auto ss = step.dup;\n auto del = n - ss[1];\n bool flag = false;\n foreach(i; 0..del+1) {\n if (ss[1 + i] != ss[1] + i) { flag = true; break;}\n ss[1+i] = -1;\n }\n\n // writeln(iota(0,ss.length));\n // writeln(step);\n // writeln(ss);\n if (!flag) { // try to append\n auto ans = solve(ss, 0);\n if (ans != -1) { writeln(ans); return; }\n }\n\n // I can't\n auto sss = step.dup;\n sss[0] = 0;\n auto ii = iota(0, step.length).map!(x=>to!int(x)).array;\n sss[] -= ii[];\n auto ans = (sss.maxElement +1) + n;\n\n writeln(ans);\n\n // END HERE\n\n}\n"}], "src_uid": "ec092209aa9f45409e5aa01d7fc784e1"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ integers.You need to find the maximum value of $$$a_{i} | ( a_{j} \\& a_{k} )$$$ over all triplets $$$(i,j,k)$$$ such that $$$i < j < k$$$.Here $$$\\&$$$ denotes the bitwise AND operation, and $$$|$$$ denotes the bitwise OR operation.", "input_spec": "The first line of input contains the integer $$$n$$$ ($$$3 \\le n \\le 10^{6}$$$), the size of the array $$$a$$$. Next line contains $$$n$$$ space separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \\le a_{i} \\le 2 \\cdot 10^{6}$$$), representing the elements of the array $$$a$$$.", "output_spec": "Output a single integer, the maximum value of the expression given in the statement. ", "sample_inputs": ["3\n2 4 6", "4\n2 8 4 7"], "sample_outputs": ["6", "12"], "notes": "NoteIn the first example, the only possible triplet is $$$(1, 2, 3)$$$. Hence, the answer is $$$2 | (4 \\& 6) = 6$$$.In the second example, there are $$$4$$$ possible triplets: $$$(1, 2, 3)$$$, value of which is $$$2|(8\\&4) = 2$$$. $$$(1, 2, 4)$$$, value of which is $$$2|(8\\&7) = 2$$$. $$$(1, 3, 4)$$$, value of which is $$$2|(4\\&7) = 6$$$. $$$(2, 3, 4)$$$, value of which is $$$8|(4\\&7) = 12$$$. The maximum value hence is $$$12$$$."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum E = 21;\n\nvoid chmax2(int[] t, int[] f) {\n /*\n foreach (j; 0 .. 2) {\n int tmp = f[j];\n foreach (i; 0 .. 2) {\n if (t[i] < tmp) {\n swap(t[i], tmp);\n }\n }\n }\n */\n int tmp = f[0];\n if (t[0] < tmp) swap(t[0], tmp);\n if (t[1] < tmp) t[1] = tmp;\n if (t[1] < f[1]) t[1] = f[1];\n}\n\nint N;\nint[] A;\n\nint L;\nint[] B;\n\nint[] mx0, mx1;\n\nbool check() {\n mx0[0 .. 1 << L] = -1;\n mx1[0 .. 1 << L] = -1;\n foreach (i; 0 .. N) {\n int tmp = i;\n if (mx0[B[i]] < tmp) swap(mx0[B[i]], tmp);\n if (mx1[B[i]] < tmp) mx1[B[i]] = tmp;\n }\n foreach (l; 0 .. L) {\n foreach (p; 0 .. 1 << L) {\n if (!((p & 1 << l))) {\n int tmp = mx0[p | 1 << l];\n if (mx0[p] < tmp) swap(mx0[p], tmp);\n if (mx1[p] < tmp) mx1[p] = tmp;\n if (mx1[p] < mx1[p | 1 << l]) mx1[p] = mx1[p | 1 << l];\n }\n }\n }\n foreach (i; 0 .. N) {\n if (i < mx1[((1 << L) - 1) & ~B[i]]) {\n return true;\n }\n }\n return false;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n int ans;\n L = 0;\n B = new int[N];\n mx0 = new int[1 << E];\n mx1 = new int[1 << E];\n foreach_reverse (e; 0 .. E) {\n foreach (i; 0 .. N) {\n if ((A[i] >> e) & 1) {\n B[i] |= 1 << L;\n }\n }\n ++L;\n if (check()) {\n ans |= 1 << e;\n } else {\n --L;\n B[] &= ((1 << L) - 1);\n }\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nenum INF = 10L^^18;\n\nint N, W;\nint[] L;\nlong[][] A;\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n W = readInt();\n L = new int[N];\n A = new long[][N];\n foreach (i; 0 .. N) {\n L[i] = readInt();\n A[i] = new long[L[i]];\n foreach (j; 0 .. L[i]) {\n A[i][j] = readLong();\n }\n }\n \n auto ans = new long[W + 1];\n \n alias Entry = Tuple!(long, \"val\", int, \"key\");\n auto que = new Entry[W];\n int qb, qe;\n \n foreach (i; 0 .. N) {\n if (W >= 2 * L[i]) {\n // always empty ok\n auto ls = A[i].dup, rs = A[i].dup;\n foreach (j; 0 .. L[i] - 1) {\n chmax(ls[j + 1], ls[j]);\n }\n foreach_reverse (j; 1 .. L[i]) {\n chmax(rs[j - 1], rs[j]);\n }\n foreach (x; 0 .. L[i]) {\n long mx = ls[x];\n chmax(mx, 0);\n debug {\n writefln(\"long ans %s %s: %s\", i, x, mx);\n }\n ans[x] += mx;\n ans[x + 1] -= mx;\n }\n foreach (x; W - L[i] .. W) {\n long mx = rs[x - (W - L[i])];\n chmax(mx, 0);\n debug {\n writefln(\"long ans %s %s: %s\", i, x, mx);\n }\n ans[x] += mx;\n ans[x + 1] -= mx;\n }\n // [L[i], W - L[i])\n {\n long mx = rs[0];\n chmax(mx, 0);\n debug {\n writefln(\"long ans %s [%s, %s): %s\", i, L[i], W - L[i], mx);\n }\n ans[L[i]] += mx;\n ans[W - L[i]] -= mx;\n }\n } else {\n qb = qe = 0;\n /*\n index j at position x\n <=> j <= x && L[i] - j <= W - x\n <=> x - (W - L[i]) <= j <= x\n */\n foreach (x; 0 .. W) {\n if (x < L[i]) {\n // push x\n for (; qe - qb >= 1 && que[qe - 1].val <= A[i][x]; --qe) {}\n que[qe++] = Entry(A[i][x], x);\n }\n for (; qe - qb >= 1 && !(x - (W - L[i]) <= que[qb].key); ++qb) {}\n assert(qe - qb >= 1);\n long mx = que[qb].val;\n if (x >= L[i] || W - 1 - x >= L[i]) {\n chmax(mx, 0);\n }\n debug {\n writefln(\"short ans %s %s: %s\", i, x, mx);\n }\n ans[x] += mx;\n ans[x + 1] -= mx;\n }\n }\n }\n \n foreach (x; 0 .. W) {\n ans[x + 1] += ans[x];\n }\n assert(ans[W] == 0);\n \n foreach (x; 0 .. W) {\n if (x > 0) {\n write(\" \");\n }\n write(ans[x]);\n }\n writeln();\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "7d489942bbdf227d16c6e61b3caa0871"} {"nl": {"description": "Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions: The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≤ i ≤ k - 1). No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≤ i ≤ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q). All elements of the sequence are good integers. Find the length of the longest good sequence.", "input_spec": "The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 105) — the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≤ ai ≤ 105; ai < ai + 1).", "output_spec": "Print a single integer — the length of the longest good sequence.", "sample_inputs": ["5\n2 3 4 6 9", "9\n1 2 3 5 6 7 8 9 10"], "sample_outputs": ["4", "4"], "notes": "NoteIn the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.math;\n\nstatic immutable N = 100000;\n\nint n, q, ans;\nint[N + 1] a, top, f;\nint[20][N + 1] prime;\n\nint main() {\n readf(\" %d\", &n);\n for (int i = 1; i <= n; ++ i) {\n readf(\" %d\", &a[i]);\n q = cast(int) sqrt(real(a[i]));\n for (int j = 2; j <= q; ++ j) {\n if (! (a[i] % j)) {\n prime[i][top[i] ++] = j;\n while (! (a[i] % j)) a[i] /= j;\n }\n }\n if (a[i] > 1) prime[i][top[i] ++] = a[i];\n }\n f[1] = 1;\n for (int i = 1; i <= n; ++ i) {\n int rec = 0;\n for (int j = 0; j < top[i]; ++ j)\n if (rec < f[prime[i][j]]) rec = f[prime[i][j]];\n ++ rec;\n for (int j = 0; j < top[i]; ++ j) f[prime[i][j]] = rec;\n }\n for (int i = 1; i <= N; ++ i)\n if (f[i] > ans) ans = f[i];\n writeln(ans);\n return 0;\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n immutable int MAX = 10^^5 + 1;\n bool[] table = new bool[](MAX);\n fill(table, true);\n table[0] = table[1] = false;\n \n int[][] P = new int[][](MAX);\n \n foreach (i; 2..MAX) {\n if (!table[i]) continue;\n P[i] ~= i;\n for (int j = i + i; j < MAX; j += i) {\n table[j] = false;\n P[j] ~= i;\n }\n }\n\n \n auto N = readln.chomp.to!int;\n auto A = readln.split.map!(to!int).array;\n auto dp = new int[](MAX);\n\n foreach (i; 0..N) {\n int tmp = 0;\n foreach (j; P[A[i]]) tmp = max(tmp, dp[j] + 1);\n foreach (j; P[A[i]]) dp[j] = tmp;\n }\n\n int ans = dp.reduce!max;\n max(ans, 1).writeln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.math;\n\nstatic immutable N = 100000;\n\nint n, q, ans;\nint[N + 1] a, top, f;\nint[N + 1][20] prime;\n\nint main() {\n readf(\" %d\", &n);\n for (int i = 1; i <= n; ++ i) {\n readf(\" %d\", &a[i]);\n q = cast(int) sqrt(real(a[i]));\n for (int j = 2; j <= q; ++ j) {\n if (! (a[i] % j)) {\n prime[i][++ top[i]] = j;\n while (! (a[i] % j)) a[i] /= j;\n }\n }\n if (a[i] > 1) prime[i][++ top[i]] = a[i];\n }\n for (int i = 1; i <= n; ++ i) {\n int rec = 0;\n for (int j = 1; j <= top[i]; ++ j)\n if (rec < f[prime[i][j]]) rec = f[prime[i][j]];\n ++ rec;\n for (int j = 1; j <= top[i]; ++ j) f[prime[i][j]] = rec;\n }\n for (int i = 1; i <= N; ++ i)\n if (f[i] > ans) ans = f[i];\n writeln(ans);\n return 0;\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n immutable int MAX = 10^^5 + 1;\n bool[] table = new bool[](MAX);\n fill(table, true);\n table[0] = table[1] = false;\n \n int[][] P = new int[][](MAX);\n \n foreach (i; 2..MAX) {\n if (!table[i]) continue;\n P[i] ~= i;\n for (int j = i + i; j < MAX; j += i) {\n table[j] = false;\n P[j] ~= i;\n }\n }\n\n \n auto N = readln.chomp.to!int;\n auto A = readln.split.map!(to!int).array;\n auto dp = new int[](MAX);\n\n foreach (i; 0..N) {\n int tmp = 0;\n foreach (j; P[A[i]]) tmp = max(tmp, dp[j] + 1);\n foreach (j; P[A[i]]) dp[j] = tmp;\n }\n\n dp.reduce!max.writeln;\n}\n"}], "src_uid": "0f8ad0ea2befbbe036fbd5e5f6680c21"} {"nl": {"description": "One very important person has a piece of paper in the form of a rectangle a × b.Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi × yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?", "input_spec": "The first line contains three integer numbers n, a and b (1 ≤ n, a, b ≤ 100). Each of the next n lines contain two numbers xi, yi (1 ≤ xi, yi ≤ 100).", "output_spec": "Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.", "sample_inputs": ["2 2 2\n1 2\n2 1", "4 10 9\n2 3\n1 1\n5 10\n9 11", "3 10 10\n6 6\n7 7\n20 5"], "sample_outputs": ["4", "56", "0"], "notes": "NoteIn the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area.In the third example there is no such pair of seals that they both can fit on a piece of paper."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto H = s[1];\n auto W = s[2];\n auto X = new int[](N);\n auto Y = new int[](N);\n foreach (i; 0..N) {\n s = readln.split.map!(to!int);\n X[i] = s[0];\n Y[i] = s[1];\n }\n\n int x1, y1, x2, y2;\n int ans = 0;\n \n foreach (i; 0..N) {\n foreach (j; i+1..N) {\n foreach (rot; 0..4) {\n if (rot == 0) x1 = X[i], y1 = Y[i], x2 = X[j], y2 = Y[j];\n if (rot == 1) x1 = X[i], y1 = Y[i], x2 = Y[j], y2 = X[j];\n if (rot == 2) x1 = Y[i], y1 = X[i], x2 = X[j], y2 = Y[j];\n if (rot == 3) x1 = Y[i], y1 = X[i], x2 = Y[j], y2 = X[j];\n if (x1 + x2 <= H && max(y1, y2) <= W) ans = max(ans, X[i] * Y[i] + X[j] * Y[j]);\n if (max(x1, x2) <= H && y1 + y2 <= W) ans = max(ans, X[i] * Y[i] + X[j] * Y[j]);\n }\n }\n }\n\n ans.writeln;\n}\n"}, {"source_code": "// Try Codeforces\n// author: Leonardone @ NEETSDKASU\nimport std.stdio, std.string, std.array, std.algorithm, std.conv;\n\nvoid main() {\n auto nab = to!(int[])(split(chomp(readln())));\n int n = nab[0], a = nab[1], b = nab[2];\n int ans = 0;\n int[101] u, w;\n for (int i = 0; i < n; i++) {\n auto xy = to!(int[])(split(chomp(readln())));\n int x = xy[0], y = xy[1];\n int s = x * y;\n if (x <= a && y <= b) {\n for (int j = 1; j < 101; j++) {\n if (u[j] > 0 && j + x <= a) {\n ans = max(ans, u[j] + s);\n }\n if (w[j] > 0 && j + y <= b) {\n ans = max(ans, w[j] + s);\n }\n }\n }\n if (y <= a && x <= b) {\n for (int j = 1; j < 101; j++) {\n if (u[j] > 0 && j + y <= a) {\n ans = max(ans, u[j] + s);\n }\n if (w[j] > 0 && j + x <= b) {\n ans = max(ans, w[j] + s);\n }\n }\n }\n if (x <= a && y <= b) { u[x] = max(u[x], s); w[y] = max(w[y], s); }\n if (y <= a && x <= b) { u[y] = max(u[y], s); w[x] = max(w[x], s); }\n }\n writeln(ans);\n}"}, {"source_code": "// tested by Hightail - https://github.com/dj3500/hightail\nimport std.stdio, std.string, std.conv, std.algorithm;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\nimport std.datetime, std.bigint;\n\nint n, a, b;\nint[] x, y;\n\nvoid main() {\n scan(n, a, b);\n\n x = new int[](n);\n y = new int[](n);\n\n foreach (i ; 0 .. n) {\n scan(x[i], y[i]);\n }\n\n bool check(int U, int V, int u, int v) {\n return (u <= U && v <= V) || (u <= V && v <= U);\n }\n\n int ans;\n\n foreach (i ; 0 .. n) {\n foreach (j ; 0 .. n) {\n if (i == j) continue;\n \n if (x[i] <= a && y[i] <= b) {\n if (check(a - x[i], b, x[j], y[j]) || check(a, b - y[i], x[j], y[j])) {\n ans = max(ans, x[i]*y[i] + x[j]*y[j]);\n }\n }\n\n if (x[i] <= b && y[i] <= a) {\n if (check(a - y[i], b, x[j], y[j]) || check(a, b - x[i], x[j], y[j])) {\n ans = max(ans, x[i]*y[i] + x[j]*y[j]);\n }\n }\n }\n }\n\n writeln(ans);\n}\n\n\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}\n\n"}, {"source_code": "// tested by Hightail - https://github.com/dj3500/hightail\nimport std.stdio, std.string, std.conv, std.algorithm;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\nimport std.datetime, std.bigint;\n\nint n, a, b;\nint[] x, y;\n\nvoid main() {\n scan(n, a, b);\n\n x = new int[](n);\n y = new int[](n);\n\n foreach (i ; 0 .. n) {\n scan(x[i], y[i]);\n }\n\n bool check(int U, int V, int u, int v) {\n return (u <= U && v <= V) || (u <= V && v <= U);\n }\n\n int ans;\n\n foreach (i ; 0 .. n) {\n foreach (j ; 0 .. n) {\n if (i == j) continue;\n \n if (x[i] <= a && y[i] <= b) {\n if (check(a - x[i], b, x[j], y[j]) || check(a, b - y[i], x[j], y[j])) {\n ans = max(ans, x[i]*y[i] + x[j]*y[j]);\n }\n }\n\n if (x[i] <= b && y[i] <= a) {\n if (check(a - y[i], b, x[j], y[j]) || check(a, b - x[i], x[j], y[j])) {\n ans = max(ans, x[i]*y[i] + x[j]*y[j]);\n }\n }\n }\n }\n\n writeln(ans);\n}\n\n\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}"}], "negative_code": [], "src_uid": "1ad63f41943e40aa8c8d5c88c29c283c"} {"nl": {"description": "This is an interactive problem.Given a simple undirected graph with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, your task is to color all the vertices such that for every color $$$c$$$, the following conditions hold: The set of vertices with color $$$c$$$ is connected; $$$s_c \\leq n_c^2$$$, where $$$n_c$$$ is the number of vertices with color $$$c$$$, and $$$s_c$$$ is the sum of degrees of vertices with color $$$c$$$. It can be shown that there always exists a way to color all the vertices such that the above conditions hold. Initially, you are only given the number $$$n$$$ of vertices and the degree of each vertex. In each query, you can choose a vertex $$$u$$$. As a response, you will be given the $$$k$$$-th edge incident to $$$u$$$, if this is the $$$k$$$-th query on vertex $$$u$$$. You are allowed to make at most $$$n$$$ queries.An undirected graph is simple if it does not contain multiple edges or self-loops.The degree of a vertex is the number of edges incident to it. A set $$$S$$$ of vertices is connected if for every two different vertices $$$u, v \\in S$$$, there is a path, which only passes through vertices in $$$S$$$, that connects $$$u$$$ and $$$v$$$. That is, there is a sequence of edges $$$(u_1, v_1), (u_2, v_2), \\dots, (u_k, v_k)$$$ with $$$k \\geq 1$$$ such that $$$u_1 = u$$$, $$$v_k = v$$$, and $$$v_i = u_{i+1}$$$ for every $$$1 \\leq i < k$$$; and $$$u_k \\in S$$$ and $$$v_k \\in S$$$ for every $$$1 \\leq i \\leq k$$$. Especially, a set containing only one vertex is connected. ", "input_spec": null, "output_spec": null, "sample_inputs": ["1\n5\n2 2 2 2 0\n\n2\n\n4\n\n2\n\n4"], "sample_outputs": ["? 1\n\n? 1\n\n? 3\n\n? 3\n\n! 1 1 2 2 3"], "notes": "NoteIn the example, there is only one test case. In the test case, there are $$$n = 5$$$ vertices with vertices $$$1, 2, 3, 4$$$ of degree $$$2$$$ and vertex $$$5$$$ of degree $$$0$$$. It is obvious that vertex $$$5$$$ is isolated, i.e., it does not connect to any other vertices. A possible interaction is shown in the sample input and output, where $$$4$$$ \"?\" queries are made on vertex $$$1$$$ twice and vertex $$$3$$$ twice. According to the responses to these queries, we know that each of vertex $$$1$$$ and vertex $$$3$$$ connects to two vertices $$$2$$$ and $$$4$$$. A possible solution is shown in the sample output, where vertex $$$1$$$ and vertex $$$2$$$ are colored by $$$1$$$, vertex $$$3$$$ and vertex $$$4$$$ are colored by $$$2$$$, and vertex $$$5$$$ is colored by $$$3$$$. It can be seen that this solution satisfies the required conditions as follows. For color $$$c = 1$$$, vertex $$$1$$$ and vertex $$$2$$$ are connected. Moreover, $$$n_1 = 2$$$ and $$$s_1 = d_1 + d_2 = 2 + 2 = 4 \\leq n_1^2 = 2^2 = 4$$$; For color $$$c = 2$$$, vertex $$$3$$$ and vertex $$$4$$$ are connected. Moreover, $$$n_2 = 2$$$ and $$$s_2 = d_3 + d_4 = 2 + 2 = 4 \\leq n_2^2 = 2^2 = 4$$$; For color $$$c = 3$$$, there is only one vertex (vertex $$$5$$$) colored by $$$3$$$. Moreover, $$$n_3 = 1$$$ and $$$s_3 = d_5 = 0 \\leq n_3^2 = 1^2 = 1$$$. "}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto d = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tauto order = n.iota.array;\r\n\t\torder.sort !((i, j) => d[i] > d[j]);\r\n\t\tauto color = new int [n];\r\n\r\n\t\tvoid recolorAs (int v, int u)\r\n\t\t{\r\n\t\t\tauto oldColor = color[v];\r\n\t\t\tauto newColor = color[u];\r\n\t\t\tforeach (i; 0..n)\r\n\t\t\t{\r\n\t\t\t\tif (color[i] == oldColor)\r\n\t\t\t\t{\r\n\t\t\t\t\tcolor[i] = newColor;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tforeach (v; order)\r\n\t\t{\r\n\t\t\tif (color[v] != 0)\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\t\t\tauto cur = d[v];\r\n\t\t\tcolor[v] = v + 1;\r\n\t\t\tforeach (step; 0..cur)\r\n\t\t\t{\r\n\t\t\t\twritefln !(\"? %s\") (v + 1);\r\n\t\t\t\tstdout.flush ();\r\n\t\t\t\tauto u = readln.strip.to !(int);\r\n\t\t\t\tu -= 1;\r\n\t\t\t\tif (color[u] == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tcolor[u] = color[v];\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\trecolorAs (u, v);\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\twritefln !(\"! %(%s %)\") (color);\r\n\t\tstdout.flush ();\r\n\t}\r\n}\r\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\nint root(int[] uf, int u) {\r\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\r\n}\r\nbool connect(int[] uf, int u, int v) {\r\n u = uf.root(u);\r\n v = uf.root(v);\r\n if (u == v) return false;\r\n if (uf[u] > uf[v]) swap(u, v);\r\n uf[u] += uf[v];\r\n uf[v] = u;\r\n return true;\r\n}\r\n\r\n\r\nint Ask(int u) {\r\n writeln(\"? \", u + 1);\r\n stdout.flush;\r\n const v = readInt - 1;\r\n return v;\r\n}\r\n\r\nvoid main() {\r\n const numCases = readInt;\r\n foreach (caseId; 0 .. numCases) {\r\n const N = readInt;\r\n auto D = new int[N];\r\n foreach (i; 0 .. N) {\r\n D[i] = readInt;\r\n }\r\n \r\n int[] us = iota(N).array;\r\n us.sort!((u, v) => (D[u] < D[v]));\r\n \r\n auto uf = new int[N];\r\n uf[] = -1;\r\n auto vis = new bool[N];\r\n foreach_reverse (u; us) if (!vis[u]) {\r\n vis[u] = true;\r\n foreach (j; 0 .. D[u]) {\r\n const v = Ask(u);\r\n uf.connect(u, v);\r\n if (vis[v]) {\r\n break;\r\n }\r\n vis[v] = true;\r\n }\r\n }\r\n \r\n write(\"!\");\r\n foreach (u; 0 .. N) {\r\n write(\" \", uf.root(u) + 1);\r\n }\r\n writeln;\r\n stdout.flush;\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "cb05d81d82d16ac3fdf8ec33e69d5ae8"} {"nl": {"description": "Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \\le 2$$$ and $$$a_5=1 \\le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \\le 5$$$, $$$a_3=4 \\le 5$$$ and $$$a_4=5 \\le 5$$$); the $$$6$$$-th granny cannot be called into the yard  — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 2\\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$.", "output_spec": "For each test case, print a single integer $$$k$$$ ($$$1 \\le k \\le n + 1$$$) — the maximum possible number of grannies in the courtyard.", "sample_inputs": ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"], "sample_outputs": ["6\n1\n6\n4"], "notes": "NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\tsort (a);\n\t\tint res = 1;\n\t\tforeach (i, c; a)\n\t\t{\n\t\t\tif (c <= i + 1)\n\t\t\t{\n\t\t\t\tres = i + 2;\n\t\t\t}\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA!int;\n\t\t\n\t\tans[ti] = 1;\n\t\ta.sort();\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] <= i+1)\n\t\t\t\tans[ti] = i+2;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e);\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "718cea81f609055cece58cae5310f703"} {"nl": {"description": "You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a $$$r \\times c$$$ grid. Each cell on the grid represents a country. Each country has a dominant religion. There are only two religions in your world. One of the religions is called Beingawesomeism, who do good for the sake of being good. The other religion is called Pushingittoofarism, who do murders for the sake of being bad.Oh, and you are actually not really all-powerful. You just have one power, which you can use infinitely many times! Your power involves missionary groups. When a missionary group of a certain country, say $$$a$$$, passes by another country $$$b$$$, they change the dominant religion of country $$$b$$$ to the dominant religion of country $$$a$$$.In particular, a single use of your power is this: You choose a horizontal $$$1 \\times x$$$ subgrid or a vertical $$$x \\times 1$$$ subgrid. That value of $$$x$$$ is up to you; You choose a direction $$$d$$$. If you chose a horizontal subgrid, your choices will either be NORTH or SOUTH. If you choose a vertical subgrid, your choices will either be EAST or WEST; You choose the number $$$s$$$ of steps; You command each country in the subgrid to send a missionary group that will travel $$$s$$$ steps towards direction $$$d$$$. In each step, they will visit (and in effect convert the dominant religion of) all $$$s$$$ countries they pass through, as detailed above. The parameters $$$x$$$, $$$d$$$, $$$s$$$ must be chosen in such a way that any of the missionary groups won't leave the grid. The following image illustrates one possible single usage of your power. Here, A represents a country with dominant religion Beingawesomeism and P represents a country with dominant religion Pushingittoofarism. Here, we've chosen a $$$1 \\times 4$$$ subgrid, the direction NORTH, and $$$s = 2$$$ steps. You are a being which believes in free will, for the most part. However, you just really want to stop receiving murders that are attributed to your name. Hence, you decide to use your powers and try to make Beingawesomeism the dominant religion in every country.What is the minimum number of usages of your power needed to convert everyone to Beingawesomeism?With god, nothing is impossible. But maybe you're not god? If it is impossible to make Beingawesomeism the dominant religion in all countries, you must also admit your mortality and say so.", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1 \\le t \\le 2\\cdot 10^4$$$) denoting the number of test cases. The first line of each test case contains two space-separated integers $$$r$$$ and $$$c$$$ denoting the dimensions of the grid ($$$1 \\le r, c \\le 60$$$). The next $$$r$$$ lines each contains $$$c$$$ characters describing the dominant religions in the countries. In particular, the $$$j$$$-th character in the $$$i$$$-th line describes the dominant religion in the country at the cell with row $$$i$$$ and column $$$j$$$, where: \"A\" means that the dominant religion is Beingawesomeism; \"P\" means that the dominant religion is Pushingittoofarism. It is guaranteed that the grid will only contain \"A\" or \"P\" characters. It is guaranteed that the sum of the $$$r \\cdot c$$$ in a single file is at most $$$3 \\cdot 10^6$$$.", "output_spec": "For each test case, output a single line containing the minimum number of usages of your power needed to convert everyone to Beingawesomeism, or the string \"MORTAL\" (without quotes) if it is impossible to do so. ", "sample_inputs": ["4\n7 8\nAAPAAAAA\nPPPPAAAA\nPPPPAAAA\nAPAAPPPP\nAPAPPAPP\nAAAAPPAP\nAAAAPPAA\n6 5\nAAAAA\nAAAAA\nAAPAA\nAAPAP\nAAAPP\nAAAPP\n4 4\nPPPP\nPPPP\nPPPP\nPPPP\n3 4\nPPPP\nPAAP\nPPPP"], "sample_outputs": ["2\n1\nMORTAL\n4"], "notes": "NoteIn the first test case, it can be done in two usages, as follows:Usage 1: Usage 2: In the second test case, it can be done with just one usage of the power. In the third test case, it is impossible to convert everyone to Beingawesomeism, so the answer is \"MORTAL\"."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto r = RD!int;\n\t\tauto c = RD!int;\n\t\tauto s = new string[](r);\n\t\tforeach (y; 0..r)\n\t\t\ts[y] = RD!string;\n\t\tbool ok1, ok2;\n\t\t{\n\t\t\tok1 = true;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tforeach (x; 0..c)\n\t\t\t\t{\n\t\t\t\t\tif (s[y][x] == 'P')\n\t\t\t\t\t{\n\t\t\t\t\t\tok1 = false;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tok2 = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok1)\n\t\t\t{\n\t\t\t\tans[ti] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tbool tmp1 = true;\n\t\t\tbool tmp2 = true;\n\t\t\tforeach (x; 0..c)\n\t\t\t{\n\t\t\t\tif (s[0][x] == 'P')\n\t\t\t\t\ttmp1 = false;\n\t\t\t\tif (s[$-1][x] == 'P')\n\t\t\t\t\ttmp2 = false;\n\t\t\t}\n\t\t\tbool tmp3 = true;\n\t\t\tbool tmp4 = true;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tif (s[y][0] == 'P')\n\t\t\t\t\ttmp3 = false;\n\t\t\t\tif (s[y][$-1] == 'P')\n\t\t\t\t\ttmp4 = false;\n\t\t\t}\n\t\t\tif (tmp1 || tmp2 || tmp3 || tmp4)\n\t\t\t{\n\t\t\t\tans[ti] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tif (s[0][0] == 'A' || s[r-1][0] == 'A' || s[0][c-1] == 'A' || s[r-1][c-1] == 'A')\n\t\t\t{\n\t\t\t\tans[ti] = 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tbool ok;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tbool tmp = true;\n\t\t\t\tforeach (x; 0..c)\n\t\t\t\t{\n\t\t\t\t\tif (s[y][x] == 'P')\n\t\t\t\t\t{\n\t\t\t\t\t\ttmp = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (tmp)\n\t\t\t\t{\n\t\t\t\t\tok = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok)\n\t\t\t{\n\t\t\t\tans[ti] = 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tforeach (x; 0..c)\n\t\t\t{\n\t\t\t\tbool tmp = true;\n\t\t\t\tforeach (y; 0..r)\n\t\t\t\t{\n\t\t\t\t\tif (s[y][x] == 'P')\n\t\t\t\t\t{\n\t\t\t\t\t\ttmp = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (tmp)\n\t\t\t\t{\n\t\t\t\t\tok = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok)\n\t\t\t{\n\t\t\t\tans[ti] = 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tbool tmp1, tmp2;\n\t\t\tforeach (x; 0..c)\n\t\t\t{\n\t\t\t\tif (s[0][x] == 'A')\n\t\t\t\t\ttmp1 = true;\n\t\t\t\tif (s[$-1][x] == 'A')\n\t\t\t\t\ttmp2 = true;\n\t\t\t}\n\t\t\tbool tmp3, tmp4;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tif (s[y][0] == 'A')\n\t\t\t\t\ttmp3 = true;\n\t\t\t\tif (s[y][$-1] == 'A')\n\t\t\t\t\ttmp4 = true;\n\t\t\t}\n\t\t\tif (tmp1 || tmp2 || tmp3 || tmp4)\n\t\t\t{\n\t\t\t\tans[ti] = 3;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tif (ok2)\n\t\t{\n\t\t\tans[ti] = 4;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tans[ti] = -1;\n\t\t}\n\t}\n\tforeach (e; ans)\n\t{\n\t\tif (e == -1)\n\t\t\twriteln(\"MORTAL\");\n\t\telse\n\t\t\twriteln(e);\n\t}\n\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto r = RD!int;\n\t\tauto c = RD!int;\n\t\tauto s = new string[](r);\n\t\tforeach (y; 0..r)\n\t\t\ts[y] = RD!string;\n\t\tbool ok1, ok2;\n\t\t{\n\t\t\tok1 = true;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tforeach (x; 0..c)\n\t\t\t\t{\n\t\t\t\t\tif (s[y][x] == 'P')\n\t\t\t\t\t{\n\t\t\t\t\t\tok1 = false;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tok2 = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok1)\n\t\t\t{\n\t\t\t\tans[ti] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tbool tmp1 = true;\n\t\t\tbool tmp2 = true;\n\t\t\tforeach (x; 0..c)\n\t\t\t{\n\t\t\t\tif (s[0][x] == 'P')\n\t\t\t\t\ttmp1 = false;\n\t\t\t\tif (s[$-1][x] == 'P')\n\t\t\t\t\ttmp2 = false;\n\t\t\t}\n\t\t\tbool tmp3 = true;\n\t\t\tbool tmp4 = true;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tif (s[y][0] == 'P')\n\t\t\t\t\ttmp3 = false;\n\t\t\t\tif (s[y][$-1] == 'P')\n\t\t\t\t\ttmp4 = false;\n\t\t\t}\n\t\t\tif (tmp1 || tmp2 || tmp3 || tmp4)\n\t\t\t{\n\t\t\t\tans[ti] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tif (s[0][0] == 'A' || s[r-1][0] == 'A' || s[0][c-1] == 'A' || s[r-1][c-1] == 'A')\n\t\t\t{\n\t\t\t\tans[ti] = 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tbool tmp1, tmp2;\n\t\t\tforeach (x; 0..c)\n\t\t\t{\n\t\t\t\tif (s[0][x] == 'A')\n\t\t\t\t\ttmp1 = true;\n\t\t\t\tif (s[$-1][x] == 'A')\n\t\t\t\t\ttmp2 = true;\n\t\t\t}\n\t\t\tbool tmp3, tmp4;\n\t\t\tforeach (y; 0..r)\n\t\t\t{\n\t\t\t\tif (s[y][0] == 'A')\n\t\t\t\t\ttmp3 = true;\n\t\t\t\tif (s[y][$-1] == 'A')\n\t\t\t\t\ttmp4 = true;\n\t\t\t}\n\t\t\tif (tmp1 || tmp2 || tmp3 || tmp4)\n\t\t\t{\n\t\t\t\tans[ti] = 3;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tif (ok2)\n\t\t{\n\t\t\tans[ti] = 4;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tans[ti] = -1;\n\t\t}\n\t}\n\tforeach (e; ans)\n\t{\n\t\tif (e == -1)\n\t\t\twriteln(\"MORTAL\");\n\t\telse\n\t\t\twriteln(e);\n\t}\n\n\tstdout.flush;\n\tdebug readln;\n}"}], "src_uid": "da18ca9f125d1524e7c0a2637b1fa3df"} {"nl": {"description": "Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.", "input_spec": "The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.", "output_spec": "On the i-th line of the output print the number of birds on the i-th wire.", "sample_inputs": ["5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "3\n2 4 1\n1\n2 2"], "sample_outputs": ["0\n12\n5\n0\n16", "3\n0\n3"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %d\", &n) > 0)\n\t{\n\t\tauto a = new int [n];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %d\", &a[i]);\n\t\t}\n\t\tint m;\n\t\treadf (\" %d\", &m);\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\tint x, y;\n\t\t\treadf (\" %d %d\", &x, &y);\n\t\t\tx--;\n\t\t\ty--;\n\t\t\tif (x - 1 >= 0)\n\t\t\t{\n\t\t\t\ta[x - 1] += y;\n\t\t\t}\n\t\t\tif (x + 1 < n)\n\t\t\t{\n\t\t\t\ta[x + 1] += a[x] - y - 1;\n\t\t\t}\n\t\t\ta[x] = 0;\n\t\t}\n\t\twritefln (\"%(%d\\n%)\", a);\n\t}\n}\n"}, {"source_code": "module sigod.codeforces.p294A;\n\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main()\n{\n\tint N;\n\tstdin.readf(\"%s\", &N);\n\tstdin.readln();\n\n\tint[] counts = read_array!int();\n\n\tint M;\n\tstdin.readf(\"%s\", &M);\n\tstdin.readln();\n\n\tforeach (i; 0 .. M) {\n\t\tint x, y;\n\n\t\tstdin.readf(\"%s %s\", &x, &y);\n\t\tstdin.readln();\n\n\t\t--x;\n\n\t\tif (x > 0) {\n\t\t\tcounts[x - 1] += y - 1;\n\t\t}\n\t\tif (x < N - 1) {\n\t\t\tcounts[x + 1] += counts[x] - y;\n\t\t}\n\n\t\tcounts[x] = 0;\n\t}\n\n\tforeach (count; counts) {\n\t\tstdout.writeln(count);\n\t}\n}\n\nprivate\nT[] read_array(T)()\n{\n\tauto input = stdin.readln().strip().split();\n\n\tT[] result = new T[input.length];\n\n\tforeach (index, ref element; input) {\n\t\tresult[index] = element.to!T();\n\t}\n\n\treturn result;\n}"}], "negative_code": [], "src_uid": "859d66fc2c204a8c002012b1fb206645"} {"nl": {"description": "Tanya wants to go on a journey across the cities of Berland. There are $$$n$$$ cities situated along the main railroad line of Berland, and these cities are numbered from $$$1$$$ to $$$n$$$. Tanya plans her journey as follows. First of all, she will choose some city $$$c_1$$$ to start her journey. She will visit it, and after that go to some other city $$$c_2 > c_1$$$, then to some other city $$$c_3 > c_2$$$, and so on, until she chooses to end her journey in some city $$$c_k > c_{k - 1}$$$. So, the sequence of visited cities $$$[c_1, c_2, \\dots, c_k]$$$ should be strictly increasing.There are some additional constraints on the sequence of cities Tanya visits. Each city $$$i$$$ has a beauty value $$$b_i$$$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $$$c_i$$$ and $$$c_{i + 1}$$$, the condition $$$c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$$$ must hold.For example, if $$$n = 8$$$ and $$$b = [3, 4, 4, 6, 6, 7, 8, 9]$$$, there are several three possible ways to plan a journey: $$$c = [1, 2, 4]$$$; $$$c = [3, 5, 6, 8]$$$; $$$c = [7]$$$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above.Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of cities in Berland. The second line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$1 \\le b_i \\le 4 \\cdot 10^5$$$), where $$$b_i$$$ is the beauty value of the $$$i$$$-th city.", "output_spec": "Print one integer — the maximum beauty of a journey Tanya can choose.", "sample_inputs": ["6\n10 7 1 9 10 15", "1\n400000", "7\n8 9 26 11 12 29 14"], "sample_outputs": ["26", "400000", "55"], "notes": "NoteThe optimal journey plan in the first example is $$$c = [2, 4, 5]$$$.The optimal journey plan in the second example is $$$c = [1]$$$.The optimal journey plan in the third example is $$$c = [3, 6]$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nvoid main()\n{\n long n; get(n);\n auto b = new long[cast(size_t)n]; get(b);\n long[long] mb;\n long m = long.min;\n zip(iota(0, n), b).each!((p) {mb[p[1] - p[0]] += b[cast(size_t)p[0]]; m = max(m, mb[p[1] - p[0]]);});\n ans(m);\n}\n"}, {"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); } bool DEBUG = 0; \nvoid log(A ...)(lazy A a){ if(DEBUG) print(a); }\nvoid print(){ writeln(\"\"); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, \" \"), print(a); }\nstring unsplit(T)(T xs){ return xs.array.to!(string[]).join(\" \"); }\nstring scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }\nT lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n int n = scan!int;\n long[] as = scan!long(n);\n\n long[] xs = new long[](600_999);\n foreach(i, a; as){\n int d = n + a.to!int - (i + 1).to!int;\n xs[d] += a;\n }\n\n long ans = 0;\n foreach(x; xs) ans.raiseTo(x);\n\n ans.writeln;\n}\n\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto B = new int[N];\n foreach (i; 0 .. N) {\n B[i] = readInt();\n }\n \n long[long] sums;\n foreach (i; 0 .. N) {\n sums[B[i] - i] += B[i];\n }\n const ans = sums.values.maxElement;\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.range;\nimport std.stdio;\nimport std.typecons;\nimport std.traits;\nimport std.array;\nstruct Input\n{\n T next(T)()\n {\n import std.conv;\n return to!T(nextWord);\n }\n string nextWord()\n {\n if (_nextWords.empty)\n\t_nextWords.insertFront(readln().split);\n string word = _nextWords.front; _nextWords.removeFront;\n return word;\n }\n import std.container;\n DList!string _nextWords;\n}\nInput input;\nvoid read(T...)(ref T args)\n{\n import std.traits;\n static foreach(i; 0 .. T.length)\n static if(isArray!(T[i]) && !is(T[i] == string))\n foreach(ref e; args[i])\n \tread(e);\n else static if(isTuple!(T[i]))\n static foreach(n; 0 .. T[i].Types.length)\n\tread(args[i][n]);\n else\n args[i] = input.next!(typeof(args[i]))();\n}\nauto itup(alias k, alias F)()\n{\n alias T = Parameters!F;\n foreach(i; 0 .. k)\n {\n T t; get(t);\n F(t);\n }\n}\nalias get = read;\nvoid wone(T)(T t, char end)\n{\n static if(isArray!T && !is(T == string))\n {\n foreach(i; 0 .. t.length - 1)\n\t write(t[i], ' ');\n write(t[$ - 1], end);\n }\n else\n write(t, end);\n}\nvoid wr(T...)(T t)\n{\n static foreach(i; 0 .. T.length)\n static if(i + 1 < T.length)\n wone(t[i], ' ');\n else\n wone(t[i], '\\n');\n}\nvoid ans(T...)(T t)\n{\n import core.stdc.stdlib;\n wr(t);\n exit(0);\n}\n\nvoid main()\n{\n int n; get(n);\n auto b = new int[n]; get(b);\n int[int] mb;\n int m = int.min;\n zip(iota(0, n), b).each!((p) {mb[p[1] - p[0]] += b[p[0]]; m = max(m, mb[p[1] - p[0]]);});\n ans(m);\n}\n"}], "src_uid": "aab8d5a2d42b4199310f3f535a6b3bd7"} {"nl": {"description": "Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment $$$0$$$. Also, let's say that the train will visit $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$ along its way, and that Alexey destination is the station $$$n$$$.Alexey learned from the train schedule $$$n$$$ integer pairs $$$(a_i, b_i)$$$ where $$$a_i$$$ is the expected time of train's arrival at the $$$i$$$-th station and $$$b_i$$$ is the expected time of departure.Also, using all information he has, Alexey was able to calculate $$$n$$$ integers $$$tm_1, tm_2, \\dots, tm_n$$$ where $$$tm_i$$$ is the extra time the train need to travel from the station $$$i - 1$$$ to the station $$$i$$$. Formally, the train needs exactly $$$a_i - b_{i-1} + tm_i$$$ time to travel from station $$$i - 1$$$ to station $$$i$$$ (if $$$i = 1$$$ then $$$b_0$$$ is the moment the train leave the terminal, and it's equal to $$$0$$$).The train leaves the station $$$i$$$, if both conditions are met: it's on the station for at least $$$\\left\\lceil \\frac{b_i - a_i}{2} \\right\\rceil$$$ units of time (division with ceiling); current time $$$\\ge b_i$$$. Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station $$$n$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of stations. Next $$$n$$$ lines contain two integers each: $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le a_i < b_i \\le 10^6$$$). It's guaranteed that $$$b_i < a_{i+1}$$$. Next line contains $$$n$$$ integers $$$tm_1, tm_2, \\dots, tm_n$$$ ($$$0 \\le tm_i \\le 10^6$$$).", "output_spec": "For each test case, print one integer — the time of Alexey's arrival at the last station.", "sample_inputs": ["2\n2\n2 4\n10 12\n0 2\n5\n1 4\n7 8\n9 10\n13 15\n19 20\n1 2 3 4 5"], "sample_outputs": ["12\n32"], "notes": "NoteIn the first test case, Alexey arrives at station $$$1$$$ without any delay at the moment $$$a_1 = 2$$$ (since $$$tm_1 = 0$$$). After that, he departs at moment $$$b_1 = 4$$$. Finally, he arrives at station $$$2$$$ with $$$tm_2 = 2$$$ extra time, or at the moment $$$12$$$.In the second test case, Alexey arrives at the first station with $$$tm_1 = 1$$$ extra time, or at moment $$$2$$$. The train, from one side, should stay at the station at least $$$\\left\\lceil \\frac{b_1 - a_1}{2} \\right\\rceil = 2$$$ units of time and from the other side should depart not earlier than at moment $$$b_1 = 4$$$. As a result, the trains departs right at the moment $$$4$$$.Using the same logic, we can figure out that the train arrives at the second station at the moment $$$9$$$ and departs at the moment $$$10$$$; at the third station: arrives at $$$14$$$ and departs at $$$15$$$; at the fourth: arrives at $$$22$$$ and departs at $$$23$$$. And, finally, arrives at the fifth station at $$$32$$$."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm, std.range, std.conv;\r\nvoid main()\r\n{\r\n int q;\r\n readf!\"%d\\n\"(q);\r\n foreach (_; 0 .. q)\r\n {\r\n int n;\r\n readf!\"%d\\n\"(n);\r\n int[] a = new int[n], b = new int[n];\r\n foreach (i; 0 .. n)\r\n readf!\"%d %d\\n\"(a[i], b[i]);\r\n int[] tm = readln.split.map!(to!int).array;\r\n int res = 0;\r\n foreach (i; 0 .. n)\r\n {\r\n res += a[i] - (i > 0 ? b[i - 1] : 0) + tm[i];\r\n if (i < n - 1)\r\n res = max(res + (b[i] - a[i] + 1) / 2, b[i]);\r\n }\r\n writeln(res);\r\n }\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = new long[](n);\r\n\t\tauto b = new long[](n);\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\ta[i] = RD;\r\n\t\t\tb[i] = RD;\r\n\t\t}\r\n\t\tauto tm = RDA;\r\n\r\n\t\tlong time;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tlong d;\r\n\t\t\tif (i == 0)\r\n\t\t\t\td = a[i] + tm[i];\r\n\t\t\telse\r\n\t\t\t\td = a[i] - b[i-1] + tm[i];\r\n\t\t\ttime += d;\r\n\t\t\tdebug writeln(\"time:\", time);\r\n\t\t\tif (i == n-1) break;\r\n\t\t\tauto w = (b[i]-a[i]+1) / 2;\r\n\t\t\ttime = max(time+w, b[i]);\r\n\t\t}\r\n\t\tans[ti] = time;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "42840fc873369e0d0d6a4ad24a43f5a6"} {"nl": {"description": "Polycarp lives on a coordinate line at the point $$$x = 0$$$. He goes to his friend that lives at the point $$$x = a$$$. Polycarp can move only from left to right, he can pass one unit of length each second.Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $$$n$$$ non-intersecting segments, the $$$i$$$-th segment which is in the rain is represented as $$$[l_i, r_i]$$$ ($$$0 \\le l_i < r_i \\le a$$$).There are $$$m$$$ umbrellas lying on the line, the $$$i$$$-th umbrella is located at point $$$x_i$$$ ($$$0 \\le x_i \\le a$$$) and has weight $$$p_i$$$. When Polycarp begins his journey, he doesn't have any umbrellas.During his journey from $$$x = 0$$$ to $$$x = a$$$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $$$x$$$ to $$$x + 1$$$ if a segment $$$[x, x + 1]$$$ is in the rain (i.e. if there exists some $$$i$$$ such that $$$l_i \\le x$$$ and $$$x + 1 \\le r_i$$$).The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving.Can Polycarp make his way from point $$$x = 0$$$ to point $$$x = a$$$? If yes, find the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.", "input_spec": "The first line contains three integers $$$a$$$, $$$n$$$ and $$$m$$$ ($$$1 \\le a, m \\le 2000, 1 \\le n \\le \\lceil\\frac{a}{2}\\rceil$$$) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \\le l_i < r_i \\le a$$$) — the borders of the $$$i$$$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $$$i$$$ and $$$j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$0 \\le x_i \\le a$$$, $$$1 \\le p_i \\le 10^5$$$) — the location and the weight of the $$$i$$$-th umbrella.", "output_spec": "Print \"-1\" (without quotes) if Polycarp can't make his way from point $$$x = 0$$$ to point $$$x = a$$$. Otherwise print one integer — the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.", "sample_inputs": ["10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2", "10 1 1\n0 9\n0 5", "10 1 1\n0 9\n1 5"], "sample_outputs": ["14", "45", "-1"], "notes": "NoteIn the first example the only possible strategy is to take the fourth umbrella at the point $$$x = 1$$$, keep it till the point $$$x = 7$$$ (the total fatigue at $$$x = 7$$$ will be equal to $$$12$$$), throw it away, move on from $$$x = 7$$$ to $$$x = 8$$$ without an umbrella, take the third umbrella at $$$x = 8$$$ and keep it till the end (the total fatigue at $$$x = 10$$$ will be equal to $$$14$$$). In the second example the only possible strategy is to take the first umbrella, move with it till the point $$$x = 9$$$, throw it away and proceed without an umbrella till the end."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nvoid main() {\n long INF = 1L << 59;\n\n auto s = readln.split.map!(to!int);\n auto A = s[0];\n auto M = s[1];\n auto N = s[2];\n auto R = M.iota.map!(_ => readln.split.map!(to!int).array).array;\n auto U = N.iota.map!(_ => readln.split.map!(to!int).array).array;\n\n auto rain = new bool[](A+1);\n auto umb = new int[][](A+1);\n\n foreach (lr; R) {\n foreach (j; lr[0]+1..lr[1]+1) {\n rain[j] = true;\n }\n }\n\n foreach (i; 0..N) {\n umb[U[i][0]] ~= i;\n }\n\n auto dp = new long[][](A+1, N+1);\n foreach (i; 0..A+1) dp[i].fill(INF);\n dp[0][N] = 0;\n\n foreach (i; 0..A) {\n // swap umbrellas\n long m = dp[i].reduce!min;\n foreach (u; umb[i]) dp[i][u] = m;\n dp[i][N] = rain[i+1] ? INF : m;\n\n //\n foreach (j; 0..N) {\n if (dp[i][j] == INF) continue;\n dp[i+1][j] = min(dp[i+1][j], dp[i][j] + U[j][1]);\n }\n dp[i+1][N] = dp[i][N];\n }\n\n long ans;\n if (rain[A]) {\n ans = dp[A][0..N].reduce!min;\n } else {\n ans = dp[A].reduce!min;\n }\n\n writeln(ans >= INF ? -1 : ans);\n}\n"}], "negative_code": [], "src_uid": "fbec761a428198f5e624c4895e395da3"} {"nl": {"description": "A pair of positive integers $$$(a,b)$$$ is called special if $$$\\lfloor \\frac{a}{b} \\rfloor = a \\bmod b$$$. Here, $$$\\lfloor \\frac{a}{b} \\rfloor$$$ is the result of the integer division between $$$a$$$ and $$$b$$$, while $$$a \\bmod b$$$ is its remainder.You are given two integers $$$x$$$ and $$$y$$$. Find the number of special pairs $$$(a,b)$$$ such that $$$1\\leq a \\leq x$$$ and $$$1 \\leq b \\leq y$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The only line of the description of each test case contains two integers $$$x$$$, $$$y$$$ ($$$1 \\le x,y \\le 10^9$$$).", "output_spec": "For each test case print the answer on a single line.", "sample_inputs": ["9\n3 4\n2 100\n4 3\n50 3\n12 4\n69 420\n12345 6789\n123456 789\n12345678 9"], "sample_outputs": ["1\n0\n2\n3\n5\n141\n53384\n160909\n36"], "notes": "NoteIn the first test case, the only special pair is $$$(3, 2)$$$.In the second test case, there are no special pairs.In the third test case, there are two special pairs: $$$(3, 2)$$$ and $$$(4, 3)$$$."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main()\r\n{ \r\n auto t = readln.chomp.to!int;\r\n \r\n while (t--) {\r\n auto v = readln.splitter.map!(to!int).array;\r\n auto x = v[0], y = v[1];\r\n \r\n long ans = 0;\r\n foreach (r; 1 .. y) {\r\n int left = x - r;\r\n if (left <= 1) { break; }\r\n \r\n int mxk = min(left / r, y);\r\n if (mxk <= r) { break; }\r\n \r\n int cur = mxk - r;\r\n \r\n ans += cur;\r\n }\r\n \r\n ans.writeln;\r\n }\r\n}"}, {"source_code": "import std;\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t; 0 .. T) solve();\r\n}\r\n\r\nvoid solve() {\r\n long X, Y; readf(\"%d %d\\n\", &X, &Y);\r\n long bs; {\r\n long lb = 0, ub = 1L<<30;\r\n while (lb + 1 < ub) {\r\n long mid = (lb + ub) / 2;\r\n (mid - 1 <= X / (mid + 1L) ? lb : ub) = mid;\r\n }\r\n bs = min(lb, Y);\r\n }\r\n long A = (1L + bs) * bs / 2L - bs;\r\n long B = 0;\r\n if (bs == Y) {\r\n } else {\r\n for (long k = 1; k <= X / (bs + 1L); k++) {\r\n long U = min(Y, X / k - 1L);\r\n long L = max(bs, X / (k + 1L) - 1L);\r\n B += k * max(0L, U - L);\r\n }\r\n }\r\n writeln(A + B);\r\n}\r\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\r\n\r\nvoid get(Args...)(ref Args args)\r\n{\r\n import std.traits, std.meta, std.typecons;\r\n\r\n static if (Args.length == 1) {\r\n alias Arg = Args[0];\r\n \r\n static if (isArray!Arg) {\r\n static if (isSomeChar!(ElementType!Arg)) {\r\n args[0] = readln.chomp.to!Arg;\r\n } else {\r\n args[0] = readln.split.to!Arg;\r\n }\r\n } else static if (isTuple!Arg) {\r\n auto input = readln.split;\r\n static foreach (i; 0..Fields!Arg.length) {\r\n args[0][i] = input[i].to!(Fields!Arg[i]);\r\n }\r\n } else {\r\n args[0] = readln.chomp.to!Arg;\r\n }\r\n } else {\r\n auto input = readln.split;\r\n assert(input.length == Args.length);\r\n\r\n static foreach (i; 0..Args.length) {\r\n args[i] = input[i].to!(Args[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid get_lines(Args...)(size_t N, ref Args args)\r\n{\r\n import std.traits, std.range;\r\n\r\n static foreach (i; 0..Args.length) {\r\n static assert(isArray!(Args[i]));\r\n args[i].length = N;\r\n }\r\n\r\n foreach (i; 0..N) {\r\n static if (Args.length == 1) {\r\n get(args[0][i]);\r\n } else {\r\n auto input = readln.split;\r\n static foreach (j; 0..Args.length) {\r\n args[j][i] = input[j].to!(ElementType!(Args[j]));\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid main()\r\n{\r\n int T; get(T);\r\n while (T--) {\r\n long x, y; get(x, y);\r\n long res;\r\n for (long i = 1; i^^2 <= x; ++i) {\r\n res += max(0, min((x - i) / i, y) - i);\r\n }\r\n writeln(res);\r\n }\r\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto x = RD;\r\n\t\tauto y = RD;\r\n\r\n\t\tfor (long i = 1; i*i < x; ++i)\r\n\t\t{\r\n\t\t\tauto xx = x - i;\r\n\t\t\tauto r = min(xx / i, y) - i;\r\n\t\t\tif (r > 0)\r\n\t\t\t\tans[ti] += r;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint x, y;\r\n\t\treadf !(\" %s %s\") (x, y);\r\n\t\tlong res = 0;\r\n\t\tfor (int r = 1; ; r++)\r\n\t\t{\r\n\t\t\tint lo = r + 2;\r\n\t\t\tint hi = min (y + 1, x / r);\r\n\t\t\tif (hi < lo)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tres += hi - lo + 1;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main()\r\n{ \r\n auto t = readln.chomp.to!int;\r\n \r\n while (t--) {\r\n auto v = readln.splitter.map!(to!int).array;\r\n auto x = v[0], y = v[1];\r\n \r\n int ans = 0;\r\n foreach (r; 1 .. y) {\r\n int left = x - r;\r\n if (left <= 1) { break; }\r\n \r\n int mxk = min(left / r, y);\r\n if (mxk <= r) { break; }\r\n \r\n int cur = mxk - r;\r\n \r\n ans += cur;\r\n }\r\n \r\n ans.writeln;\r\n }\r\n}"}, {"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main()\r\n{ \r\n auto t = readln.chomp.to!int;\r\n \r\n while (t--) {\r\n auto v = readln.splitter.map!(to!int).array;\r\n auto x = v[0], y = v[1];\r\n \r\n int ans = 0;\r\n foreach (b; 1 .. y + 1) {\r\n int left = x - b;\r\n if (left <= 1) { break; }\r\n \r\n int mxk = min(left / b, y);\r\n if (mxk - b <= 0) { break; }\r\n \r\n int cur = mxk - b;\r\n \r\n debug { writeln(b, ' ', cur); }\r\n \r\n ans += cur;\r\n }\r\n \r\n ans.writeln;\r\n }\r\n}"}, {"source_code": "import core.bitop, std.bitmanip;\r\nimport core.checkedint;\r\nimport std.algorithm, std.functional, std.meta;\r\nimport std.array, std.container;\r\nimport std.bigint;\r\nimport std.conv;\r\nimport std.math, std.numeric;\r\nimport std.range, std.range.interfaces;\r\nimport std.stdio, std.string;\r\nimport std.ascii, std.typecons;\r\n\r\nvoid main()\r\n{ \r\n auto t = readln.chomp.to!int;\r\n \r\n while (t--) {\r\n auto v = readln.splitter.map!(to!int).array;\r\n auto x = v[0], y = v[1];\r\n \r\n int sq = sqrt(x.to!real).to!int;\r\n \r\n int ans = 0;\r\n foreach (b; 1 .. min(y, sq) + 1) {\r\n int left = x - b;\r\n if (left <= 1) { break; }\r\n \r\n int mxk = min(left / b, y);\r\n if (mxk - b <= 0) { break; }\r\n \r\n int cur = mxk - b;\r\n \r\n debug { writeln(b, ' ', cur); }\r\n \r\n ans += cur;\r\n }\r\n \r\n ans.writeln;\r\n }\r\n}"}], "src_uid": "efdb966e414050c5e51e8beb7bb06b20"} {"nl": {"description": "One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.Please, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 105) — the number of laptops. Next n lines contain two integers each, ai and bi (1 ≤ ai, bi ≤ n), where ai is the price of the i-th laptop, and bi is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality). All ai are distinct. All bi are distinct. ", "output_spec": "If Alex is correct, print \"Happy Alex\", otherwise print \"Poor Alex\" (without the quotes).", "sample_inputs": ["2\n1 2\n2 1"], "sample_outputs": ["Happy Alex"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\n\nvoid main() {\n\tint n;\n\treadf(\"%s\", &n);\n\tforeach(_; 0..n) {\n\t\tint ai, bi;\n\t\treadf(\" %s %s \", &ai, &bi);\n\t\tif (ai != bi) {\n\t\t\twriteln(\"Happy Alex\");\n\t\t\treturn;\n\t\t}\n\t}\n\twriteln(\"Poor Alex\");\n}"}, {"source_code": "import std.typecons;\nimport std.algorithm;\n\nalias Note = Tuple!(uint, \"a\", uint, \"b\");\n\nbool noteLess(const Note a, const Note b) {\n\tif (a.a == b.a) {\n\t\treturn a.b > b.b;\n\t}\n\n\treturn a.a < b.a;\n}\n\nbool solve(Note[] ns) {\n\tsort!(noteLess)(ns);\n\tforeach (i; 1..ns.length) {\n\t\tNote prev = ns[i - 1];\n\t\tNote cur = ns[i];\n\t\tif (prev.a < cur.a && prev.b > cur.b) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nunittest {\n\t{\n\t\tNote[] ns = [Note(1, 2), Note(2, 1)];\n\t\tassert(solve(ns));\n\t}\n\n\t{\n\t\tNote[] ns = [Note(2, 1), Note(1, 2)];\n\t\tassert(solve(ns));\n\t}\n\n\t{\n\t\tNote[] ns = [Note(1, 1)];\n\t\tassert(false == solve(ns));\n\t}\n\n\t{\n\t\tNote[] ns = [Note(1, 1), Note(2, 2), Note(3, 3), Note(4, 4), Note(5, 5)];\n\t\tassert(false == solve(ns));\n\t}\n\n\t{\n\t\tNote[] ns = [Note(4, 5), Note(2, 2), Note(3, 3), Note(1, 1), Note(5, 4)];\n\t\tassert(solve(ns));\n\t}\n}\n\nint main(string[] argv) {\n\timport std.stdio;\n\tuint n = 0;\n\treadf(\" %s\", &n);\n\tauto ns = new Note[](n);\n\tforeach (i; 0..n) {\n\t\treadf(\" %s %s\", &(ns[i].a), &(ns[i].b));\n\t}\n\twriteln(solve(ns) ? \"Happy Alex\" : \"Poor Alex\");\n return 0;\n}\n"}, {"source_code": "import std.stdio;\n\nvoid main() {\n int n; readf(\"%d\\n\", &n);\n bool poor = true;\n for (int i = 0; i < n; ++i) {\n int a, b;\n readf(\"%d %d\\n\", &a, &b);\n if (poor && a != b) {\n poor = false;\n }\n }\n if (poor)\n writeln(\"Poor Alex\");\n else\n writeln(\"Happy Alex\");\n}"}, {"source_code": "import std.stdio;\nimport std.string;\nimport std.conv;\nimport std.regex;\nimport std.algorithm.iteration;\nimport std.algorithm.sorting;\nimport std.typecons;\n\nvoid main()\n{\n auto n = stdin.readln.strip.to!int;\n alias DicEntry = Tuple!(int, \"price\", int, \"quality\");\n DicEntry[] prices;\n prices.length = n;\n \n for(int i=0; ia.price < b.price);\n for(int i=0; i prices[i+1].quality) {\n s = \"Happy Alex\";\n break;\n }\n }\n writeln(s);\n}\n"}, {"source_code": "// https://codeforces.com/problemset/problem/456/A\n\nimport std.stdio;\nimport std.conv;\nimport std.string;\nimport std.algorithm;\n\nvoid main() {\n int n = readln.chomp.to!int;\n int[][] laptops;\n\n int a, b;\n foreach(idx; 0..n) {\n int[] laptop;\n readf(\"%d %d\\n\", &a, &b);\n laptop ~= [a,b];\n laptops ~= laptop;\n }\n\n bool happy = false;\n //laptops.sort();\n\n for(int i = 0; i < n; i++)\n if(laptops[i][0] != laptops[i][1])\n happy = true;\n\n /* brute force\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(laptops[i][0] < laptops[j][0] &&\n laptops[i][1] > laptops[j][1])\n happy = true;\n }\n }\n */\n \n if(happy)\n writeln(\"Happy Alex\");\n else\n writeln(\"Poor Alex\");\n}\n\n"}, {"source_code": "import std.stdio, std.string, std.algorithm, std.range, std.conv;\n\nvoid main() {\n int n = readln.chomp.to!int;\n int[][] info;\n\n foreach (i; 0..n) {\n info ~= readln.chomp.split.map!(to!int).array;\n }\n\n string ans = \"Poor Alex\";\n info.sort;\n foreach (i; 1..n) {\n if (info[i][1] < info[i - 1][1]) {\n ans = \"Happy Alex\";\n break;\n }\n }\n\n ans.writeln;\n}"}, {"source_code": "import std.stdio;\nimport std.algorithm;\n\nunittest{\n assert(solve([[1,2],[2,1]]) == true, \"Test 1\");\n assert(solve([[1,1],[2,2]]) == false, \"Test 2\");\n assert(solve([[2,2],[3,3],[1,1]]) == false, \"Test 3\");\n\n}\n\nbool solve(int[2][] lap){\n for(int i =1; i < lap.length; i++){\n if(!(lap[i][0] == lap[i][1])){\n return true;\n }\n }\n return false;\n}\n\nvoid main(){\n int n;\n readf(\"%s\\n\",&n);\n\n int[2][] lap = new int[2][n];\n for(int i = 0; i < n; i++){\n int p,q;\n readf(\"%s %s\\n\",&p,&q);\n lap[i][0] = p;\n lap[i][1] = q;\n }\n\n if (solve(lap)) {\n writeln(\"Happy Alex\");\n } else {\n writeln(\"Poor Alex\");\n }\n}\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint N;\nint[] A, B;\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tN = readInt;\n\t\tA = new int[N];\n\t\tB = new int[N];\n\t\tforeach (i; 0 .. N) {\n\t\t\tA[i] = readInt;\n\t\t\tB[i] = readInt;\n\t\t}\n\t\t\n\t\tauto ps = new Pair!(int, int)[N];\n\t\tforeach (i; 0 .. N) {\n\t\t\tps[i] = pair(A[i], B[i]);\n\t\t}\n\t\tps.sort();\ndebug{\nwriteln(\"ps = \",ps);\n}\n\t\t\n\t\tbool ans;\n\t\tint max;\n\t\tforeach (i; 0 .. N) {\n\t\t\tif (max > ps[i].y) {\n\t\t\t\tans = true;\n\t\t\t}\n\t\t\tchmax(max, ps[i].y);\n\t\t}\n\t\twriteln(ans ? \"Happy Alex\" : \"Poor Alex\");\n\t\t\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [{"source_code": "import std.stdio;\n\nvoid main() {\n\tint n;\n\treadf(\"%s\", &n);\n\tforeach(_; 0..n) {\n\t\tint ai, bi;\n\t\treadf(\" %s %s \", &ai, &bi);\n\t\tif (ai != bi) {\n\t\t\twriteln(\"Poor Alex\");\n\t\t\treturn;\n\t\t}\n\t}\n\twriteln(\"Happy Alex\");\n}"}, {"source_code": "import std.stdio;\nimport std.algorithm;\n\nunittest{\n assert(solve([[1,2,0],[2,1,1]]) == true, \"Test 1\");\n}\n\nbool solve(int[3][] lap){\n for(int i =1; i < lap.length; i++){\n if(!(lap[i][0] == lap[i][i])){\n return true;\n }\n }\n return false;\n}\n\nvoid main(){\n int n;\n readf(\"%s\\n\",&n);\n\n int[3][] lap = new int[3][n];\n for(int i = 0; i < n; i++){\n int p,q;\n readf(\"%s %s\\n\",&p,&q);\n lap[i][0] = p;\n lap[i][1] = q;\n lap[i][2] = i;\n }\n\n if (solve(lap)) {\n writeln(\"Happy Alex\");\n } else {\n writeln(\"Poor Alex\");\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.algorithm;\n\nunittest{\n // assert(solve([[1,2,0],[2,1,1]]) == true, \"Test 1\");\n}\n\nbool solve(int[3][] lap){\n sort!((int[3] a,int[3] b){ return a[0] < b[0];})(lap);\n //writeln(lap);\n\n for(int i =1; i < lap.length; i++){\n for(int j = 0; j < i; j++){\n if(lap[j][1] > lap[i][1]){\n return true;\n }\n }\n }\n return false;\n}\n\nvoid main(){\n int n;\n readf(\"%s\\n\",&n);\n\n int[3][] lap = new int[3][n];\n for(int i = 0; i < n; i++){\n int p,q;\n readf(\"%s %s\\n\",&p,&q);\n lap[i][0] = p;\n lap[i][1] = q;\n lap[i][2] = i;\n }\n\n if (solve(lap)) {\n writeln(\"Happy Allex\");\n } else {\n writeln(\"Poor Allex\");\n }\n}\n"}, {"source_code": "import std.stdio;\nimport std.algorithm;\n\nunittest{\n assert(solve([[1,2],[2,1]]) == true, \"Test 1\");\n}\n\nbool solve(int[2][] lap){\n for(int i =1; i < lap.length; i++){\n for(int j = 0; j < i; j++){\n if(!(lap[i] == lap[j])){\n return true;\n }\n }\n }\n return false;\n}\n\nvoid main(){\n int n;\n readf(\"%s\\n\",&n);\n\n int[2][] lap = new int[2][n];\n for(int i = 0; i < n; i++){\n int p,q;\n readf(\"%s %s\\n\",&p,&q);\n lap[i][0] = p;\n lap[i][1] = q;\n }\n\n if (solve(lap)) {\n writeln(\"Happy Allex\");\n } else {\n writeln(\"Poor Allex\");\n }\n}\n"}], "src_uid": "c21a84c4523f7ef6cfa232cba8b6ee2e"} {"nl": {"description": "After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size $$$n \\times m$$$ (where $$$n$$$ is the number of rows, $$$m$$$ is the number of columns) and starts to draw snakes in cells.Polycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only $$$26$$$ letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed $$$26$$$.Since by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals $$$1$$$, i.e. each snake has size either $$$1 \\times l$$$ or $$$l \\times 1$$$, where $$$l$$$ is snake's length. Note that snakes can't bend.When Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake \"over the top\" and overwrites the previous value in the cell.Recently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) — the number of test cases to solve. Then $$$t$$$ test cases follow. The first line of the test case description contains two integers $$$n$$$, $$$m$$$ ($$$1 \\le n,m \\le 2000$$$) — length and width of the checkered sheet of paper respectively. Next $$$n$$$ lines of test case description contain $$$m$$$ symbols, which are responsible for the content of the corresponding cell on the sheet. It can be either lowercase Latin letter or symbol dot ('.'), which stands for an empty cell. It is guaranteed that the total area of all sheets in one test doesn't exceed $$$4\\cdot10^6$$$.", "output_spec": "Print the answer for each test case in the input. In the first line of the output for a test case print YES if it is possible to draw snakes, so that you can get a sheet of paper from the input. If it is impossible, print NO. If the answer to this question is positive, then print the way to draw snakes in the following format. In the next line print one integer $$$k$$$ ($$$0 \\le k \\le 26$$$) — number of snakes. Then print $$$k$$$ lines, in each line print four integers $$$r_{1,i}$$$, $$$c_{1,i}$$$, $$$r_{2,i}$$$ and $$$c_{2,i}$$$ — coordinates of extreme cells for the $$$i$$$-th snake ($$$1 \\le r_{1,i}, r_{2,i} \\le n$$$, $$$1 \\le c_{1,i}, c_{2,i} \\le m$$$). Snakes should be printed in order of their drawing. If there are multiple solutions, you are allowed to print any of them. Note that Polycarp starts drawing of snakes with an empty sheet of paper.", "sample_inputs": ["1\n5 6\n...a..\n..bbb.\n...a..\n.cccc.\n...a..", "3\n3 3\n...\n...\n...\n4 4\n..c.\nadda\nbbcb\n....\n3 5\n..b..\naaaaa\n..b..", "2\n3 3\n...\n.a.\n...\n2 2\nbb\ncc"], "sample_outputs": ["YES\n3\n1 4 5 4\n2 3 2 5\n4 2 4 5", "YES\n0\nYES\n4\n2 1 2 4\n3 1 3 4\n1 3 3 3\n2 2 2 3\nNO", "YES\n1\n2 2 2 2\nYES\n3\n1 1 1 2\n1 1 1 2\n2 1 2 2"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint tests;\n\treadf !(\" %s\") (tests);\nmultitest_loop:\n\tforeach (test; 0..tests)\n\t{\n\t\tint rows, cols;\n\t\treadf !(\" %s %s\") (rows, cols);\n\t\treadln;\n\t\tchar [] [] board;\n\t\tboard.reserve (rows);\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\tboard ~= readln.strip.dup;\n\t\t}\n\n\t\tint [] [] ans;\n\t\tbool started = false;\n\t\tint aRow = int.max;\n\t\tint aCol = int.max;\n\t\tforeach_reverse (letter; 'a'..'{')\n\t\t{\n\t\t\tint rLo = int.max;\n\t\t\tint rHi = int.min;\n\t\t\tint cLo = int.max;\n\t\t\tint cHi = int.min;\n\t\t\tforeach (row; 0..rows)\n\t\t\t{\n\t\t\t\tforeach (col; 0..cols)\n\t\t\t\t{\n\t\t\t\t\tif (board[row][col] == letter)\n\t\t\t\t\t{\n\t\t\t\t\t\trLo = min (rLo, row);\n\t\t\t\t\t\trHi = max (rHi, row);\n\t\t\t\t\t\tcLo = min (cLo, col);\n\t\t\t\t\t\tcHi = max (cHi, col);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (rLo == int.max)\n\t\t\t{\n\t\t\t\tif (started)\n\t\t\t\t{\n\t\t\t\t\tans ~= [aRow, aCol, aRow, aCol];\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (rLo != rHi && cLo != cHi)\n\t\t\t{\n\t\t\t\twriteln (\"NO\");\n\t\t\t\tcontinue multitest_loop;\n\t\t\t}\n\t\t\tans ~= [rLo, cLo, rHi, cHi];\n\t\t\tforeach (row; rLo..rHi + 1)\n\t\t\t{\n\t\t\t\tforeach (col; cLo..cHi + 1)\n\t\t\t\t{\n\t\t\t\t\tif (board[row][col] != letter &&\n\t\t\t\t\t board[row][col] != '*')\n\t\t\t\t\t{\n\t\t\t\t\t\twriteln (\"NO\");\n\t\t\t\t\t\tcontinue multitest_loop;\n\t\t\t\t\t}\n\t\t\t\t\tboard[row][col] = '*';\n\t\t\t\t}\n\t\t\t}\n\t\t\tstarted = true;\n\t\t\taRow = rLo;\n\t\t\taCol = cLo;\n\t\t}\n\t\tdebug {writefln (\"%-(%s\\n%)\", board);}\n\t\treverse (ans);\n\t\twriteln (\"YES\");\n\t\twritefln (\"%s%(\\n%(%s %)%)\", ans.length,\n\t\t ans.map !(line => line.map !(x => x + 1)));\n\t}\n}\n"}], "negative_code": [], "src_uid": "f34d21b9568c758cacc1d00af1316c86"} {"nl": {"description": "A string is called palindrome if it reads the same from left to right and from right to left. For example \"kazak\", \"oo\", \"r\" and \"mikhailrubinchikkihcniburliahkim\" are palindroms, but strings \"abb\" and \"ij\" are not.You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes. You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.", "input_spec": "The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.", "output_spec": "Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.", "sample_inputs": ["aabc", "aabcd"], "sample_outputs": ["abba", "abcba"], "notes": null}, "positive_code": [{"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto s = readln().strip();\n\n int[char] countOfLetters;\n\n foreach (letter; s)\n if (countOfLetters.get(letter, false) == false)\n countOfLetters[letter] = s.countchars(letter.to! (string));\n\n debug {writeln(countOfLetters);};\n\n string result;\n\n foreach (key; countOfLetters.keys())\n if (countOfLetters[key] / 2 > 0) {\n result ~= key.to! (string).repeat(countOfLetters[key] / 2)\n .join();\n if (countOfLetters[key] % 2 == 0)\n countOfLetters.remove(key);\n else\n countOfLetters[key] = 1;\n }\n\n while (countOfLetters.length > 1) {\n auto minKey = minPos(countOfLetters.keys()).front.to! (char);\n auto maxKey = minPos!(\"a > b\")(countOfLetters.keys()).front.to! (char);\n\n result ~= minKey.to! (string).repeat((countOfLetters[minKey] + 1) / 2).join();\n result ~= maxKey.to! (string).repeat((countOfLetters[minKey] - 1) / 2).join();\n countOfLetters.remove(minKey);\n countOfLetters.remove(maxKey);\n }\n\n if (countOfLetters.length == 1) {\n auto temp = countOfLetters.keys().front.to! (char);\n result = result.dup.sort.to! (string) ~\n temp.to! (string).repeat(countOfLetters[temp]).join() ~\n result.dup.sort.reverse.to! (string);\n } else\n result = result.dup.sort.to! (string) ~ result.dup.sort.reverse.to! (string);\n\n writeln(result);\n\n\treturn 0;\n}\n"}], "negative_code": [{"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto s = readln().strip();\n\n int[char] countOfLetters;\n\n foreach (letter; s)\n if (countOfLetters.get(letter, false) == false)\n countOfLetters[letter] = s.countchars(letter.to! (string));\n\n debug {writeln(countOfLetters);};\n\n string result;\n\n foreach (key; countOfLetters.keys())\n if (countOfLetters[key] % 2 == 0) {\n result ~= key.to! (string).repeat(countOfLetters[key] / 2)\n .join();\n countOfLetters.remove(key);\n }\n\n while (countOfLetters.length > 1) {\n auto minKey = minPos(countOfLetters.keys()).front.to! (char);\n auto maxKey = minPos!(\"a > b\")(countOfLetters.keys()).front.to! (char);\n\n result ~= minKey.to! (string).repeat((countOfLetters[minKey] + 1) / 2).join();\n result ~= maxKey.to! (string).repeat((countOfLetters[minKey] - 1) / 2).join();\n countOfLetters.remove(minKey);\n countOfLetters.remove(maxKey);\n }\n\n if (countOfLetters.length == 1)\n result = result.dup.sort.to! (string) ~\n countOfLetters.keys().front.to! (string) ~\n result.dup.sort.reverse.to! (string);\n else\n result = result.dup.sort.to! (string) ~ result.dup.sort.reverse.to! (string);\n\n writeln(result);\n\n\treturn 0;\n}\n"}, {"source_code": "module main;\n\nimport std.stdio;\nimport std.range;\nimport std.string;\nimport std.algorithm;\nimport std.math;\nimport std.typecons;\nimport std.conv;\nimport std.functional;\nimport std.container;\n\n\nint main()\n{\n auto s = readln().strip();\n\n int[char] countOfLetters;\n\n foreach (letter; s)\n if (countOfLetters.get(letter, false) == false)\n countOfLetters[letter] = s.countchars(letter.to! (string));\n\n debug {writeln(countOfLetters);};\n\n string result;\n\n foreach (key; countOfLetters.keys())\n if (countOfLetters[key] % 2 == 0) {\n result ~= key.to! (string).repeat(countOfLetters[key] / 2)\n .join();\n countOfLetters.remove(key);\n }\n\n while (countOfLetters.length > 1) {\n auto minKey = minPos(countOfLetters.keys()).front.to! (char);\n auto maxKey = minPos!(\"a > b\")(countOfLetters.keys()).front.to! (char);\n\n result ~= minKey.to! (string).repeat((countOfLetters[minKey] + 1) / 2).join();\n result ~= maxKey.to! (string).repeat((countOfLetters[minKey] - 1) / 2).join();\n countOfLetters.remove(minKey);\n countOfLetters.remove(maxKey);\n }\n\n if (countOfLetters.length == 1) {\n auto temp = countOfLetters.keys().front.to! (char);\n result = result.dup.sort.to! (string) ~\n temp.to! (string).repeat(countOfLetters[temp]).join() ~\n result.dup.sort.reverse.to! (string);\n } else\n result = result.dup.sort.to! (string) ~ result.dup.sort.reverse.to! (string);\n\n writeln(result);\n\n\treturn 0;\n}\n"}], "src_uid": "f996224809df700265d940b12622016f"} {"nl": {"description": "A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner.", "input_spec": "The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters.", "output_spec": "Print the sought number of ways to cut string t in two so that each part made s happy. ", "sample_inputs": ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"], "sample_outputs": ["2", "0"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n string s, t;\n s = readln.chomp;\n t = readln.chomp;\n \n int GetPos(string s, string t) {\n int idx = 0;\n foreach (i, e; t) {\n if (e == s[idx]) { ++idx; }\n \n if (idx == s.length) { return i.to!int; }\n }\n \n return t.length.to!int;\n }\n \n int le = GetPos(s, t);\n int r = t.length.to!int - 1 - GetPos(s.dup.retro.to!string, t.dup.retro.to!string);\n \n debug { writeln(le, ' ', r); }\n \n int ans = max(0, r - le);\n \n ans.writeln;\n}"}], "negative_code": [], "src_uid": "724fa4b1d35b8765048857fa8f2f6802"} {"nl": {"description": "Bob is playing a game named \"Walk on Matrix\".In this game, player is given an $$$n \\times m$$$ matrix $$$A=(a_{i,j})$$$, i.e. the element in the $$$i$$$-th row in the $$$j$$$-th column is $$$a_{i,j}$$$. Initially, player is located at position $$$(1,1)$$$ with score $$$a_{1,1}$$$. To reach the goal, position $$$(n,m)$$$, player can move right or down, i.e. move from $$$(x,y)$$$ to $$$(x,y+1)$$$ or $$$(x+1,y)$$$, as long as player is still on the matrix.However, each move changes player's score to the bitwise AND of the current score and the value at the position he moves to.Bob can't wait to find out the maximum score he can get using the tool he recently learnt  — dynamic programming. Here is his algorithm for this problem. However, he suddenly realize that the algorithm above fails to output the maximum score for some matrix $$$A$$$. Thus, for any given non-negative integer $$$k$$$, he wants to find out an $$$n \\times m$$$ matrix $$$A=(a_{i,j})$$$ such that $$$1 \\le n,m \\le 500$$$ (as Bob hates large matrix); $$$0 \\le a_{i,j} \\le 3 \\cdot 10^5$$$ for all $$$1 \\le i\\le n,1 \\le j\\le m$$$ (as Bob hates large numbers); the difference between the maximum score he can get and the output of his algorithm is exactly $$$k$$$. It can be shown that for any given integer $$$k$$$ such that $$$0 \\le k \\le 10^5$$$, there exists a matrix satisfying the above constraints.Please help him with it!", "input_spec": "The only line of the input contains one single integer $$$k$$$ ($$$0 \\le k \\le 10^5$$$).", "output_spec": "Output two integers $$$n$$$, $$$m$$$ ($$$1 \\le n,m \\le 500$$$) in the first line, representing the size of the matrix. Then output $$$n$$$ lines with $$$m$$$ integers in each line, $$$a_{i,j}$$$ in the $$$(i+1)$$$-th row, $$$j$$$-th column.", "sample_inputs": ["0", "1"], "sample_outputs": ["1 1\n300000", "3 4\n7 3 3 1\n4 8 3 6\n7 7 7 3"], "notes": "NoteIn the first example, the maximum score Bob can achieve is $$$300000$$$, while the output of his algorithm is $$$300000$$$.In the second example, the maximum score Bob can achieve is $$$7\\&3\\&3\\&3\\&7\\&3=3$$$, while the output of his algorithm is $$$2$$$."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto K = readln.chomp.to!int;\n writeln(\"3 3\");\n auto IK = 0b100000000000000000 + K;\n auto IO = 0b100000000000000000;\n auto OK = K;\n auto ans =\n [[IK, IO, 0],\n [OK, IK, OK],\n [0, 0, OK]];\n foreach (a; ans) {\n foreach (b; a) write(b, \" \");\n writeln;\n }\n}\n"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.ascii, std.typecons;\n\nvoid main()\n{ \n int k;\n readf(\"%s\", &k);\n readln;\n \n auto ans = new int[][] (3, 2);\n \n immutable int bigBit = 2 ^^ 17;\n \n ans[0][0] = bigBit + k;\n ans[1][0] = bigBit;\n ans[0][1] = k;\n \n ans[1][1] = bigBit + k;\n \n ans[2][0] = 0;\n ans[2][1] = k;\n \n writeln(3, ' ', 2);\n foreach (i; 0 .. 3) {\n ans[i].map!(to!string).join(\" \").writeln;\n }\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nvoid main()\n{\n\tauto k = RD!int;\n\tint digit;\n\tauto x = k;\n\twhile (x != 0)\n\t{\n\t\tx >>= 1;\n\t\t++digit;\n\t}\n\n\tint y = 1 << digit;\n\tint z = k | y;\n\n\tauto ans = new int[][](2, 3);\n\tans[0][0] = z;\n\tans[1][0] = y;\n\tans[0][1] = k;\n\tans[1][1] = z;\n\tans[0][2] = 0;\n\tans[1][2] = k;\n\t\n\twriteln(\"2 3\");\n\tforeach (i; 0..2)\n\t{\n\t\tans[i].map!(to!string).join(\" \").writeln;\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto K = readln.chomp.to!int;\n auto ans =\n [[0b1000000000000000000, 0b0111111111111111111, 0b0111111111111111111],\n [0b1000000000000000000, 0, K],\n [0b1000000000000000000, 0, K]];\n foreach (a; ans) {\n foreach (b; a) write(b, \" \");\n writeln;\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto K = readln.chomp.to!int;\n writeln(\"3 3\");\n auto ans =\n [[0b1000000000000000000, 0b0111111111111111111, 0b0111111111111111111],\n [0b1000000000000000000, 0, K],\n [0b1000000000000000000, 0b1000000000000000000, 0b1000000000000000000+K]];\n foreach (a; ans) {\n foreach (b; a) write(b, \" \");\n writeln;\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto K = readln.chomp.to!int;\n writeln(\"3 3\");\n auto ans =\n [[0b1000000000000000000, 0b0111111111111111111, 0b0111111111111111111],\n [0b1000000000000000000, 0, K],\n [0b1000000000000000000, 0, K]];\n foreach (a; ans) {\n foreach (b; a) write(b, \" \");\n writeln;\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto K = readln.chomp.to!int;\n writeln(\"3 3\");\n auto IK = 0b1000000000000000000 + K;\n auto IO = 0b1000000000000000000;\n auto OK = K;\n auto ans =\n [[IK, IO, IO],\n [OK, IO, IO],\n [OK, OK, IK]];\n foreach (a; ans) {\n foreach (b; a) write(b, \" \");\n writeln;\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto K = readln.chomp.to!int;\n writeln(\"3 3\");\n auto IK = 0b1000000000000000000 + K;\n auto IO = 0b1000000000000000000;\n auto OK = K;\n auto ans =\n [[IK, IO, 0],\n [OK, IO, OK],\n [0, 0, OK]];\n foreach (a; ans) {\n foreach (b; a) write(b, \" \");\n writeln;\n }\n}\n"}], "src_uid": "fc0442e5cda2498a1818702e5e3eeec4"} {"nl": {"description": "There are $$$n$$$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.The $$$i$$$-th student has integer programming skill $$$a_i$$$. All programming skills are distinct and between $$$1$$$ and $$$n$$$, inclusive.Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $$$k$$$ closest students to the left of him and $$$k$$$ closest students to the right of him (if there are less than $$$k$$$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the number of students and the value determining the range of chosen students during each move, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student. It is guaranteed that all programming skills are distinct.", "output_spec": "Print a string of $$$n$$$ characters; $$$i$$$-th character should be 1 if $$$i$$$-th student joins the first team, or 2 otherwise.", "sample_inputs": ["5 2\n2 4 5 3 1", "5 1\n2 1 3 5 4", "7 1\n7 2 1 3 5 4 6", "5 1\n2 4 5 3 1"], "sample_outputs": ["11111", "22111", "1121122", "21112"], "notes": "NoteIn the first example the first coach chooses the student on a position $$$3$$$, and the row becomes empty (all students join the first team).In the second example the first coach chooses the student on position $$$4$$$, and the row becomes $$$[2, 1]$$$ (students with programming skills $$$[3, 4, 5]$$$ join the first team). Then the second coach chooses the student on position $$$1$$$, and the row becomes empty (and students with programming skills $$$[1, 2]$$$ join the second team).In the third example the first coach chooses the student on position $$$1$$$, and the row becomes $$$[1, 3, 5, 4, 6]$$$ (students with programming skills $$$[2, 7]$$$ join the first team). Then the second coach chooses the student on position $$$5$$$, and the row becomes $$$[1, 3, 5]$$$ (students with programming skills $$$[4, 6]$$$ join the second team). Then the first coach chooses the student on position $$$3$$$, and the row becomes $$$[1]$$$ (students with programming skills $$$[3, 5]$$$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $$$1$$$ joins the second team).In the fourth example the first coach chooses the student on position $$$3$$$, and the row becomes $$$[2, 1]$$$ (students with programming skills $$$[3, 4, 5]$$$ join the first team). Then the second coach chooses the student on position $$$1$$$, and the row becomes empty (and students with programming skills $$$[1, 2]$$$ join the second team)."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1];\n auto A = readln.split.map!(to!int).array;\n\n auto B = N.iota.map!(i => tuple(i, A[i])).array;\n B.sort!\"a[1] > b[1]\";\n\n auto L = N.iota.map!(i => i.to!int - 1).array;\n auto R = N.iota.map!(i => i.to!int + 1).array;\n\n auto ans = new int[](N);\n auto used = new bool[](N);\n\n for (int p = 0, t = 0; p < N; t ^= 1) {\n auto start = B[p][0];\n ans[start] = t + 1;\n used[start] = true;\n\n int new_l = -1, new_r = N;\n\n for (int j = 0, i = L[start]; j < K && i != -1; ++j, i = L[i], new_l = i) {\n ans[i] = t + 1;\n used[i] = true;\n } \n for (int j = 0, i = R[start]; j < K && i != N; ++j, i = R[i], new_r = i) {\n ans[i] = t + 1;\n used[i] = true;\n }\n\n if (new_l != -1) {\n R[new_l] = new_r;\n }\n if (new_r != N) {\n L[new_r] = new_l;\n }\n\n while (p < N && used[B[p][0]]) ++p;\n }\n\n ans.map!(a => a.to!string).join(\"\").writeln;\n}"}, {"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, k;\n readf(\"%s %s\", &n, &k);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n auto place = new int[] (n+1);\n foreach (i, e; arr) { place[e] = i.to!int; }\n \n auto adjr = n.iota.map!(x => x+1).array;\n auto adjle = n.iota.map!(x => x-1).array;\n \n auto ans = new int[] (n);\n \n int now = 1;\n foreach_reverse (e; (n+1).iota.dropOne) {\n int idx = place[e];\n \n if (ans[idx] != 0) { continue; }\n \n debug { e.writeln; }\n \n ans[idx] = now;\n int idxr = adjr[idx], step = 1;\n while (step <= k && idxr < n) {\n ans[idxr] = now;\n idxr = adjr[idxr];\n ++step;\n }\n int idxle = adjle[idx];\n step = 1;\n while (step <= k && idxle >= 0) {\n ans[idxle] = now;\n idxle = adjle[idxle];\n ++step;\n }\n \n if (idxle >= 0) { adjr[idxle] = idxr; }\n if (idxr < n) { adjle[idxr] = idxle; }\n \n now = 3 - now;\n }\n \n ans.writefln!\"%(%s%)\";\n}"}, {"source_code": "import std.stdio, std.conv, std.string;\nimport std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n\nint DEBUG_LEVEL = 0;\nvoid print()(){ writeln(\"\"); }\nvoid print(T, A ...)(T t, lazy A a){ write(t), print(a); }\nvoid print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }\n\nconst long mod = 1_000_000_007;\n\nvoid main(string[] args){\n\tif(args.length > 1 && args[1] == \"-debug\"){\n\t\tif(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;\n\t\telse DEBUG_LEVEL = 1;\n\t}\n\t\n\tint n = read.to!int;\n\tint k = read.to!int;\n\t\n\tNode[] nodes;\n\tforeach(i; 0 .. n){\n\t\tint a = read.to!int;\n\t\tnodes ~= new Node(i, a);\n\t}\n\t\n\tforeach(i; 0 .. n - 1){\n\t\tnodes[i].next = nodes[i + 1];\n\t\tnodes[i + 1].prev = nodes[i];\n\t}\n\t\n\tnodes.sort!\"a.value>b.value\"();\n\t\n\tint t = 1;\n\tforeach(nd; nodes){\n\t\tif(nd.isDone) continue;\n\t\telse{\n\t\t\tnd.setTeam(t);\n\t\t\tNode prev = nd.takePrev(t, k);\n\t\t\tNode next = nd.takeNext(t, k);\n\t\t\tif(prev) prev.next = next;\n\t\t\tif(next) next.prev = prev;\n\t\t}\n\t\tt = 3 - t;\n\t}\n\t\n\tnodes.sort!\"a.id nd.team.to!string).array.join(\"\").writeln;\n}\n\nclass Node{\n\tint id;\n\tint value;\n\tint team;\n\tbool isDone = 0;\n\tNode prev, next;\n\tthis(int id, int value){\n\t\tthis.id = id;\n\t\tthis.value = value;\n\t}\n\tvoid setTeam(int t){\n\t\tthis.isDone = 1;\n\t\tthis.team = t;\n\t}\n\tNode takePrev(int t, int k){\n\t\tthis.setTeam(t);\n\t\tif(k == 0) return this.prev;\n\t\telse if(this.prev) return this.prev.takePrev(t, k - 1);\n\t\telse return null;\n\t}\n\tNode takeNext(int t, int k){\n\t\tthis.setTeam(t);\n\t\tif(k == 0) return this.next;\n\t\telse if(this.next) return this.next.takeNext(t, k - 1);\n\t\telse return null;\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto K = s[1];\n auto A = readln.split.map!(to!int).array;\n\n auto B = N.iota.map!(i => tuple(i, A[i])).array;\n B.sort!\"a[1] > b[1]\";\n\n auto L = N.iota.map!(i => i.to!int - 1).array;\n auto R = N.iota.map!(i => i.to!int + 1).array;\n\n auto ans = new int[](N);\n auto used = new bool[](N);\n\n for (int p = 0, t = 0; p < N; t ^= 1) {\n auto start = B[p][0];\n ans[start] = t + 1;\n used[start] = true;\n\n int new_l, new_r;\n\n for (int j = 0, i = L[start]; j < K && i != -1; ++j, i = L[i], new_l = i) {\n ans[i] = t + 1;\n used[i] = true;\n } \n for (int j = 0, i = R[start]; j < K && i != N; ++j, i = R[i], new_r = i) {\n ans[i] = t + 1;\n used[i] = true;\n }\n\n if (new_l != -1) {\n R[new_l] = new_r;\n }\n if (new_r != N) {\n L[new_r] = new_l;\n }\n\n while (p < N && used[B[p][0]]) ++p;\n }\n\n ans.map!(a => a.to!string).join(\"\").writeln;\n}"}, {"source_code": "import std.stdio, std.conv, std.string;\nimport std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nstring read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\n\nint DEBUG_LEVEL = 0;\nvoid print()(){ writeln(\"\"); }\nvoid print(T, A ...)(T t, lazy A a){ write(t), print(a); }\nvoid print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }\n\nconst long mod = 1_000_000_007;\n\nvoid main(string[] args){\n\tif(args.length > 1 && args[1] == \"-debug\"){\n\t\tif(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;\n\t\telse DEBUG_LEVEL = 1;\n\t}\n\t\n\tint n = read.to!int;\n\tint k = read.to!int;\n\t\n\tNode[] nodes;\n\tforeach(i; 0 .. n){\n\t\tint a = read.to!int;\n\t\tnodes ~= new Node(i, a);\n\t}\n\t\n\tforeach(i; 0 .. n - 1){\n\t\tnodes[i].next = nodes[i + 1];\n\t\tnodes[i + 1].prev = nodes[i];\n\t}\n\t\n\tnodes.sort!\"a.value>b.value\"();\n\t\n\tint t = 1;\n\tforeach(nd; nodes){\n\t\tif(nd.isDone) continue;\n\t\telse{\n\t\t\tnd.setTeam(t);\n\t\t\tNode prev = nd.takePrev(t, k);\n\t\t\tNode next = nd.takeNext(t, k);\n\t\t\tif(prev) prev.next = next;\n\t\t\tif(next) next.prev = prev;\n\t\t}\n\t\tt = 3 - t;\n\t}\n\t\n\tnodes.sort!\"a.id nd.team.to!string).array.join(\" \").writeln;\n}\n\nclass Node{\n\tint id;\n\tint value;\n\tint team;\n\tbool isDone = 0;\n\tNode prev, next;\n\tthis(int id, int value){\n\t\tthis.id = id;\n\t\tthis.value = value;\n\t}\n\tvoid setTeam(int t){\n\t\tthis.isDone = 1;\n\t\tthis.team = t;\n\t}\n\tNode takePrev(int t, int k){\n\t\tthis.setTeam(t);\n\t\tif(k == 0) return this.prev;\n\t\telse if(this.prev) return this.prev.takePrev(t, k - 1);\n\t\telse return null;\n\t}\n\tNode takeNext(int t, int k){\n\t\tthis.setTeam(t);\n\t\tif(k == 0) return this.next;\n\t\telse if(this.next) return this.next.takeNext(t, k - 1);\n\t\telse return null;\n\t}\n}\n"}], "src_uid": "235131226fee9d04efef4673185c1c9b"} {"nl": {"description": "You like playing chess tournaments online.In your last tournament you played $$$n$$$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $$$0$$$ points. When you win you get $$$1$$$ or $$$2$$$ points: if you have won also the previous game you get $$$2$$$ points, otherwise you get $$$1$$$ point. If you win the very first game of the tournament you get $$$1$$$ point (since there is not a \"previous game\").The outcomes of the $$$n$$$ games are represented by a string $$$s$$$ of length $$$n$$$: the $$$i$$$-th character of $$$s$$$ is W if you have won the $$$i$$$-th game, while it is L if you have lost the $$$i$$$-th game.After the tournament, you notice a bug on the website that allows you to change the outcome of at most $$$k$$$ of your games (meaning that at most $$$k$$$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug.Compute the maximum score you can get by cheating in the optimal way.", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\le t \\le 20,000$$$) — the number of test cases. The description of the test cases follows. The first line of each testcase contains two integers $$$n, k$$$ ($$$1\\le n\\le 100,000$$$, $$$0\\le k\\le n$$$) – the number of games played and the number of outcomes that you can change. The second line contains a string $$$s$$$ of length $$$n$$$ containing only the characters W and L. If you have won the $$$i$$$-th game then $$$s_i=\\,$$$W, if you have lost the $$$i$$$-th game then $$$s_i=\\,$$$L. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$200,000$$$.", "output_spec": "For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way.", "sample_inputs": ["8\n5 2\nWLWLL\n6 5\nLLLWWL\n7 1\nLWLWLWL\n15 5\nWWWLLLWWWLLLWWW\n40 7\nLLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL\n1 0\nL\n1 1\nL\n6 1\nWLLWLW"], "sample_outputs": ["7\n11\n6\n26\n46\n0\n1\n6"], "notes": "NoteExplanation of the first testcase. Before changing any outcome, the score is $$$2$$$. Indeed, you won the first game, so you got $$$1$$$ point, and you won also the third, so you got another $$$1$$$ point (and not $$$2$$$ because you lost the second game).An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $$$7=1+2+2+2$$$: $$$1$$$ point for the first game and $$$2$$$ points for the second, third and fourth game.Explanation of the second testcase. Before changing any outcome, the score is $$$3$$$. Indeed, you won the fourth game, so you got $$$1$$$ point, and you won also the fifth game, so you got $$$2$$$ more points (since you won also the previous game).An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $$$11 = 1+2+2+2+2+2$$$: $$$1$$$ point for the first game and $$$2$$$ points for all the other games."}, "positive_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid play(){\n int n, k;\n n = rd!int, k = rd!int;\n dchar[] arr = rd!(dchar[]);\n auto g1 = redBlackTree!(true, int);\n auto g2 = redBlackTree!(true, int);\n int streak = 0, take = 0, w = 0;\n if(arr[0] == 'L') --streak;\n foreach(i; 0..n){\n if(arr[i] == 'W'){\n if(streak > 0){\n ++streak;\n if(streak % 2){\n g1.insert(streak);\n }else{\n g2.insert(streak);\n }\n }\n streak = 0;\n ++w;\n if(i > 0 && arr[i-1] == 'W') ++w;\n }else{\n streak += 2;\n ++take;\n }\n }\n if(!w && streak > 0 && k > 0){\n streak -= 1;\n w = 1;\n k -= 1;\n }\n if(streak > 0){\n if(streak % 2){\n g1.insert(streak);\n }else{\n g2.insert(streak);\n }\n }\n /* writeln(g1, g2); */\n\n ll pt = w;\n take = min(take, k);\n while(g1.length && take > 0){\n ll top = g1.front;\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n g1.removeFront;\n }\n while(g2.length && take > 0){\n ll top = g2.front;\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n g2.removeFront;\n }\n writeln(pt);\n}\n\nint main(){\n long t = 1;\n t = rd; // Toggle!\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nalias ll = long;\nalias rbt = RedBlackTree;\nalias sr = SortedRange;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\treadln;\n\t\tauto s = readln.strip.map !(q{a == 'W'}).array;\n\t\tauto zeroes = s.count (0);\n\t\tauto ones = s.count (1);\n\t\tif (ones == 0)\n\t\t{\n\t\t\twriteln (max (0, k * 2 - 1));\n\t\t\tcontinue;\n\t\t}\n\t\tint start0 = 0;\n\t\twhile (s.front == 0)\n\t\t{\n\t\t\tstart0 += 1;\n\t\t\ts.popFront ();\n\t\t}\n\t\tint finish0 = 0;\n\t\twhile (s.back == 0)\n\t\t{\n\t\t\tfinish0 += 1;\n\t\t\ts.popBack ();\n\t\t}\n\n\t\tauto h = s.group.filter !(q{a[0] == 1}).count.to !(int);\n\t\tauto res = sum (s) * 2 - h;\n\t\tauto g = s.group.filter !(q{a[0] == 0}).map !(q{a[1]}).array;\n\t\tg.sort;\n\t\twhile (k > 0 && !g.empty)\n\t\t{\n\t\t\tauto d = min (k, g.front);\n\t\t\tk -= d;\n\t\t\tres += 2 * d + (d == g.front);\n\t\t\tg.popFront ();\n\t\t}\n\t\tif (k > 0 && finish0 > 0)\n\t\t{\n\t\t\tauto d = min (k, finish0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\tif (k > 0 && start0 > 0)\n\t\t{\n\t\t\tauto d = min (k, start0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n const K = readInt();\n const S = readToken();\n \n int ans;\n int k = K;\n chmin(k, cast(int)(S.count('L')));\n if (S.count('W') == 0) {\n ans = max(2 * k - 1, 0);\n } else {\n foreach (i; 0 .. N) {\n if (S[i] == 'W') {\n ans += (i - 1 >= 0 && S[i - 1] == 'W') ? 2 : 1;\n }\n }\n int[] ds;\n for (int i = 0, j; i < N; i = j) {\n for (j = i; j < N && S[i] == S[j]; ++j) {}\n if (S[i] == 'L' && !(i == 0 || j == N)) {\n ds ~= j - i;\n }\n }\n ds.sort;\n foreach (d; ds) {\n if (k >= d) {\n k -= d;\n ans += 2 * d + 1;\n }\n }\n ans += 2 * k;\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto s = RD!string;\n\n\t\tauto cnt = new int[](n+1);\n\t\tint streak, lc;\n\t\tbool start;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (s[i] == 'W')\n\t\t\t{\n\t\t\t\tstart = true;\n\t\t\t\t++cnt[streak];\n\t\t\t\tstreak = 0;\n\t\t\t\tif (i == 0)\n\t\t\t\t\t++ans[ti];\n\t\t\t\telse if (s[i-1] == 'W')\n\t\t\t\t\tans[ti] += 2;\n\t\t\t\telse\n\t\t\t\t\t++ans[ti];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (start)\n\t\t\t\t\t++streak;\n\t\t\t\t++lc;\n\t\t\t}\n\t\t}\n\t\tdebug writeln(\"ans:\", ans[ti]);\n\t\tdebug writeln(cnt);\n\t\tk = min(k, lc);\n\n\t\tforeach (i; 1..n+1)\n\t\t{\n\t\t\tauto c = min(k / i, cnt[i]);\n\t\t\tk -= i * c;\n\t\t\tif (c != 0)\n\t\t\t\tans[ti] += c * (i * 2 + 1);\n\t\t\tdebug writeln(\"k:\", k, \" ans:\", ans[ti]);\n\t\t}\n\t\tif (start)\n\t\t\tans[ti] += k * 2;\n\t\telse if (k >= 1)\n\t\t\tans[ti] += (k-1) * 2 + 1;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid play(){\n int n, k;\n n = rd!int, k = rd!int;\n dchar[] arr = rd!(dchar[]);\n auto gaps = redBlackTree!(true, tup);\n int streak = 0, take = 0, w = 0;\n if(arr[0] == 'L') --streak;\n foreach(i; 0..n){\n if(arr[i] == 'W'){\n ll val = streak + 1;\n if(streak > 0){ gaps.insert(tup(val/2, -val)); }\n streak = 0;\n ++w;\n if(i > 0 && arr[i-1] == 'W') ++w;\n }else{\n streak += 2;\n ++take;\n }\n }\n if(streak) gaps.insert(tup(streak/2, -streak));\n /* writeln(gaps); */\n\n ll pt = w;\n take = min(take, k);\n while(gaps.length && take > 0){\n ll top = - gaps.front[1];\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n gaps.removeFront;\n }\n writeln(pt);\n}\n\nint main(){\n long t = 1;\n t = rd; // Toggle!\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nalias ll = long;\nalias rbt = RedBlackTree;\nalias sr = SortedRange;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid play(){\n int n, k;\n n = rd!int, k = rd!int;\n dchar[] arr = rd!(dchar[]);\n auto g1 = redBlackTree!(true, int);\n auto g2 = redBlackTree!(true, int);\n int streak = 0, take = 0, w = 0;\n if(arr[0] == 'L') --streak;\n foreach(i; 0..n){\n if(arr[i] == 'W'){\n if(streak > 0){\n ++streak;\n if(streak % 2){\n g1.insert(streak);\n }else{\n g2.insert(streak);\n }\n }\n streak = 0;\n ++w;\n if(i > 0 && arr[i-1] == 'W') ++w;\n }else{\n streak += 2;\n ++take;\n }\n }\n if(!w && streak > 0 && k > 0){\n streak -= 1;\n w = 1;\n k -= 1;\n }\n if(streak > 0){\n if(streak % 2){\n g1.insert(streak);\n }else{\n g2.insert(streak);\n }\n }\n writeln(g1, g2);\n\n ll pt = w;\n take = min(take, k);\n while(g1.length && take > 0){\n ll top = g1.front;\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n g1.removeFront;\n }\n while(g2.length && take > 0){\n ll top = g2.front;\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n g2.removeFront;\n }\n writeln(pt);\n}\n\nint main(){\n long t = 1;\n t = rd; // Toggle!\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nalias ll = long;\nalias rbt = RedBlackTree;\nalias sr = SortedRange;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid play(){\n int n, k;\n n = rd!int, k = rd!int;\n dchar[] arr = rd!(dchar[]);\n auto g1 = redBlackTree!(true, int);\n auto g2 = redBlackTree!(true, int);\n int streak = 0, take = 0, w = 0;\n if(arr[0] == 'L') --streak;\n foreach(i; 0..n){\n if(arr[i] == 'W'){\n if(streak > 0){\n ++streak;\n if(streak % 2){\n g1.insert(streak);\n }else{\n g2.insert(streak);\n }\n }\n streak = 0;\n ++w;\n if(i > 0 && arr[i-1] == 'W') ++w;\n }else{\n streak += 2;\n ++take;\n }\n }\n if(streak > 0){\n if(streak % 2){\n g1.insert(streak);\n }else{\n g2.insert(streak);\n }\n }\n /* writeln(g1, g2); */\n\n ll pt = w;\n take = min(take, k);\n while(g1.length && take > 0){\n ll top = g1.front;\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n g1.removeFront;\n }\n while(g2.length && take > 0){\n ll top = g2.front;\n if(top/2 <= take){\n pt += top;\n take -= top/2;\n }else{\n pt += take*2;\n take = 0;\n }\n g2.removeFront;\n }\n writeln(pt);\n}\n\nint main(){\n long t = 1;\n t = rd; // Toggle!\n while(t--) play(); // Let's play!\n stdout.flush;\n return 0;\n}\n\n/**********It's A Me Mario!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nalias ll = long;\nalias rbt = RedBlackTree;\nalias sr = SortedRange;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nvoid show(A...)(A a) { debug{ foreach(t; a){ write(t, \"| \"); } writeln; } }\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\treadln;\n\t\tauto s = readln.strip.map !(q{a == 'W'}).array;\n\t\tauto zeroes = s.count (0);\n\t\tauto ones = s.count (1);\n\t\tif (ones == 0)\n\t\t{\n\t\t\twriteln (max (0, k * 2 - 1));\n\t\t\tcontinue;\n\t\t}\n\t\tint start0 = 0;\n\t\twhile (s.front == 0)\n\t\t{\n\t\t\tstart0 += 1;\n\t\t\ts.popFront ();\n\t\t}\n\t\tint finish0 = 0;\n\t\twhile (s.back == 0)\n\t\t{\n\t\t\tfinish0 += 1;\n\t\t\ts.popBack ();\n\t\t}\n\n\t\tauto h = s.group.filter !(q{a[0] == 1}).count.to !(int);\n\t\tauto res = sum (s) * 2 - h;\n\t\tauto g = s.group.filter !(q{a[0] == 0}).map !(q{a[1]}).array;\n\t\tg.sort;\n\t\twhile (k > 0 && !g.empty)\n\t\t{\n\t\t\tauto d = min (k, g.front);\n\t\t\tk -= d;\n\t\t\tres += 2 * d + (d == g.front);\n\t\t\tg.popFront ();\n\t\t}\n\t\tif (k > 0 && finish0 > 0)\n\t\t{\n\t\t\tauto d = min (k, finish0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\tif (k > 0 && start0 > 0)\n\t\t{\n\t\t\tauto d = min (k, start0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d - 1 + (d == start0);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\treadln;\n\t\tauto s = readln.strip.map !(q{a == 'W'}).array;\n\t\tauto zeroes = s.count (0);\n\t\tauto ones = s.count (1);\n\t\tif (ones == 0)\n\t\t{\n\t\t\twriteln (max (0, k * 2 - 1));\n\t\t\tcontinue;\n\t\t}\n\t\tint start0 = 0;\n\t\twhile (s.front == 0)\n\t\t{\n\t\t\tstart0 += 1;\n\t\t\ts.popFront ();\n\t\t}\n\t\tint finish0 = 0;\n\t\twhile (s.back == 0)\n\t\t{\n\t\t\tfinish0 += 1;\n\t\t\ts.popBack ();\n\t\t}\n\n\t\tauto h = s.group.filter !(q{a[0] == 1}).count.to !(int);\n\t\tauto res = sum (s) * 2 - h;\n\t\tauto g = s.group.filter !(q{a[0] == 0}).map !(q{a[1]}).array;\n\t\tg.sort;\n\t\twhile (k > 0 && !g.empty)\n\t\t{\n\t\t\tauto d = min (k, g.front);\n\t\t\tk -= d;\n\t\t\tres += 2 * d + (d == g.front);\n\t\t\tg.popFront ();\n\t\t}\n\t\tif (k > 0 && finish0 > 0)\n\t\t{\n\t\t\tauto d = min (k, finish0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\tif (k > 0 && start0 > 0)\n\t\t{\n\t\t\tauto d = min (k, start0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d - (d != start0);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\treadln;\n\t\tauto s = readln.strip.map !(q{a == 'W'}).array;\n\t\tauto zeroes = s.count (0);\n\t\tauto ones = s.count (1);\n\t\tif (ones == 0)\n\t\t{\n\t\t\twriteln (max (0, k * 2 - 1));\n\t\t\tcontinue;\n\t\t}\n\t\tint start0 = 0;\n\t\twhile (s.front == 0)\n\t\t{\n\t\t\tstart0 += 1;\n\t\t\ts.popFront ();\n\t\t}\n\t\tint finish0 = 0;\n\t\twhile (s.back == 0)\n\t\t{\n\t\t\tfinish0 += 1;\n\t\t\ts.popBack ();\n\t\t}\n\n\t\tauto h = s.group.filter !(q{a[0] == 1}).count.to !(int);\n\t\tauto res = sum (s) * 2 - h;\n\t\tauto g = s.group.filter !(q{a[0] == 0}).map !(q{a[1]}).array;\n\t\tg.sort;\n\t\twhile (k > 0 && !g.empty)\n\t\t{\n\t\t\tauto d = min (k, g.front);\n\t\t\tk -= d;\n\t\t\tres += 2 * d + (d == g.front);\n\t\t\tg.popFront ();\n\t\t}\n\t\tif (k > 0 && finish0 > 0)\n\t\t{\n\t\t\tauto d = min (k, finish0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\tif (k > 0 && start0 > 0)\n\t\t{\n\t\t\tauto d = min (k, start0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d - (d == start0);\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tint n, k;\n\t\treadf !(\" %s %s\") (n, k);\n\t\treadln;\n\t\tauto s = readln.strip.map !(q{a == 'W'}).array;\n\t\twriteln (s);\n\t\tauto zeroes = s.count (0);\n\t\tauto ones = s.count (1);\n\t\tif (ones == 0)\n\t\t{\n\t\t\twriteln (max (0, k * 2 - 1));\n\t\t\tcontinue;\n\t\t}\n\t\tint start0 = 0;\n\t\twhile (s.front == 0)\n\t\t{\n\t\t\tstart0 += 1;\n\t\t\ts.popFront ();\n\t\t}\n\t\tint finish0 = 0;\n\t\twhile (s.back == 0)\n\t\t{\n\t\t\tfinish0 += 1;\n\t\t\ts.popBack ();\n\t\t}\n\n\t\tauto h = s.group.filter !(q{a[0] == 1}).count.to !(int);\n\t\tauto res = sum (s) * 2 - h;\n\t\tauto g = s.group.filter !(q{a[0] == 0}).map !(q{a[1]}).array;\n\t\tg.sort;\n\t\twhile (k > 0 && !g.empty)\n\t\t{\n\t\t\tauto d = min (k, g.front);\n\t\t\tk -= d;\n\t\t\tres += 2 * d + (d == g.front);\n\t\t\tg.popFront ();\n\t\t}\n\t\tif (k > 0 && finish0 > 0)\n\t\t{\n\t\t\tauto d = min (k, finish0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\tif (k > 0 && start0 > 0)\n\t\t{\n\t\t\tauto d = min (k, start0);\n\t\t\tk -= d;\n\t\t\tres += 2 * d;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "src_uid": "c4c3c07b2ba6df49d5a7d6d2d0d1895f"} {"nl": {"description": "Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her. Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parenti such that parenti < i.There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c as well.An object is considered to be neither a part of itself nor a special case of itself.Now, Harry has to answer two type of queries: 1 u v: he needs to tell if object v is a special case of object u. 2 u v: he needs to tell if object v is a part of object u. ", "input_spec": "First line of input contains the number n (1 ≤ n ≤ 105), the number of objects. Next n lines contain two integer parenti and typei ( - 1 ≤ parenti < i parenti ≠ 0,  - 1 ≤ typei ≤ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. If typei = 1, this implies that the object i is a part of object parenti). In case the i-th object has no parent, both parenti and typei are -1. Next line contains an integer q (1 ≤ q ≤ 105), the number of queries. Next q lines each represent a query having three space separated integers typei, ui, vi (1 ≤ typei ≤ 2, 1 ≤ u, v ≤ n).", "output_spec": "Output will contain q lines, each containing the answer for the corresponding query as \"YES\" (affirmative) or \"NO\" (without quotes). You can output each letter in any case (upper or lower).", "sample_inputs": ["3\n-1 -1\n1 0\n2 0\n2\n1 1 3\n2 1 3", "3\n-1 -1\n1 0\n1 1\n2\n2 2 3\n2 3 2"], "sample_outputs": ["YES\nNO", "YES\nNO"], "notes": "NoteIn test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint N;\nint[][] P;\n\nint[][][] graph;\nint zeit;\nint[][] rs, dis, fin;\n\nvoid dfs(int s, int r, int u) {\n rs[s][u] = r;\n dis[s][u] = zeit++;\n foreach (v; graph[s][u]) {\n dfs(s, r, v);\n }\n fin[s][u] = zeit++;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n N = readInt();\n P = new int[][](2, N);\n foreach (u; 0 .. N) {\n const p = readInt() - 1;\n const t = readInt();\n P[0][u] = P[1][u] = -1;\n if (t != -1) {\n P[t][u] = p;\n }\n }\n \n graph = new int[][][](2, N);\n foreach (s; 0 .. 2) {\n foreach (u; 0 .. N) {\n if (P[s][u] != -1) {\n graph[s][P[s][u]] ~= u;\n }\n }\n }\n \n rs = new int[][](2, N);\n dis = new int[][](2, N);\n fin = new int[][](2, N);\n foreach (s; 0 .. 2) {\n rs[s][] = -1;\n zeit = 0;\n foreach (u; 0 .. N) {\n if (rs[s][u] == -1) {\n dfs(s, u, u);\n }\n }\n }\n debug {\n writeln(\"rs = \", rs);\n writeln(\"dis = \", dis);\n writeln(\"fin = \", fin);\n }\n \n const Q = readInt();\n foreach (q; 0 .. Q) {\n const t = readInt();\n const u = readInt() - 1;\n const v = readInt() - 1;\n bool ans;\n if (t == 1) {\n ans = (dis[0][u] < dis[0][v] && fin[0][v] < fin[0][u]);\n } else {\n foreach (r; [rs[1][v], rs[0][u]]) {\n ans = ans || (dis[1][r] < dis[1][v] && fin[1][v] < fin[1][r] && dis[0][r] <= dis[0][u] && fin[0][u] <= fin[0][r]);\n }\n }\n writeln(ans ? \"YES\" : \"NO\");\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "6fcaecc5530fe44ed4517c128e3ddd98"} {"nl": {"description": "You are given an undirected weighted connected graph with $$$n$$$ vertices and $$$m$$$ edges without loops and multiple edges.The $$$i$$$-th edge is $$$e_i = (u_i, v_i, w_i)$$$; the distance between vertices $$$u_i$$$ and $$$v_i$$$ along the edge $$$e_i$$$ is $$$w_i$$$ ($$$1 \\le w_i$$$). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by $$$1$$$. You can increase the weight of each edge multiple (possibly, zero) times.Suppose that the initial MST cost is $$$k$$$. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains $$$k$$$, but MST is unique (it means that there is only one way to choose MST in the obtained graph).Your problem is to calculate the minimum number of operations required to do it.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5, n - 1 \\le m \\le 2 \\cdot 10^5$$$) — the number of vertices and the number of edges in the initial graph. The next $$$m$$$ lines contain three integers each. The $$$i$$$-th line contains the description of the $$$i$$$-th edge $$$e_i$$$. It is denoted by three integers $$$u_i, v_i$$$ and $$$w_i$$$ ($$$1 \\le u_i, v_i \\le n, u_i \\ne v_i, 1 \\le w \\le 10^9$$$), where $$$u_i$$$ and $$$v_i$$$ are vertices connected by the $$$i$$$-th edge and $$$w_i$$$ is the weight of this edge. It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each $$$i$$$ from $$$1$$$ to $$$m$$$ $$$u_i \\ne v_i$$$ and for each unordered pair of vertices $$$(u, v)$$$ there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.", "output_spec": "Print one integer — the minimum number of operations to unify MST of the initial graph without changing the cost of MST.", "sample_inputs": ["8 10\n1 2 1\n2 3 2\n2 4 5\n1 4 2\n6 3 3\n6 1 3\n3 5 2\n3 7 1\n4 8 1\n6 2 4", "4 3\n2 1 3\n4 3 4\n2 4 1", "3 3\n1 2 1\n2 3 2\n1 3 3", "3 3\n1 2 1\n2 3 3\n1 3 3", "1 0", "5 6\n1 2 2\n2 3 1\n4 5 3\n2 4 2\n1 4 2\n1 5 3"], "sample_outputs": ["1", "0", "0", "1", "0", "2"], "notes": "NoteThe picture corresponding to the first example: You can, for example, increase weight of the edge $$$(1, 6)$$$ or $$$(6, 3)$$$ by $$$1$$$ to unify MST.The picture corresponding to the last example: You can, for example, increase weights of edges $$$(1, 5)$$$ and $$$(2, 4)$$$ by $$$1$$$ to unify MST."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.string;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto M = s[1];\n auto pq = new BinaryHeap!(Array!(Tuple!(int, int, long)), \"a[2] > b[2]\");\n foreach (i; 0..M) {\n s = readln.split.map!(to!int);\n pq.insert(tuple(s[0]-1, s[1]-1, s[2].to!long));\n }\n auto uf = new UnionFind(N);\n int cnt = 0;\n long ans = 0;\n long last_w = -1;\n\n while (cnt < N - 1) {\n Tuple!(int, int, long)[] E1;\n Tuple!(int, int, long)[] E2;\n long w = pq.front[2];\n while (!pq.empty && pq.front[2] == w) {\n E1 ~= pq.front;\n pq.removeFront;\n }\n foreach (e; E1) {\n int u = e[0];\n int v = e[1];\n if (uf.find(u) == uf.find(v)) {\n\n } else {\n E2 ~= e;\n }\n }\n foreach (e; E2) {\n int u = e[0];\n int v = e[1];\n if (uf.find(u) == uf.find(v)) {\n ans += 1;\n } else {\n cnt += 1;\n uf.unite(u, v);\n }\n }\n }\n\n ans.writeln;\n}\n\n\nclass UnionFind {\n int N;\n int[] table;\n\n this(int n) {\n N = n;\n table = new int[](N);\n fill(table, -1);\n }\n\n int find(int x) {\n return table[x] < 0 ? x : (table[x] = find(table[x]));\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (table[x] > table[y]) swap(x, y);\n table[x] += table[y];\n table[y] = x;\n }\n}\n"}], "negative_code": [], "src_uid": "92afa6f770493109facb1b9ca8d94e0c"} {"nl": {"description": "Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers — coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations.When Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly.", "input_spec": "Each of the eight lines contains three space-separated integers — the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 106 in their absolute value.", "output_spec": "If there is a way to restore the cube, then print in the first line \"YES\". In each of the next eight lines print three integers — the restored coordinates of the points. The numbers in the i-th output line must be a permutation of the numbers in i-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them. If there is no valid way, print \"NO\" (without the quotes) in the first line. Do not print anything else.", "sample_inputs": ["0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1", "0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1"], "sample_outputs": ["YES\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n0 1 1\n1 0 1\n1 1 0\n1 1 1", "NO"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MAX_N = 8;\nimmutable int MAX_D = 3;\n\nlong dist (int [MAX_D] p, int [MAX_D] q)\n{\n\tlong res = 0L;\n\tforeach (j; 0..MAX_D)\n\t{\n \tlong cur = q[j] - p[j];\n \tres += cur * cur;\n\t}\n\treturn res;\n}\n\nint [MAX_D] [] build (int [MAX_D] [] a)\n{\n\tlong d01 = dist (a[0], a[1]);\n\tif (d01 == 0L)\n\t{\n\t\treturn null;\n\t}\n\tlong d02 = dist (a[0], a[2]);\n\tif (d02 != d01)\n\t{\n\t\treturn null;\n\t}\n\tlong d03 = dist (a[0], a[3]);\n\tif (d03 != d01)\n\t{\n\t\treturn null;\n\t}\n\tlong d12 = dist (a[1], a[2]);\n\tif (d12 != d01 * 2)\n\t{\n\t\treturn null;\n\t}\n\tlong d13 = dist (a[1], a[3]);\n\tif (d13 != d12)\n\t{\n\t\treturn null;\n\t}\n\tlong d23 = dist (a[2], a[3]);\n\tif (d23 != d12)\n\t{\n\t\treturn null;\n\t}\n\n\tint [MAX_D] [] f;\n\tforeach (i; 0..MAX_N)\n\t{\n\t\tint [MAX_D] c = a[0];\n\t\tif (i & 1)\n\t\t{\n\t\t\tc[] += a[1][] - a[0][];\n\t\t}\n\t\tif (i & 2)\n\t\t{\n\t\t\tc[] += a[2][] - a[0][];\n\t\t}\n\t\tif (i & 4)\n\t\t{\n\t\t\tc[] += a[3][] - a[0][];\n\t\t}\n\t\tf ~= c;\n\t}\n\tdebug {writefln (\"Candidate:\\n%(%(%s %)\\n%)\", f);}\n\n\tint [MAX_D] [] g = a[MAX_N / 2..$].dup;\nsearch_loop:\t\n\tforeach (i; [3, 5, 6, 7])\n\t{\n\t\tforeach (ref c; g)\n\t\t{\n\t\t\tdo\n\t\t\t{\n\t\t\t\tif (c == f[i])\n\t\t\t\t{\n\t\t\t\t\tc[] = int.min;\n\t\t\t\t\tcontinue search_loop;\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile (nextPermutation (c[]));\n\t\t}\n\t\treturn null;\n\t}\n\n\treturn f;\n}\n\nvoid answer (int [MAX_D] [] f, int [MAX_D] [] c)\n{\n\twriteln (\"YES\");\nanswer_loop:\n\tdo\n\t{\nlocal_loop:\n\t\tforeach (i; 0..MAX_N)\n\t\t{\n\t\t\tsort (c[i][]);\n\t\t\tdo\n\t\t\t{\n\t\t\t\tif (f[i][] == c[i][])\n\t\t\t\t{\n\t\t\t\t\tcontinue local_loop;\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile (nextPermutation (c[i][]));\n\t\t\tcontinue answer_loop;\n\t\t}\n\t\twritefln (\"%(%(%s %)\\n%)\", f);\n\t\treturn;\n\t}\n\twhile (nextPermutation (f));\n\tassert (false);\n}\n\nvoid main ()\n{\n\tint [MAX_D] [] a = new int [MAX_D] [MAX_N];\n\tforeach (i; 0..MAX_N)\n\t{\n\t\tforeach (j; 0..MAX_D)\n\t\t{\n\t\t\treadf (\" %s\", &a[i][j]);\n\t\t}\n\t}\n\n\tint [MAX_D] [] c = a.dup;\n\tforeach (i; 0..MAX_N)\n\t{\n\t\tsort (a[i][]);\n\t}\n\tsort (a);\n\n\tint [MAX_D] [] b = new int [MAX_D] [MAX_N / 2];\n\tforeach (i; 0..MAX_N / 2)\n\t{\n\t\tforeach (j; 0..MAX_D)\n\t\t{\n\t\t\tb[i][j] = int.min;\n\t\t}\n\t}\n\n\tdo\n\t{\n\t\tif (b[] == a[0..MAX_N / 2])\n\t\t{\n\t\t\tcontinue;\n\t\t}\n\t\tb[] = a[0..MAX_N / 2];\n\t\tdo\n\t\t{\n\t\t\tdo\n\t\t\t{\n\t\t\t\tdo\n\t\t\t\t{\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tauto f = build (a);\n\t\t\t\t\t\tif (f !is null)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tanswer (f, c);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\twhile (nextPermutation (a[3][]));\n\t\t\t\t}\n\t\t\t\twhile (nextPermutation (a[2][]));\n\t\t\t}\n\t\t\twhile (nextPermutation (a[1][]));\n\t\t}\n\t\twhile (nextPermutation (a[0][]));\n\t}\n\twhile (nextPermutation (a[1..$]));\n\n\twriteln (\"NO\");\n}\n\n// the following is a Phobos snipped with \"ref\" stripped\n// library code start\nbool nextPermutation(alias less=\"a binaryFun!less(i.front, a))(\n takeExactly(retro(range), n));\n\n assert(!j.empty); // shouldn't happen since i.front < last.front\n swap(i.front, j.front);\n reverse(takeExactly(retro(range), n));\n\n return true;\n}\n// library code end\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint[][] PERM3S = [\n\t[ 0, 1, 2 ], \n\t[ 0, 2, 1 ], \n\t[ 1, 0, 2 ], \n\t[ 1, 2, 0 ], \n\t[ 2, 0, 1 ], \n\t[ 2, 1, 0 ], \n];\n\nlong dot(long[] a, long[] b) {\n\tlong ret;\n\tforeach (d; 0 .. 3) {\n\t\tret += a[d] * b[d];\n\t}\n\treturn ret;\n}\n\nlong[][] A;\n\nvoid solve() {\n\tforeach (i; 0 .. 8) {\n\t\tforeach (j; 0 .. 8) foreach (k; 0 .. 8) foreach (l; 0 .. 8) {\n\t\t\tif (j != i && k != i && l != i && j < k && k < l) {\n\t\t\t\tforeach (sj; 0 .. 6) foreach (sk; 0 .. 6) foreach (sl; 0 .. 6) {\n\t\t\t\t\tlong[][] vs = new long[][](8, 3);\n\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\tvs[1][d] = A[j][PERM3S[sj][d]] - A[i][d];\n\t\t\t\t\t\tvs[2][d] = A[k][PERM3S[sk][d]] - A[i][d];\n\t\t\t\t\t\tvs[4][d] = A[l][PERM3S[sl][d]] - A[i][d];\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tconst pjj = dot(vs[1], vs[1]);\n\t\t\t\t\tconst pjk = dot(vs[1], vs[2]);\n\t\t\t\t\tconst pjl = dot(vs[1], vs[4]);\n\t\t\t\t\tconst pkk = dot(vs[2], vs[2]);\n\t\t\t\t\tconst pkl = dot(vs[2], vs[4]);\n\t\t\t\t\tconst pll = dot(vs[4], vs[4]);\n\t\t\t\t\tif (pjj == pkk && pkk == pll && pll > 0 && pjk == 0 && pjl == 0 && pkl == 0) {\ndebug{\nwriteln(i,\" \",j,\" \",k,\" \",l);\nwriteln(vs[1],\" \",vs[2],\" \",vs[4]);\n}\n\t\t\t\t\t\tlong[][] as, bs;\n\t\t\t\t\t\tforeach (m; 0 .. 8) {\n\t\t\t\t\t\t\tif (m != i && m != j && m != k && m != l) {\n\t\t\t\t\t\t\t\tas ~= A[m];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tforeach (f; 0 .. 8) if (f & (f - 1)) {\n\t\t\t\t\t\t\tlong[] b = new long[3];\n\t\t\t\t\t\t\tif (f & 1) b[] += vs[1][];\n\t\t\t\t\t\t\tif (f & 2) b[] += vs[2][];\n\t\t\t\t\t\t\tif (f & 4) b[] += vs[4][];\n\t\t\t\t\t\t\tbs ~= b;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tassert(as.length == 4);\n\t\t\t\t\t\tassert(bs.length == 4);\n\t\t\t\t\t\tint[] perm = [ 0, 1, 2, 3 ];\n\t\t\t\t\t\tdo {\n\t\t\t\t\t\t\tbool ok = true;\n\t\t\t\t\t\t\tint[] ss;\n\t\t\t\t\t\t\tforeach (x; 0 .. 4) {\n\t\t\t\t\t\t\t\tbool found;\n\t\t\t\t\t\t\t\tforeach (s; 0 .. 6) {\n\t\t\t\t\t\t\t\t\tbool eq = true;\n\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\teq = eq && (bs[perm[x]][d] == as[x][PERM3S[s][d]] - A[i][d]);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (eq) {\n\t\t\t\t\t\t\t\t\t\tfound = true;\n\t\t\t\t\t\t\t\t\t\tss ~= s;\n\t\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tok = ok && found;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (ok) {\ndebug{\nwriteln(\"ss = \",ss);\n}\n\t\t\t\t\t\t\t\twriteln(\"YES\");\n\t\t\t\t\t\t\t\tint pos;\n\t\t\t\t\t\t\t\tforeach (h; 0 .. 8) {\n\t\t\t\t\t\t\t\t\tconst s = (h == i) ? 0 : (h == j) ? sj : (h == k) ? sk : (h == l) ? sl : ss[pos++];\n\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\tif (d > 0) write(\" \");\n\t\t\t\t\t\t\t\t\t\twrite(A[h][PERM3S[s][d]]);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\twriteln;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} while (perm.nextPermutation);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\twriteln(\"NO\");\n}\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tA = new long[][](8, 3);\n\t\tforeach (i; 0 .. 8) {\n\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\tA[i][d] = readLong;\n\t\t\t}\n\t\t}\n\t\t\n\t\tsolve;\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nimmutable int MAX_N = 8;\nimmutable int MAX_D = 3;\n\nlong dist (int [MAX_D] p, int [MAX_D] q)\n{\n\tlong res = 0L;\n\tforeach (j; 0..MAX_D)\n\t{\n \tlong cur = q[j] - p[j];\n \tres += cur * cur;\n\t}\n\treturn res;\n}\n\nint [MAX_D] [] build (int [MAX_D] [] a)\n{\n\tlong d01 = dist (a[0], a[1]);\n\tif (d01 == 0L)\n\t{\n\t\treturn null;\n\t}\n\tlong d02 = dist (a[0], a[2]);\n\tif (d02 != d01)\n\t{\n\t\treturn null;\n\t}\n\tlong d03 = dist (a[0], a[3]);\n\tif (d03 != d01)\n\t{\n\t\treturn null;\n\t}\n\tlong d12 = dist (a[1], a[2]);\n\tif (d12 != d01 * 2)\n\t{\n\t\treturn null;\n\t}\n\tlong d13 = dist (a[1], a[3]);\n\tif (d13 != d12)\n\t{\n\t\treturn null;\n\t}\n\tlong d23 = dist (a[2], a[3]);\n\tif (d23 != d12)\n\t{\n\t\treturn null;\n\t}\n\n\tint [MAX_D] [] f;\n\tforeach (i; 0..MAX_N)\n\t{\n\t\tint [MAX_D] c = a[0];\n\t\tif (i & 1)\n\t\t{\n\t\t\tc[] += a[1][] - a[0][];\n\t\t}\n\t\tif (i & 2)\n\t\t{\n\t\t\tc[] += a[2][] - a[0][];\n\t\t}\n\t\tif (i & 4)\n\t\t{\n\t\t\tc[] += a[3][] - a[0][];\n\t\t}\n\t\tf ~= c;\n\t}\n\tdebug {writefln (\"Candidate:\\n%(%(%s %)\\n%)\", f);}\n\n\tint [MAX_D] [] g = a[MAX_N / 2..$].dup;\nsearch_loop:\t\n\tforeach (i; [3, 5, 6, 7])\n\t{\n\t\tforeach (ref c; g)\n\t\t{\n\t\t\tdo\n\t\t\t{\n\t\t\t\tif (c == f[i])\n\t\t\t\t{\n\t\t\t\t\tc[] = int.min;\n\t\t\t\t\tcontinue search_loop;\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile (nextPermutation (c[]));\n\t\t}\n\t\treturn null;\n\t}\n\n\treturn f;\n}\n\nvoid main ()\n{\n\tint [MAX_D] [] a = new int [MAX_D] [MAX_N];\n\tforeach (i; 0..MAX_N)\n\t{\n\t\tforeach (j; 0..MAX_D)\n\t\t{\n\t\t\treadf (\" %s\", &a[i][j]);\n\t\t}\n\t\tsort (a[i][]);\n\t}\n\tsort (a);\n\n\tint [MAX_D] [] b = new int [MAX_D] [MAX_N / 2];\n\tforeach (i; 0..MAX_N / 2)\n\t{\n\t\tforeach (j; 0..MAX_D)\n\t\t{\n\t\t\tb[i][j] = int.min;\n\t\t}\n\t}\n\n\tdo\n\t{\n\t\tif (b[] == a[0..MAX_N / 2])\n\t\t{\n\t\t\tcontinue;\n\t\t}\n\t\tb[] = a[0..MAX_N / 2];\n\t\tdo\n\t\t{\n\t\t\tdo\n\t\t\t{\n\t\t\t\tdo\n\t\t\t\t{\n\t\t\t\t\tdo\n\t\t\t\t\t{\n\t\t\t\t\t\tauto f = build (a);\n\t\t\t\t\t\tif (f !is null)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\twriteln (\"YES\");\n\t\t\t\t\t\t\twritefln\n\t\t\t\t\t\t\t (\"%(%(%s %)\\n%)\",\n\t\t\t\t\t\t\t f);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\twhile (nextPermutation (a[3][]));\n\t\t\t\t}\n\t\t\t\twhile (nextPermutation (a[2][]));\n\t\t\t}\n\t\t\twhile (nextPermutation (a[1][]));\n\t\t}\n\t\twhile (nextPermutation (a[0][]));\n\t}\n\twhile (nextPermutation (a[1..$]));\n\n\twriteln (\"NO\");\n}\n\n// the following is a Phobos snipped with \"ref\" stripped\n// library code start\nbool nextPermutation(alias less=\"a binaryFun!less(i.front, a))(\n takeExactly(retro(range), n));\n\n assert(!j.empty); // shouldn't happen since i.front < last.front\n swap(i.front, j.front);\n reverse(takeExactly(retro(range), n));\n\n return true;\n}\n// library code end\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint[][] PERM3S = [\n\t[ 0, 1, 2 ], \n\t[ 0, 2, 1 ], \n\t[ 1, 0, 2 ], \n\t[ 1, 2, 0 ], \n\t[ 2, 0, 1 ], \n\t[ 2, 1, 0 ], \n];\n\nlong dot(long[] a, long[] b) {\n\tlong ret;\n\tforeach (d; 0 .. 3) {\n\t\tret += a[d] * b[d];\n\t}\n\treturn ret;\n}\n\nlong[][] A;\n\nvoid solve() {\n\tforeach (i; 0 .. 8) {\n\t\tforeach (j; 0 .. 8) foreach (k; 0 .. 8) foreach (l; 0 .. 8) {\n\t\t\tif (j != i && k != i && l != i && j < k && k < l) {\n\t\t\t\tforeach (sj; 0 .. 6) foreach (sk; 0 .. 6) foreach (sl; 0 .. 6) {\n\t\t\t\t\tlong[][] vs = new long[][](8, 3);\n\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\tvs[1][d] = A[j][PERM3S[sj][d]] - A[i][d];\n\t\t\t\t\t\tvs[2][d] = A[k][PERM3S[sk][d]] - A[i][d];\n\t\t\t\t\t\tvs[4][d] = A[l][PERM3S[sl][d]] - A[i][d];\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tconst pjj = dot(vs[1], vs[1]);\n\t\t\t\t\tconst pjk = dot(vs[1], vs[2]);\n\t\t\t\t\tconst pjl = dot(vs[1], vs[4]);\n\t\t\t\t\tconst pkk = dot(vs[2], vs[2]);\n\t\t\t\t\tconst pkl = dot(vs[2], vs[4]);\n\t\t\t\t\tconst pll = dot(vs[4], vs[4]);\n\t\t\t\t\tif (pjj == pkk && pkk == pll && pll > 0 && pjk == 0 && pjl == 0 && pkl == 0) {\n// writeln(vs[1],\" \",vs[2],\" \",vs[4]);\n\t\t\t\t\t\tlong[][] as, bs;\n\t\t\t\t\t\tforeach (m; 0 .. 8) {\n\t\t\t\t\t\t\tif (m != i && m != j && m != k && m != l) {\n\t\t\t\t\t\t\t\tas ~= A[m];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tforeach (h; 0 .. 8) if (h & (h - 1)) {\n\t\t\t\t\t\t\tlong[] b = new long[3];\n\t\t\t\t\t\t\tif (h & 1) b[] += vs[1][];\n\t\t\t\t\t\t\tif (h & 2) b[] += vs[2][];\n\t\t\t\t\t\t\tif (h & 4) b[] += vs[4][];\n\t\t\t\t\t\t\tbs ~= b;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tassert(as.length == 4);\n\t\t\t\t\t\tassert(bs.length == 4);\n\t\t\t\t\t\tint[] perm = [ 0, 1, 2, 3 ];\n\t\t\t\t\t\tdo {\n\t\t\t\t\t\t\tbool ok = true;\n\t\t\t\t\t\t\tint[] ss;\n\t\t\t\t\t\t\tforeach (x; 0 .. 4) {\n\t\t\t\t\t\t\t\tbool found;\n\t\t\t\t\t\t\t\tforeach (s; 0 .. 6) {\n\t\t\t\t\t\t\t\t\tbool eq = true;\n\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\teq = eq && (bs[x][d] == as[perm[x]][PERM3S[s][d]] - A[i][d]);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (eq) {\n\t\t\t\t\t\t\t\t\t\tfound = true;\n\t\t\t\t\t\t\t\t\t\tss ~= s;\n\t\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tok = ok && found;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (ok) {\n\t\t\t\t\t\t\t\twriteln(\"YES\");\n\t\t\t\t\t\t\t\tint pos;\n\t\t\t\t\t\t\t\tforeach (h; 0 .. 8) {\n\t\t\t\t\t\t\t\t\tif (h == i) {\n\t\t\t\t\t\t\t\t\t\twriteln(A[i].to!string.removechars(\"[],\"));\n\t\t\t\t\t\t\t\t\t} else if (h == j) {\n\t\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\t\tif (d > 0) write(\" \");\n\t\t\t\t\t\t\t\t\t\t\twrite(A[j][PERM3S[sj][d]]);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\twriteln;\n\t\t\t\t\t\t\t\t\t} else if (h == k) {\n\t\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\t\tif (d > 0) write(\" \");\n\t\t\t\t\t\t\t\t\t\t\twrite(A[k][PERM3S[sk][d]]);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\twriteln;\n\t\t\t\t\t\t\t\t\t} else if (h == l) {\n\t\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\t\tif (d > 0) write(\" \");\n\t\t\t\t\t\t\t\t\t\t\twrite(A[l][PERM3S[sl][d]]);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\twriteln;\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tconst s = ss[pos];\n\t\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\t\tif (d > 0) write(\" \");\n\t\t\t\t\t\t\t\t\t\t\twrite(as[pos][PERM3S[s][d]]);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\twriteln;\n\t\t\t\t\t\t\t\t\t\t++pos;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} while (perm.nextPermutation);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\twriteln(\"NO\");\n}\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tA = new long[][](8, 3);\n\t\tforeach (i; 0 .. 8) {\n\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\tA[i][d] = readLong;\n\t\t\t}\n\t\t}\n\t\t\n\t\tsolve;\n\t}\n\t} catch (EOFException) {}\n}\n\n"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, in T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, in T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp( const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint 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; }\nint lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\nint[][] PERM3S = [\n\t[ 0, 1, 2 ], \n\t[ 0, 2, 1 ], \n\t[ 1, 0, 2 ], \n\t[ 1, 2, 0 ], \n\t[ 2, 0, 1 ], \n\t[ 2, 1, 0 ], \n];\n\nlong dot(long[] a, long[] b) {\n\tlong ret;\n\tforeach (d; 0 .. 3) {\n\t\tret += a[d] * b[d];\n\t}\n\treturn ret;\n}\n\nlong[][] A;\n\nvoid solve() {\n\tforeach (i; 0 .. 8) {\n\t\tforeach (j; 0 .. 8) foreach (k; 0 .. 8) foreach (l; 0 .. 8) {\n\t\t\tif (j != i && k != i && l != i && j < k && k < l) {\n\t\t\t\tforeach (sj; 0 .. 6) foreach (sk; 0 .. 6) foreach (sl; 0 .. 6) {\n\t\t\t\t\tlong[][] vs = new long[][](8, 3);\n\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\tvs[1][d] = A[j][PERM3S[sj][d]] - A[i][d];\n\t\t\t\t\t\tvs[2][d] = A[k][PERM3S[sk][d]] - A[i][d];\n\t\t\t\t\t\tvs[4][d] = A[l][PERM3S[sl][d]] - A[i][d];\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tconst pjj = dot(vs[1], vs[1]);\n\t\t\t\t\tconst pjk = dot(vs[1], vs[2]);\n\t\t\t\t\tconst pjl = dot(vs[1], vs[4]);\n\t\t\t\t\tconst pkk = dot(vs[2], vs[2]);\n\t\t\t\t\tconst pkl = dot(vs[2], vs[4]);\n\t\t\t\t\tconst pll = dot(vs[4], vs[4]);\n\t\t\t\t\tif (pjj == pkk && pkk == pll && pll > 0 && pjk == 0 && pjl == 0 && pkl == 0) {\n// writeln(vs[1],\" \",vs[2],\" \",vs[4]);\n\t\t\t\t\t\tlong[][] as, bs;\n\t\t\t\t\t\tforeach (m; 0 .. 8) {\n\t\t\t\t\t\t\tif (m != i && m != j && m != k && m != l) {\n\t\t\t\t\t\t\t\tas ~= A[m];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tforeach (h; 0 .. 8) if (h & (h - 1)) {\n\t\t\t\t\t\t\tlong[] b = new long[3];\n\t\t\t\t\t\t\tif (h & 1) b[] += vs[1][];\n\t\t\t\t\t\t\tif (h & 2) b[] += vs[2][];\n\t\t\t\t\t\t\tif (h & 4) b[] += vs[4][];\n\t\t\t\t\t\t\tbs ~= b;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tassert(as.length == 4);\n\t\t\t\t\t\tassert(bs.length == 4);\n\t\t\t\t\t\tint[] perm = [ 0, 1, 2, 3 ];\n\t\t\t\t\t\tdo {\n\t\t\t\t\t\t\tbool ok = true;\n\t\t\t\t\t\t\tint[] ss;\n\t\t\t\t\t\t\tforeach (x; 0 .. 4) {\n\t\t\t\t\t\t\t\tbool found;\n\t\t\t\t\t\t\t\tforeach (s; 0 .. 6) {\n\t\t\t\t\t\t\t\t\tbool eq = true;\n\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\teq = eq && (bs[x][d] == as[perm[x]][PERM3S[s][d]] - A[i][d]);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (eq) {\n\t\t\t\t\t\t\t\t\t\tfound = true;\n\t\t\t\t\t\t\t\t\t\tss ~= s;\n\t\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tok = ok && found;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (ok) {\n\t\t\t\t\t\t\t\twriteln(\"YES\");\n\t\t\t\t\t\t\t\tint pos;\n\t\t\t\t\t\t\t\tforeach (h; 0 .. 8) {\n\t\t\t\t\t\t\t\t\tif (h == i) {\n\t\t\t\t\t\t\t\t\t\twriteln(A[i].to!string.removechars(\"[],\"));\n\t\t\t\t\t\t\t\t\t} else if (h == j) {\n\t\t\t\t\t\t\t\t\t\twriteln(vs[1].to!string.removechars(\"[],\"));\n\t\t\t\t\t\t\t\t\t} else if (h == k) {\n\t\t\t\t\t\t\t\t\t\twriteln(vs[2].to!string.removechars(\"[],\"));\n\t\t\t\t\t\t\t\t\t} else if (h == l) {\n\t\t\t\t\t\t\t\t\t\twriteln(vs[4].to!string.removechars(\"[],\"));\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tconst s = ss[pos];\n\t\t\t\t\t\t\t\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\t\t\t\t\t\t\t\tif (d > 0) write(\" \");\n\t\t\t\t\t\t\t\t\t\t\twrite(as[pos][PERM3S[s][d]]);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\twriteln;\n\t\t\t\t\t\t\t\t\t\t++pos;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} while (perm.nextPermutation);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\twriteln(\"NO\");\n}\n\nvoid main(string[] args) {\n\ttry {\n\tfor (; ; ) {\n\t\tA = new long[][](8, 3);\n\t\tforeach (i; 0 .. 8) {\n\t\t\tforeach (d; 0 .. 3) {\n\t\t\t\tA[i][d] = readLong;\n\t\t\t}\n\t\t}\n\t\t\n\t\tsolve;\n\t}\n\t} catch (EOFException) {}\n}\n\n"}], "src_uid": "55da4611bc78d55c228d0ce78bd02fd3"} {"nl": {"description": "Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases.A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $$$n$$$ stairs, then it is made of $$$n$$$ columns, the first column is $$$1$$$ cell high, the second column is $$$2$$$ cells high, $$$\\ldots$$$, the $$$n$$$-th column if $$$n$$$ cells high. The lowest cells of all stairs must be in the same row.A staircase with $$$n$$$ stairs is called nice, if it may be covered by $$$n$$$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $$$7$$$ stairs looks like: Find out the maximal number of different nice staircases, that can be built, using no more than $$$x$$$ cells, in total. No cell can be used more than once.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 1000)$$$  — the number of test cases. The description of each test case contains a single integer $$$x$$$ $$$(1 \\le x \\le 10^{18})$$$  — the number of cells for building staircases.", "output_spec": "For each test case output a single integer  — the number of different nice staircases, that can be built, using not more than $$$x$$$ cells, in total.", "sample_inputs": ["4\n1\n8\n6\n1000000000000000000"], "sample_outputs": ["1\n2\n1\n30"], "notes": "NoteIn the first test case, it is possible to build only one staircase, that consists of $$$1$$$ stair. It's nice. That's why the answer is $$$1$$$.In the second test case, it is possible to build two different nice staircases: one consists of $$$1$$$ stair, and another consists of $$$3$$$ stairs. This will cost $$$7$$$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $$$2$$$.In the third test case, it is possible to build only one of two nice staircases: with $$$1$$$ stair or with $$$3$$$ stairs. In the first case, there will be $$$5$$$ cells left, that may be used only to build a staircase with $$$2$$$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $$$1$$$. If Jett builds a staircase with $$$3$$$ stairs, then there are no more cells left, so the answer is $$$1$$$ again."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;\n\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\n\n\nvoid solve(){\n long n;\n n = rd;\n long tim = 2; \n ll[] arr, pref;\n arr ~= 1;\n pref ~= 1;\n foreach(i; 1..32){\n arr ~= 2*arr[i-1] + tim * tim;\n tim *= 2;\n pref ~= arr[i] + pref[i-1];\n }\n /* writeln(pref); */\n ll maxi = 0;\n foreach(i; 1..32){\n if(pref[i] > n){\n maxi = i;\n break;\n }\n }\n /* writeln(arr); */\n writeln(maxi);\n}\n\nvoid main(){\n long t = 1;\n t = rd;\n while(t--) solve();\n stdout.flush;\n}\n\n"}, {"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;\n\nvoid solve()\n{\n auto N = readln.chomp.to!long;\n long x;\n foreach (long i; 0..32) {\n x += x + (2L^^i)^^2;\n if (N >= x) {\n N -= x;\n } else {\n writeln(i);\n return;\n }\n }\n}\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n solve();\n }\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto x = RD-1;\n\t\tans[ti] = 1;\n\t\tlong y = 1;\n\t\tlong cnt = 1;\n\t\tforeach (i; 0..10^^7)\n\t\t{\n\t\t\ty *= 2;\n\t\t\tlong z = cnt*2 + y^^2; \n\t\t\tif (z > x) break;\n\t\t\tx -= z;\n\t\t\t++ans[ti];\n\t\t\tcnt = z;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;\n\nalias ll = long;\nalias rbt = redBlackTree;\nalias tup = Tuple!(long, long);\nstatic string[] inp;\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\n\n\nvoid solve(){\n long n;\n n = rd;\n long tim = 2; \n ll[] arr;\n arr ~= 1;\n foreach(i; 1..62){\n arr ~= 2*arr[i-1] + tim * tim;\n if(n < arr.back){\n break;\n }\n tim *= 2;\n }\n arr.popBack;\n /* writeln(arr); */\n writeln(arr.length);\n}\n\nvoid main(){\n long t = 1;\n t = rd;\n while(t--) solve();\n stdout.flush;\n}\n\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto x = RD-1;\n\t\tans[ti] = 1;\n\t\tlong y = 1;\n\t\tforeach (i; 0..10^^7)\n\t\t{\n\t\t\ty *= 2;\n\t\t\tlong z = y + y^^2; \n\t\t\tif (z > x) break;\n\t\t\tx -= z;\n\t\t\t++ans[ti];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "src_uid": "f0806ab99cf4da228abe3cd8073884d9"} {"nl": {"description": "We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types: Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 ≤ q < k). The given operation is correct — both subsegments do not touch unexistent elements. Determine the value in position x of array b, that is, find value bx. For each query of the second type print the result — the value of the corresponding element of array b.", "input_spec": "The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| ≤ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| ≤ 109). Next m lines contain the descriptions of the queries. The i-th line first contains integer ti — the type of the i-th query (1 ≤ ti ≤ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 ≤ xi, yi, ki ≤ n) — the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 ≤ xi ≤ n) — the position in array b. All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.", "output_spec": "For each second type query print the result on a single line.", "sample_inputs": ["5 10\n1 2 0 -1 3\n3 1 5 -2 0\n2 5\n1 3 3 3\n2 5\n2 4\n2 1\n1 2 1 4\n2 1\n2 4\n1 4 2 1\n2 2"], "sample_outputs": ["0\n3\n-1\n3\n2\n3\n-1"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto a = readln.chomp.split.map!(to!int).array;\n auto b = readln.chomp.split.map!(to!int).array;\n \n struct q {\n int t, x, y, k;\n }\n \n auto qs = new q[] (m);\n foreach (i; 0 .. m) {\n readf(\"%s\", &qs[i].t);\n if (qs[i].t == 1) {\n readf(\" %s %s %s\", &qs[i].x, &qs[i].y, &qs[i].k);\n } else {\n readf(\" %s\", &qs[i].x);\n }\n readln;\n }\n \n debug { qs.writeln; }\n \n auto ans = new int[] (m);\n \n alias elem = Tuple!(int, int);\n auto rbt = make!(RedBlackTree!elem);\n foreach_reverse (i, cq; qs) {\n if (cq.t == 2) {\n rbt.insert(tuple(cq.x, i.to!int));\n } else {\n auto seq = rbt.upperBound(tuple(cq.y, 0)).until!(t => t[0] > cq.y + cq.k - 1);\n debug { cq.y.writeln; seq.writeln; }\n \n foreach (e; seq) {\n ans[e[1]] = a[cq.x + e[0] - cq.y - 1];\n }\n \n auto cpy = seq.array.dup;\n foreach (e; cpy) { rbt.removeKey(e); }\n }\n }\n \n foreach (e; rbt) {\n ans[e[1]] = b[e[0] - 1];\n }\n \n foreach (i; 0 .. m) {\n if (qs[i].t == 1) { continue; }\n \n ans[i].writeln;\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.container;\nimport std.exception;\nimport std.format;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int MED_L = 1 << 17;\nimmutable int MAX_L = MED_L << 1;\n\nvoid main ()\n{\n\tint n, m;\n\twhile (scanf (\" %d %d\", &n, &m) > 0)\n\t{\n\t\tauto a = new int [n];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tscanf (\" %d\", &a[i]);\n\t\t}\n\t\tauto b = new int [n];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tscanf (\" %d\", &b[i]);\n\t\t}\n\n\t\tauto x = new int [m];\n\t\tauto y = new int [m];\n\t\tauto z = new int [m];\n\t\tauto t = new int [MAX_L];\n\t\tt[] = -1;\n\n\t\tvoid mark (int q, int lo, int hi)\n\t\t{\n\t\t\tlo += MED_L;\n\t\t\thi += MED_L;\n\t\t\twhile (lo <= hi)\n\t\t\t{\n\t\t\t\tif (lo & 1)\n\t\t\t\t{\n\t\t\t\t\tdebug {writefln (\"t[%s] = %s\", lo, q);}\n\t\t\t\t\tt[lo] = q;\n\t\t\t\t\tlo++;\n\t\t\t\t}\n\t\t\t\tif (!(hi & 1))\n\t\t\t\t{\n\t\t\t\t\tdebug {writefln (\"t[%s] = %s\", hi, q);}\n\t\t\t\t\tt[hi] = q;\n\t\t\t\t\thi--;\n\t\t\t\t}\n\t\t\t\tlo >>= 1;\n\t\t\t\thi >>= 1;\n\t\t\t}\n\t\t}\n\n\t\tint value (int p)\n\t\t{\n\t\t\tint res = b[p];\n\t\t\tint q = -1;\n\t\t\tfor (int s = p + MED_L; s > 0; s >>= 1)\n\t\t\t{\n\t\t\t\tq = max (q, t[s]);\n\t\t\t}\n\t\t\tif (q != -1)\n\t\t\t{\n\t\t\t\tdebug {writefln (\"q = %s, p = %s\", q, p);}\n\t\t\t\tdebug {writefln (\"index = %s + %s - %s = %s\",\n\t\t\t\t x[q], p, y[q],\n\t\t\t\t x[q] + p - y[q]);}\n\t\t\t\tres = a[x[q] + p - y[q]];\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tforeach (j; 0..m)\n\t\t{\n\t\t\tint k;\n\t\t\tscanf (\" %d\", &k);\n\t\t\tif (k == 1)\n\t\t\t{\n\t\t\t\tscanf (\" %d %d %d\", &x[j], &y[j], &z[j]);\n\t\t\t\tx[j]--;\n\t\t\t\ty[j]--;\n\t\t\t\tmark (j, y[j], y[j] + z[j] - 1);\n\t\t\t}\n\t\t\telse if (k == 2)\n\t\t\t{\n\t\t\t\tint p;\n\t\t\t\tscanf (\" %d\", &p);\n\t\t\t\tdebug {writefln (\"p = %d\", p);}\n\t\t\t\tp--;\n\t\t\t\tint v = value (p);\n\t\t\t\tprintf (\"%d\\n\", v);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tassert (false);\n\t\t\t}\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nalias Tuple!(int, \"x\", int, \"y\") Pair;\n\nclass SegmentTree {\n int l;\n int r;\n Pair val;\n SegmentTree l_son;\n SegmentTree r_son;\n\n this(int l, int r) {\n this.l = l;\n this.r = r;\n this.val.x = this.val.y = -1;\n if (l != r) {\n this.l_son = new SegmentTree(l, (l+r)/2);\n this.r_son = new SegmentTree((l+r)/2+1, r);\n }\n }\n\n void Upd() {\n if (this.val.x != -1) {\n this.l_son.val = this.val;\n this.r_son.val = this.val;\n this.val.x = this.val.y = -1;\n }\n }\n\n void Col(int l, int r, int x, int y) {\n if (l > this.r || r < this.l) {\n return;\n }\n if (l <= this.l && r >= this.r) {\n this.val.x = x;\n this.val.y = y;\n return;\n }\n Upd();\n this.l_son.Col(l, r, x, y);\n this.r_son.Col(l, r, x, y);\n }\n\n Pair Val(int pos) {\n if (this.val.x != -1 || this.l == this.r) {\n return this.val;\n }\n return pos <= this.l_son.r ? this.l_son.Val(pos) : this.r_son.Val(pos);\n }\n}\n\nvoid main() {\n //stdin.open(\"input.txt\", \"r\");\n //stdout.open(\"output.txt\", \"w\");\n int n;\n int m;\n readf(\"%d %d \", &n, &m);\n int[] a = new int[n];\n int[] b = new int[n];\n foreach (i; 0..n) {\n readf(\"%d \", &a[i]);\n }\n foreach (i; 0..n) {\n readf(\"%d \", &b[i]);\n }\n SegmentTree tree = new SegmentTree(0, n-1);\n foreach (i; 0..m) {\n int t;\n readf(\"%d \", &t);\n if (t == 1) {\n int x;\n int y;\n int k;\n readf(\"%d %d %d \", &x, &y, &k);\n x--;\n y--;\n tree.Col(y, y+k-1, x, y);\n } else {\n int pos;\n readf(\"%d \", &pos);\n pos--;\n Pair cur_val = tree.Val(pos);\n if (cur_val.x == -1) {\n writeln(b[pos]);\n } else {\n writeln(a[cur_val.x + pos - cur_val.y]);\n }\n }\n }\n}"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional, std.meta;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto a = readln.chomp.split.map!(to!int).array;\n auto b = readln.chomp.split.map!(to!int).array;\n \n struct q {\n int t, x, y, k;\n }\n \n auto qs = new q[] (m);\n foreach (i; 0 .. m) {\n readf(\"%s\", &qs[i].t);\n if (qs[i].t == 1) {\n readf(\" %s %s %s\", &qs[i].x, &qs[i].y, &qs[i].k);\n } else {\n readf(\" %s\", &qs[i].x);\n }\n readln;\n }\n \n debug { qs.writeln; }\n \n auto ans = new int[] (m);\n \n alias elem = Tuple!(int, int);\n auto rbt = make!(RedBlackTree!elem);\n foreach_reverse (i, cq; qs) {\n if (cq.t == 2) {\n rbt.insert(tuple(cq.x, i.to!int));\n } else {\n auto seq = rbt.upperBound(tuple(cq.y, 0)).until!(t => t[0] > cq.y + cq.k);\n debug { cq.y.writeln; seq.writeln; }\n \n foreach (e; seq) {\n ans[e[1]] = a[cq.x + e[0] - cq.y - 1];\n }\n \n auto cpy = seq.array.dup;\n foreach (e; cpy) { rbt.removeKey(e); }\n }\n }\n \n foreach (e; rbt) {\n ans[e[1]] = b[e[0] - 1];\n }\n \n foreach (i; 0 .. m) {\n if (qs[i].t == 1) { continue; }\n \n ans[i].writeln;\n }\n}"}], "src_uid": "27c703f9846064af2b0deab93d394272"} {"nl": {"description": "It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.", "output_spec": "Print a single line with the winner's name. If Alice wins print \"Alice\", otherwise print \"Bob\" (without quotes).", "sample_inputs": ["2\n2 3", "2\n5 3", "3\n5 6 7"], "sample_outputs": ["Alice", "Alice", "Bob"], "notes": "NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tauto a = new int [n];\n\t\tint d = 0;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\treadf (\" %s\", &a[i]);\n\t\t\td = gcd (d, a[i]);\n\t\t}\n\t\ta[] /= d;\n\t\tsort (a);\n\t\tint left = a[$ - 1] - a.length;\n\t\twriteln ((left % 2 == 1) ? \"Alice\" : \"Bob\");\n\t}\n}\n"}], "negative_code": [], "src_uid": "3185ae6b4b681a10a21d02e67f08fd19"} {"nl": {"description": "This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury picked an integer $$$x$$$ not less than $$$0$$$ and not greater than $$$2^{14} - 1$$$. You have to guess this integer.To do so, you may ask no more than $$$2$$$ queries. Each query should consist of $$$100$$$ integer numbers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{100}$$$ (each integer should be not less than $$$0$$$ and not greater than $$$2^{14} - 1$$$). In response to your query, the jury will pick one integer $$$i$$$ ($$$1 \\le i \\le 100$$$) and tell you the value of $$$a_i \\oplus x$$$ (the bitwise XOR of $$$a_i$$$ and $$$x$$$). There is an additional constraint on the queries: all $$$200$$$ integers you use in the queries should be distinct.It is guaranteed that the value of $$$x$$$ is fixed beforehand in each test, but the choice of $$$i$$$ in every query may depend on the integers you send.", "input_spec": null, "output_spec": "To give the answer, your program should print one line $$$!$$$ $$$x$$$ with a line break in the end. After that, it should flush the output and terminate gracefully.", "sample_inputs": ["0\n32"], "sample_outputs": ["? 3 5 6\n? 32 24 37\n! 5"], "notes": "NoteThe example of interaction is not correct — you should sumbit exactly $$$100$$$ integers in each query. Everything else is correct.Hacks are forbidden in this problem."}, "positive_code": [{"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\nimport std.meta;\nimport std.traits;\n\nvoid main()\n{\n write(\"? \");\n iota(1, uint(101)).map!(n => n << 7).each!(n => write(n, \" \"));\n writeln;\n stdout.flush;\n auto anslo = next!uint & uint(0b00000001111111);\n\n write(\"? \");\n iota(1, uint(101)).each!(n => write(n, \" \"));\n writeln;\n stdout.flush;\n auto anshi = next!uint & uint(0b11111110000000);\n\n return writeln(\"! \", anslo | anshi);\n}\n\nvoid read(T...)(ref T t)\n{\n static DList!string _words;\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n"}], "negative_code": [{"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\nimport std.meta;\nimport std.traits;\n\nvoid main()\n{\n write(\"? \");\n iota(0, uint(100)).map!(n => n << 7).each!(n => write(n, \" \"));\n writeln;\n stdout.flush;\n auto anslo = next!uint & uint(0b00000001111111);\n\n write(\"? \");\n iota(0, uint(100)).each!(n => write(n, \" \"));\n writeln;\n stdout.flush;\n auto anshi = next!uint & uint(0b11111110000000);\n\n return writeln(\"! \", anslo | anshi);\n}\n\nvoid read(T...)(ref T t)\n{\n static DList!string _words;\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n"}], "src_uid": "c7f31e0c57cf15f71c401d826c3ee0ef"} {"nl": {"description": "This is an interactive problem.Vladik has favorite game, in which he plays all his free time.Game field could be represented as n × m matrix which consists of cells of three types: «.» — normal cell, player can visit it. «F» — finish cell, player has to finish his way there to win. There is exactly one cell of this type. «*» — dangerous cell, if player comes to this cell, he loses. Initially player is located in the left top cell with coordinates (1, 1). Player has access to 4 buttons \"U\", \"D\", \"L\", \"R\", each of them move player up, down, left and right directions respectively.But it’s not that easy! Sometimes friends play game and change functions of buttons. Function of buttons \"L\" and \"R\" could have been swapped, also functions of buttons \"U\" and \"D\" could have been swapped. Note that functions of buttons can be changed only at the beginning of the game.Help Vladik win the game!", "input_spec": "First line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — number of rows and columns respectively. Each of next n lines contains m characters describing corresponding row of field. Set of characters in field is described above. Guaranteed that cell with coordinates (1, 1) is normal and there is at least one way from initial cell to finish cell without dangerous cells. ", "output_spec": null, "sample_inputs": ["4 3\n...\n**.\nF*.\n...\n1 1\n1 2\n1 3\n1 3\n2 3\n3 3\n4 3\n4 2\n4 1\n3 1"], "sample_outputs": ["R\nL\nL\nD\nU\nU\nU\nR\nR\nD"], "notes": "NoteIn first test case all four directions swapped with their opposite directions. Protocol of interaction In more convenient form:This test could be presenter for hack in following way: 4 3 1 1...**.F*.... "}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdlib;;\n\nvoid main() {\n immutable int INF = 1 << 28;\n\n auto s = readln.split.map!(to!int);\n auto H = s[0];\n auto W = s[1];\n auto B = H.iota.map!(_ => readln.chomp).array;\n\n int gr, gc;\n foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;\n\n int[] dr = [0, 0, -1, 1];\n int[] dc = [-1, 1, 0, 0];\n auto dist = new int[](H * W);\n fill(dist, INF);\n dist[0] = 0;\n auto prev = new int[](H * W);\n fill(prev, -1);\n alias Tuple!(int, \"from\", int, \"to\", int, \"cost\") Edge;\n auto q = new BinaryHeap!(Array!Edge, \"a.cost >= b.cost\");\n q.insert(Edge(-1, 0, 0));\n\n while (!q.empty) {\n auto n = q.front;\n auto pr = n.from / W;\n auto pc = n.from % W;\n auto r = n.to / W;\n auto c = n.to % W;\n q.removeFront;\n if (dist[n.to] < n.cost)\n continue;\n dist[n.to] = n.cost;\n prev[n.to] = n.from;\n\n foreach (i; 0..4) {\n auto nr = r + dr[i];\n auto nc = c + dc[i];\n auto next = nr * W + nc;\n if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')\n continue;\n if (dist[next] <= dist[n.to] + 1)\n continue;\n dist[next] = dist[n.to] + 1;\n q.insert(Edge(n.to, next, dist[n.to] + 1));\n }\n }\n\n int[] root;\n int x = gr * W + gc;\n while (prev[x] != -1) {\n root = x ~ root;\n x = prev[x];\n }\n\n\n bool swapLR, swapUD;\n Tuple!(int, int) cmd(char dir) {\n if (swapLR && dir == 'L') dir = 'R';\n else if (swapLR && dir == 'R') dir = 'L';\n else if (swapUD && dir == 'U') dir = 'D';\n else if (swapUD && dir == 'D') dir = 'U';\n\n writeln(dir);\n stdout.flush;\n s = readln.split.map!(to!int);\n\n if (s[0] - 1 == gr && s[1] - 1 == gc)\n exit(0);\n return tuple(s[0] - 1, s[1] - 1);\n }\n\n\n\n Tuple!(int, int) pos;\n\n if (H == 1) {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (true) pos = cmd('R');\n return;\n }\n if (W == 1) {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (true) pos = cmd('D');\n return;\n }\n\n if (B[0][1] != '*') {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n pos = cmd('L');\n while (true) {\n if (B[pos[0]+1][pos[1]] != '*')\n break;\n pos = cmd('R');\n }\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n pos = cmd('U');\n while (pos[1] != 0) pos = cmd('L');\n }\n else {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n pos = cmd('D');\n while (true) {\n if (B[pos[0]][pos[1]+1] != '*')\n break;\n pos = cmd('D');\n }\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n pos = cmd('L');\n while (pos[0] != 0) pos = cmd('U');\n }\n\n foreach (co; root) {\n int nr = co / W;\n int nc = co % W;\n if (pos[0] - nr == 1) pos = cmd('U');\n else if (pos[0] - nr == -1) pos = cmd('D');\n else if (pos[1] - nc == 1) pos = cmd('L');\n else pos = cmd('R');\n }\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\n//import core.stdc.stdio;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nalias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];\n\nauto getAddrTuple() {\n return tuple();\n}\n\nauto getAddrTuple(T, U...)(ref T head, ref U tail) {\n return tuple(&head, getAddrTuple(tail).expand);\n}\n\nbool read(T...)(ref T vars) if (vars.length > 0) {\n return readf(' ' ~ replicate(\"%s \", vars.length), getAddrTuple(vars).expand) == vars.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nalias Pos = Tuple!(int, `y`, int, `x`);\n\nenum { U, R, D, L }\n\nchar[101][100] grid;\nchar[100][100] way;\nPos[10^^4] q;\nchar[4] dirs = \"URDL\";\nPos cur, finish;\n\nvoid move(int d) {\n writeln(dirs[d]);\n stdout.flush();\n readf(\" %s %s\", &cur.y, &cur.x);\n if (!~cur.y)\n exit(1);\n cur.y--;\n cur.x--;\n if (cur == finish)\n exit(0);\n}\n\nvoid swapLR() {\n dirs[R] = 'L';\n dirs[L] = 'R';\n}\n\nvoid swapUD() {\n dirs[D] = 'U';\n dirs[U] = 'D';\n}\n\nvoid main() {\n int n, m;\n readf(\"%s %s \", &n, &m);\n memset(grid.ptr, '*', grid.sizeof);\n foreach (int i, ref row; grid[0 .. n]) {\n auto s = row[ ];\n readln(s);\n s[$ - 1] = '*';\n auto tail = s[0 .. m].find('F');\n if (!tail.empty)\n finish = Pos(i, m - cast(int)tail.length);\n }\n assert(finish.y || finish.x);\n\n if (n == 1) {\n move(R);\n if (!cur.x)\n swapLR();\n while (true)\n move(R);\n }\n\n if (m == 1) {\n move(D);\n if (!cur.y)\n swapUD();\n while (true)\n move(D);\n }\n\n const lrFound = grid[0][1] != '*';\n if (lrFound) {\n move(R);\n if (!cur.x)\n swapLR();\n else\n move(L);\n }\n\n const udFound = grid[1][0] != '*';\n if (udFound) {\n move(D);\n if (!cur.y)\n swapUD();\n else\n move(U);\n }\n\n if (!lrFound) {\n assert(udFound);\n foreach (i; 1 .. n) {\n assert(grid[i][0] != '*');\n if (grid[i][1] != '*') {\n foreach (k; 0 .. i)\n move(D);\n move(R);\n if (!cur.x)\n swapLR();\n break;\n }\n }\n } else if (!udFound) {\n assert(lrFound);\n foreach (j; 1 .. m) {\n assert(grid[0][j] != '*');\n if (grid[1][j] != '*') {\n foreach (k; 0 .. j)\n move(R);\n move(D);\n if (!cur.y)\n swapUD();\n break;\n }\n }\n }\n\n int qr = 0, qw = 1;\n q[0] = finish;\n while (true) {\n assert(qr != qw);\n Pos p = q[qr++];\n if (p == cur)\n while (true)\n move(way[cur.y][cur.x]);\n\n enum gen(string cond, char c) = `\n if (`~cond~` && grid[nextPos.y][nextPos.x] != '*') {\n grid[nextPos.y][nextPos.x] = '*';\n way[nextPos.y][nextPos.x] = `~c~`;\n q[qw++] = nextPos;\n }\n `;\n\n Pos nextPos = Pos(p.y - 1, p.x);\n mixin(gen!(`nextPos.y >= 0`, 'D'));\n nextPos = Pos(p.y, p.x + 1);\n mixin(gen!(`nextPos.x < m`, 'L'));\n nextPos = Pos(p.y + 1, p.x);\n mixin(gen!(`nextPos.y < n`, 'U'));\n nextPos = Pos(p.y, p.x - 1);\n mixin(gen!(`nextPos.x >= 0`, 'R'));\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n immutable int INF = 1 << 28;\n\n auto s = readln.split.map!(to!int);\n auto H = s[0];\n auto W = s[1];\n auto B = H.iota.map!(_ => readln.chomp).array;\n\n int gr, gc;\n foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;\n\n int[] dr = [0, 0, -1, 1];\n int[] dc = [-1, 1, 0, 0];\n auto dist = new int[](H * W);\n fill(dist, INF);\n dist[0] = 0;\n auto prev = new int[](H * W);\n fill(prev, -1);\n alias Tuple!(int, \"from\", int, \"to\", int, \"cost\") Edge;\n auto q = new BinaryHeap!(Array!Edge, \"a.cost >= b.cost\");\n q.insert(Edge(-1, 0, 0));\n\n while (!q.empty) {\n auto n = q.front;\n auto pr = n.from / W;\n auto pc = n.from % W;\n auto r = n.to / W;\n auto c = n.to % W;\n q.removeFront;\n if (dist[n.to] < n.cost)\n continue;\n dist[n.to] = n.cost;\n prev[n.to] = n.from;\n\n foreach (i; 0..4) {\n auto nr = r + dr[i];\n auto nc = c + dc[i];\n auto next = nr * W + nc;\n if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')\n continue;\n if (dist[next] <= dist[n.to] + 1)\n continue;\n dist[next] = dist[n.to] + 1;\n q.insert(Edge(n.to, next, dist[n.to] + 1));\n }\n }\n\n int[] root;\n int x = gr * W + gc;\n while (prev[x] != -1) {\n root = x ~ root;\n x = prev[x];\n }\n\n\n bool swapLR, swapUD;\n Tuple!(int, int) cmd(char dir) {\n if (swapLR && dir == 'L') dir = 'R';\n else if (swapLR && dir == 'R') dir = 'L';\n else if (swapUD && dir == 'U') dir = 'D';\n else if (swapUD && dir == 'D') dir = 'U';\n\n writeln(dir);\n stdout.flush;\n s = readln.split.map!(to!int);\n return tuple(s[0] - 1, s[1] - 1);\n }\n\n\n\n Tuple!(int, int) pos;\n\n if (H == 1) {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (pos[0] != gr || pos[1] != gc) pos = cmd('R');\n return;\n }\n if (W == 1) {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (pos[0] != gr || pos[1] != gc) pos = cmd('D');\n return;\n }\n\n if (B[0][1] == '.') {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (true) {\n if (pos[0] == gr && pos[1] == gc)\n return;\n if (B[pos[0]+1][pos[1]] == '.')\n break;\n pos = cmd('R');\n }\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n pos = cmd('U');\n while (pos[1] != 0) pos = cmd('L');\n }\n else {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (true) {\n if (pos[0] == gr && pos[1] == gc)\n return;\n if (B[pos[0]][pos[1]+1] == '.')\n break;\n pos = cmd('D');\n }\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n pos = cmd('L');\n while (pos[0] != 0) pos = cmd('U');\n }\n\n foreach (co; root) {\n int nr = co / W;\n int nc = co % W;\n if (pos[0] - nr == 1) pos = cmd('U');\n else if (pos[0] - nr == -1) pos = cmd('D');\n else if (pos[1] - nc == 1) pos = cmd('L');\n else pos = cmd('R');\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdlib;;\n\nvoid main() {\n immutable int INF = 1 << 28;\n\n auto s = readln.split.map!(to!int);\n auto H = s[0];\n auto W = s[1];\n auto B = H.iota.map!(_ => readln.chomp).array;\n\n int gr, gc;\n foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;\n\n int[] dr = [0, 0, -1, 1];\n int[] dc = [-1, 1, 0, 0];\n auto dist = new int[](H * W);\n fill(dist, INF);\n dist[0] = 0;\n auto prev = new int[](H * W);\n fill(prev, -1);\n alias Tuple!(int, \"from\", int, \"to\", int, \"cost\") Edge;\n auto q = new BinaryHeap!(Array!Edge, \"a.cost >= b.cost\");\n q.insert(Edge(-1, 0, 0));\n\n while (!q.empty) {\n auto n = q.front;\n auto pr = n.from / W;\n auto pc = n.from % W;\n auto r = n.to / W;\n auto c = n.to % W;\n q.removeFront;\n if (dist[n.to] < n.cost)\n continue;\n dist[n.to] = n.cost;\n prev[n.to] = n.from;\n\n foreach (i; 0..4) {\n auto nr = r + dr[i];\n auto nc = c + dc[i];\n auto next = nr * W + nc;\n if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')\n continue;\n if (dist[next] <= dist[n.to] + 1)\n continue;\n dist[next] = dist[n.to] + 1;\n q.insert(Edge(n.to, next, dist[n.to] + 1));\n }\n }\n\n int[] root;\n int x = gr * W + gc;\n while (prev[x] != -1) {\n root = x ~ root;\n x = prev[x];\n }\n\n\n bool swapLR, swapUD;\n Tuple!(int, int) cmd(char dir) {\n if (swapLR && dir == 'L') dir = 'R';\n else if (swapLR && dir == 'R') dir = 'L';\n else if (swapUD && dir == 'U') dir = 'D';\n else if (swapUD && dir == 'D') dir = 'U';\n\n writeln(dir);\n stdout.flush;\n s = readln.split.map!(to!int);\n\n if (s[0] - 1 == gr && s[1] - 1 == gc)\n exit(0);\n return tuple(s[0] - 1, s[1] - 1);\n }\n\n\n\n Tuple!(int, int) pos;\n\n if (H == 1) {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (true) pos = cmd('R');\n return;\n }\n if (W == 1) {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (true) pos = cmd('D');\n return;\n }\n\n if (B[0][1] != '*') {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (true) {\n if (B[pos[0]+1][pos[1]] != '*')\n break;\n pos = cmd('R');\n }\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n pos = cmd('U');\n while (pos[1] != 0) pos = cmd('L');\n }\n else {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (true) {\n if (B[pos[0]][pos[1]+1] != '*')\n break;\n pos = cmd('D');\n }\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n pos = cmd('L');\n while (pos[0] != 0) pos = cmd('U');\n }\n\n foreach (co; root) {\n int nr = co / W;\n int nc = co % W;\n if (pos[0] - nr == 1) pos = cmd('U');\n else if (pos[0] - nr == -1) pos = cmd('D');\n else if (pos[1] - nc == 1) pos = cmd('L');\n else pos = cmd('R');\n }\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop;\n\nvoid main() {\n immutable int INF = 1 << 28;\n\n auto s = readln.split.map!(to!int);\n auto H = s[0];\n auto W = s[1];\n auto B = H.iota.map!(_ => readln.chomp).array;\n\n int gr, gc;\n foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;\n\n int[] dr = [0, 0, -1, 1];\n int[] dc = [-1, 1, 0, 0];\n auto dist = new int[](H * W);\n fill(dist, INF);\n dist[0] = 0;\n auto prev = new int[](H * W);\n fill(prev, -1);\n alias Tuple!(int, \"from\", int, \"to\", int, \"cost\") Edge;\n auto q = new BinaryHeap!(Array!Edge, \"a.cost >= b.cost\");\n q.insert(Edge(-1, 0, 0));\n\n while (!q.empty) {\n auto n = q.front;\n auto pr = n.from / W;\n auto pc = n.from % W;\n auto r = n.to / W;\n auto c = n.to % W;\n q.removeFront;\n if (dist[n.to] < n.cost)\n continue;\n dist[n.to] = n.cost;\n prev[n.to] = n.from;\n\n foreach (i; 0..4) {\n auto nr = r + dr[i];\n auto nc = c + dc[i];\n auto next = nr * W + nc;\n if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')\n continue;\n if (dist[next] <= dist[n.to] + 1)\n continue;\n dist[next] = dist[n.to] + 1;\n q.insert(Edge(n.to, next, dist[n.to] + 1));\n }\n }\n\n int[] root;\n int x = gr * W + gc;\n while (prev[x] != -1) {\n root = x ~ root;\n x = prev[x];\n }\n\n\n bool swapLR, swapUD;\n Tuple!(int, int) cmd(char dir) {\n if (swapLR && dir == 'L') dir = 'R';\n else if (swapLR && dir == 'R') dir = 'L';\n else if (swapUD && dir == 'U') dir = 'D';\n else if (swapUD && dir == 'D') dir = 'U';\n\n writeln(dir);\n stdout.flush;\n s = readln.split.map!(to!int);\n return tuple(s[0] - 1, s[1] - 1);\n }\n\n\n\n Tuple!(int, int) pos;\n\n if (H == 1) {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (pos[0] != gr || pos[1] != gc) pos = cmd('R');\n return;\n }\n if (W == 1) {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (pos[0] != gr || pos[1] != gc) pos = cmd('D');\n return;\n }\n\n if (B[0][1] == '.') {\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n while (true) {\n if (pos[0] == gr && pos[1] == gc)\n return;\n if (B[pos[0]+1][pos[1]] == '.')\n break;\n pos = cmd('R');\n }\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n pos = cmd('U');\n while (pos[1] != 0) pos = cmd('L');\n }\n else {\n pos = cmd('D');\n if (pos[0] == 0) swapUD = true;\n while (true) {\n if (pos[0] == gr && pos[1] == gc)\n return;\n if (B[pos[0]][pos[1]+1] == '.')\n break;\n pos = cmd('D');\n }\n pos = cmd('R');\n if (pos[1] == 0) swapLR = true;\n pos = cmd('L');\n while (pos[0] != 0) pos = cmd('U');\n }\n\n foreach (co; root) {\n int nr = co / W;\n int nc = co % W;\n if (pos[0] - nr == 1) pos = cmd('U');\n else if (pos[0] - nr == -1) pos = cmd('D');\n else if (pos[0] - nc == 1) pos = cmd('L');\n else pos = cmd('R');\n }\n}\n"}], "src_uid": "79879630a9358ea152a6f1616ef9b630"} {"nl": {"description": "Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.Note that the order of the points inside the group of three chosen points doesn't matter.", "input_spec": "The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got. It is guaranteed that the coordinates of the points in the input strictly increase.", "output_spec": "Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["4 3\n1 2 3 4", "4 2\n-3 -2 -1 0", "5 19\n1 10 20 30 50"], "sample_outputs": ["4", "2", "1"], "notes": "NoteIn the first sample any group of three points meets our conditions.In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.In the third sample only one group does: {1, 10, 20}."}, "positive_code": [{"source_code": "import std.stdio : writeln, writefln;\nimport std.array;\nimport std.range;\nimport std.algorithm : filter, max, sort;\nimport std.math : sqrt;\n\nint n;\nint d;\n\nvoid swap(T)(ref T a, ref T b){\n\tT tmp = a;\n\ta = b;\n\tb = tmp;\n}\n\nvoid main(){\n\tn.next;\n\td.next;\n\tint[] list = new int[n];\n\t\n\tfor(int i=0; i1){\n\t\t\t// rC_2 スタートを除いた範囲から重複を許さず、残り2個選ぶ組み合わせ\n\t\t\tcount += r*(r-1)/2;\n\t\t}\n\t}\n\t\n\tcount.writeln;\n}\n\nimport std.stdio : readln, chomp;\nimport std.conv : to;\nimport std.string : split;\nshared string[] input;\nshared string delim = \" \";\nT next(T)()\nin\n{\n\tassert(hasNext());\n}\nout\n{\n\tinput.popFront;\n}\nbody\n{\n\treturn input.front.to!T;\n}\n\nvoid next(T)(ref T v){\n\tv = next!T();\n}\n\nbool hasNext(){\n\tif(input.length > 0){\n\t\treturn true;\n\t}\n\t\n\tstring str = readln;\n\tif(str.length > 0){\n\t\tinput ~= str.chomp.split(delim);\n\t\treturn true;\n\t}else{\n\t\treturn false;\n\t}\n}\n\n\nvoid dbg(T...)(T vs)\n{\n\timport std.stdio : stderr;\n\tforeach(v; vs)\n\t\tstderr.write(v.to!string ~ \" \");\n\tstderr.write(\"\\n\");\n}\n\nT clone(T)(T v){\n\tT v_;\n\tstatic if(isInputRange!(T)){\n\t\tforeach(ite; v){\n\t\t\tv_ ~= ite.clone;\n\t\t}\n\t}else{\n\t\tv_ = v;\n\t}\n\t\n\treturn v_;\n}\n\n"}], "negative_code": [{"source_code": "import std.stdio : writeln, writefln;\nimport std.array;\nimport std.range;\nimport std.algorithm : filter, max, sort;\nimport std.math : sqrt;\n\nint n;\nint d;\n\nvoid swap(T)(ref T a, ref T b){\n\tT tmp = a;\n\ta = b;\n\tb = tmp;\n}\n\nvoid main(){\n\tn.next;\n\td.next;\n\tint[] list = new int[n];\n\t\n\tfor(int i=0; i 0){\n\t\treturn true;\n\t}\n\t\n\tstring str = readln;\n\tif(str.length > 0){\n\t\tinput ~= str.chomp.split(delim);\n\t\treturn true;\n\t}else{\n\t\treturn false;\n\t}\n}\n\n\nvoid dbg(T...)(T vs)\n{\n\timport std.stdio : stderr;\n\tforeach(v; vs)\n\t\tstderr.write(v.to!string ~ \" \");\n\tstderr.write(\"\\n\");\n}\n\nT clone(T)(T v){\n\tT v_;\n\tstatic if(isInputRange!(T)){\n\t\tforeach(ite; v){\n\t\t\tv_ ~= ite.clone;\n\t\t}\n\t}else{\n\t\tv_ = v;\n\t}\n\t\n\treturn v_;\n}\n\n"}, {"source_code": "import std.stdio : writeln, writefln;\nimport std.array;\nimport std.range;\nimport std.algorithm : filter, max, sort;\nimport std.math : sqrt;\n\nint n;\nint d;\n\nvoid swap(T)(ref T a, ref T b){\n\tT tmp = a;\n\ta = b;\n\tb = tmp;\n}\n\nvoid main(){\n\tn.next;\n\td.next;\n\tint[] list = new int[n];\n\t\n\tfor(int i=0; i1){\n\t\t\t// rC_2 スタートを除いた範囲から重複を許さず、残り2個選ぶ組み合わせ\n\t\t\tcount += r*(r-1)/2;\n\t\t}\n\t}\n\t\n\tcount.writeln;\n}\n\nimport std.stdio : readln, chomp;\nimport std.conv : to;\nimport std.string : split;\nshared string[] input;\nshared string delim = \" \";\nT next(T)()\nin\n{\n\tassert(hasNext());\n}\nout\n{\n\tinput.popFront;\n}\nbody\n{\n\treturn input.front.to!T;\n}\n\nvoid next(T)(ref T v){\n\tv = next!T();\n}\n\nbool hasNext(){\n\tif(input.length > 0){\n\t\treturn true;\n\t}\n\t\n\tstring str = readln;\n\tif(str.length > 0){\n\t\tinput ~= str.chomp.split(delim);\n\t\treturn true;\n\t}else{\n\t\treturn false;\n\t}\n}\n\n\nvoid dbg(T...)(T vs)\n{\n\timport std.stdio : stderr;\n\tforeach(v; vs)\n\t\tstderr.write(v.to!string ~ \" \");\n\tstderr.write(\"\\n\");\n}\n\nT clone(T)(T v){\n\tT v_;\n\tstatic if(isInputRange!(T)){\n\t\tforeach(ite; v){\n\t\t\tv_ ~= ite.clone;\n\t\t}\n\t}else{\n\t\tv_ = v;\n\t}\n\t\n\treturn v_;\n}\n\n"}], "src_uid": "1f6491999bec55cb8d960181e830f4c8"} {"nl": {"description": "Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's \"Black Square\", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.", "output_spec": "Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.", "sample_inputs": ["5 4\nWWWW\nWWWB\nWWWB\nWWBB\nWWWW", "1 2\nBB", "3 3\nWWW\nWWW\nWWW"], "sample_outputs": ["5", "-1", "1"], "notes": "NoteIn the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.In the third example all cells are colored white, so it's sufficient to color any cell black."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint rows, cols;\n\twhile (readf (\" %s %s\", &rows, &cols) > 0)\n\t{\n\t\treadln;\n\t\tstring [] s;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\ts ~= readln.strip;\n\t\t}\n\n\t\tint blacks = 0;\n\t\tint minRow = int.max;\n\t\tint maxRow = int.min;\n\t\tint minCol = int.max;\n\t\tint maxCol = int.min;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\tforeach (col; 0..cols)\n\t\t\t{\n\t\t\t\tif (s[row][col] == 'B')\n\t\t\t\t{\n\t\t\t\t\tblacks += 1;\n\t\t\t\t\tminRow = min (minRow, row);\n\t\t\t\t\tmaxRow = max (maxRow, row);\n\t\t\t\t\tminCol = min (minCol, col);\n\t\t\t\t\tmaxCol = max (maxCol, col);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint side = max (maxRow - minRow + 1, maxCol - minCol + 1);\n\t\tif (blacks == 0)\n\t\t{\n\t\t\twriteln (1);\n\t\t}\n\t\telse if (side > min (rows, cols))\n\t\t{\n\t\t\twriteln (-1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (side * side - blacks);\n\t\t}\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint rows, cols;\n\twhile (readf (\" %s %s\", &rows, &cols) > 0)\n\t{\n\t\treadln;\n\t\tstring [] s;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\ts ~= readln.strip;\n\t\t}\n\n\t\tint blacks = 0;\n\t\tint minRow = int.max;\n\t\tint maxRow = int.min;\n\t\tint minCol = int.max;\n\t\tint maxCol = int.min;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\tforeach (col; 0..cols)\n\t\t\t{\n\t\t\t\tif (s[row][col] == 'B')\n\t\t\t\t{\n\t\t\t\t\tblacks += 1;\n\t\t\t\t\tminRow = min (minRow, row);\n\t\t\t\t\tmaxRow = max (maxRow, row);\n\t\t\t\t\tminCol = min (minCol, col);\n\t\t\t\t\tmaxCol = max (maxCol, col);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint side = max (maxRow - minRow + 1, maxCol - minCol + 1);\n\t\tif (side > min (rows, cols))\n\t\t{\n\t\t\twriteln (-1);\n\t\t}\n\t\telse if (blacks == 0)\n\t\t{\n\t\t\twriteln (1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\twriteln (side * side - blacks);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint rows, cols;\n\twhile (readf (\" %s %s\", &rows, &cols) > 0)\n\t{\n\t\treadln;\n\t\tstring [] s;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\ts ~= readln.strip;\n\t\t}\n\n\t\tint blacks = 0;\n\t\tint minRow = int.max;\n\t\tint maxRow = int.min;\n\t\tint minCol = int.max;\n\t\tint maxCol = int.min;\n\t\tforeach (row; 0..rows)\n\t\t{\n\t\t\tforeach (col; 0..cols)\n\t\t\t{\n\t\t\t\tif (s[row][col] == 'B')\n\t\t\t\t{\n\t\t\t\t\tblacks += 1;\n\t\t\t\t\tminRow = min (minRow, row);\n\t\t\t\t\tmaxRow = max (maxRow, row);\n\t\t\t\t\tminCol = min (minCol, col);\n\t\t\t\t\tmaxCol = max (maxCol, col);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint side = max (maxRow - minRow + 1, maxCol - minCol + 1);\n\t\twriteln (side > min (rows, cols) ? -1 : side * side - blacks);\n\t}\n}\n"}], "src_uid": "cd6bc23ea61c43b38c537f9e04ad11a6"} {"nl": {"description": "Vasya’s elder brother Petya loves playing computer games. In one of his favourite computer games Petya reached the final level where a fight with the boss take place.While playing the game Petya found spell scrolls and now he is about to use them. Let’s describe the way fighting goes on this level:1) The boss has two parameters: max — the initial amount of health and reg — regeneration rate per second.2) Every scroll also has two parameters: powi — spell power measured in percents — the maximal amount of health counted off the initial one, which allows to use the scroll (i.e. if the boss has more than powi percent of health the scroll cannot be used); and dmgi the damage per second inflicted upon the boss if the scroll is used. As soon as a scroll is used it disappears and another spell is cast upon the boss that inflicts dmgi of damage per second upon him until the end of the game.During the battle the actions per second are performed in the following order: first the boss gets the damage from all the spells cast upon him, then he regenerates reg of health (at the same time he can’t have more than max of health), then the player may use another scroll (no more than one per second).The boss is considered to be defeated if at the end of a second he has nonpositive ( ≤ 0) amount of health.Help Petya to determine whether he can win with the set of scrolls available to him and if he can, determine the minimal number of seconds he needs to do it.", "input_spec": "The first line contains three integers N, max and reg (1 ≤ N, max, reg ≤ 1000) –– the amount of scrolls and the parameters of the boss. The next N lines contain two integers powi and dmgi each — the parameters of the i-th scroll (0 ≤ powi ≤ 100, 1 ≤ dmgi ≤ 2000). ", "output_spec": "In case Petya can’t complete this level, output in the single line NO. Otherwise, output on the first line YES. On the second line output the minimal time after which the boss can be defeated and the number of used scrolls. In the next lines for each used scroll output space-separated number of seconds passed from the start of the battle to the moment the scroll was used and the number of the scroll. Scrolls are numbered starting from 1 in the input order. The first scroll is considered to be available to be used after 0 seconds. Output scrolls in the order they were used. It is not allowed to use scrolls after the boss is defeated.", "sample_inputs": ["2 10 3\n100 3\n99 1", "2 100 10\n100 11\n90 9"], "sample_outputs": ["NO", "YES\n19 2\n0 1\n10 2"], "notes": null}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, mx, reg;\n readf(\"%s %s %s\", &n, &mx, ®);\n readln;\n \n struct scroll { int id, pw, dmg; }\n \n scroll[] scs;\n scs ~= scroll(0, 0, 0);\n foreach (i; 1 .. n+1) {\n int pw, dmg;\n readf(\"%s %s\", &pw, &dmg);\n readln;\n \n scs ~= scroll(i, pw, dmg);\n }\n \n auto used = new bool[] (n+1);\n Tuple!(int, int)[] ans;\n int cur = mx;\n int sec = 0;\n int sm = 0;\n for (; sec <= 2100; ++sec) {\n cur = min(cur - sm + reg, mx);\n if (cur <= 0) { break; }\n \n int best = 0;\n foreach (i, e; scs) {\n if (used[i]) { continue; }\n \n if (e.pw * mx >= cur * 100 && e.dmg > scs[best].dmg) {\n best = i.to!int;\n }\n }\n \n if (best != 0) {\n used[best] = true;\n ans ~= tuple(sec, best);\n sm += scs[best].dmg;\n }\n }\n \n if (sec > 2100) {\n writeln(\"NO\");\n return;\n }\n \n writeln(\"YES\");\n writeln(sec, ' ', ans.length);\n ans.each!(e => writeln(e[0], ' ', e[1]));\n}"}], "negative_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport core.checkedint;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.bigint;\nimport std.conv;\nimport std.datetime.stopwatch;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, mx, reg;\n readf(\"%s %s %s\", &n, &mx, ®);\n readln;\n \n struct scroll { int id, pw, dmg; }\n \n scroll[] scs;\n foreach (i; 1 .. n+1) {\n int pw, dmg;\n readf(\"%s %s\", &pw, &dmg);\n readln;\n \n scs ~= scroll(i, pw, dmg);\n }\n \n auto used = new bool[] (n+1);\n Tuple!(int, int)[] ans;\n int cur = mx;\n int sec = 0;\n int sm = 0;\n for (; sec <= 2100; ++sec) {\n cur = min(cur - sm + reg, mx);\n if (cur <= 0) { break; }\n \n foreach (i, e; scs) {\n if (used[i]) { continue; }\n \n if (e.pw * mx >= cur * 100) {\n used[i] = true;\n sm += e.dmg;\n ans ~= tuple(sec, e.id);\n }\n }\n }\n \n if (sec > 2100) {\n writeln(\"NO\");\n return;\n }\n \n writeln(\"YES\");\n writeln(sec, ' ', ans.length);\n ans.each!(e => writeln(e[0], ' ', e[1]));\n}"}], "src_uid": "e9c486e2d942700e0644dff29b6e3be6"} {"nl": {"description": "This is an interactive problem.Vasya and Petya are going to play the following game: Petya has some positive integer number $$$a$$$. After that Vasya should guess this number using the following questions. He can say a pair of non-negative integer numbers $$$(x, y)$$$. Petya will answer him: \"x\", if $$$(x \\bmod a) \\geq (y \\bmod a)$$$. \"y\", if $$$(x \\bmod a) < (y \\bmod a)$$$. We define $$$(x \\bmod a)$$$ as a remainder of division $$$x$$$ by $$$a$$$.Vasya should guess the number $$$a$$$ using no more, than 60 questions.It's guaranteed that Petya has a number, that satisfies the inequality $$$1 \\leq a \\leq 10^9$$$.Help Vasya playing this game and write a program, that will guess the number $$$a$$$.", "input_spec": null, "output_spec": null, "sample_inputs": ["start\nx\nx\nstart\nx\nx\ny\nstart\nx\nx\ny\ny\nend"], "sample_outputs": ["? 0 0\n? 10 1\n! 1\n? 0 0\n? 3 4\n? 2 5\n! 2\n? 2 4\n? 2 5\n? 3 10\n? 9 1\n! 3"], "notes": "NoteIn the first test, you should play $$$3$$$ games with Petya's numbers $$$1$$$, $$$2$$$ and $$$3$$$.In the first game, Petya will answer \"x\" (without quotes) to any question, because $$$(x \\bmod 1) = 0$$$ for any integer $$$x$$$. In the second game, if you will ask pair $$$(0, 0)$$$, the answer will be \"x\" (without quotes), because $$$(0 \\bmod 2) \\geq (0 \\bmod 2)$$$. But if you will ask pair $$$(2, 5)$$$, the answer will be \"y\" (without quotes), because $$$(2 \\bmod 2) < (5 \\bmod 2)$$$, because $$$(2 \\bmod 2) = 0$$$ and $$$(5 \\bmod 2) = 1$$$."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip;\n\nlong X;\nint cnt = 0;\n\nvoid ans(long x) {\n debug {\n writeln((x == X) ? \"OK \": \"NG\");\n writeln(cnt);\n return;\n }\n writeln(\"! \", x);\n stdout.flush;\n}\n\nbool ask(long x, long y) {\n debug {\n cnt += 1;\n return x % X >= y % X;\n }\n writeln(\"? \", x, \" \", y);\n stdout.flush;\n return readln.chomp == \"x\";\n}\n\nvoid solve() {\n if (ask(1, 2)) {\n if (ask(2, 1)) {\n ans(1);\n } else {\n ans(2);\n }\n return;\n }\n\n long mn = 2;\n long mx = 4;\n\n while (true) {\n if (ask(mn, mx)) {\n break;\n } else {\n mx *= 2;\n mn *= 2;\n }\n }\n\n while (mx - mn > 1) {\n long next_mn = (mx + mn) / 2;\n if (ask(next_mn, mx)) {\n mn = next_mn;\n } else {\n mx = next_mn;\n }\n }\n\n ans (mx);\n}\n\nvoid main() {\n debug {\n X = readln.chomp.to!int;\n solve;\n return;\n }\n while (true) {\n auto s = readln.chomp;\n if (s == \"start\") solve;\n else break;\n }\n}\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nbool ask (int x, int y)\n{\n\twriteln (\"?\", \" \", x, \" \", y);\n\tstdout.flush ();\n\tauto s = readln.strip;\n\tif (s == \"e\")\n\t{\n\t\tassert (false);\n\t}\n\treturn (s == \"x\");\n}\n\nvoid main ()\n{\n\tstring s;\n\twhile ((s = readln.strip) != \"\")\n\t{\n\t\tif (s == \"end\")\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\tif (s != \"start\")\n\t\t{\n\t\t\tassert (false);\n\t\t}\n\n\t\timmutable int start = 0;\n\t\tint x = start;\n\t\tint y = x;\n\t\tint d = 1;\n\t\tdo\n\t\t{\n\t\t\tx = y;\n\t\t\ty += d;\n\t\t\td *= 2;\n\t\t}\n\t\twhile (!ask (x, y));\n\n\t\tint lo = x + 1;\n\t\tint hi = y;\n\t\twhile (lo != hi)\n\t\t{\n\t\t\tauto me = (lo + hi) / 2;\n\t\t\tif (ask (x, me))\n\t\t\t{\n\t\t\t\thi = me;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlo = me + 1;\n\t\t\t}\n\t\t}\n\n\t\twriteln (\"!\", \" \", lo);\n\t\tstdout.flush ();\n\t}\n}\n"}], "negative_code": [], "src_uid": "eab8e5ac203d9f10c893ea35d249fe84"} {"nl": {"description": "The only difference between easy and hard versions is the constraints.Polycarp has to write a coursework. The coursework consists of $$$m$$$ pages.Polycarp also has $$$n$$$ cups of coffee. The coffee in the $$$i$$$-th cup Polycarp has $$$a_i$$$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days).Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least).Let's consider some day of Polycarp's work. Consider Polycarp drinks $$$k$$$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $$$a_{i_1}, a_{i_2}, \\dots, a_{i_k}$$$. Then the first cup he drinks gives him energy to write $$$a_{i_1}$$$ pages of coursework, the second cup gives him energy to write $$$max(0, a_{i_2} - 1)$$$ pages, the third cup gives him energy to write $$$max(0, a_{i_3} - 2)$$$ pages, ..., the $$$k$$$-th cup gives him energy to write $$$max(0, a_{i_k} - k + 1)$$$ pages.If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day.Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 10^9$$$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the caffeine dosage of coffee in the $$$i$$$-th cup.", "output_spec": "If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it.", "sample_inputs": ["5 8\n2 3 1 1 2", "7 10\n1 3 4 2 1 4 2", "5 15\n5 5 5 5 5", "5 16\n5 5 5 5 5", "5 26\n5 5 5 5 5"], "sample_outputs": ["4", "2", "1", "2", "-1"], "notes": "NoteIn the first example Polycarp can drink fourth cup during first day (and write $$$1$$$ page), first and second cups during second day (and write $$$2 + (3 - 1) = 4$$$ pages), fifth cup during the third day (and write $$$2$$$ pages) and third cup during the fourth day (and write $$$1$$$ page) so the answer is $$$4$$$. It is obvious that there is no way to write the coursework in three or less days.In the second example Polycarp can drink third, fourth and second cups during first day (and write $$$4 + (2 - 1) + (3 - 2) = 6$$$ pages) and sixth cup during second day (and write $$$4$$$ pages) so the answer is $$$2$$$. It is obvious that Polycarp cannot write the whole coursework in one day in this test.In the third example Polycarp can drink all cups of coffee during first day and write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$$$ pages of coursework.In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$$$ pages of coursework and during second day he will write $$$5$$$ pages of coursework. This is enough to complete it.In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1."}, "positive_code": [{"source_code": "import core.bitop, std.bitmanip;\nimport std.algorithm, std.functional;\nimport std.array, std.container;\nimport std.conv;\nimport std.math, std.numeric;\nimport std.range, std.range.interfaces;\nimport std.stdio, std.string;\nimport std.typecons;\n\nimmutable int INF = 10L ^^ 9 + 23;\n\nvoid main()\n{\n int n, m;\n readf(\"%s %s\", &n, &m);\n readln;\n \n auto arr = readln.chomp.split.map!(to!int).array;\n \n arr.sort().reverse();\n \n debug { arr.writeln; }\n \n bool isOk(int md) {\n long eng = 0;\n int sb = 0;\n foreach (i, e; arr) {\n if (i > 0 && i % md == 0) { sb += 1; }\n \n eng += max(e - sb, 0);\n }\n \n return eng >= m;\n }\n \n int le = 1, r = n+1;\n while (le < r) {\n int md = (le + r) / 2;\n if (isOk(md)) { r = md; }\n else { le = md + 1; }\n }\n \n writeln(le == n+1 ? -1 : le);\n}"}, {"source_code": "void main() {\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int n;\n long m;\n rd(n, m);\n auto a = readln.split.to!(long[]);\n\n sort!\"a>b\"(a);\n if (m > reduce!\"a+b\"(a)) {\n writeln(-1);\n return;\n }\n long _s = 0;\n foreach (int i; 0 .. n) {\n _s += max(0L, a[i] - i);\n }\n if (_s >= m) {\n writeln(1);\n return;\n }\n bool enough(long k) {\n long s = 0;\n foreach (i; 0 .. n) {\n s += max(0L, a[i] - (i / k));\n }\n return s >= m;\n }\n\n long ng = 1, ok = n;\n while (ok - ng > 1) {\n auto md = (ok + ng) / 2;\n if (enough(md)) {\n ok = md;\n } else {\n ng = md;\n }\n }\n writeln(ok);\n}\n\nvoid rd(T...)(ref T x) {\n import std.stdio : readln;\n import std.string : split;\n import std.conv : to;\n\n auto l = readln.split;\n assert(l.length == x.length);\n foreach (i, ref e; x)\n e = l[i].to!(typeof(e));\n}\n"}], "negative_code": [], "src_uid": "1b79ff21b5c1df4c54236071a585a52e"} {"nl": {"description": "You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj.Segment [l1, r1] lies within segment [l2, r2] iff l1 ≥ l2 and r1 ≤ r2.Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 3·105) — the number of segments. Each of the next n lines contains two integers li and ri (1 ≤ li ≤ ri ≤ 109) — the i-th segment.", "output_spec": "Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1.", "sample_inputs": ["5\n1 10\n2 9\n3 9\n2 3\n2 9", "3\n1 5\n2 6\n6 20"], "sample_outputs": ["2 1", "-1 -1"], "notes": "NoteIn the first example the following pairs are considered correct: (2, 1), (3, 1), (4, 1), (5, 1) — not even touching borders; (3, 2), (4, 2), (3, 5), (4, 5) — touch one border; (5, 2), (2, 5) — match exactly. "}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nimmutable long INF = 1L << 59;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto A = new Tuple!(int, int)[](N);\n foreach (i; 0..N) {\n auto s = readln.split.map!(to!int);\n A[i] = tuple(s[0], s[1]);\n }\n\n Tuple!(int, int)[][int] B;\n foreach (i, a; A.enumerate)\n B[a[0]] ~= tuple(a[1], (i+1).to!int);\n\n foreach (k; B.keys)\n B[k].sort();\n\n foreach (k; B.keys)\n if (B[k].length > 1) {\n writeln(B[k].front[1], \" \", B[k].back[1]);\n return;\n }\n\n auto K = B.keys.dup;\n K.sort!\"a > b\"();\n Tuple!(int, int) minv = B[K[0]].front;\n\n foreach (i; 1..K.length) {\n if (B[K[i]].back[0] >= minv[0]) {\n writeln(minv[1], \" \", B[K[i]].back[1]);\n return;\n }\n minv = min(minv, B[K[i]].front);\n }\n\n writeln(-1, \" \", -1);\n}\n"}, {"source_code": "import std.stdio, std.string;\nimport std.typecons: Tuple, tuple;\nimport std.algorithm;\n\nbool within(Tuple!(int, int, int) t0, Tuple!(int, int, int) t1)\n{\n if (t0[0] >= t1[0] && t0[1] <= t1[1])\n {\n return true;\n }\n return false;\n}\n\nvoid solve(Tuple!(int, int, int)[] s)\n{\n s.sort();\n foreach (idx; 0 .. s.length - 1)\n {\n if (within(s[idx], s[idx + 1]))\n {\n writeln(s[idx][2], \" \", s[idx + 1][2]);\n return;\n }\n if (within(s[idx + 1], s[idx]))\n {\n writeln(s[idx + 1][2], \" \", s[idx][2]);\n return;\n }\n }\n writeln(\"-1 -1\");\n}\n\nint main(string[] args)\n{\n int n;\n while (readf(\"%d\\n\", &n) == 1)\n {\n auto s = new Tuple!(int, int, int)[n];\n foreach (i; 0 .. n)\n {\n int l, r;\n readf(\"%d %d\\n\", &l, &r);\n s[i] = tuple(l, r, i + 1);\n }\n solve(s);\n }\n return 0;\n}"}, {"source_code": "import std.algorithm.sorting;\nimport std.conv;\nimport std.stdio;\nimport std.typecons;\n\nvoid main()\n{\n uint n;\n readf!\"%d\\n\"(n);\n\n Tuple!(uint, uint, uint)[] x;\n\n foreach (i; 0..n) {\n uint a, b;\n \n readf!\"%d %d\\n\"(a, b);\n\n x ~= tuple(a, b, i+1);\n }\n\n x.sort!((a, b) => (a[0] == b[0]) ? (a[1] > b[1]) : (a[0] < b[0]));\n\n // current max\n auto m = x[0][1];\n auto mi = x[0][2];\n\n foreach (i; 1..n) {\n if (x[i][0] <= m && x[i][1] <= m) {\n writefln(\"%d %d\", x[i][2], mi);\n return;\n }\n\n if (x[i][1] > m) {\n m = x[i][1];\n mi = x[i][2];\n }\n }\n writeln(\"-1 -1\");\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf (\"%s\", &n);\n readln;\n Tuple!(ulong, ulong)[] r;\n foreach (_; 0..n) {\n auto nrs = readln.splitter.map!(to!ulong).array;\n r ~= tuple(nrs[0], nrs[1]);\n }\n int[] b = new int[] (r.length);\n makeIndex!(q{a[0] != b[0] ? a[0] < b[0] : a[1] > b[1]})(r, b);\n \n debug { writeln (r); }\n debug { writeln (b); }\n \n int outer = cast(int) b.front;\n auto ans = tuple(-1, -1);\n foreach (p; b.dropOne()) {\n if (r[p][1] <= r[outer][1]) {\n ans = tuple(cast(int) p, outer);\n break;\n } else {\n outer = cast(int) p;\n }\n }\n \n if (ans[0] != -1) ans[0] += 1, ans[1] += 1;\n writeln (ans[0], ' ', ans[1]);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf (\"%s\", &n);\n readln;\n Tuple!(int, int)[] r;\n foreach (_; 0..n) {\n auto nrs = readln.splitter.map!(to!int).array;\n r ~= tuple(nrs[0], nrs[1]);\n }\n auto b = r.length.iota.array\n .multiSort!((x, y) => r[x][0] < r[y][0], (x,y) => r[x][1] > r[y][1]);\n \n debug { writeln (r); }\n debug { writeln (b); }\n \n int right = cast(int) b.front();\n auto ans = tuple(-1, -1);\n foreach (p; b.dropOne()) {\n debug { writeln(p); }\n if (r[p][1] <= r[right][1]) {\n ans = tuple(cast(int) p, right);\n break;\n } else {\n right = cast(int) p;\n }\n }\n \n if (ans[0] != -1) ans[0] += 1, ans[1] += 1;\n writeln (ans[0], ' ', ans[1]);\n}"}, {"source_code": "void main(){\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int n; scanf(\"%d\", &n);\n struct P{int l, r;}\n auto seq=new P[](0);\n foreach(i; 0..n){\n int a, b; scanf(\"%d %d\", &a, &b);\n seq~=P(a, b);\n }\n \n struct LRi{int l, r, id;}\n auto lri=new LRi[](0);\n foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);\n sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l(a.l==b.l ? a.r>b.r : a.l(a.l==b.l ? a.r>b.r : a.l b\"();\n Tuple!(int, int) minv = B[K[0]];\n\n foreach (i; 1..K.length) {\n if (B[K[i]][0] >= minv[0]) {\n writeln(minv[1], \" \", B[K[i]][1]);\n return;\n }\n minv = min(minv, B[K[i]]);\n }\n\n writeln(-1, \" \", -1);\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nimmutable long INF = 1L << 59;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto A = new Tuple!(int, int)[](N);\n foreach (i; 0..N) {\n auto s = readln.split.map!(to!int);\n A[i] = tuple(s[0], s[1]);\n }\n\n Tuple!(int, int)[][int] B;\n foreach (i, a; A.enumerate)\n B[a[0]] ~= tuple(a[1], (i+1).to!int);\n\n foreach (k; B.keys)\n B[k].sort();\n\n foreach (k; B.keys)\n foreach (i; 0..B[k].length.to!int-1)\n if (B[k][i][0] == B[k][i+1][0]) {\n writeln(B[k][i][1], \" \", B[k][i+1][1]);\n return;\n }\n\n auto K = B.keys.dup;\n K.sort!\"a > b\"();\n Tuple!(int, int) minv = B[K[0]].front;\n\n foreach (i; 1..K.length) {\n if (B[K[i]].back[0] >= minv[0]) {\n writeln(minv[1], \" \", B[K[i]].front[1]);\n return;\n }\n minv = min(minv, B[K[i]].back);\n }\n\n writeln(-1, \" \", -1);\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nimmutable long INF = 1L << 59;\n\nvoid main() {\n auto N = readln.chomp.to!int;\n auto A = new Tuple!(int, int)[](N);\n foreach (i; 0..N) {\n auto s = readln.split.map!(to!int);\n A[i] = tuple(s[0], s[1]);\n }\n\n Tuple!(int, int)[][int] B;\n foreach (i, a; A.enumerate)\n B[a[0]] ~= tuple(a[1], (i+1).to!int);\n\n foreach (k; B.keys)\n B[k].sort();\n\n foreach (k; B.keys)\n foreach (i; 0..B[k].length.to!int-1)\n if (B[k][i][0] == B[k][i+1][0]) {\n writeln(B[k][i][1], \" \", B[k][i+1][1]);\n return;\n }\n\n auto K = B.keys.dup;\n K.sort!\"a > b\"();\n Tuple!(int, int) minv = B[K[0]].front;\n\n foreach (i; 1..K.length) {\n if (B[K[i]].back[0] >= minv[0]) {\n writeln(minv[1], \" \", B[K[i]].back[1]);\n return;\n }\n minv = min(minv, B[K[i]].front);\n }\n\n writeln(-1, \" \", -1);\n}\n"}, {"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nimmutable long INF = 1L << 59;\n\nvoid main() {\n auto sc = new Scanner(stdin);\n\n int N; sc.read(N);\n auto A = new Tuple!(int, int)[](N);\n foreach (i; 0..N) {\n int a, b; sc.read(a); sc.read(b);\n A[i] = tuple(a-1, b-1);\n }\n\n Tuple!(int, int)[][int] B;\n foreach (i, a; A.enumerate)\n B[a[0]] ~= tuple(a[1], (i+1).to!int);\n\n foreach (k; B.keys)\n B[k].sort();\n\n foreach (k; B.keys)\n foreach (i; 0..B[k].length.to!int-1)\n if (B[k][i][0] == B[k][i+1][0]) {\n writeln(B[k][i][1], \" \", B[k][i+1][1]);\n return;\n }\n\n auto K = B.keys.dup;\n K.sort!\"a > b\"();\n Tuple!(int, int) minv = B[K[0]].front;\n\n foreach (i; 1..K.length) {\n if (B[K[i]].back[0] >= minv[0]) {\n writeln(minv[1], \" \", B[K[i]].back[1]);\n return;\n }\n minv = min(minv, B[K[i]].front);\n }\n\n writeln(-1, \" \", -1);\n}\n\nclass Scanner {\n import std.stdio : File;\n import std.conv : to;\n import std.range : front, popFront, array, ElementType;\n import std.array : split;\n import std.traits : isSomeChar, isStaticArray, isArray;\n import std.algorithm : map;\n File f;\n this(File f) {\n this.f = f;\n }\n char[512] lineBuf;\n char[] line;\n private bool succ() {\n import std.range.primitives : empty, front, popFront;\n import std.ascii : isWhite;\n while (true) {\n while (!line.empty && line.front.isWhite) {\n line.popFront;\n }\n if (!line.empty) break;\n if (f.eof) return false;\n line = lineBuf[];\n f.readln(line);\n }\n return true;\n }\n\n private bool readSingle(T)(ref T x) {\n import std.algorithm : findSplitBefore;\n import std.string : strip;\n import std.conv : parse;\n if (!succ()) return false;\n static if (isArray!T) {\n alias E = ElementType!T;\n static if (isSomeChar!E) {\n\n\n auto r = line.findSplitBefore(\" \");\n x = r[0].strip.dup;\n line = r[1];\n } else {\n auto buf = line.split.map!(to!E).array;\n static if (isStaticArray!T) {\n\n assert(buf.length == T.length);\n }\n x = buf;\n line.length = 0;\n }\n } else {\n x = line.parse!T;\n }\n return true;\n }\n int read(T, Args...)(ref T x, auto ref Args args) {\n if (!readSingle(x)) return 0;\n static if (args.length == 0) {\n return 1;\n } else {\n return 1 + read(args);\n }\n }\n}\n"}, {"source_code": "import std.algorithm.sorting;\nimport std.conv;\nimport std.stdio;\nimport std.typecons;\n\nvoid main()\n{\n uint n;\n readf!\"%d\\n\"(n);\n\n Tuple!(uint, uint)[] x;\n\n foreach (i; 0..n) {\n uint a, b;\n \n readf!\"%d %d\\n\"(a, b);\n\n x ~= tuple(a, b);\n }\n\n x.sort!((a, b) => (a[0] == b[0]) ? (b[0] > a[0]) : (a[0] < b[0]));\n\n // current max\n auto m = x[0][1];\n auto mi = 1;\n\n foreach (i; 1..n) {\n if (x[i][0] <= m && x[i][1] <= m) {\n writefln(\"%d %d\", i+1, mi);\n return;\n }\n\n if (x[i][1] > m) {\n m = x[i][1];\n mi = i+1;\n }\n }\n writeln(\"-1 -1\");\n}\n"}, {"source_code": "import std.algorithm.sorting;\nimport std.conv;\nimport std.stdio;\nimport std.typecons;\n\nvoid main()\n{\n uint n;\n readf!\"%d\\n\"(n);\n\n Tuple!(uint, uint, uint)[] x;\n\n foreach (i; 0..n) {\n uint a, b;\n \n readf!\"%d %d\\n\"(a, b);\n\n x ~= tuple(a, b, i+1);\n }\n\n x.sort!((a, b) => (a[0] == b[0]) ? (b[0] > a[0]) : (a[0] < b[0]));\n\n // current max\n auto m = x[0][1];\n auto mi = x[0][2];\n\n foreach (i; 1..n) {\n if (x[i][0] <= m && x[i][1] <= m) {\n writefln(\"%d %d\", x[i][2], mi);\n return;\n }\n\n if (x[i][1] > m) {\n m = x[i][1];\n mi = x[i][2];\n }\n }\n writeln(\"-1 -1\");\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf (\"%s\", &n);\n readln;\n Tuple!(int, int)[] a;\n foreach (_; 0..n) {\n auto nrs = readln.splitter.map!(to!int).array;\n a ~= tuple(nrs[0], nrs[1]);\n }\n \n debug { writeln (a); }\n \n a.multiSort!(q{ a[0] < b[0] }, q{ a[1] > b[1] });\n \n debug { writeln (a); }\n \n int right = 0;\n auto ans = tuple(-1, -1);\n foreach (int i, e; a.dropOne()) {\n debug { writeln(e); }\n if (e[1] <= a[right][1]) {\n ans = tuple(right, i);\n break;\n } else {\n right = i;\n }\n }\n \n writeln (ans[0], ' ', ans[1]);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf (\"%s\", &n);\n readln;\n Tuple!(int, int)[] r;\n foreach (_; 0..n) {\n auto nrs = readln.splitter.map!(to!int).array;\n r ~= tuple(nrs[0], nrs[1]);\n }\n auto b = r.length.iota.array;\n \n debug { writeln (r); }\n \n b.multiSort!((x, y) => r[x][0] < r[y][0], (x,y) => r[x][1] > r[y][1]);\n \n debug { writeln (b); }\n \n int right = 0;\n auto ans = tuple(-1, -1);\n foreach (p; b.dropOne()) {\n debug { writeln(p); }\n auto e = r[p];\n if (e[1] <= r[right][1]) {\n ans = tuple(cast(int) p, right);\n break;\n } else {\n right = cast(int) p;\n }\n }\n \n if (ans[0] != -1) ans[0] += 1, ans[1] += 1;\n writeln (ans[0], ' ', ans[1]);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n;\n readf (\"%s\", &n);\n readln;\n Tuple!(int, int)[] a;\n foreach (_; 0..n) {\n auto nrs = readln.splitter.map!(to!int).array;\n a ~= tuple(nrs[0], nrs[1]);\n }\n \n debug { writeln (a); }\n \n a.multiSort!(q{ a[0] < b[0] }, q{ a[1] > b[1] });\n \n debug { writeln (a); }\n \n int right = 0;\n auto ans = tuple(-1, -1);\n foreach (int i, e; a.dropOne().enumerate(1)) {\n debug { writeln(e); }\n if (e[1] <= a[right][1]) {\n ans = tuple(right, i);\n break;\n } else {\n right = i;\n }\n }\n \n if (ans[0] != -1) ans[0] += 1, ans[1] += 1;\n writeln (ans[0], ' ', ans[1]);\n}"}, {"source_code": "void main(){\n import std.stdio, std.string, std.conv, std.algorithm;\n\n int n; scanf(\"%d\", &n);\n struct P{int l, r;}\n auto seq=new P[](0);\n foreach(i; 0..n){\n int a, b; scanf(\"%d %d\", &a, &b);\n seq~=P(a, b);\n }\n \n struct LRi{int l, r, id;}\n auto lri=new LRi[](0);\n foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);\n sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l(a.l==b.l ? a.r>b.r : a.l(a.l==b.l ? a.r>b.r : a.l= w[i]) {\n int j = stack[--top];\n result -= cast(long) size[j] * w[j];\n size[i] += size[j], parent[j] = i;\n }\n stack[top++] = i, result += cast(long) size[i] * w[i];\n // for (i = 0; i < top; ++i) {\n // write(\"(\", w[stack[i]], \" \", size[stack[i]], \"),\");\n // }\n // writeln();\n return result;\n }\n\n int findRoot(int u) {\n if (parent[u] != u) {\n parent[u] = findRoot(parent[u]);\n }\n return parent[u];\n }\n\n byte[] s;\n uint[] w;\n int top = 0;\n ulong result = 0;\n int[] fail, nextDiff, stack, parent, size;\n}\n\nvoid main() {\n import std.bigint : BigInt;\n import std.conv : to;\n import std.string : chomp;\n\n const int q = stdin.readln.chomp.to!int;\n auto s = new Solver(q);\n BigInt ans = 0;\n for (int i = 0; i < q; ++i) {\n string line = stdin.readln;\n s.s[i] = cast(byte)((line[0] - 'a' + ans) % 26);\n s.w[i] = cast(uint)(line[2 .. $].chomp.to!uint ^ (ans & 0x3FFFFFFF));\n // writeln(\"append \", cast(char)('a' + s.s[i]), ' ', s.w[i]);\n ans += s.add(i);\n writeln(ans);\n }\n}\n"}], "negative_code": [{"source_code": "import std.stdio : stdin, writeln;\n\nstruct Solver {\n this(int n) {\n s = new byte[n], w = new uint[n], fail = new int[n], nextDiff = new int[n],\n stack = new int[n], parent = new int[n], size = new int[n];\n }\n\n ulong add(int i) {\n fail[i] = -1;\n if (i) {\n int j = i - 1;\n nextDiff[j] = ~fail[j] ? (s[fail[j] + 1] == s[i] ? nextDiff[fail[j]] : fail[j]) : -1;\n while (~j) {\n j = fail[j];\n if (s[j + 1] == s[i]) {\n fail[i] = j + 1;\n break;\n }\n }\n for (j = i - 1; ~j;) {\n if (s[j + 1] != s[i]) {\n int r = findRoot(i - j);\n result -= w[r], size[r]--;\n j = fail[j];\n }\n else {\n j = nextDiff[j];\n }\n }\n }\n parent[i] = i, size[i] = s[0] == s[i];\n while (top && w[stack[top - 1]] >= w[i]) {\n int j = stack[--top];\n result -= cast(long) size[j] * w[j];\n size[i] += size[j], parent[j] = i;\n }\n stack[top++] = i, result += cast(long) size[i] * w[i];\n return result;\n }\n\n int findRoot(int u) {\n if (parent[u] != u) {\n parent[u] = findRoot(parent[u]);\n }\n return parent[u];\n }\n\n byte[] s;\n uint[] w;\n int top = 0;\n ulong result = 0;\n int[] fail, nextDiff, stack, parent, size;\n}\n\nvoid main() {\n import std.bigint : BigInt;\n import std.conv : to;\n import std.string : chomp;\n\n const int q = stdin.readln.chomp.to!int;\n auto s = new Solver(q);\n BigInt ans = 0;\n for (int i = 0; i < q; ++i) {\n string line = stdin.readln;\n s.s[i] = cast(byte)((line[0] - 'a' + ans) % 26);\n s.w[i] = cast(uint)(line[2 .. $].chomp.to!uint ^ (ans & 0x3FFFFFFF));\n ans += s.add(i);\n writeln(ans);\n }\n}\n"}], "src_uid": "9b3a2ca67946780a36c66cb298da2cfb"} {"nl": {"description": "You are given an integer $$$n$$$. In one move, you can either multiply $$$n$$$ by two or divide $$$n$$$ by $$$6$$$ (if it is divisible by $$$6$$$ without the remainder).Your task is to find the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ or determine if it's impossible to do that.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$).", "output_spec": "For each test case, print the answer — the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ if it's possible to do that or -1 if it's impossible to obtain $$$1$$$ from $$$n$$$.", "sample_inputs": ["7\n1\n2\n3\n12\n12345\n15116544\n387420489"], "sample_outputs": ["0\n-1\n2\n-1\n-1\n12\n36"], "notes": "NoteConsider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer $$$15116544$$$: Divide by $$$6$$$ and get $$$2519424$$$; divide by $$$6$$$ and get $$$419904$$$; divide by $$$6$$$ and get $$$69984$$$; divide by $$$6$$$ and get $$$11664$$$; multiply by $$$2$$$ and get $$$23328$$$; divide by $$$6$$$ and get $$$3888$$$; divide by $$$6$$$ and get $$$648$$$; divide by $$$6$$$ and get $$$108$$$; multiply by $$$2$$$ and get $$$216$$$; divide by $$$6$$$ and get $$$36$$$; divide by $$$6$$$ and get $$$6$$$; divide by $$$6$$$ and get $$$1$$$. "}, "positive_code": [{"source_code": "import std.stdio;\n\nvoid main() {\n uint t, n, twos, threes;\n\n scanf(\"%d\", &t);\n while(t--) {\n int ans;\n scanf(\"%d\", &n);\n\n twos = 0;\n while(n % 2 ==0) {\n twos++;\n n /= 2;\n }\n\n threes = 0;\n while(n % 3 == 0) {\n threes++;\n n /= 3;\n }\n\n if(n != 1 || twos > threes) ans = -1;\n else if (twos == threes) ans = twos;\n else ans = twos + (threes - twos) * 2;\n\n\n printf(\"%d\\n\", ans);\n }\n\n\n}"}, {"source_code": "import std.stdio;\n\nvoid main()\n{\n int t;\n readf!\"%d\\n\"(t);\n\n foreach (i; 0..t) {\n long n;\n readf!\"%d\\n\"(n);\n\n auto s = 0;\n // remove as many factors of 6 as possible\n while (n % 6 == 0) {\n n /= 6;\n s += 1;\n }\n\n while (n % 3 == 0) {\n n /= 3;\n s += 2; // multiply, then divide\n }\n\n if (n == 1) {\n writeln(s);\n } else {\n writeln(-1);\n }\n }\n}"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nint solve (int n)\n{\n\tif (n == 1)\n\t{\n\t\treturn 0;\n\t}\n\telse if (n % 3 != 0)\n\t{\n\t\treturn -1;\n\t}\n\telse if (n % 2 != 0)\n\t{\n\t\tint res = solve (n * 2);\n\t\treturn (res == -1) ? -1 : res + 1;\n\t}\n\telse\n\t{\n\t\tint res = solve (n / 6);\n\t\treturn (res == -1) ? -1 : res + 1;\n\t}\n}\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\treadln.strip.to !(int).solve.writeln;\n\t}\n}\n"}], "negative_code": [], "src_uid": "3ae468c425c7b156983414372fd35ab8"} {"nl": {"description": "There is a frog staying to the left of the string $$$s = s_1 s_2 \\ldots s_n$$$ consisting of $$$n$$$ characters (to be more precise, the frog initially stays at the cell $$$0$$$). Each character of $$$s$$$ is either 'L' or 'R'. It means that if the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'L', the frog can jump only to the left. If the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell $$$0$$$.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the $$$n+1$$$-th cell. The frog chooses some positive integer value $$$d$$$ before the first jump (and cannot change it later) and jumps by no more than $$$d$$$ cells at once. I.e. if the $$$i$$$-th character is 'L' then the frog can jump to any cell in a range $$$[max(0, i - d); i - 1]$$$, and if the $$$i$$$-th character is 'R' then the frog can jump to any cell in a range $$$[i + 1; min(n + 1; i + d)]$$$.The frog doesn't want to jump far, so your task is to find the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it can jump by no more than $$$d$$$ cells at once. It is guaranteed that it is always possible to reach $$$n+1$$$ from $$$0$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The $$$i$$$-th test case is described as a string $$$s$$$ consisting of at least $$$1$$$ and at most $$$2 \\cdot 10^5$$$ characters 'L' and 'R'. It is guaranteed that the sum of lengths of strings over all test cases does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum |s| \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it jumps by no more than $$$d$$$ at once.", "sample_inputs": ["6\nLRLRRLL\nL\nLLR\nRRRR\nLLLLLL\nR"], "sample_outputs": ["3\n2\n3\n1\n7\n1"], "notes": "NoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$.In the third test case of the example, the frog can choose $$$d=3$$$, jump to the cell $$$3$$$ from the cell $$$0$$$ and then to the cell $$$4$$$ from the cell $$$3$$$.In the fourth test case of the example, the frog can choose $$$d=1$$$ and jump $$$5$$$ times to the right.In the fifth test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$.In the sixth test case of the example, the frog can choose $$$d=1$$$ and jump $$$2$$$ times to the right."}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;\n\nvoid main()\n{\n auto T = readln.chomp.to!int;\n foreach (_; 0..T) {\n auto S = readln.chomp;\n int max_l, l;\n foreach (c; S) {\n if (c == 'L') {\n l += 1;\n max_l = max(l, max_l);\n } else {\n l = 0;\n }\n }\n writeln(max_l + 1);\n }\n}"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }\n\nT binarySearch(alias pred, T)(T ok, T ng)\n{ \n\twhile (abs(ok-ng) > 1)\n\t{\n\t\tauto mid = (ok+ng)/2;\n\t\tif (unaryFun!pred(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\treturn ok;\n}\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto s = RD!string;\n\t\tauto n = cast(int)s.length;\n\t\tbool f(int d)\n\t\t{\n\t\t\tint[] open = [0, d-1];\n\t\t\tauto used = new bool[](n+1);\n\t\t\twhile (!open.empty)\n\t\t\t{\n\t\t\t\tauto pos = open.front; open.popFront;\n\t\t\t\tif (pos == n) return true;\n\t\t\t\tint nPos1, nPos2;\n\t\t\t\tif (s[pos] == 'L')\n\t\t\t\t{\n\t\t\t\t\tnPos1 = max(0, pos-1);\n\t\t\t\t\tnPos2 = max(0, pos-d);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tnPos1 = min(n, pos+1);\n\t\t\t\t\tnPos2 = min(n, pos+d);\n\t\t\t\t}\n\t\t\t\tif (used[nPos1] == false)\n\t\t\t\t{\n\t\t\t\t\topen ~= nPos1;\n\t\t\t\t\tused[nPos1] = true;\n\t\t\t\t}\n\t\t\t\tif (used[nPos2] == false)\n\t\t\t\t{\n\t\t\t\t\topen ~= nPos2;\n\t\t\t\t\tused[nPos2] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t\tans[ti] = binarySearch!(f)(n+1, 0);\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "6451507b21854d5b81aeaf0491ddf584"} {"nl": {"description": "The \"BerCorp\" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).", "input_spec": "The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.", "output_spec": "Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).", "sample_inputs": ["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"], "sample_outputs": ["0", "2", "1"], "notes": "NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.container;\nimport std.exception;\nimport std.math;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int MAX_N = 205;\n\nbool [MAX_N] [MAX_N] a;\nbool [MAX_N] b;\nint n, m;\n\nvoid recur (int v)\n{\n\tif (b[v])\n\t{\n\t\treturn;\n\t}\n\tb[v] = true;\n\tforeach (i; 0..n + m)\n\t{\n\t\tif (a[v][i])\n\t\t{\n\t\t\trecur (i);\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\twhile (readf (\" %s %s\", &n, &m) > 0)\n\t{\n\t\tforeach (i; 0..n + m)\n\t\t{\n\t\t\tforeach (j; 0..n + m)\n\t\t\t{\n\t\t\t\ta[i][j] = false;\n\t\t\t}\n\t\t}\n\t\tbool ok = false;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint k;\n\t\t\treadf (\" %s\", &k);\n\t\t\tforeach (j; 0..k)\n\t\t\t{\n\t\t\t\tint p;\n\t\t\t\treadf (\" %s\", &p);\n\t\t\t\tp--;\n\t\t\t\ta[i][n + p] = true;\n\t\t\t\ta[n + p][i] = true;\n\t\t\t\tok = true;\n\t\t\t}\n\t\t}\n\t\tforeach (i; 0..n + m)\n\t\t{\n\t\t\tb[i] = false;\n\t\t}\n\t\tint res = !ok;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (!b[i])\n\t\t\t{\n\t\t\t\trecur (i);\n\t\t\t\tres++;\n\t\t\t}\n\t\t}\n\t\tres--;\n\t\twritefln (\"%s\", res);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;\nimport std.uni, std.math, std.container, std.typecons, std.typetuple;\nimport core.bitop, std.datetime;\n\nimmutable int mod = 10^^9 + 7;\n\nvoid main(){\n int n, m;\n readVars(n, m);\n\n int z;\n auto uf = new UnionFind(m + 1);\n auto exist = new bool[](m + 1);\n\n foreach(i ; 0 .. n){\n auto line = readln.split.to!(int[]);\n if(line[0] == 0){\n z++;\n }\n else{\n foreach(j ; 1 .. line.length){\n exist[line[j]] = true;\n\n if(j > 1){\n uf.unite(line[j], line[j - 1]);\n }\n }\n }\n }\n\n if(z == n){\n writeln(n);\n return;\n }\n\n auto rbt = redBlackTree!int;\n\n foreach(i ; 1 .. m + 1){\n if (exist[i]) {\n rbt.insert(uf.find_root(i));\n }\n }\n\n auto ans = rbt.length.to!int + z - 1;\n\n writeln(ans);\n}\n\nclass UnionFind {\nprivate:\n int[] p;\n int[] rank;\n\npublic:\n this(int N){\n p = iota(N).array;\n rank = new int[](N);\n }\n\n int find_root(int x){\n if(x != p[x]){\n p[x] = find_root(p[x]);\n }\n\n return p[x];\n }\n\n bool same(int x, int y){\n return find_root(x) == find_root(y);\n }\n\n void unite(int x, int y){\n x = find_root(x);\n y = find_root(y);\n\n if(x == y) return;\n\n if(rank[x] < rank[y]){\n p[x] = y;\n }\n else {\n p[y] = x;\n\n if(rank[x] == rank[y]) rank[x]++;\n }\n }\n}\n\n\nvoid readVars(T...)(auto ref T args){\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}, {"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\nimport std.c.stdio;\n\nvoid main() {\n int n, m; readf(\"%d %d\\n\", &n, &m);\n bool[][] F = new bool[][n];\n foreach (ref L; F) L = new bool[m];\n for (int i = 0; i < n; i++) {\n int c; scanf(\"%d \", &c);\n for (int j = 0; j < c; j++) {\n int l; scanf(\"%d\", &l);\n l--;\n F[i][l] = true;\n }\n }\n bool[] U = new bool[n];\n int c = 0;\n void dfs(int v) {\n if (U[v]) return;\n U[v] = true;\n for (int i = 0; i < m; i++) {\n if (F[v][i]) {\n for (int j = 0; j < n; j++) {\n if (F[j][i]) {\n dfs(j);\n }\n }\n }\n }\n }\n for (int i = 0; i < n; i++) {\n if (U[i]) continue;\n c++;\n dfs(i);\n }\n int f = 0;\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++) \n if (F[i][j]) \n f = 1; \n writeln(c-f);\n}\n"}], "negative_code": [{"source_code": "import std.algorithm;\nimport std.container;\nimport std.exception;\nimport std.math;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int MAX_N = 205;\n\nbool [MAX_N] [MAX_N] a;\nbool [MAX_N] b;\nint n, m;\n\nvoid recur (int v)\n{\n\tif (b[v])\n\t{\n\t\treturn;\n\t}\n\tb[v] = true;\n\tforeach (i; 0..n + m)\n\t{\n\t\tif (a[v][i])\n\t\t{\n\t\t\trecur (i);\n\t\t}\n\t}\n}\n\nvoid main ()\n{\n\twhile (readf (\" %s %s\", &n, &m) > 0)\n\t{\n\t foreach (i; 0..n + m)\n\t {\n\t \tforeach (j; 0..n + m)\n\t \t{\n\t \t\ta[i][j] = false;\n\t \t}\n\t }\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint k;\n\t\t\treadf (\" %s\", &k);\n\t\t\tforeach (j; 0..k)\n\t\t\t{\n\t\t\t\tint p;\n\t\t\t\treadf (\" %s\", &p);\n\t\t\t\tp--;\n\t\t\t\ta[i][n + p] = true;\n\t\t\t\ta[n + p][i] = true;\n\t\t\t}\n\t\t}\n\t\tforeach (i; 0..n + m)\n\t\t{\n\t\t\tb[i] = false;\n\t\t}\n\t\tint res;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (!b[i])\n\t\t\t{\n\t\t\t\trecur (i);\n\t\t\t\tres++;\n\t\t\t}\n\t\t}\n\t\tres--;\n\t\twritefln (\"%s\", res);\n\t}\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;\nimport std.uni, std.math, std.container, std.typecons, std.typetuple;\nimport core.bitop, std.datetime;\n\nimmutable int mod = 10^^9 + 7;\n\nvoid main(){\n int n, m;\n readVars(n, m);\n\n int z;\n auto uf = new UnionFind(m + 1);\n auto exist = new bool[](m + 1);\n\n foreach(i ; 0 .. n){\n auto line = readln.split.to!(int[]);\n if(line[0] == 0){\n z++;\n }\n else{\n foreach(j ; 1 .. line.length){\n exist[line[j]] = true;\n\n if(j > 1){\n uf.unite(line[j], line[j - 1]);\n }\n }\n }\n }\n\n auto rbt = redBlackTree!int;\n\n foreach(i ; 1 .. m + 1){\n if (exist[i]) {\n rbt.insert(uf.find_root(i));\n }\n }\n\n auto ans = rbt.length.to!int + z - 1;\n\n writeln(ans);\n}\n\nclass UnionFind {\nprivate:\n int[] p;\n int[] rank;\n\npublic:\n this(int N){\n p = iota(N).array;\n rank = new int[](N);\n }\n\n int find_root(int x){\n if(x != p[x]){\n p[x] = find_root(p[x]);\n }\n\n return p[x];\n }\n\n bool same(int x, int y){\n return find_root(x) == find_root(y);\n }\n\n void unite(int x, int y){\n x = find_root(x);\n y = find_root(y);\n\n if(x == y) return;\n\n if(rank[x] < rank[y]){\n p[x] = y;\n }\n else {\n p[y] = x;\n\n if(rank[x] == rank[y]) rank[x]++;\n }\n }\n}\n\n\nvoid readVars(T...)(auto ref T args){\n auto line = readln.split;\n foreach(ref arg ; args){\n arg = line.front.to!(typeof(arg));\n line.popFront;\n }\n if(!line.empty){\n writeln(line);\n throw new Exception(\"args num < input num\");\n }\n}"}, {"source_code": "import std.stdio, std.string, std.algorithm;\nimport std.conv, std.array;\nimport std.container;\nimport std.typecons;\n\nvoid main() {\n int n, m; readf(\"%d %d\\n\", &n, &m);\n bool[][] F = new bool[][n];\n foreach (ref L; F) L = new bool[m];\n for (int i = 0; i < n; i++) {\n int c; readf(\"%d \", &c);\n int[] ls = stdin.readln.split.map!(function int(x){return x.to!int - 1;}).array;\n foreach (l; ls) {\n F[i][l] = true;\n }\n }\n bool[] U = new bool[n];\n int c = 0;\n void dfs(int v) {\n if (U[v]) return;\n U[v] = true;\n for (int i = 0; i < m; i++) {\n if (F[v][i]) {\n for (int j = 0; j < n; j++) {\n if (F[j][i]) {\n dfs(j);\n }\n }\n }\n }\n }\n for (int i = 0; i < n; i++) {\n if (U[i]) continue;\n c++;\n dfs(i);\n }\n writeln(c-1);\n}\n"}], "src_uid": "e2836276aee2459979b232e5b29e6d57"} {"nl": {"description": "Polycarp has $$$x$$$ of red and $$$y$$$ of blue candies. Using them, he wants to make gift sets. Each gift set contains either $$$a$$$ red candies and $$$b$$$ blue candies, or $$$a$$$ blue candies and $$$b$$$ red candies. Any candy can belong to at most one gift set.Help Polycarp to find the largest number of gift sets he can create.For example, if $$$x = 10$$$, $$$y = 12$$$, $$$a = 5$$$, and $$$b = 2$$$, then Polycarp can make three gift sets: In the first set there will be $$$5$$$ red candies and $$$2$$$ blue candies; In the second set there will be $$$5$$$ blue candies and $$$2$$$ red candies; In the third set will be $$$5$$$ blue candies and $$$2$$$ red candies. Note that in this example there is one red candy that Polycarp does not use in any gift set.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. Each test case consists of a single string containing four integers $$$x$$$, $$$y$$$, $$$a$$$, and $$$b$$$ ($$$1 \\le x, y, a, b \\le 10^9$$$).", "output_spec": "For each test case, output one number — the maximum number of gift sets that Polycarp can make.", "sample_inputs": ["9\n10 12 2 5\n1 1 2 2\n52 311 13 27\n1000000000 1000000000 1 1\n1000000000 1 1 1000000000\n1 1000000000 1000000000 1\n1 2 1 1\n7 8 1 2\n4 1 2 3"], "sample_outputs": ["3\n0\n4\n1000000000\n1\n1\n1\n5\n0"], "notes": null}, "positive_code": [{"source_code": "// code by cutx64\r\n\r\nmodule main;\r\n\r\nimport std.string, std.array, std.container, std.typecons;\r\nimport std.stdio, std.format, std.file;\r\nimport std.algorithm, std.conv, std.functional, std.range, core.bitop;\r\n// import std.bigint, std.complex, std.bitmanip;\r\n// import std.math, std.mathspecial, std.numeric;\r\n\r\nvoid main() {\r\n\r\n int tests;\r\n readf!\"%s\\n\"(tests);\r\n foreach(per_test; 0 .. tests) {\r\n long x, y, a, b;\r\n readf!\"%s %s %s %s\\n\"(x, y, a, b);\r\n if (a < b) swap(a, b);\r\n long low = 0, high = 10 ^^ 9, ans = 0;\r\n bool check(long v) {\r\n if (v * b > x) return false;\r\n long low = 0, high = v, ans = 0;\r\n while (low <= high) {\r\n long mid = (low + high) >> 1;\r\n if (mid * a + (v - mid) * b <= x) ans = mid, low = mid + 1;\r\n else high = mid - 1;\r\n }\r\n return ans * b + (v - ans) * a <= y;\r\n }\r\n while (low <= high) {\r\n long mid = (low + high) >> 1;\r\n if (check(mid)) low = mid + 1, ans = mid;\r\n else high = mid - 1;\r\n }\r\n ans.writeln;\r\n }\r\n\r\n} // main"}], "negative_code": [], "src_uid": "e6eb839ef4e688796050b34f1ca599a5"} {"nl": {"description": "You are given two integers $$$n$$$ and $$$m$$$ and an array $$$a$$$ of $$$n$$$ integers. For each $$$1 \\le i \\le n$$$ it holds that $$$1 \\le a_i \\le m$$$.Your task is to count the number of different arrays $$$b$$$ of length $$$n$$$ such that: $$$1 \\le b_i \\le m$$$ for each $$$1 \\le i \\le n$$$, and $$$\\gcd(b_1,b_2,b_3,...,b_i) = a_i$$$ for each $$$1 \\le i \\le n$$$. Here $$$\\gcd(a_1,a_2,\\dots,a_i)$$$ denotes the greatest common divisor (GCD) of integers $$$a_1,a_2,\\ldots,a_i$$$.Since this number can be too large, print it modulo $$$998\\,244\\,353$$$.", "input_spec": "Each test consist of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 10^9$$$) — the length of the array $$$a$$$ and the maximum possible value of the element. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le m$$$) — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ across all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the number of different arrays satisfying the conditions above. Since this number can be large, print it modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["5\n\n3 5\n\n4 2 1\n\n2 1\n\n1 1\n\n5 50\n\n2 3 5 2 3\n\n4 1000000000\n\n60 30 1 1\n\n2 1000000000\n\n1000000000 2"], "sample_outputs": ["3\n1\n0\n595458194\n200000000"], "notes": "NoteIn the first test case, the possible arrays $$$b$$$ are: $$$[4,2,1]$$$; $$$[4,2,3]$$$; $$$[4,2,5]$$$. In the second test case, the only array satisfying the demands is $$$[1,1]$$$.In the third test case, it can be proven no such array exists."}, "positive_code": [{"source_code": "import core.bitop;\r\nimport std.algorithm;\r\nimport std.conv;\r\nimport std.numeric;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int mod = 998_244_353;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tint res = 1;\r\n\t\tint cur = 0;\r\n\t\tforeach (ref next; a)\r\n\t\t{\r\n\t\t\tif (cur % next != 0)\r\n\t\t\t{\r\n\t\t\t\tres = 0;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\r\n\t\t\tint num;\r\n\t\t\tif (cur == 0)\r\n\t\t\t{\r\n\t\t\t\tnum = 1;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\tif (cur == next || cur == 0)\r\n\t\t\t{\r\n\t\t\t\tnum = m / next;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tint s = cur / next;\r\n\t\t\t\tint [] p;\r\n\t\t\t\tfor (int d = 2; d * d <= s; d++)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (s % d == 0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tp ~= d;\r\n\t\t\t\t\t\tdo\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\ts /= d;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\twhile (s % d == 0);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tif (s > 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tp ~= s;\r\n\t\t\t\t}\r\n\t\t\t\tdebug {writeln (\"p = \", p);}\r\n\r\n\t\t\t\tnum = 0;\r\n\t\t\t\tauto len = p.length.to !(int);\r\n\t\t\t\tforeach (u; 0..(1 << len))\r\n\t\t\t\t{\r\n\t\t\t\t\tint e = 1;\r\n\t\t\t\t\tforeach (i; 0..len)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (u & (1 << i))\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\te *= -p[i];\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\tnum += m / (next * e);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tdebug {writeln (\"num = \", num);}\r\n\t\t\tres = (res * 1L * num) % mod;\r\n\t\t\tcur = next;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "import core.bitop;\r\nimport std.algorithm;\r\nimport std.conv;\r\nimport std.numeric;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int mod = 998_244_353;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\r\n\t\tint res = 1;\r\n\t\tint cur = 0;\r\n\t\tforeach (ref next; a)\r\n\t\t{\r\n\t\t\tif (cur % next != 0)\r\n\t\t\t{\r\n\t\t\t\tres = 0;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\r\n\t\t\tint num;\r\n\t\t\tif (cur == 0)\r\n\t\t\t{\r\n\t\t\t\tnum = 1;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\tif (cur == next || cur == 0)\r\n\t\t\t{\r\n\t\t\t\tnum = m / next;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tint s = cur / next;\r\n\t\t\t\tint [] p;\r\n\t\t\t\tfor (int d = 2; d * d <= s; d++)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (s % d == 0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tp ~= d;\r\n\t\t\t\t\t\tdo\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\ts /= d;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\twhile (s % d == 0);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tif (s > 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tp ~= s;\r\n\t\t\t\t}\r\n\t\t\t\tdebug {writeln (\"p = \", p);}\r\n\r\n\t\t\t\tnum = 0;\r\n\t\t\t\tauto len = p.length.to !(int);\r\n\t\t\t\tforeach (u; 0..(1 << len))\r\n\t\t\t\t{\r\n\t\t\t\t\tint e = 1;\r\n\t\t\t\t\tforeach (i; 0..len)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (u & (1 << i))\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\te *= -p[i];\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\tnum += m / (next * e);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tdebug {writeln (\"num = \", num);}\r\n\t\t\tres = (res * 1L * num) % mod;\r\n\t\t\tcur = next;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "116dd636a4f2547aef01a68f98c46ca2"} {"nl": {"description": "Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.", "input_spec": "The first line of input contains the integer n (1 ≤ n ≤ 3·105) — the number of boxes. Each of the next 2n lines of input starts with a string \"add\" or \"remove\". If the line starts with the \"add\", an integer x (1 ≤ x ≤ n) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain \"add\" operations, all the boxes added are distinct, and n lines contain \"remove\" operations. It is also guaranteed that a box is always added before it is required to be removed.", "output_spec": "Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.", "sample_inputs": ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack."}, "positive_code": [{"source_code": "// tested by Hightail - https://github.com/dj3500/hightail\nimport std.stdio, std.string, std.conv, std.algorithm;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\n\nint n;\n\nvoid main() {\n scan(n);\n\n auto bh = new BinaryHeap!(Array!int, \"a > b\")();\n\n int ans;\n int nn = 1;\n int b = 0;\n\n foreach (i ; 0 .. 2*n) {\n auto line = readln.split;\n\n if (line[0] == \"add\") {\n int x = line[1].to!int;\n\n if (!bh.empty && x > bh.front) {\n b = bh.front;\n }\n\n bh.insert(x);\n }\n else {\n debug {\n writeln(\"next:\", nn);\n writeln(\"b:\", b);\n writeln(\"top:\", bh.front);\n writeln;\n }\n if (nn == b) {\n ans++;\n b = 0;\n }\n\n bh.popFront();\n nn++;\n }\n }\n\n writeln(ans);\n}\n\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}"}, {"source_code": "// tested by Hightail - https://github.com/dj3500/hightail\nimport std.stdio, std.string, std.conv, std.algorithm;\nimport std.range, std.array, std.math, std.typecons, std.container, core.bitop;\n\nimmutable inf = 10^^7;\nint n;\n\nvoid main() {\n scan(n);\n\n auto st = Stack!int(n);\n int ans, nn = 1;\n\n foreach (i ; 0 .. 2*n) {\n auto line = readln.split;\n\n if (line[0] == \"add\") {\n int x = line[1].to!int;\n st.push(x);\n }\n else {\n if (!st.empty && st.top != nn) {\n debug {\n writeln(\"nn:\", nn);\n }\n ans++;\n st.clear();\n }\n else if (!st.empty) {\n st.pop();\n }\n\n nn++;\n }\n }\n\n writeln(ans);\n}\n\n\nvoid scan(T...)(ref T args) {\n string[] line = readln.split;\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}\n\nstruct Stack(T) {\nprivate:\n int N, peek;\n T[] data;\n\npublic:\n this(int size) \n {\n N = size;\n data = new T[](N);\n }\n\n bool empty() @property\n {\n return peek == 0;\n }\n\n bool full() @property\n {\n return peek == N;\n }\n\n void push(T x) @property\n {\n assert(!full);\n data[peek++] = x;\n }\n\n void pop() @property\n {\n assert(!empty);\n --peek;\n }\n\n T top() @property\n {\n return data[peek - 1];\n }\n\n void clear() @property\n {\n peek = 0;\n }\n\n int length() @property\n {\n return peek;\n }\n\n}"}], "negative_code": [], "src_uid": "2535fc09ce74b829c26e1ebfc1ee17c6"} {"nl": {"description": "Omkar is standing at the foot of Celeste mountain. The summit is $$$n$$$ meters away from him, and he can see all of the mountains up to the summit, so for all $$$1 \\leq j \\leq n$$$ he knows that the height of the mountain at the point $$$j$$$ meters away from himself is $$$h_j$$$ meters. It turns out that for all $$$j$$$ satisfying $$$1 \\leq j \\leq n - 1$$$, $$$h_j < h_{j + 1}$$$ (meaning that heights are strictly increasing).Suddenly, a landslide occurs! While the landslide is occurring, the following occurs: every minute, if $$$h_j + 2 \\leq h_{j + 1}$$$, then one square meter of dirt will slide from position $$$j + 1$$$ to position $$$j$$$, so that $$$h_{j + 1}$$$ is decreased by $$$1$$$ and $$$h_j$$$ is increased by $$$1$$$. These changes occur simultaneously, so for example, if $$$h_j + 2 \\leq h_{j + 1}$$$ and $$$h_{j + 1} + 2 \\leq h_{j + 2}$$$ for some $$$j$$$, then $$$h_j$$$ will be increased by $$$1$$$, $$$h_{j + 2}$$$ will be decreased by $$$1$$$, and $$$h_{j + 1}$$$ will be both increased and decreased by $$$1$$$, meaning that in effect $$$h_{j + 1}$$$ is unchanged during that minute.The landslide ends when there is no $$$j$$$ such that $$$h_j + 2 \\leq h_{j + 1}$$$. Help Omkar figure out what the values of $$$h_1, \\dots, h_n$$$ will be after the landslide ends. It can be proven that under the given constraints, the landslide will always end in finitely many minutes.Note that because of the large amount of input, it is recommended that your code uses fast IO.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^6$$$). The second line contains $$$n$$$ integers $$$h_1, h_2, \\dots, h_n$$$ satisfying $$$0 \\leq h_1 < h_2 < \\dots < h_n \\leq 10^{12}$$$ — the heights.", "output_spec": "Output $$$n$$$ integers, where the $$$j$$$-th integer is the value of $$$h_j$$$ after the landslide has stopped.", "sample_inputs": ["4\n2 6 7 8"], "sample_outputs": ["5 5 6 7"], "notes": "NoteInitially, the mountain has heights $$$2, 6, 7, 8$$$.In the first minute, we have $$$2 + 2 \\leq 6$$$, so $$$2$$$ increases to $$$3$$$ and $$$6$$$ decreases to $$$5$$$, leaving $$$3, 5, 7, 8$$$.In the second minute, we have $$$3 + 2 \\leq 5$$$ and $$$5 + 2 \\leq 7$$$, so $$$3$$$ increases to $$$4$$$, $$$5$$$ is unchanged, and $$$7$$$ decreases to $$$6$$$, leaving $$$4, 5, 6, 8$$$.In the third minute, we have $$$6 + 2 \\leq 8$$$, so $$$6$$$ increases to $$$7$$$ and $$$8$$$ decreases to $$$7$$$, leaving $$$4, 5, 7, 7$$$.In the fourth minute, we have $$$5 + 2 \\leq 7$$$, so $$$5$$$ increases to $$$6$$$ and $$$7$$$ decreases to $$$6$$$, leaving $$$4, 6, 6, 7$$$.In the fifth minute, we have $$$4 + 2 \\leq 6$$$, so $$$4$$$ increases to $$$5$$$ and $$$6$$$ decreases to $$$5$$$, leaving $$$5, 5, 6, 7$$$.In the sixth minute, nothing else can change so the landslide stops and our answer is $$$5, 5, 6, 7$$$."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tint n;\n\twhile (readf !(\" %s\") (n) > 0)\n\t{\n\t\treadln;\n\t\tauto h = readln.splitter.map !(to !(long)).array;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\th[i] -= i;\n\t\t}\n\n\t\tlong total = h.sum;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint num = n - i;\n\t\t\tlong cur = (total + num - 1) / num;\n\t\t\tif (h[i] < cur)\n\t\t\t{\n\t\t\t\tlong delta = cur - h[i];\n\t\t\t\th[i] += delta;\n\t\t\t\th[i + 1] -= delta;\n\t\t\t}\n\t\t\ttotal -= h[i];\n\t\t}\n\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\th[i] += i;\n\t\t}\n\t\twritefln !(\"%(%s %)\") (h);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto H = new long[N];\n foreach (i; 0 .. N) {\n H[i] = readLong();\n }\n \n long val = H.sum;\n foreach (i; 0 .. N) {\n val -= i;\n }\n auto ans = new long[N];\n ans[] = val / N;\n ans[0 .. cast(int)(val % N)] += 1;\n foreach (i; 0 .. N) {\n ans[i] += i;\n }\n \n foreach (i; 0 .. N) {\n if (i > 0) write(\" \");\n write(ans[i]);\n }\n writeln;\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "612884cad3d52cc952f2b49674e70a08"} {"nl": {"description": "You are given four integer values $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$.Check if there exists a string that contains: $$$a$$$ letters 'A'; $$$b$$$ letters 'B'; $$$c$$$ letters 'C'; no other letters; exactly $$$m$$$ pairs of adjacent equal letters (exactly $$$m$$$ such positions $$$i$$$ that the $$$i$$$-th letter is equal to the $$$(i+1)$$$-th one). ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. Each of the next $$$t$$$ lines contains the description of the testcase — four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$ ($$$1 \\le a, b, c \\le 10^8$$$; $$$0 \\le m \\le 10^8$$$).", "output_spec": "For each testcase print \"YES\" if there exists a string that satisfies all the requirements. Print \"NO\" if there are no such strings. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).", "sample_inputs": ["3\n2 2 1 0\n1 1 1 1\n1 2 3 2"], "sample_outputs": ["YES\nNO\nYES"], "notes": "NoteIn the first testcase strings \"ABCAB\" or \"BCABA\" satisfy the requirements. There exist other possible strings.In the second testcase there's no way to put adjacent equal letters if there's no letter that appears at least twice.In the third testcase string \"CABBCC\" satisfies the requirements. There exist other possible strings."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.algorithm, std.array;\r\n\r\nvoid main()\r\n{\r\n int t;\r\n scanf(\"%d\", &t);\r\n getchar();\r\n foreach(i; 0..t){\r\n\t long a,b,c,m;\r\n\t scanf(\"%lld %lld %lld %lld\", &a, &b, &c, &m);\r\n\t auto top_max = a + b + c - 3;\r\n\t auto max_v = max(a,b,c);\r\n\t auto min_v = min(a,b,c);\r\n\t auto mid_v = a + b + c - max_v - min_v;\r\n\t if ((m > top_max) || (max_v > (min_v + mid_v + 1 + m)))\r\n\t\t writeln(\"NO\");\r\n\t else\r\n\t\t writeln(\"YES\");\r\n }\r\n}\r\n"}], "negative_code": [], "src_uid": "fc547fc83ebbcc3c058a069ef9fef62c"} {"nl": {"description": "Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.", "output_spec": "Print the maximum number of people that may come to Famil Door's party.", "sample_inputs": ["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"], "sample_outputs": ["2", "4"], "notes": "NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\n\nvoid main()\n{\n\tint n;\n\t\n\tscanf(\"%d\", &n);\n\t\n\tbool[] used = new bool[n];\n\tchar[] gender = new char[n];\n\tint[] from = new int[n];\n\tint[] to = new int[n];\n\t\n\tfor (int i = 0; i < n; i++) {\n\t\tchar[10] s;\n\t\tscanf(\"%s%d%d\", s.ptr, &from[i], &to[i]);\n\t\tgender[i] = s[0];\n\t}\n\t\n\tint res = 0;\n\t\n\tfor (int i = 1; i <= 366; i++) {\n\t\tchar[2] genders = ['F', 'M'];\n\t\tint[2] found = [0, 0];\n\t\tfor (int j = 0; j < 2; j++) {\n\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\tif (!used[k] && gender[k] == genders[j] && from[k] <= i && to[k] >= i) {\n\t\t\t\t\tfound[j]++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tres = max(res, min(found[0], found[1]));\n\t}\n\t\n\tprintf(\"%d\\n\", 2 * res);\n}\n"}, {"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.array: Appender;\nimport std.datetime;\nimport std.algorithm;\n\nvoid main()\n{\n\n\tauto n = to!int(strip(readln));\n\tint[] r = new int[377];\n\tint[] c = new int[377];\n\tfor (auto i = 0; i < n; i++)\n\t{\n\t\tauto str = split(strip(readln));\n\t\tforeach (num; to!int(str[1])..to!int(str[2])+1)\n\t\t{\n\t\t\tif (str[0] == \"F\")\n\t\t\t\tr[num]++;\n\t\t\telse\n\t\t\t\tc[num]++;\n\t\t}\n\t}\n\tint count;\n\tforeach (i, ele; r)\n\t{\n\t\tcount = max(min(r[i], c[i]), count);\n\t}\n\twriteln(2 * count);\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.array: Appender;\nimport std.datetime;\nimport std.algorithm;\n\nvoid main()\n{\n\n\tauto n = to!int(strip(readln));\n\tint[] r = new int[377];\n\tint[] c = new int[377];\n\tfor (auto i = 0; i < n; i++)\n\t{\n\t\tauto str = split(strip(readln));\n\t\tforeach (num; to!int(str[1])..to!int(str[2]))\n\t\t{\n\t\t\tif (str[0] == \"F\")\n\t\t\t\tr[num]++;\n\t\t\telse\n\t\t\t\tc[num]++;\n\t\t}\n\t}\n\tint count;\n\tforeach (i, ele; r)\n\t{\n\t\tcount = max(min(r[i], c[i]), count);\n\t}\n\n\twriteln(2 * count);\n}\n"}, {"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.array: Appender;\nimport std.datetime;\nimport std.algorithm;\n\nvoid main()\n{\n\n\tauto n = to!int(strip(readln));\n\tint[] r = new int[377];\n\tint[] c = new int[377];\n\tfor (auto i = 0; i < n; i++)\n\t{\n\t\tauto str = split(strip(readln));\n\t\tforeach (num; to!int(str[1])..to!int(str[2]))\n\t\t{\n\t\t\tif (str[0] == \"F\")\n\t\t\t\tr[num]++;\n\t\t\telse\n\t\t\t\tc[num]++;\n\t\t}\n\t}\n\tint count;\n\tforeach (i, ele; r)\n\t{\n\t\tcount = max(min(r[i], c[i]), count);\n\t}\n\n\twriteln(count);\n}\n"}, {"source_code": "import std.stdio;\nimport std.conv;\nimport std.string;\nimport std.array: Appender;\nimport std.datetime;\nimport std.algorithm;\n\nvoid main()\n{\n\n\tauto n = to!int(strip(readln));\n\tint[] r = new int[377];\n\tint[] c = new int[377];\n\tfor (auto i = 0; i < n; i++)\n\t{\n\t\tauto str = split(strip(readln));\n\t\tforeach (num; to!int(str[1])..to!int(str[2]))\n\t\t{\n\t\t\tif (str[0] == \"F\")\n\t\t\t\tr[num]++;\n\t\t\telse\n\t\t\t\tc[num]++;\n\t\t}\n\t}\n\tint count;\n\tforeach (i, ele; r)\n\t{\n\t\tcount = min(r[i], c[i]);\n\t}\n\n\twriteln(count);\n}\n"}], "src_uid": "75d500bad37fbd2c5456a942bde09cd3"} {"nl": {"description": "You are given a multiset (i. e. a set that can contain multiple equal integers) containing $$$2n$$$ integers. Determine if you can split it into exactly $$$n$$$ pairs (i. e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i. e. when divided by $$$2$$$, the remainder is $$$1$$$).", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\leq t\\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1\\leq n\\leq 100$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1,a_2,\\dots, a_{2n}$$$ ($$$0\\leq a_i\\leq 100$$$) — the numbers in the set.", "output_spec": "For each test case, print \"Yes\" if it can be split into exactly $$$n$$$ pairs so that the sum of the two elements in each pair is odd, and \"No\" otherwise. You can print each letter in any case.", "sample_inputs": ["5\n2\n2 3 4 5\n3\n2 3 4 5 5 5\n1\n2 4\n1\n2 3\n4\n1 5 3 2 6 7 3 4"], "sample_outputs": ["Yes\nNo\nNo\nYes\nNo"], "notes": "NoteIn the first test case, a possible way of splitting the set is $$$(2,3)$$$, $$$(4,5)$$$.In the second, third and fifth test case, we can prove that there isn't any possible way.In the fourth test case, a possible way of splitting the set is $$$(2,3)$$$."}, "positive_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1542/problem/A\n// math, implementation\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n\nwhile(t--) {\n int n = readln.chomp.to!int;\n int[] a = readln.split.map!(to!int).array;\n int even = 0;\n int odd = 0;\n foreach(item; a) {\n if(item % 2 == 0) {\n even += 1;\n } else {\n odd += 1;\n }\n }\n if(even == odd) {\n \"Yes\".writeln;\n } else {\n \"No\".writeln;\n }\n}\n}\n"}, {"source_code": "// code by cutx64\r\n\r\nmodule main;\r\n\r\nimport std.string, std.array, std.container, std.typecons;\r\nimport std.stdio, std.format, std.file, std.outbuffer;\r\nimport std.algorithm, std.conv, std.functional, std.range, core.bitop;\r\n// import std.bigint, std.complex, std.bitmanip;\r\n// import std.math, std.mathspecial, std.numeric;\r\n\r\nvoid main() {\r\n\r\n int tests; readf!\"%s\\n\"(tests);\r\n immutable(long) mod = 10 ^^ 9 + 7;\r\n while(tests--) {\r\n int n;\r\n readf!\"%s\\n\"(n);\r\n auto a = readln.splitter.map!(to!int).array;\r\n int odd = a.count!(x => x % 2 == 1);\r\n writeln([\"Yes\", \"No\"][odd != n]);\r\n }\r\n\r\n} // main\r\n"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\n\nvoid main()\n{\n long t;\n readf!\" %d \"(t);\n while (t--) {\n long n;\n readf!\" %d \"(n);\n auto a = readln.strip.split.map!(to!long).array;\n auto b = a.filter!(x => x % 2 == 0).array;\n auto c = a.filter!(x => x % 2 == 1).array;\n if (b.length == c.length) {\n writeln(\"Yes\");\n } else {\n writeln(\"No\");\n }\n }\n}\n"}, {"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\nvoid theCode(){\n ll n = scan;\n auto arr = scanArray;\n ll odd = 0;\n for(int i = 0; i < 2*n; ++i){\n if(arr[i] % 2 == 1){\n ++odd;\n }\n }\n if(odd == n){\n writeln(\"Yes\");\n }else{\n writeln(\"No\");\n }\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) theCode();\n}\n\n/********* That's All Folks! *********/\nimport std.stdio, std.random, std.functional, std.container, std.algorithm;\nimport std.numeric, std.range, std.typecons, std.string, std.math, std.conv;\nstring[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\nalias ll = long, tup = Tuple!(long, \"x\", long, \"y\");\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto a = RDA;\r\n\r\n\t\tlong cnt;\r\n\t\tforeach (e; a)\r\n\t\t{\r\n\t\t\tif (e % 2)\r\n\t\t\t\t++cnt;\r\n\t\t}\r\n\r\n\t\tans[ti] = cnt == n;\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e ? \"Yes\" : \"No\");\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [], "src_uid": "d5bd27c969d9cd910f13baa53c247871"} {"nl": {"description": "You are given strings $$$S$$$ and $$$T$$$, consisting of lowercase English letters. It is guaranteed that $$$T$$$ is a permutation of the string abc. Find string $$$S'$$$, the lexicographically smallest permutation of $$$S$$$ such that $$$T$$$ is not a subsequence of $$$S'$$$.String $$$a$$$ is a permutation of string $$$b$$$ if the number of occurrences of each distinct character is the same in both strings.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a string $$$S$$$ ($$$1 \\le |S| \\le 100$$$), consisting of lowercase English letters. The second line of each test case contains a string $$$T$$$ that is a permutation of the string abc. (Hence, $$$|T| = 3$$$). Note that there is no limit on the sum of $$$|S|$$$ across all test cases.", "output_spec": "For each test case, output a single string $$$S'$$$, the lexicographically smallest permutation of $$$S$$$ such that $$$T$$$ is not a subsequence of $$$S'$$$.", "sample_inputs": ["7\nabacaba\nabc\ncccba\nacb\ndbsic\nbac\nabracadabra\nabc\ndddddddddddd\ncba\nbbc\nabc\nac\nabc"], "sample_outputs": ["aaaacbb\nabccc\nbcdis\naaaaacbbdrr\ndddddddddddd\nbbc\nac"], "notes": "NoteIn the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence."}, "positive_code": [{"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n auto s = cast(byte[])readString;\n auto t = cast(byte[])readString;\n auto ca = s.count!(c => c == 'a');\n auto cb = s.count!(c => c == 'b');\n auto cc = s.count!(c => c == 'c');\n sort(s);\n if (t != \"abc\") return writeln(cast(string)s);\n if (ca > 0 && cb > 0 && cc > 0)\n {\n s = s[ca + cb + cc .. $];\n foreach(i; 0 .. ca) write('a');\n foreach(i; 0 .. cc) write('c');\n foreach(i; 0 .. cb) write('b');\n }\n writeln(cast(string)s);\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}, {"source_code": "import std;\n\nvoid\nprintSmallest (in int[dchar] frec) {\n foreach (dchar c; frec.keys.sort)\n write(c.repeat(frec[c]));\n writeln();\n}\n\nvoid main () {\n int tests;\n readf(\"%s\\n\", tests);\n foreach (i; 0 .. tests) {\n string s, t;\n s = readln()[0 .. $ - 1];\n t = readln()[0 .. $ - 1];\n\n int[dchar] frec;\n\n foreach (c; s)\n ++ frec[c];\n\n if (t == \"abc\" && 'a' in frec && 'b' in frec && 'c' in frec) {\n static foreach (c; \"acb\") {\n write(c.repeat(frec[c]));\n frec.remove(c);\n }\n }\n printSmallest(frec);\n }\n}\n\n//\"\"\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.utf;\r\n\r\nimmutable int limit = 128;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto s = readln.strip.dup;\r\n\t\tauto t = readln.strip;\r\n\t\tint [limit] num;\r\n\t\tforeach (ref c; s)\r\n\t\t{\r\n\t\t\tnum[c] += 1;\r\n\t\t}\r\n\t\tauto p = limit.iota.array;\r\n\t\tif (t == \"abc\" && num['a'] && num['b'] && num['c'])\r\n\t\t{\r\n\t\t\tswap (p['b'], p['c']);\r\n\t\t}\r\n\t\tforeach (ref c; p)\r\n\t\t{\r\n\t\t\tforeach (i; 0..num[c])\r\n\t\t\t{\r\n\t\t\t\twrite (cast (char) (c));\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln;\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new string[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto S = RD!string;\r\n\t\tauto T = RD!string;\r\n\r\n\t\tauto cnt = new long[](26);\r\n\t\tforeach (c; S)\r\n\t\t\t++cnt[c-'a'];\r\n\r\n\t\tif (T == \"abc\")\r\n\t\t{\r\n\t\t\tif (cnt[0] == 0 || cnt[1] == 0 || cnt[2] == 0)\r\n\t\t\t{\r\n\t\t\t\tforeach (i; 0..26)\r\n\t\t\t\t\tforeach (_; 0..cnt[i])\r\n\t\t\t\t\t\tans[ti] ~= cast(char)('a'+i);\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tforeach (_; 0..cnt[0])\r\n\t\t\t\t\tans[ti] ~= \"a\";\r\n\t\t\t\tforeach (_; 0..cnt[2])\r\n\t\t\t\t\tans[ti] ~= \"c\";\r\n\t\t\t\tforeach (_; 0..cnt[1])\r\n\t\t\t\t\tans[ti] ~= \"b\";\r\n\t\t\t\tforeach (i; 3..26)\r\n\t\t\t\t\tforeach (_; 0..cnt[i])\r\n\t\t\t\t\t\tans[ti] ~= cast(char)('a'+i);\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tforeach (i; 0..26)\r\n\t\t\t\tforeach (_; 0..cnt[i])\r\n\t\t\t\t\tans[ti] ~= cast(char)('a'+i);\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [{"source_code": "immutable multi = true;\n\nvoid solve(int tc)\n{\n auto s = cast(byte[])readString;\n auto t = cast(byte[])readString;\n sort(s);\n if (t != \"abc\") return writeln(cast(string)s);\n foreach(i, si; s)\n if (s[i .. min($, i + 3)] == \"abc\")\n {\n\tswap(s[i + 1], s[i + 2]);\n }\n writeln(cast(string)s);\n}\n\n// main {{{\nvoid main()\n{\n\tauto t = multi? readInt!int : 1; \n\tforeach(tc; 0 .. t) solve(tc);\n}\n// }}}\n// input {{{\nint currChar;\nstatic this()\n{\n\tcurrChar = ' ';\n}\n\nchar topChar()\n{\n\treturn cast(char) currChar;\n}\n\nvoid popChar()\n{\n\timport core.stdc.stdio;\n\tcurrChar = getchar();\n}\n\nauto readInt(T)()\n{\n\tT num = 0;\n\tint sgn = 0;\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (topChar == '-' || topChar == '+')\n\t{\n\t\tsgn ^= (topChar == '-');\n\t\tpopChar;\n\t}\n\twhile (topChar.isDigit)\n\t{\n\t\tnum *= 10;\n\t\tnum += (topChar - '0');\n\t\tpopChar;\n\t}\n\tif (sgn)\n\t\treturn -num;\n\treturn num;\n}\n\nstring readString()\n{\n\tstring res = \"\";\n\twhile (topChar.isWhite)\n\t\tpopChar;\n\twhile (!topChar.isWhite)\n\t{\n\t\tres ~= topChar;\n\t\tpopChar;\n\t}\n\treturn res;\n}\nvoid writes(T...)(T t)\n{\n\tstatic foreach(i, ti; t)\n\t{\n\t\tstatic if (isIterable!(T[i]))\n\t\t{\n\t\t\tforeach(e; ti)\n\t\t\t{\n\t\t\t\twrite(e, \" \");\n\t\t\t}\n\t\t}\n\t}\n}\nauto ra(V)()\n{\n\tint n = readInt!int;\n\treturn ma(n, readInt!V);\n}\nauto rt(V)()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\treturn ma(n, ma(m, readInt!V));\n}\nauto rbg()\n{\n\tint n = readInt!int;\n\tint m = readInt!int;\n\tauto adj = new int[][](n);\n\tforeach(i; 0 .. m)\n\t{\n\t\tauto u = readInt!int - 1;\n\t\tauto v = readInt!int - 1;\n\t\tadj[u] ~= v;\n\t\tadj[v] ~= u;\n\t}\n\treturn adj;\n}\nauto ma(V)(size_t n, lazy V v)\n{\n\tauto arr = new V[](n);\n\tforeach(ref ai; arr) ai = v;\n\treturn arr;\n}\n// }}}\n// imports {{{\nimport std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\nimport std.container;\nimport std.range;\nimport std.array;\nimport std.ascii;\n// }}}\n"}], "src_uid": "419ef3579fe142c295ec4d89ee7becfc"} {"nl": {"description": "Let $$$\\mathsf{AND}$$$ denote the bitwise AND operation, and $$$\\mathsf{OR}$$$ denote the bitwise OR operation.You are given an array $$$a$$$ of length $$$n$$$ and a non-negative integer $$$k$$$. You can perform at most $$$k$$$ operations on the array of the following type: Select an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and replace $$$a_i$$$ with $$$a_i$$$ $$$\\mathsf{OR}$$$ $$$2^j$$$ where $$$j$$$ is any integer between $$$0$$$ and $$$30$$$ inclusive. In other words, in an operation you can choose an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and set the $$$j$$$-th bit of $$$a_i$$$ to $$$1$$$ ($$$0 \\leq j \\leq 30$$$). Output the maximum possible value of $$$a_1$$$ $$$\\mathsf{AND}$$$ $$$a_2$$$ $$$\\mathsf{AND}$$$ $$$\\dots$$$ $$$\\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations. ", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le k \\le 10^9$$$). Then a single line follows, containing $$$n$$$ integers describing the arrays $$$a$$$ ($$$0 \\leq a_i < 2^{31}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single line containing the maximum possible $$$\\mathsf{AND}$$$ value of $$$a_1$$$ $$$\\mathsf{AND}$$$ $$$a_2$$$ $$$\\mathsf{AND}$$$ $$$\\dots$$$ $$$\\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations.", "sample_inputs": ["4\n3 2\n2 1 1\n7 0\n4 6 6 28 6 6 12\n1 30\n0\n4 4\n3 1 3 1"], "sample_outputs": ["2\n4\n2147483646\n1073741825"], "notes": "NoteFor the first test case, we can set the bit $$$1$$$ ($$$2^1$$$) of the last $$$2$$$ elements using the $$$2$$$ operations, thus obtaining the array [$$$2$$$, $$$3$$$, $$$3$$$], which has $$$\\mathsf{AND}$$$ value equal to $$$2$$$.For the second test case, we can't perform any operations so the answer is just the $$$\\mathsf{AND}$$$ of the whole array which is $$$4$$$."}, "positive_code": [{"source_code": "import std;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, k;\r\n\t\treadf !(\" %s %s\") (n, k);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tint res = 0;\r\n\t\tforeach_reverse (j; 0..31)\r\n\t\t{\r\n\t\t\tauto num = a.count !(x => (x & (1 << j)) == 0);\r\n\t\t\tif (k >= num)\r\n\t\t\t{\r\n\t\t\t\tres += (1 << j);\r\n\t\t\t\tk -= num;\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "0751386917a5750695312773005684ec"} {"nl": {"description": "You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset. ", "input_spec": "First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers. Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.", "output_spec": "If it is not possible to select k numbers in the desired way, output «No» (without the quotes). Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them. ", "sample_inputs": ["3 2 3\n1 8 4", "3 3 3\n1 8 4", "4 3 5\n2 7 7 7"], "sample_outputs": ["Yes\n1 4", "No", "Yes\n2 7 7"], "notes": null}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n const K = readInt();\n const M = readInt();\n auto A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n \n auto iss = new int[][M];\n foreach (i; 0 .. N) {\n iss[A[i] % M] ~= i;\n }\n \n foreach (r; 0 .. M) {\n if (iss[r].length >= K) {\n writeln(\"Yes\");\n foreach (k; 0 .. K) {\n if (k > 0) write(\" \");\n write(A[iss[r][k]]);\n }\n writeln;\n goto found;\n }\n }\n writeln(\"No\");\n found:\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "55bd1849ef13b52788a0b5685c4fcdac"} {"nl": {"description": "Genos needs your help. He was asked to solve the following programming problem by Saitama:The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string \"0011\" and string \"0110\" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.", "input_spec": "The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000). The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000). Both strings are guaranteed to consist of characters '0' and '1' only.", "output_spec": "Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.", "sample_inputs": ["01\n00111", "0011\n0110"], "sample_outputs": ["3", "2"], "notes": "NoteFor the first sample case, there are four contiguous substrings of b of length |a|: \"00\", \"01\", \"11\", and \"11\". The distance between \"01\" and \"00\" is |0 - 0| + |1 - 0| = 1. The distance between \"01\" and \"01\" is |0 - 0| + |1 - 1| = 0. The distance between \"01\" and \"11\" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string \"11\". The sum of these edit distances is 1 + 0 + 1 + 1 = 3.The second sample case is described in the statement."}, "positive_code": [{"source_code": "import std.stdio;\n\nint main() {\n debug stdin = File(\"in.txt\", \"r\");\n\n string a, b;\n readf(\" %s\\n\", &a);\n readf(\" %s\\n\", &b);\n int zcnt = 0;\n long answer = 0;\n int frontidx = 0, backidx = b.length - a.length, seglen = backidx + 1;\n foreach (i; 0 .. seglen) {\n if (b[i] == '1') ++zcnt;\n }\n foreach (c; a) {\n if (c == '0') answer += zcnt;\n else answer += seglen - zcnt;\n if (++backidx < b.length && b[backidx] == '1') ++zcnt;\n if (b[frontidx++] == '1') --zcnt;\n }\n writeln(answer);\n \n return 0;\n}"}], "negative_code": [], "src_uid": "ed75bd272f6d3050426548435423ca92"} {"nl": {"description": "Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!A password is an array $$$a$$$ of $$$n$$$ positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index $$$i$$$ such that $$$1 \\leq i < n$$$ and $$$a_{i} \\neq a_{i+1}$$$, delete both $$$a_i$$$ and $$$a_{i+1}$$$ from the array and put $$$a_{i}+a_{i+1}$$$ in their place. For example, for array $$$[7, 4, 3, 7]$$$ you can choose $$$i = 2$$$ and the array will become $$$[7, 4+3, 7] = [7, 7, 7]$$$. Note that in this array you can't apply this operation anymore.Notice that one operation will decrease the size of the password by $$$1$$$. What is the shortest possible length of the password after some number (possibly $$$0$$$) of operations?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the password. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},\\dots,a_{n}$$$ ($$$1 \\leq a_{i} \\leq 10^9$$$) — the initial contents of your password. The sum of $$$n$$$ over all test cases will not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each password, print one integer: the shortest possible length of the password after some number of operations.", "sample_inputs": ["2\n4\n2 1 3 1\n2\n420 420"], "sample_outputs": ["1\n2"], "notes": "NoteIn the first test case, you can do the following to achieve a length of $$$1$$$:Pick $$$i=2$$$ to get $$$[2, 4, 1]$$$Pick $$$i=1$$$ to get $$$[6, 1]$$$Pick $$$i=1$$$ to get $$$[7]$$$In the second test case, you can't perform any operations because there is no valid $$$i$$$ that satisfies the requirements mentioned above."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\tauto n = readln.strip.to !(int);\n\t\tauto a = readln.splitter.map !(to !(int)).array;\n\t\twriteln (a.minElement == a.maxElement ? n : 1);\n\t}\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const N = readInt();\n auto A = new long[N];\n foreach (i; 0 .. N) {\n A[i] = readLong();\n }\n \n const ans = A.all!(a => (a == A[0])) ? N : 1;\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nalias type = multi;\nstruct TestCase\n{\n mixin prettyPrinting;\n\n long n;\n @Dim(\"n\") long[] a;\n\n void solve(long tc = -1)\n {\n auto set = redBlackTree!long(a);\n if (set.length > 1)\n return writeln(1);\n return writeln(a.length);\n }\n}\n\nstruct Interval\n{\n long l, h;\n\n bool empty()\n {\n return l > h;\n }\n}\n\nInterval intersect(Interval a, Interval b)\n{\n return Interval(max(a.l, b.l), min(a.h, b.h));\n}\n\nstruct If\n{\n string condition;\n}\nenum ignore = If(\"false\");\nstruct Dim\n{\n string dimensions;\n int levels;\n this(string dimensions)\n {\n this.dimensions = dimensions;\n levels = cast(int)dimensions.count(\",\") + 1;\n }\n}\n\ntemplate getArrayLevel(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));\n else\n enum getArrayLevel = 0;\n}\n\nstatic foreach(fieldName; 'a' .. cast(char)('z' + 1))\n {\n static if (fieldName != 'o')\n mixin(\"enum d\", fieldName, \" = Dim(\\\"\", fieldName, \"\\\");\");\n }\n\n\nmixin template prettyPrinting()\n{\n string toString()\n {\n alias structType = typeof(this);\n auto res = appender!(string)();\n res.put(structType.stringof);\n res.put(\"(\");\n static foreach(fieldName; FieldNameTuple!structType)\n {\n res.put(text(\" \", fieldName, \": \", mixin(fieldName)));\n }\n res.put(\" )\");\n res.put(\"\\n\");\n return res[];\n }\n}\n\nstruct Graph(N)\n{\n size_t size;\n this(S)(S size)\n {\n adjacencyList.length = cast(size_t) size;\n this.size = cast(size_t) size;\n this.visited = makeSlice!bool(size);\n }\n N[][] adjacencyList;\n alias ne = neighbours;\n N[] neighbours(N n)\n {\n return adjacencyList.at(n);\n }\n alias bcon = biconnect;\n void biconnect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n adjacencyList.at(b) ~= a;\n }\n alias con = connect;\n void connect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n }\n void connect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n connect(edge[0], edge[1]);\n }\n }\n void biconnect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n biconnect(edge[0], edge[1]);\n }\n }\n alias deg = degree!long;\n auto degree(T)(N a)\n {\n return cast(T) adjacencyList.at(a).length;\n }\n\n auto visited = new bool[0];\n void dfs(Visitor)(long startNode)\n {\n auto visitor = Visitor.init;\n void internalDfs(long node)\n {\n visited.set(node, true);\n static if (is(typeof({visitor.n = node;})))\n visitor.n = node;\n static if (is(typeof({visitor.pre;})))\n visitor.pre;\n foreach(w; ne(node))\n if (!visited.at(w))\n {\n static if (is(typeof({visitor.w = w;})))\n visitor.w = w;\n static if (is(typeof(visitor.pren)))\n visitor.pren;\n internalDfs(w);\n static if (is(typeof(visitor.postn)))\n visitor.postn;\n }\n static if (is(typeof(visitor.post)))\n visitor.post;\n }\n internalDfs(startNode);\n }\n void dfs(long startNode)\n {\n struct DefVisitor {}\n dfs!DefVisitor(startNode);\n }\n}\n\nDList!string _words;\n\ntemplate removeArray(W, int times)\n{\n static if (times == 0)\n alias removeArray = W;\n else static if (times == 1)\n alias removeArray = typeof(function(){W w; return w[0];}());\n else\n {\n static assert(isDynamicArray!W);\n alias removeArray = removeArray!(removeArray!(W, 1), times - 1);\n }\n}\n\ntemplate baseType(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));\n else\n alias baseType = W;\n}\n\nauto makeSlice(E, W, T...)(W size, T otherSizes)\n{\n static if (T.length == 0)\n {\n E[] res;\n res.length = cast(size_t) size;\n return res;\n }\n else\n {\n typeof(makeSlice!E(otherSizes))[] res;\n res.length = cast(size_t) size;\n foreach(ref row; res)\n row = makeSlice!E(otherSizes);\n return res;\n }\n}\n\nvoid read(T...)(ref T t)\n{\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, If))\n enum condition = getUDAs!(fieldSymbol, If)[0].condition;\n else\n enum condition = \"true\";\n\n if (mixin(condition))\n {\n static if (hasUDA!(fieldSymbol, Dim))\n {\n static assert(isDynamicArray!(typeOfField)\n , text(W.stringof, \".\", fieldName, \" has Dim UDA but isn't a Dynamic Array.\"));\n enum uda = getUDAs!(fieldSymbol, Dim)[0];\n static assert(getArrayLevel!(typeOfField) == uda.levels,\n text(W.stringof, \".\", fieldName, \" has a Dim UDA with different number of dimensions than it's type.\"));\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", W.stringof, \".\", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new int[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto a = RDA!int;\n\n\t\tint x = a[0];\n\t\tbool ok;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tif (a[i] != x)\n\t\t\t{\n\t\t\t\tok = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tans[ti] = ok ? 1 : n;\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "7b80d3f3cd4f93e4277c76c76dc41f42"} {"nl": {"description": "This is an easier version of the problem. In this version $$$n \\le 1000$$$The outskirts of the capital are being actively built up in Berland. The company \"Kernel Panic\" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $$$n$$$ plots along the highway and is preparing to build $$$n$$$ skyscrapers, one skyscraper per plot.Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.Formally, let's number the plots from $$$1$$$ to $$$n$$$. Then if the skyscraper on the $$$i$$$-th plot has $$$a_i$$$ floors, it must hold that $$$a_i$$$ is at most $$$m_i$$$ ($$$1 \\le a_i \\le m_i$$$). Also there mustn't be integers $$$j$$$ and $$$k$$$ such that $$$j < i < k$$$ and $$$a_j > a_i < a_k$$$. Plots $$$j$$$ and $$$k$$$ are not required to be adjacent to $$$i$$$.The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 1000$$$) — the number of plots. The second line contains the integers $$$m_1, m_2, \\ldots, m_n$$$ ($$$1 \\leq m_i \\leq 10^9$$$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.", "output_spec": "Print $$$n$$$ integers $$$a_i$$$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them.", "sample_inputs": ["5\n1 2 3 2 1", "3\n10 6 8"], "sample_outputs": ["1 2 3 2 1", "10 6 6"], "notes": "NoteIn the first example, you can build all skyscrapers with the highest possible height.In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $$$[10, 6, 6]$$$ is optimal. Note that the answer of $$$[6, 6, 8]$$$ also satisfies all restrictions, but is not optimal."}, "positive_code": [{"source_code": "import std.stdio, std.string, std.conv, std.range;\nimport std.algorithm, std.array, std.typecons, std.container;\nimport std.math, std.numeric, std.random, core.bitop;\n\nenum inf = 1_001_001_001;\nenum infl = 1_001_001_001_001_001_001L;\n\n\nvoid main() {\n int N;\n scan(N);\n auto m = readln.split.to!(int[]);\n\n long ans;\n int k = -1;\n\n foreach (top ; 0 .. N) {\n long res = m[top];\n long l = m[top], r = m[top];\n long ls, rs;\n\n foreach_reverse (i ; 0 .. top) {\n chmin(l, m[i]);\n ls += l;\n }\n\n foreach (i ; top + 1 .. N) {\n chmin(r, m[i]);\n rs += r;\n }\n\n res += ls + rs;\n if (res > ans) {\n ans = res;\n k = top;\n }\n }\n\n auto a = new int[](N);\n a[k] = m[k];\n int l = m[k], r = m[k];\n foreach_reverse (i ; 0 .. k) {\n chmin(l, m[i]);\n a[i] = l;\n }\n foreach (i ; k + 1 .. N) {\n chmin(r, m[i]);\n a[i] = r;\n }\n\n writefln(\"%(%s %)\", a);\n}\n\n\n\n\n\n\n\n\n\n\n\n\nvoid scan(T...)(ref T args) {\n import std.stdio : readln;\n import std.algorithm : splitter;\n import std.conv : to;\n import std.range.primitives;\n\n auto line = readln().splitter();\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}\n\nbool chmin(T, U...)(ref T x, U args) {\n bool isChanged;\n\n foreach (arg; args) {\n if (x > arg) {\n x = arg;\n isChanged = true;\n }\n }\n\n return isChanged;\n}\n\nbool chmax(T, U...)(ref T x, U args) {\n bool isChanged;\n\n foreach (arg; args) {\n if (x < arg) {\n x = arg;\n isChanged = true;\n }\n }\n\n return isChanged;\n}\n"}], "negative_code": [{"source_code": "import std.stdio, std.string, std.conv, std.range;\nimport std.algorithm, std.array, std.typecons, std.container;\nimport std.math, std.numeric, std.random, core.bitop;\n\nenum inf = 1_001_001_001;\nenum infl = 1_001_001_001_001_001_001L;\n\n\nvoid main() {\n int N;\n scan(N);\n auto m = readln.split.to!(int[]);\n\n long mn = inf;\n\n foreach (i ; 0 .. N) {\n chmin(mn, m[i]);\n }\n\n long ps;\n foreach (i ; 0 .. N - 1) {\n ps += m[i];\n }\n\n long ss;\n foreach_reverse (i ; 1 .. N) {\n ss += m[i];\n }\n\n auto f = mn + ss;\n auto g = mn + ps;\n\n auto ans = new long[](N);\n\n if (f > g) {\n ans[0] = mn;\n foreach (i ; 1 .. N) {\n ans[i] = m[i];\n }\n }\n else {\n ans[N - 1] = mn;\n foreach (i ; 0 .. N - 1) {\n ans[i] = m[i];\n }\n }\n\n writefln(\"%(%s %)\", ans);\n}\n\n\n\n\n\n\n\n\n\n\n\n\nvoid scan(T...)(ref T args) {\n import std.stdio : readln;\n import std.algorithm : splitter;\n import std.conv : to;\n import std.range.primitives;\n\n auto line = readln().splitter();\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}\n\nbool chmin(T, U...)(ref T x, U args) {\n bool isChanged;\n\n foreach (arg; args) {\n if (x > arg) {\n x = arg;\n isChanged = true;\n }\n }\n\n return isChanged;\n}\n\nbool chmax(T, U...)(ref T x, U args) {\n bool isChanged;\n\n foreach (arg; args) {\n if (x < arg) {\n x = arg;\n isChanged = true;\n }\n }\n\n return isChanged;\n}\n"}, {"source_code": "import std.stdio, std.string, std.conv, std.range;\nimport std.algorithm, std.array, std.typecons, std.container;\nimport std.math, std.numeric, std.random, core.bitop;\n\nenum inf = 1_001_001_001;\nenum infl = 1_001_001_001_001_001_001L;\n\n\nvoid main() {\n int N;\n scan(N);\n auto m = readln.split.to!(int[]);\n\n long pm = inf;\n long ps;\n foreach (i ; 0 .. N - 1) {\n chmin(pm, m[i]);\n ps += m[i];\n }\n\n long sm = inf;\n long ss;\n foreach_reverse (i ; 1 .. N) {\n chmin(sm, m[i]);\n ss += m[i];\n }\n\n auto f = sm + ss;\n auto g = pm + ps;\n\n auto ans = new long[](N);\n\n if (f > g) {\n ans[0] = sm;\n foreach (i ; 1 .. N) {\n ans[i] = m[i];\n }\n }\n else {\n ans[N - 1] = pm;\n foreach (i ; 0 .. N - 1) {\n ans[i] = m[i];\n }\n }\n\n writefln(\"%(%s %)\", ans);\n}\n\n\n\n\n\n\n\n\n\n\n\n\nvoid scan(T...)(ref T args) {\n import std.stdio : readln;\n import std.algorithm : splitter;\n import std.conv : to;\n import std.range.primitives;\n\n auto line = readln().splitter();\n foreach (ref arg; args) {\n arg = line.front.to!(typeof(arg));\n line.popFront();\n }\n assert(line.empty);\n}\n\nvoid fillAll(R, T)(ref R arr, T value) {\n static if (is(typeof(arr[] = value))) {\n arr[] = value;\n }\n else {\n foreach (ref e; arr) {\n fillAll(e, value);\n }\n }\n}\n\nbool chmin(T, U...)(ref T x, U args) {\n bool isChanged;\n\n foreach (arg; args) {\n if (x > arg) {\n x = arg;\n isChanged = true;\n }\n }\n\n return isChanged;\n}\n\nbool chmax(T, U...)(ref T x, U args) {\n bool isChanged;\n\n foreach (arg; args) {\n if (x < arg) {\n x = arg;\n isChanged = true;\n }\n }\n\n return isChanged;\n}\n"}], "src_uid": "88e6cd9ef45b956be8bee5d4fedd5725"} {"nl": {"description": "After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.Formally the parking can be represented as a matrix 109 × 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 ≤ i < x, 1 ≤ j < y. The upper left fragment 5 × 5 of the parking Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.", "input_spec": "The first line contains one integer q (1 ≤ q ≤ 104) — the number of Leha's requests. The next q lines contain five integers x1, y1, x2, y2, k (1 ≤ x1 ≤ x2 ≤ 109, 1 ≤ y1 ≤ y2 ≤ 109, 1 ≤ k ≤ 2·109) — parameters of Leha's requests.", "output_spec": "Print exactly q lines — in the first line print the answer to the first request, in the second — the answer to the second request and so on.", "sample_inputs": ["4\n1 1 1 1 1\n3 2 5 4 5\n1 1 5 5 10000\n1 4 2 5 2"], "sample_outputs": ["1\n13\n93\n0"], "notes": "NoteLet's analyze all the requests. In each case the requested submatrix is highlighted in blue.In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.In the third request (k = 10000) Leha asks about the upper left frament 5 × 5 of the parking. Since k is big enough, the answer is equal to 93.In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nstruct ModInt(int M_) {\n import std.conv : to;\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n if (a < 0) return (this = inv()^^(-a));\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e > 0; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op: \"-\")() const { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const {\n return mixin(\"ModInt(this) \" ~ op ~ \"= a\");\n }\n ModInt opBinaryRight(string op)(long a) const {\n return mixin(\"ModInt(a) \" ~ op ~ \"= this\");\n }\n bool opCast(T: bool)() const { return (x != 0); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 7;\nalias Mint = ModInt!MO;\n\nenum E = 35;\n\n// < m, < n, < k\nMint solve(long m, long n, long k) {\n debug {\n writefln(\"solve %s %s %s\", m, n, k);\n }\n auto crt0 = new Mint[][][](2, 2, 2);\n auto crt1 = new Mint[][][](2, 2, 2);\n crt0[0][0][0] = 1;\n foreach_reverse (e; 0 .. E) {\n auto nxt0 = new Mint[][][](2, 2, 2);\n auto nxt1 = new Mint[][][](2, 2, 2);\n const mm = cast(int)((m >> e) & 1);\n const nn = cast(int)((n >> e) & 1);\n const kk = cast(int)((k >> e) & 1);\n foreach (s; 0 .. 2) foreach (t; 0 .. 2) foreach (u; 0 .. 2) {\n foreach (x; 0 .. 2) foreach (y; 0 .. 2) {\n const z = x ^ y;\n if ((s || x <= mm) && (t || y <= nn) && (u || z <= kk)) {\n const ss = (s || x < mm) ? 1 : 0;\n const tt = (t || y < nn) ? 1 : 0;\n const uu = (u || z < kk) ? 1 : 0;\n nxt0[ss][tt][uu] += crt0[s][t][u];\n nxt1[ss][tt][uu] += crt1[s][t][u] * 2 + crt0[s][t][u] * z;\n }\n }\n }\n debug {\n if (e <= 5) {\n writeln(\"e = \", e);\n writeln(\"nxt0 = \", nxt0);\n writeln(\"nxt1 = \", nxt1);\n }\n }\n crt0 = nxt0;\n crt1 = nxt1;\n }\n Mint ret;\n ret += crt1[1][1][1];\n ret += crt0[1][1][1];\n return ret;\n}\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt();\n foreach (caseId; 0 .. numCases) {\n const X1 = readLong() - 1;\n const Y1 = readLong() - 1;\n const X2 = readLong();\n const Y2 = readLong();\n const K = readLong();\n Mint ans;\n ans += solve(X1, Y1, K);\n ans -= solve(X1, Y2, K);\n ans -= solve(X2, Y1, K);\n ans += solve(X2, Y2, K);\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "1ab085026ce43810acf98cc4bf8faf26"} {"nl": {"description": "Little Vasya has received a young builder’s kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way possible.", "input_spec": "The first line contains an integer N (1 ≤ N ≤ 1000) — the number of bars at Vasya’s disposal. The second line contains N space-separated integers li — the lengths of the bars. All the lengths are natural numbers not exceeding 1000.", "output_spec": "In one line output two numbers — the height of the largest tower and their total number. Remember that Vasya should use all the bars.", "sample_inputs": ["3\n1 2 3", "4\n6 5 6 7"], "sample_outputs": ["1 3", "2 3"], "notes": null}, "positive_code": [{"source_code": "import std.string, std.stdio, std.algorithm, std.range, std.conv;\n\nvoid main() {\n int N = readln.chomp.to!int;\n auto l = readln.chomp.split.map!(to!int);\n int[int] table;\n foreach (e; l) {\n table[e]++;\n }\n\n writeln(table.values.minPos!(\"a > b\")[0], \" \", table.values.filter!(\"a > 0\").array.length);\n}"}], "negative_code": [], "src_uid": "9a92221c760a3b6a1e9318f948fe0473"} {"nl": {"description": "Students of Winter Informatics School are going to live in a set of houses connected by underground passages. Teachers are also going to live in some of these houses, but they can not be accommodated randomly. For safety reasons, the following must hold: All passages between two houses will be closed, if there are no teachers in both of them. All other passages will stay open. It should be possible to travel between any two houses using the underground passages that are open. Teachers should not live in houses, directly connected by a passage. Please help the organizers to choose the houses where teachers will live to satisfy the safety requirements or determine that it is impossible.", "input_spec": "The first input line contains a single integer $$$t$$$ — the number of test cases ($$$1 \\le t \\le 10^5$$$). Each test case starts with two integers $$$n$$$ and $$$m$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$, $$$0 \\le m \\le 3 \\cdot 10^5$$$) — the number of houses and the number of passages. Then $$$m$$$ lines follow, each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$), describing a passage between the houses $$$u$$$ and $$$v$$$. It is guaranteed that there are no two passages connecting the same pair of houses. The sum of values $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$, and the sum of values $$$m$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, if there is no way to choose the desired set of houses, output \"NO\". Otherwise, output \"YES\", then the total number of houses chosen, and then the indices of the chosen houses in arbitrary order.", "sample_inputs": ["2\n3 2\n3 2\n2 1\n4 2\n1 4\n2 3", "1\n17 27\n1 8\n2 9\n3 10\n4 11\n5 12\n6 13\n7 14\n8 9\n8 14\n8 15\n9 10\n9 15\n10 11\n10 15\n10 17\n11 12\n11 17\n12 13\n12 16\n12 17\n13 14\n13 16\n14 16\n14 15\n15 16\n15 17\n16 17"], "sample_outputs": ["YES\n2\n1 3 \nNO", "YES\n8\n1 3 4 5 6 9 14 17"], "notes": "NoteThe picture below shows the second example test. "}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..m)\r\n\t\t{\r\n\t\t\tint u, v;\r\n\t\t\treadf !(\" %s %s\") (u, v);\r\n\t\t\tu -= 1;\r\n\t\t\tv -= 1;\r\n\t\t\tadj[u] ~= v;\r\n\t\t\tadj[v] ~= u;\r\n\t\t}\r\n\r\n\t\tauto vis = new bool [n];\r\n\t\tauto c = new int [n];\r\n\t\tc[] = -1;\r\n\r\n\t\tvoid recur (int v)\r\n\t\t{\r\n\t\t\tif (vis[v])\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\tvis[v] = true;\r\n\t\t\tif (c[v] == -1)\r\n\t\t\t{\r\n\t\t\t\tc[v] = 0;\r\n\t\t\t\tforeach (u; adj[v])\r\n\t\t\t\t{\r\n\t\t\t\t\tc[u] = 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0);\r\n\r\n\t\tif (vis.all)\r\n\t\t{\r\n\t\t\twriteln (\"YES\");\r\n\t\t\tauto answer = n.iota.filter !(i => c[i] == 0).array;\r\n\t\t\tanswer[] += 1;\r\n\t\t\twriteln (answer.length);\r\n\t\t\twritefln !(\"%(%s %)\") (answer);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\twriteln (\"NO\");\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[][](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto m = RD!int;\r\n\t\tauto edges = new int[][](n);\r\n\t\tforeach (i; 0..m)\r\n\t\t{\r\n\t\t\tauto u = RD!int-1;\r\n\t\t\tauto v = RD!int-1;\r\n\t\t\tedges[u] ~= v;\r\n\t\t\tedges[v] ~= u;\r\n\t\t}\r\n\r\n\t\tint[] q = [0];\r\n\t\tauto state = new int[](n);\r\n\t\tauto used = new bool[](n);\r\n\t\tused[0] = true;\r\n\t\twhile (!q.empty)\r\n\t\t{\r\n\t\t\tauto u = q.front; q.popFront;\r\n\t\t\tbool can = true;\r\n\t\t\tforeach (v; edges[u])\r\n\t\t\t{\r\n\t\t\t\tif (state[v] == 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tcan = false;\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (can)\r\n\t\t\t\tstate[u] = 1;\r\n\t\t\telse\r\n\t\t\t\tstate[u] = 2;\r\n\t\t\tforeach (v; edges[u])\r\n\t\t\t{\r\n\t\t\t\tif (used[v]) continue;\r\n\t\t\t\tq ~= v;\r\n\t\t\t\tused[v] = true;\r\n\t\t\t}\r\n\t\t}\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (state[i] == 0)\r\n\t\t\t{\r\n\t\t\t\tans[ti].length = 0;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\telse if (state[i] == 1)\r\n\t\t\t\tans[ti] ~= i+1;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\tif (e.empty)\r\n\t\t\twriteln(\"NO\");\r\n\t\telse\r\n\t\t{\r\n\t\t\twriteln(\"YES\");\r\n\t\t\twriteln(e.length);\r\n\t\t\te.map!(to!string).join(\" \").writeln;\r\n\t\t}\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..m)\r\n\t\t{\r\n\t\t\tint u, v;\r\n\t\t\treadf !(\" %s %s\") (u, v);\r\n\t\t\tu -= 1;\r\n\t\t\tv -= 1;\r\n\t\t\tadj[u] ~= v;\r\n\t\t\tadj[v] ~= u;\r\n\t\t}\r\n\r\n\t\tauto c = new int [n];\r\n\t\tc[] = -1;\r\n\r\n\t\tvoid recur (int v, int cur)\r\n\t\t{\r\n\t\t\tif (c[v] >= 0)\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\tc[v] = cur;\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u, !cur);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0, 0);\r\n\t\tdebug {writeln (c);}\r\n\r\n\t\tif (c.any !(q{a == -1}))\r\n\t\t{\r\n\t\t\twriteln (\"NO\");\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tforeach (v; 0..n)\r\n\t\t\t{\r\n\t\t\t\tif (c[v] == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tforeach (u; adj[v])\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (c[u] == 0)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tc[v] = 2;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\twriteln (\"YES\");\r\n\t\t\tauto answer = n.iota.filter !(i => c[i] == 0).array;\r\n\t\t\tanswer[] += 1;\r\n\t\t\twriteln (answer.length);\r\n\t\t\twritefln !(\"%(%s %)\") (answer);\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..m)\r\n\t\t{\r\n\t\t\tint u, v;\r\n\t\t\treadf !(\" %s %s\") (u, v);\r\n\t\t\tu -= 1;\r\n\t\t\tv -= 1;\r\n\t\t\tadj[u] ~= v;\r\n\t\t\tadj[v] ~= u;\r\n\t\t}\r\n\r\n\t\tauto c = new int [n];\r\n\t\tc[] = -1;\r\n\r\n\t\tvoid recur (int v, int cur)\r\n\t\t{\r\n\t\t\tif (c[v] >= 0)\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\tc[v] = cur;\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u, !cur);\r\n\t\t\t}\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\tif (c[u] == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tc[v] = 2;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0, 0);\r\n\t\tdebug {writeln (c);}\r\n\r\n\t\tif (c.any !(q{a == -1}))\r\n\t\t{\r\n\t\t\twriteln (\"NO\");\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\twriteln (\"YES\");\r\n\t\t\tauto answer = n.iota.filter !(i => c[i] == 0).array;\r\n\t\t\tanswer[] += 1;\r\n\t\t\twriteln (answer.length);\r\n\t\t\twritefln !(\"%(%s %)\") (answer);\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..m)\r\n\t\t{\r\n\t\t\tint u, v;\r\n\t\t\treadf !(\" %s %s\") (u, v);\r\n\t\t\tu -= 1;\r\n\t\t\tv -= 1;\r\n\t\t\tadj[u] ~= v;\r\n\t\t\tadj[v] ~= u;\r\n\t\t}\r\n\r\n\t\tauto c = new int [n];\r\n\t\tc[] = -1;\r\n\r\n\t\tvoid recur (int v, int cur)\r\n\t\t{\r\n\t\t\tif (c[v] >= 0)\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\tc[v] = cur;\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u, !cur);\r\n\t\t\t}\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\tif (c[v] == c[u])\r\n\t\t\t\t{\r\n\t\t\t\t\tc[v] = 2;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0, 0);\r\n\t\tdebug {writeln (c);}\r\n\r\n\t\tif (c.any !(q{a == -1}))\r\n\t\t{\r\n\t\t\twriteln (\"NO\");\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\twriteln (\"YES\");\r\n\t\t\tauto answer = n.iota.filter !(i => c[i] == 0).array;\r\n\t\t\tanswer[] += 1;\r\n\t\t\twriteln (answer.length);\r\n\t\t\twritefln !(\"%(%s %)\") (answer);\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m;\r\n\t\treadf !(\" %s %s\") (n, m);\r\n\t\tauto adj = new int [] [n];\r\n\t\tforeach (j; 0..m)\r\n\t\t{\r\n\t\t\tint u, v;\r\n\t\t\treadf !(\" %s %s\") (u, v);\r\n\t\t\tu -= 1;\r\n\t\t\tv -= 1;\r\n\t\t\tadj[u] ~= v;\r\n\t\t\tadj[v] ~= u;\r\n\t\t}\r\n\r\n\t\tauto c = new int [n];\r\n\t\tc[] = -1;\r\n\r\n\t\tvoid recur (int v, int cur)\r\n\t\t{\r\n\t\t\tif (c[v] >= 0)\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\tc[v] = cur;\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\trecur (u, !cur);\r\n\t\t\t}\r\n\t\t\tforeach (u; adj[v])\r\n\t\t\t{\r\n\t\t\t\tif (c[v] == c[u])\r\n\t\t\t\t{\r\n\t\t\t\t\tc[v] = 2;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trecur (0, 0);\r\n\r\n\t\tif (c.any !(q{a == -1}))\r\n\t\t{\r\n\t\t\twriteln (\"NO\");\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\twriteln (\"YES\");\r\n\t\t\tauto answer = n.iota.filter !(i => c[i] == 1).array;\r\n\t\t\tanswer[] += 1;\r\n\t\t\twriteln (answer.length);\r\n\t\t\twritefln !(\"%(%s %)\") (answer);\r\n\t\t}\r\n\t}\r\n}\r\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\nlong mod = 10^^9 + 7;\r\n//long mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new int[][](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto m = RD!int;\r\n\t\tauto edges = new int[][](n);\r\n\t\tforeach (i; 0..m)\r\n\t\t{\r\n\t\t\tauto u = RD!int-1;\r\n\t\t\tauto v = RD!int-1;\r\n\t\t\tedges[u] ~= v;\r\n\t\t\tedges[v] ~= u;\r\n\t\t}\r\n\r\n\t\tauto edges2 = new int[][](n);\r\n\t\tint[] q = [0];\r\n\t\tauto used = new bool[](n);\r\n\t\tused[0] = true;\r\n\t\twhile (!q.empty)\r\n\t\t{\r\n\t\t\tauto u = q.front; q.popFront;\r\n\t\t\tforeach (v; edges[u])\r\n\t\t\t{\r\n\t\t\t\tif (used[v]) continue;\r\n\t\t\t\tedges2[u] ~= v;\r\n\t\t\t\tedges2[v] ~= u;\r\n\t\t\t\tq ~= v;\r\n\t\t\t\tused[v] = true;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tbool ok = true;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (!used[i])\r\n\t\t\t{\r\n\t\t\t\tok = false;\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (!ok) continue;\r\n\r\n\t\tvoid dfs(int pos, int last, int s)\r\n\t\t{\r\n\t\t\tif (s == 1)\r\n\t\t\t\tans[ti] ~= pos+1;\r\n\t\t\t\r\n\t\t\tforeach (v; edges2[pos])\r\n\t\t\t{\r\n\t\t\t\tif (v == last) continue;\r\n\t\t\t\tdfs(v, pos, s^1);\r\n\t\t\t}\r\n\t\t}\r\n\t\tdfs(0, -1, 1);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\tif (e.empty)\r\n\t\t\twriteln(\"NO\");\r\n\t\telse\r\n\t\t{\r\n\t\t\twriteln(\"YES\");\r\n\t\t\twriteln(e.length);\r\n\t\t\te.map!(to!string).join(\" \").writeln;\r\n\t\t}\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}], "src_uid": "d4e1ec5445de029895a9c47ab89db3a2"} {"nl": {"description": "The integers shop sells $$$n$$$ segments. The $$$i$$$-th of them contains all integers from $$$l_i$$$ to $$$r_i$$$ and costs $$$c_i$$$ coins.Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.After shopping, Vasya will get some more integers as a gift. He will get integer $$$x$$$ as a gift if and only if all of the following conditions are satisfied: Vasya hasn't bought $$$x$$$. Vasya has bought integer $$$l$$$ that is less than $$$x$$$. Vasya has bought integer $$$r$$$ that is greater than $$$x$$$. Vasya can get integer $$$x$$$ as a gift only once so he won't have the same integers after receiving a gift.For example, if Vasya buys segment $$$[2, 4]$$$ for $$$20$$$ coins and segment $$$[7, 8]$$$ for $$$22$$$ coins, he spends $$$42$$$ coins and receives integers $$$2, 3, 4, 7, 8$$$ from these segments. He also gets integers $$$5$$$ and $$$6$$$ as a gift.Due to the technical issues only the first $$$s$$$ segments (that is, segments $$$[l_1, r_1], [l_2, r_2], \\ldots, [l_s, r_s]$$$) will be available tomorrow in the shop.Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.For each $$$s$$$ from $$$1$$$ to $$$n$$$, find how many coins will Vasya spend if only the first $$$s$$$ segments will be available.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the number of segments in the shop. Each of next $$$n$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$ ($$$1 \\leq l_i \\leq r_i \\leq 10^9, 1 \\leq c_i \\leq 10^9$$$) — the ends of the $$$i$$$-th segments and its cost. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output $$$n$$$ integers: the $$$s$$$-th ($$$1 \\leq s \\leq n$$$) of them should be the number of coins Vasia will spend in the shop if only the first $$$s$$$ segments will be available.", "sample_inputs": ["3\n2\n2 4 20\n7 8 22\n2\n5 11 42\n5 11 42\n6\n1 4 4\n5 8 9\n7 8 7\n2 10 252\n1 11 271\n1 10 1"], "sample_outputs": ["20\n42\n42\n42\n4\n13\n11\n256\n271\n271"], "notes": "NoteIn the first test case if $$$s = 1$$$ then Vasya can buy only the segment $$$[2, 4]$$$ for $$$20$$$ coins and get $$$3$$$ integers.The way to get $$$7$$$ integers for $$$42$$$ coins in case $$$s = 2$$$ is described in the statement.In the second test case note, that there can be the same segments in the shop."}, "positive_code": [{"source_code": "import std;\n\nalias Segment = Tuple!(uint, \"left\", uint, \"right\", uint, \"cost\");\n\nvoid main () {\n immutable tests = (){ uint t; readf!\"%s\\n\"(t); return t; }();\n\n foreach (test_index; 0 .. tests) {\n immutable n = (){ uint n; readf!\"%s\\n\"(n); return n; }();\n\n immutable Segment[] stock = (){\n Segment[] ret = new Segment[n];\n\n foreach (ref tup; ret)\n readf!\"%s %s %s\\n\"(tup.expand);\n\n return ret.assumeUnique;\n }();\n\n\n enum cost = (size_t a, size_t b){\n if (a == b)\n return stock[a].cost;\n return stock[a].cost + stock[b].cost;\n };\n\n enum length = (size_t a, size_t b) {\n assert(stock[a].left <= stock[b].right);\n return stock[b].right - stock[a].left;\n };\n\n size_t cheapestLow, cheapestHigh;\n writeln(cost(0, 0));\n\n alias T = Tuple!(size_t, \"smol\", size_t, \"big\");\n T prev = T(0, 0);\n foreach (s; 1 .. n) {\n immutable now = stock[s];\n T[] options = [prev, T(s, s)];\n\n if (now.left <= stock[cheapestHigh].right)\n options ~= T(s, cheapestHigh);\n\n if (now.right >= stock[cheapestLow].left)\n options ~= T(cheapestLow, s);\n\n enum better = (T a, T b) {\n assert(stock[a.big].right >= stock[a.smol].left);\n assert(stock[b.big].right >= stock[b.smol].left);\n\n auto d1 = length(a.expand);\n auto d2 = length(b.expand);\n\n auto c1 = cost(a.expand);\n auto c2 = cost(b.expand);\n\n if (d1 != d2)\n return d1 > d2;\n return c1 < c2;\n };\n\n auto ans = options.sort!((a, b) => better(a, b));\n\n //foreach (tup; ans)\n //writefln(\"[%1$s, %2$s] -> [%4$s, %5$s]\", stock[tup.smol].expand, stock[tup.big].expand);\n\n assert(length(ans[0].expand) >= length(ans[1].expand));\n\n writeln(cost(ans[0].expand));\n\n prev = ans[0];\n\n if (now.left < stock[cheapestLow].left ||\n now.left == stock[cheapestLow].left && now.cost < stock[cheapestLow].cost)\n cheapestLow = s;\n\n if (now.right > stock[cheapestHigh].right ||\n now.right == stock[cheapestHigh].right && now.cost < stock[cheapestHigh].cost)\n cheapestHigh = s;\n }\n\n }\n}\n// \"\"\n"}, {"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nconst bool dbg = true;\r\nvoid DBG(alias x)() { static if (dbg) stderr.writefln(\"DBG: %s = %s\", x.stringof, x); }\r\nvoid MSG(in string x) { static if (dbg) stderr.writefln(\"MSG: %s\", x); }\r\n\r\nvoid solve() {\r\n int N = readln.chomp.to!int;\r\n long[] L = new long[N],\r\n R = new long[N],\r\n C = new long[N];\r\n long[Tuple!(long, long)] P;\r\n for (int i = 0; i < N; i++) {\r\n scanf(\"%lld %lld %lld\\n\", &L[i], &R[i], &C[i]);\r\n }\r\n long m = long.max, M = 0;\r\n long mc = long.max, Mc = long.max;\r\n for (int s = 0; s < N; s++) {\r\n auto key = tuple(L[s], R[s]);\r\n if (key in P) {\r\n P[key] = min(P[key], C[s]);\r\n } else {\r\n P[key] = C[s];\r\n }\r\n long ans = long.max;\r\n if (L[s] <= m) {\r\n mc = (L[s] == m ? min(mc, C[s]) : C[s]);\r\n m = L[s];\r\n }\r\n if (M <= R[s]) {\r\n Mc = (R[s] == M ? min(Mc, C[s]) : C[s]);\r\n M = R[s];\r\n }\r\n ans = min(ans, mc + Mc);\r\n if (tuple(m, M) in P) {\r\n ans = min(ans, P[tuple(m, M)]);\r\n }\r\n writeln(ans);\r\n }\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t ; 0 .. T) {\r\n solve();\r\n }\r\n}\r\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\ndouble euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }\r\ndouble[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }\r\ndouble norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }\r\ndouble dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[][](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto l = new long[](n);\r\n\t\tauto r = new long[](n);\r\n\t\tauto c = new long[](n);\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tl[i] = RD;\r\n\t\t\tr[i] = RD;\r\n\t\t\tc[i] = RD;\r\n\t\t}\r\n\r\n\t\tint li, ri, xi;\r\n\t\tans[ti] ~= c[0];\r\n\t\tforeach (i; 1..n)\r\n\t\t{\r\n\t\t\tif (l[i] == l[li])\r\n\t\t\t{\r\n\t\t\t\tif (c[i] < c[li])\r\n\t\t\t\t\tli = i;\r\n\t\t\t}\r\n\t\t\telse if (l[i] < l[li])\r\n\t\t\t\tli = i;\r\n\t\t\t\r\n\t\t\tif (r[i] == r[ri])\r\n\t\t\t{\r\n\t\t\t\tif (c[i] < c[ri])\r\n\t\t\t\t\tri = i;\r\n\t\t\t}\r\n\t\t\telse if (r[i] > r[ri])\r\n\t\t\t\tri = i;\r\n\r\n\t\t\tif (r[i] - l[i] == r[xi] - l[xi])\r\n\t\t\t{\r\n\t\t\t\tif (c[i] < c[xi])\r\n\t\t\t\t\txi = i;\r\n\t\t\t}\r\n\t\t\telse if (r[i] - l[i] > r[xi] - l[xi])\r\n\t\t\t\txi = i;\r\n\r\n\t\t\tdebug writeln(\"li:\", li, \" ri:\", ri, \" xi:\", xi);\r\n\t\t\tlong tmp;\r\n\t\t\tif (r[xi] - l[xi] > r[ri] - l[li])\r\n\t\t\t\ttmp = c[xi];\r\n\t\t\telse if (r[xi] - l[xi] == r[ri] - l[li])\r\n\t\t\t\ttmp = min(c[xi], c[li] + c[ri]);\r\n\t\t\telse\r\n\t\t\t\ttmp = c[li] + c[ri];\r\n\t\t\tans[ti] ~= tmp;\r\n\t\t}\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t{\r\n\t\tforeach (ee; e)\r\n\t\t\twriteln(ee);\r\n\t}\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.typecons;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto lo = new int [n];\r\n\t\tauto hi = new int [n];\r\n\t\tauto cost = new int [n];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\treadf !(\" %s %s %s\") (lo[i], hi[i], cost[i]);\r\n\t\t}\r\n\t\treadln;\r\n\r\n\t\tauto f = tuple (int.max, -1);\r\n\t\tauto g = tuple (int.max, -1);\r\n\t\tauto h = tuple (int.max, int.max, -1);\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tf = min (f, tuple (+lo[i], cost[i]));\r\n\t\t\tg = min (g, tuple (-hi[i], cost[i]));\r\n\t\t\th = min (h, tuple (+lo[i], -hi[i], cost[i]));\r\n\t\t\tint res = f[1] + g[1];\r\n\t\t\tif (h[0] == f[0] && h[1] == g[0])\r\n\t\t\t{\r\n\t\t\t\tres = min (res, h[2]);\r\n\t\t\t}\r\n\t\t\twriteln (res);\r\n\t\t}\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std;\n\n\nalias Segment = Tuple!(int, \"left\", int, \"right\", int, \"cost\");\n\n\nvoid main () {\n immutable tests = (){ uint t; readf!\"%s\\n\"(t); return t; }();\n\n foreach (test_index; 0 .. tests) {\n immutable n = (){ uint n; readf!\"%s\\n\"(n); return n; }();\n\n immutable Segment[] inventory = (){\n Segment[] ret;\n ret.reserve(n);\n\n foreach (_; 0 .. n) {\n int left, right, cost;\n readf!\"%s %s %s\\n\"(left, right, cost);\n ret ~= Segment(left, right, cost);\n }\n return ret.assumeUnique;\n }();\n\n enum cost = (size_t l, size_t r) {\n if (l != r)\n return inventory[l].cost + inventory[r].cost;\n else\n return inventory[l].cost;\n };\n\n alias T = Tuple!(size_t, \"small\", size_t, \"big\");\n auto prev = T(0, 0);\n\n writeln(cost(0, 0));\n foreach (s; 1 .. n) {\n T[] options = [\n T(s, prev.big),\n T(prev.small, s),\n T(s, s),\n prev\n ];\n\n enum better = (T a, T b) {\n auto d1 = inventory[a.big].right - inventory[a.small].left;\n auto d2 = inventory[b.big].right - inventory[b.small].left;\n\n auto c1 = cost(a.expand);\n auto c2 = cost(b.expand);\n\n if (d1 != d2)\n return d1 > d2;\n return c1 < c2;\n };\n\n auto best = options.sort!((a, b) => better(a, b));\n\n writeln(cost(best.front.expand));\n prev = best.front;\n }\n\n }\n}\n// \"\"\n"}, {"source_code": "import std;\n\n\nalias Segment = Tuple!(uint, \"left\", uint, \"right\", uint, \"cost\");\n\n\nvoid main () {\n immutable tests = (){ uint t; readf!\"%s\\n\"(t); return t; }();\n\n foreach (test_index; 0 .. tests) {\n immutable n = (){ uint n; readf!\"%s\\n\"(n); return n; }();\n\n immutable Segment[] inventory = (){\n Segment[] ret;\n ret.reserve(n);\n\n foreach (_; 0 .. n) {\n uint left, right, cost;\n readf!\"%s %s %s\\n\"(left, right, cost);\n ret ~= Segment(left, right, cost);\n }\n return ret.assumeUnique;\n }();\n\n enum cost = (size_t l, size_t r) {\n if (l != r)\n return inventory[l].cost + inventory[r].cost;\n else\n return inventory[l].cost;\n };\n\n alias T = Tuple!(size_t, \"small\", size_t, \"big\");\n auto prev = T(0, 0);\n\n writeln(cost(0, 0));\n foreach (s; 1 .. n) {\n T now = prev;\n\n if (inventory[s].left < inventory[prev.small].left)\n now.small = s;\n\n if (inventory[s].right > inventory[prev.small].right)\n now.big = s;\n\n if (inventory[s].left == inventory[now.small].left && cost(s, now.big) < cost(now.expand))\n now.small = s;\n\n if (inventory[s].right == inventory[now.big].right && cost(s, now.small) < cost(now.expand))\n now.big = s;\n\n writeln(cost(now.expand));\n prev = now;\n }\n\n }\n}\n// \"\"\n"}, {"source_code": "import std;\n\n\nalias Segment = Tuple!(uint, \"left\", uint, \"right\", uint, \"cost\");\n\n\nvoid main () {\n immutable tests = (){ uint t; readf!\"%s\\n\"(t); return t; }();\n\n foreach (test_index; 0 .. tests) {\n immutable n = (){ uint n; readf!\"%s\\n\"(n); return n; }();\n\n immutable Segment[] inventory = (){\n Segment[] ret;\n ret.reserve(n);\n\n foreach (_; 0 .. n) {\n uint left, right, cost;\n readf!\"%s %s %s\\n\"(left, right, cost);\n ret ~= Segment(left, right, cost);\n }\n return ret.assumeUnique;\n }();\n\n\n enum cost = (size_t l, size_t r) {\n if (l != r)\n return inventory[l].cost + inventory[r].cost;\n else\n return inventory[l].cost;\n };\n\n size_t left = 0, right = 0;\n writeln(cost(left, right));\n size_t biggest, smallest;\n\n foreach (s; 1 .. n) {\n if (inventory[s].left < inventory[smallest].left)\n smallest = s;\n if (inventory[s].right > inventory[biggest].right)\n biggest = s;\n\n\n if (inventory[s].left < inventory[left].left)\n left = s;\n if (inventory[s].right > inventory[right].right)\n right = s;\n\n\n immutable v1 = inventory[s].right - inventory[s].left;\n immutable v2 = inventory[right].right - inventory[left].left;\n\n if (v1 > v2 ||\n v1 == v2 && cost(s, s) < cost(left, right)) {\n immutable q = inventory[s].right - inventory[smallest].left;\n immutable r = inventory[biggest].right - inventory[s].left;\n\n if (q > r || q == r && cost(s, smallest) < cost(s, biggest))\n left = smallest, right = s;\n else\n right = biggest, left = s;\n }\n\n\n writeln(cost(left, right));\n }\n }\n}\n// \"\"\n"}, {"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nconst bool dbg = true;\r\nvoid DBG(alias x)() { static if (dbg) stderr.writefln(\"DBG: %s = %s\", x.stringof, x); }\r\nvoid MSG(in string x) { static if (dbg) stderr.writefln(\"MSG: %s\", x); }\r\n\r\nvoid solve() {\r\n int N = readln.chomp.to!int;\r\n long[] L = new long[N],\r\n R = new long[N],\r\n C = new long[N];\r\n long[Tuple!(long, long)] P;\r\n for (int i = 0; i < N; i++) {\r\n scanf(\"%lld %lld %lld\\n\", &L[i], &R[i], &C[i]);\r\n }\r\n long m = long.max, M = 0;\r\n long mc = long.max, Mc = long.max;\r\n for (int s = 0; s < N; s++) {\r\n P[tuple(L[s], R[s])] = C[s];\r\n long ans = long.max;\r\n if (L[s] <= m) {\r\n mc = (L[s] == m ? min(mc, C[s]) : C[s]);\r\n m = L[s];\r\n }\r\n if (M <= R[s]) {\r\n Mc = (R[s] == M ? min(Mc, C[s]) : C[s]);\r\n M = R[s];\r\n }\r\n ans = min(ans, mc + Mc);\r\n if (tuple(m, M) in P) {\r\n ans = min(ans, P[tuple(m, M)]);\r\n }\r\n writeln(ans);\r\n }\r\n}\r\n\r\nvoid main() {\r\n int T = readln.chomp.to!int;\r\n foreach (t ; 0 .. T) {\r\n solve();\r\n }\r\n}\r\n"}], "src_uid": "ee773d908fc297cc692aaecf6af299c9"} {"nl": {"description": "Ashish has two strings $$$a$$$ and $$$b$$$, each of length $$$n$$$, and an integer $$$k$$$. The strings only contain lowercase English letters.He wants to convert string $$$a$$$ into string $$$b$$$ by performing some (possibly zero) operations on $$$a$$$.In one move, he can either choose an index $$$i$$$ ($$$1 \\leq i\\leq n-1$$$) and swap $$$a_i$$$ and $$$a_{i+1}$$$, or choose an index $$$i$$$ ($$$1 \\leq i \\leq n-k+1$$$) and if $$$a_i, a_{i+1}, \\ldots, a_{i+k-1}$$$ are all equal to some character $$$c$$$ ($$$c \\neq$$$ 'z'), replace each one with the next character $$$(c+1)$$$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $$$a$$$. Help Ashish determine if it is possible to convert string $$$a$$$ into $$$b$$$ after performing some (possibly zero) operations on it.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^5$$$) — the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $$$n$$$ ($$$2 \\leq n \\leq 10^6$$$) and $$$k$$$ ($$$1 \\leq k \\leq n$$$). The second line of each test case contains the string $$$a$$$ of length $$$n$$$ consisting of lowercase English letters. The third line of each test case contains the string $$$b$$$ of length $$$n$$$ consisting of lowercase English letters. It is guaranteed that the sum of values $$$n$$$ among all test cases does not exceed $$$10^6$$$.", "output_spec": "For each test case, print \"Yes\" if Ashish can convert $$$a$$$ into $$$b$$$ after some moves, else print \"No\". You may print the letters of the answer in any case (upper or lower).", "sample_inputs": ["4\n3 3\nabc\nbcd\n4 2\nabba\nazza\n2 1\nzz\naa\n6 2\naaabba\nddddcc"], "sample_outputs": ["No\nYes\nNo\nYes"], "notes": "NoteIn the first test case it can be shown that it is impossible to convert $$$a$$$ into $$$b$$$.In the second test case,\"abba\" $$$\\xrightarrow{\\text{inc}}$$$ \"acca\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"azza\".Here \"swap\" denotes an operation of the first type, and \"inc\" denotes an operation of the second type.In the fourth test case,\"aaabba\" $$$\\xrightarrow{\\text{swap}}$$$ \"aaabab\" $$$\\xrightarrow{\\text{swap}}$$$ \"aaaabb\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"ddaabb\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"ddddbb\" $$$\\xrightarrow{\\text{inc}}$$$ $$$\\ldots$$$ $$$\\xrightarrow{\\text{inc}}$$$ \"ddddcc\"."}, "positive_code": [{"source_code": "// Cheese-Cracker: cheese-cracker.github.io\n\n// Main Logic Here\nvoid play(){\n long[27] cmap = 0;\n auto n = rd!int;\n auto k = rd!int;\n dchar[] s1 = rd!(dchar[]);\n dchar[] s2 = rd!(dchar[]);\n foreach(dchar c; s1){ int z = to!int(c - 'a'); ++cmap[z+1]; }\n foreach(dchar c; s2){ int z = to!int(c - 'a'); --cmap[z+1]; }\n show(cmap);\n foreach(int i; 1..27){\n if(cmap[i] > 0 && cmap[i] % k == 0){\n foreach(int j; i+1..27){\n if(cmap[j] < 0 && (-cmap[j]) % k == 0){\n ll take = min(cmap[i], -cmap[j]);\n cmap[i] -= take;\n cmap[j] += take;\n }\n }\n }\n }\n show(cmap);\n foreach(i; 1..27){\n if(cmap[i] != 0){\n writeln(\"No\");\n return;\n }\n }\n writeln(\"Yes\");\n}\n\nint main(){\n long t = 1;\n t = rd; // Toggle for testcases!\n while(t--) play(); // Let's play!\n return 0;\n}\n\n/**********That's All Folks!**********/\nimport std.stdio, std.conv, std.functional, std.string, std.algorithm;\nimport std.container, std.range, std.typecons, std.numeric, std.math, std.random;\nstatic string[] inp; // Read input\nT rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}\nT[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }\nalias ll = long;\nalias tup = Tuple!(long, \"x\", long, \"y\");\nvoid show(A...)(A a) { debug{ foreach(t; a){ stderr.write(t, \"| \"); } stderr.writeln; } } // Debug\n/* Add if TLE, T max(T = long)(T a, T b){ return (a > b) ? a : b; } */\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new bool[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto k = RD!int;\n\t\tauto a = RD!string;\n\t\tauto b = RD!string;\n\t\tauto aa = new int[](26);\n\t\tauto bb = new int[](26);\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\t++aa[a[i]-'a'];\n\t\t\t++bb[b[i]-'a'];\n\t\t}\n\t\tauto c = new int[](26);\n\t\tforeach (i; 0..26)\n\t\t{\n\t\t\tc[i] = aa[i] - bb[i];\n\t\t}\n\t\tbool ok = true;\n\t\tforeach (i; 0..25)\n\t\t{\n\t\t\tif (c[i] < 0)\n\t\t\t{\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (c[i] % k)\n\t\t\t{\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tc[i+1] += c[i];\n\t\t\t}\n\t\t}\n\t\tif (c[$-1] != 0)\n\t\t\tok = false;\n\t\tans[ti] = ok;\n\t}\n\n\tforeach (e; ans)\n\t{\n\t\twriteln(e ? \"Yes\" : \"No\");\n\t}\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "09d0f11bdb9eca2161dee83a335dd2b7"} {"nl": {"description": "A binary string is a string where each character is either 0 or 1. Two binary strings $$$a$$$ and $$$b$$$ of equal length are similar, if they have the same character in some position (there exists an integer $$$i$$$ such that $$$a_i = b_i$$$). For example: 10010 and 01111 are similar (they have the same character in position $$$4$$$); 10010 and 11111 are similar; 111 and 111 are similar; 0110 and 1001 are not similar. You are given an integer $$$n$$$ and a binary string $$$s$$$ consisting of $$$2n-1$$$ characters. Let's denote $$$s[l..r]$$$ as the contiguous substring of $$$s$$$ starting with $$$l$$$-th character and ending with $$$r$$$-th character (in other words, $$$s[l..r] = s_l s_{l + 1} s_{l + 2} \\dots s_r$$$).You have to construct a binary string $$$w$$$ of length $$$n$$$ which is similar to all of the following strings: $$$s[1..n]$$$, $$$s[2..n+1]$$$, $$$s[3..n+2]$$$, ..., $$$s[n..2n-1]$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 50$$$). The second line of each test case contains the binary string $$$s$$$ of length $$$2n - 1$$$. Each character $$$s_i$$$ is either 0 or 1.", "output_spec": "For each test case, print the corresponding binary string $$$w$$$ of length $$$n$$$. If there are multiple such strings — print any of them. It can be shown that at least one string $$$w$$$ meeting the constraints always exists.", "sample_inputs": ["4\n1\n1\n3\n00000\n4\n1110000\n2\n101"], "sample_outputs": ["1\n000\n1010\n00"], "notes": "NoteThe explanation of the sample case (equal characters in equal positions are bold):The first test case: $$$\\mathbf{1}$$$ is similar to $$$s[1..1] = \\mathbf{1}$$$. The second test case: $$$\\mathbf{000}$$$ is similar to $$$s[1..3] = \\mathbf{000}$$$; $$$\\mathbf{000}$$$ is similar to $$$s[2..4] = \\mathbf{000}$$$; $$$\\mathbf{000}$$$ is similar to $$$s[3..5] = \\mathbf{000}$$$. The third test case: $$$\\mathbf{1}0\\mathbf{10}$$$ is similar to $$$s[1..4] = \\mathbf{1}1\\mathbf{10}$$$; $$$\\mathbf{1}01\\mathbf{0}$$$ is similar to $$$s[2..5] = \\mathbf{1}10\\mathbf{0}$$$; $$$\\mathbf{10}1\\mathbf{0}$$$ is similar to $$$s[3..6] = \\mathbf{10}0\\mathbf{0}$$$; $$$1\\mathbf{0}1\\mathbf{0}$$$ is similar to $$$s[4..7] = 0\\mathbf{0}0\\mathbf{0}$$$. The fourth test case: $$$0\\mathbf{0}$$$ is similar to $$$s[1..2] = 1\\mathbf{0}$$$; $$$\\mathbf{0}0$$$ is similar to $$$s[2..3] = \\mathbf{0}1$$$. "}, "positive_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1400/problem/A\n// greedy\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n while(t--) {\n int n = readln.chomp.to!int;\n string s = readln.strip;\n\n char[] ans;\n\n foreach(_; 0..n)\n ans ~= s[n-1];\n\n ans.writeln;\n }\n}\n\n"}, {"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\n\nvoid main ()\n{\n\tforeach (test; 0..readln.strip.to !(int))\n\t{\n\t\treadln;\n\t\treadln.strip.stride (2).writeln;\n\t}\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new string[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto s = RD!string;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tans[ti] ~= s[$/2];\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [{"source_code": "// Vicfred\n// https://codeforces.com/contest/1400/problem/A\n// greedy\nimport std.conv;\nimport std.stdio;\nimport std.string;\n\nvoid main() {\n int t = readln.chomp.to!int;\n while(t--) {\n int n = readln.chomp.to!int;\n string s = readln.strip;\n\n char[] ans;\n\n foreach(_; 0..n-1)\n ans ~= '1';\n\n ans ~= s[n-1];\n ans.writeln;\n }\n}\n\n"}], "src_uid": "b37bbf1fe9ac5144cfa230633ccbdd79"} {"nl": {"description": "One day, you are accepted as being Dr. Chanek's assistant. The first task given by Dr. Chanek to you is to take care and store his magical stones.Dr. Chanek has $$$N$$$ magical stones with $$$N$$$ being an even number. Those magical stones are numbered from $$$1$$$ to $$$N$$$. Magical stone $$$i$$$ has a strength of $$$A_i$$$. A magical stone can be painted with two colours, namely the colour black or the colour white. You are tasked to paint the magical stones with the colour black or white and store the magical stones into a magic box with a magic coefficient $$$Z$$$ ($$$0 \\leq Z \\leq 2$$$). The painting of the magical stones must be done in a way such that there are $$$\\frac{N}{2}$$$ black magical stones and $$$\\frac{N}{2}$$$ white magical stones.Define $$$\\text{concat}(x, y)$$$ for two integers $$$x$$$ and $$$y$$$ as the result of concatenating the digits of $$$x$$$ to the left of $$$y$$$ in their decimal representation without changing the order. As an example, $$$\\text{concat}(10, 24)$$$ will result in $$$1024$$$.For a magic box with a magic coefficient $$$Z$$$, magical stone $$$i$$$ will react with magical stone $$$j$$$ if the colours of both stones are different and $$$\\text{concat}(A_i, A_j) \\times \\text{concat}(A_j, A_i) + A_i \\times A_j \\equiv Z \\mod 3$$$. A magical stone that is reacting will be very hot and dangerous. Because of that, you must colour the magical stones and determine the magic coefficient $$$Z$$$ of the magic box in a way such that there is no magical stone that reacts, or report if it is impossible.", "input_spec": "The first line contains a single even integer $$$N$$$ ($$$2 \\le N \\le 10^5$$$) — the number of magical stones Dr. Chanek has. The second line contains $$$N$$$ integer $$$A_1, A_2, \\ldots, A_N$$$ ($$$1 \\leq A_i \\leq 10^9$$$) — the strengths of all magical stones.", "output_spec": "If it is not possible to satisfy the condition of the problem, output $$$-1$$$. Otherwise, output two lines. The first line contains an integer $$$Z$$$ denoting the magic coefficient of the magic box. The second line contains a string $$$S$$$ of length $$$N$$$. $$$S_i$$$ is $$$0$$$ if magical stone $$$i$$$ is coloured black or $$$1$$$ if magical stone $$$i$$$ is coloured white. If there are more than one possibilities of colouring and choosing the magic coefficient $$$Z$$$, output any of them.", "sample_inputs": ["4\n4 10 9 14"], "sample_outputs": ["0\n1001"], "notes": "NoteBy giving the above colouring, it can be seen that: $$$i=1, j=2 \\longrightarrow \\text{concat}(4, 10) \\times \\text{concat}(10, 4) + 10 \\times 4 = 410 \\times 104 + 40 = 42680 \\equiv 2 \\mod 3$$$ $$$i=1, j=3 \\longrightarrow \\text{concat}(4, 9) \\times \\text{concat}(9, 4) + 4 \\times 9 = 49 \\times 94 + 36 = 4642 \\equiv 1 \\mod 3$$$ $$$i=4, j=2 \\longrightarrow \\text{concat}(14, 10) \\times \\text{concat}(10, 14) + 10 \\times 14 = 1410 \\times 1014 + 140 = 1429880 \\equiv 2 \\mod 3$$$ $$$i=4, j=3 \\longrightarrow \\text{concat}(14, 9) \\times \\text{concat}(9, 14) + 14 \\times 9 = 149 \\times 914 + 126 = 136312 \\equiv 1 \\mod 3$$$ Because of that, by choosing $$$Z = 0$$$, it can be guaranteed that there is no magical stone that reacts."}, "positive_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid main() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").map!((x) => (x.to!int % 3)).array;\r\n int[] B = new int[3];\r\n foreach (i; 0 .. 3) {\r\n B[i] = A.count!((a) => a == i);\r\n }\r\n int Z = -1;\r\n char[] ans = new char[N];\r\n if (B[0] >= N/2) {\r\n Z = 2;\r\n int C = 0;\r\n for (int i = 0; i < N; i++) {\r\n if (A[i] == 0 && C < N/2) {\r\n ans[i] = '0';\r\n C++;\r\n } else {\r\n ans[i] = '1';\r\n }\r\n }\r\n } else if (B[1] >= N/2) {\r\n Z = 0;\r\n int C = 0;\r\n for (int i = 0; i < N; i++) {\r\n if (A[i] == 1 && C < N/2) {\r\n ans[i] = '0';\r\n C++;\r\n } else {\r\n ans[i] = '1';\r\n }\r\n }\r\n } else if (B[2] >= N/2) {\r\n Z = 0;\r\n int C = 0;\r\n for (int i = 0; i < N; i++) {\r\n if (A[i] == 2 && C < N/2) {\r\n ans[i] = '0';\r\n C++;\r\n } else {\r\n ans[i] = '1';\r\n }\r\n }\r\n } else {\r\n Z = 0;\r\n int C = 0;\r\n for (int i = 0; i < N; i++) {\r\n if (A[i] == 0) {\r\n ans[i] = '0';\r\n } else if (C < N/2) {\r\n ans[i] = '1';\r\n C++;\r\n } else {\r\n ans[i] = '0';\r\n }\r\n }\r\n }\r\n writeln(Z);\r\n writeln(ans);\r\n}\r\n"}], "negative_code": [{"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid main() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").map!(to!int).array;\r\n int Z = 0;\r\n for (int i = 0; i < N; i++) {\r\n int r = A[i] % 3;\r\n Z = (Z + r * r) % 3;\r\n }\r\n Z = Z * 2 * N % 3;\r\n writeln(Z);\r\n char[] s = new char[N];\r\n for (int i = 0; i < N; i++) {\r\n s[i] = (i % 2 == 0 ? '0' : '1');\r\n }\r\n string ans = cast(string)s;\r\n writeln(ans);\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid main() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").map!(to!int).array;\r\n int Z = 0;\r\n for (int i = 0; i < N; i++) {\r\n int r = A[i] % 3;\r\n Z = (Z + r * r) % 3;\r\n }\r\n writeln(Z);\r\n char[] s = new char[N];\r\n for (int i = 0; i < N; i++) {\r\n s[i] = (i % 2 == 0 ? '0' : '1');\r\n }\r\n string ans = cast(string)s;\r\n writeln(ans);\r\n}\r\n"}, {"source_code": "import std.stdio;\r\nimport std.typecons;\r\nimport core.stdc.stdio : scanf;\r\nimport std.conv;\r\nimport std.array;\r\nimport std.string;\r\nimport std.range;\r\nimport std.algorithm;\r\n\r\nvoid main() {\r\n int N = readln.chomp.to!int;\r\n auto A = readln.chomp.split(\" \").map!(to!int).array;\r\n int Z = 0;\r\n for (int i = 0; i < N; i++) {\r\n int r = A[i] % 3;\r\n Z = (Z + r * r) % 3;\r\n }\r\n writeln(Z);\r\n char[] s = new char[N+1];\r\n for (int i = 0; i < N; i++) {\r\n s[i] = (i % 2 == 0 ? '0' : '1');\r\n }\r\n s[N] = '\\0';\r\n string ans = cast(string)s;\r\n writeln(ans);\r\n}\r\n"}], "src_uid": "381a292a15cd0613eafd2f6ddf011165"} {"nl": {"description": "While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $$$H$$$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $$$L$$$ centimeters. Exactly at this point the flower touched the water surface. Suppose that the lily grows at some point $$$A$$$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $$$A$$$. Also suppose that initially the flower was exactly above the point $$$A$$$, i.e. its stem was vertical. Can you determine the depth of the lake at point $$$A$$$?", "input_spec": "The only line contains two integers $$$H$$$ and $$$L$$$ ($$$1 \\le H < L \\le 10^{6}$$$).", "output_spec": "Print a single number — the depth of the lake at point $$$A$$$. The absolute or relative error should not exceed $$$10^{-6}$$$. Formally, let your answer be $$$A$$$, and the jury's answer be $$$B$$$. Your answer is accepted if and only if $$$\\frac{|A - B|}{\\max{(1, |B|)}} \\le 10^{-6}$$$.", "sample_inputs": ["1 2", "3 5"], "sample_outputs": ["1.5000000000000", "2.6666666666667"], "notes": null}, "positive_code": [{"source_code": "import std.stdio, std.math;\n\nvoid main() {\n long h, l;\n readf(\" %s %s\", h, l);\n auto result = (cast(float)(l*l - h*h)) / (2 * h);\n writef(\"%.9s\", result);\n}\n"}, {"source_code": "import std.stdio;\n\nvoid main()\n{\n float h, l;\n readf!\"%f %f\\n\"(h, l);\n\n writef!\"%.6f\\n\"((l^^2 - h^^2) / (2*h));\n}\n"}, {"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n double h,l;\n readf!\"%f %f\"(h,l);\n readln;\n writefln!\"%.13f\"(l^^2/(2*h)-0.5*h);\n}\n\n"}], "negative_code": [{"source_code": "//ahmat\nimport core.stdc.stdio:scanf,printf,puts;\nimport core.bitop;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.functional;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.container;\nimport std.typecons;\nimport std.math;\nimport std.numeric;\npragma(inline,true);\n\nvoid main(){\n double h,l;\n readf!\"%f %f\"(h,l);\n readln;\n writefln!\"%.13f\"(l^^2/(2*h)-0.5);\n}\n\n"}], "src_uid": "2cd5807e3f9685d4eff88f2283904f0d"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$ of equal length $$$n$$$. In one move, you can swap any two adjacent characters of the string $$$s$$$.You need to find the minimal number of operations you need to make string $$$s$$$ lexicographically smaller than string $$$t$$$.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. ", "input_spec": "The first line of input contains one integer $$$q$$$ ($$$1 \\le q \\le 10\\,000$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase English letters. The third line of each test case contains the string $$$t$$$ consisting of $$$n$$$ lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print in a separate line the minimal number of operations you need to make string $$$s$$$ lexicographically smaller than string $$$t$$$, or $$$-1$$$, if it's impossible.", "sample_inputs": ["4\n1\na\na\n3\nrll\nrrr\n3\ncaa\naca\n5\nababa\naabba"], "sample_outputs": ["-1\n0\n2\n2"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int letters = 26;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto s = readln.strip.map !(q{a - 'a'}).array;\r\n\t\tauto t = readln.strip.map !(q{a - 'a'}).array;\r\n\t\tauto where = new int [] [letters];\r\n\t\tforeach (i, ref c; s)\r\n\t\t{\r\n\t\t\twhere[c] ~= i.to !(int);\r\n\t\t}\r\n\r\n\t\tauto half = 1;\r\n\t\twhile (half < n)\r\n\t\t{\r\n\t\t\thalf <<= 1;\r\n\t\t}\r\n\t\tauto total = half << 1;\r\n\t\tauto r = new int [total];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tr[i + half] = i;\r\n\t\t}\r\n\r\n\t\tvoid alter (int lo, int hi, int delta)\r\n\t\t{\r\n\t\t\tfor (lo += half, hi += half; lo < hi;\r\n\t\t\t lo >>= 1, hi >>= 1)\r\n\t\t\t{\r\n\t\t\t\tif (lo & 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tr[lo++] += delta;\r\n\t\t\t\t}\r\n\t\t\t\tif (hi & 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tr[--hi] += delta;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint value (int pos)\r\n\t\t{\r\n\t\t\tint res = 0;\r\n\t\t\tfor (pos += half; pos > 0; pos >>= 1)\r\n\t\t\t{\r\n\t\t\t\tres += r[pos];\r\n\t\t\t}\r\n\t\t\treturn res;\r\n\t\t}\r\n\r\n\t\tlong res = long.max;\r\n\t\tlong cur = 0;\r\nmain_loop:\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tforeach (z; 0..t[i])\r\n\t\t\t{\r\n\t\t\t\tif (!where[z].empty)\r\n\t\t\t\t{\r\n\t\t\t\t\tauto temp = value (where[z].front);\r\n\t\t\t\t\tres = min (res, cur + temp - i);\r\n\t\t\t\t\tif (temp == i)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tbreak main_loop;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tauto z = t[i];\r\n\t\t\tif (where[z].empty)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tauto temp = value (where[z].front);\r\n\t\t\tcur += temp - i;\r\n\t\t\talter (0, where[z].front, +1);\r\n\t\t\twhere[z].popFront ();\r\n\t\t}\r\n\t\tif (res == long.max)\r\n\t\t{\r\n\t\t\tres = -1;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int letters = 26;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto s = readln.strip.map !(q{a - 'a'}).array;\r\n\t\tauto t = readln.strip.map !(q{a - 'a'}).array;\r\n\t\tauto where = new int [] [letters];\r\n\t\tforeach (i, ref c; s)\r\n\t\t{\r\n\t\t\twhere[c] ~= i.to !(int);\r\n\t\t}\r\n\r\n\t\tauto half = 1;\r\n\t\twhile (half < n)\r\n\t\t{\r\n\t\t\thalf <<= 1;\r\n\t\t}\r\n\t\tauto total = 1 << half;\r\n\t\tauto r = new int [total];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tr[i + half] = i;\r\n\t\t}\r\n\r\n\t\tvoid alter (int lo, int hi, int delta)\r\n\t\t{\r\n\t\t\tdebug {writeln (\"alter \", lo, \" \", hi, \" \", delta);}\r\n\t\t\tfor (lo += half, hi += half; lo < hi;\r\n\t\t\t lo >>= 1, hi >>= 1)\r\n\t\t\t{\r\n\t\t\t\tif (lo & 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tr[lo++] += delta;\r\n\t\t\t\t}\r\n\t\t\t\tif (hi & 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tr[--hi] += delta;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint value (int pos)\r\n\t\t{\r\n\t\t\tdebug {writeln (\"value \", pos);}\r\n\t\t\tint res = 0;\r\n\t\t\tfor (pos += half; pos > 0; pos >>= 1)\r\n\t\t\t{\r\n\t\t\t\tres += r[pos];\r\n\t\t\t}\r\n\t\t\treturn res;\r\n\t\t}\r\n\r\n\t\tlong res = long.max;\r\n\t\tlong cur = 0;\r\nmain_loop:\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tforeach (z; 0..t[i])\r\n\t\t\t{\r\n\t\t\t\tif (!where[z].empty)\r\n\t\t\t\t{\r\n\t\t\t\t\tauto temp = value (where[z].front);\r\n\t\t\t\t\tdebug {writeln (z, \": \",\r\n\t\t\t\t\t temp, \" \", i);}\r\n\t\t\t\t\tres = min (res, cur + temp - i);\r\n\t\t\t\t\tif (temp == i)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tbreak main_loop;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tauto z = t[i];\r\n\t\t\tif (where[z].empty)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tauto temp = value (where[z].front);\r\n\t\t\tdebug {writeln (z, \"!: \", temp, \" \", i);}\r\n\t\t\tcur += temp - i;\r\n\t\t\talter (i, temp, +1);\r\n\t\t\twhere[z].popFront ();\r\n\t\t}\r\n\t\tif (res == long.max)\r\n\t\t{\r\n\t\t\tres = -1;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int letters = 26;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto s = readln.strip.map !(q{a - 'a'}).array;\r\n\t\tauto t = readln.strip.map !(q{a - 'a'}).array;\r\n\t\tauto where = new int [] [letters];\r\n\t\tforeach (i, ref c; s)\r\n\t\t{\r\n\t\t\twhere[c] ~= i.to !(int);\r\n\t\t}\r\n\r\n\t\tauto half = 1;\r\n\t\twhile (half < n)\r\n\t\t{\r\n\t\t\thalf <<= 1;\r\n\t\t}\r\n\t\tauto total = 1 << half;\r\n\t\tauto r = new int [total];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tr[i + half] = i;\r\n\t\t}\r\n\r\n\t\tvoid alter (int lo, int hi, int delta)\r\n\t\t{\r\n\t\t\tdebug {writeln (\"alter \", lo, \" \", hi, \" \", delta);}\r\n\t\t\tfor (lo += half, hi += half; lo < hi;\r\n\t\t\t lo >>= 1, hi >>= 1)\r\n\t\t\t{\r\n\t\t\t\tif (lo & 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tr[lo++] += delta;\r\n\t\t\t\t}\r\n\t\t\t\tif (hi & 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tr[--hi] += delta;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint value (int pos)\r\n\t\t{\r\n\t\t\tdebug {writeln (\"value \", pos);}\r\n\t\t\tint res = 0;\r\n\t\t\tfor (pos += half; pos > 0; pos >>= 1)\r\n\t\t\t{\r\n\t\t\t\tres += r[pos];\r\n\t\t\t}\r\n\t\t\treturn res;\r\n\t\t}\r\n\r\n\t\tlong res = long.max;\r\n\t\tlong cur = 0;\r\nmain_loop:\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tforeach (z; 0..t[i])\r\n\t\t\t{\r\n\t\t\t\tif (!where[z].empty)\r\n\t\t\t\t{\r\n\t\t\t\t\tauto temp = value (where[z].front);\r\n\t\t\t\t\tdebug {writeln (z, \": \",\r\n\t\t\t\t\t temp, \" \", i);}\r\n\t\t\t\t\tres = min (res, cur + temp - i);\r\n\t\t\t\t\tif (temp == i)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tbreak main_loop;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tauto z = t[i];\r\n\t\t\tif (where[z].empty)\r\n\t\t\t{\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tauto temp = value (where[z].front);\r\n\t\t\tdebug {writeln (z, \"!: \", temp, \" \", i);}\r\n\t\t\tcur += temp - i;\r\n\t\t\talter (i, where[z].front, +1);\r\n\t\t\twhere[z].popFront ();\r\n\t\t}\r\n\t\tif (res == long.max)\r\n\t\t{\r\n\t\t\tres = -1;\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "src_uid": "71ee54d8881f20ae4b1d8bb9783948c0"} {"nl": {"description": "Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105), which represents how many numbers the array has. The next line contains n space-separated integers — the array's description. All elements of the array lie in the range from 1 to 109, inclusive.", "output_spec": "Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.", "sample_inputs": ["5\n1 2 3 4 5", "5\n2 3 4 5 6", "3\n2 2 2"], "sample_outputs": ["1 1 2 3 4", "1 2 3 4 5", "1 2 2"], "notes": null}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\n\nvoid main() {\n\tint n;\n\tint[] a;\n\tint temp;\n\n\treadf(\" %s %s\", &n, &temp);\n\n\ta ~= temp;\n\tint max = temp;\n\n\tint idx = 0;\n\tforeach (i; 1 .. n) {\n\t\treadf(\" %s\", &temp);\n\t\ta ~= temp;\n\t\tif (temp > max) {\n\t\t\tmax = temp;\n\t\t\tidx = i;\n\t\t}\n\t}\n\n\ta[idx] = (a[idx] == 1) ? 2 : 1;\n\n\twritefln(\"%(%s %)\", sort(a));\n}"}, {"source_code": "import std.stdio\n , std.typecons\n , std.traits\n , std.algorithm\n , std.range\n , std.container;\n\n\nvoid main() {\n const len = {\n uint t;\n readf(\"%s\\n\", &t);\n return t;\n }();\n \n uint[] data = new uint[len];\n \n auto mymax = tuple(0, 0);\n foreach(idx, ref i; data) {\n readf(\"%s \", &i);\n if(i > mymax[1]) { mymax[1] = i; mymax[0] = idx; }\n }\n \n if(mymax[1] != 1) {\n data[mymax[0]] = 1;\n }\n else\n {\n data[mymax[0]] = 2;\n }\n \n /*data.sort()\n .y!((auto ref arr){\n foreach(ref a; arr) {\n write(a, \" \");\n }\n });\n */\n \n (auto ref arr){\n foreach(ref a; arr) {\n write(a, \" \");\n }\n }\n ( data.sort() );\n}\n\nalias y(alias clos) = clos;\n\nalias var(alias elem) = std.traits.Unqual!(typeof(elem));\n\nauto mut(T)(auto ref T in_val) {\n std.traits.Unqual!(T) rv = in_val;\n return rv;\n}\n\nauto imut(T)(auto ref T in_val) {\n immutable immrv = in_val;\n return immrv;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n int N; scanf(\"%d\\n\", &N);\n auto xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n\n auto ys = new int[N];\n if (xs.all!\"a == 1\") {\n ys[] = 1;\n ys.back = 2;\n } else {\n ys[0] = 1;\n for (int i = 1; i < N; i++) {\n ys[i] = xs[i - 1];\n }\n }\n writefln(\"%(%s %)\", ys);\n}\n"}], "negative_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\n\nvoid main() {\n\tint n;\n\tint[] a;\n\tint temp;\n\n\treadf(\" %s %s\", &n, &temp);\n\n\ta ~= temp;\n\tint max = temp;\n\n\tint idx = 0;\n\tforeach (i; 1 .. n) {\n\t\treadf(\" %s\", &temp);\n\t\ta ~= temp;\n\t\tif (temp > max) {\n\t\t\tmax = temp;\n\t\t\tidx = i;\n\t\t}\n\t}\n\n\t(a[idx] == 1) ? a[idx] = 2 : a[idx] = 1;\n\n\twritefln(\"%(%s %)\", sort(a));\n}"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n int N; scanf(\"%d\\n\", &N);\n auto xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n\n auto ys = new int[N];\n ys[0] = 1;\n for (int i = 1; i < N; i++) {\n ys[i] = xs[i - 1];\n }\n writefln(\"%(%s %)\", ys);\n}\n"}, {"source_code": "import std.stdio;\nimport std.ascii;\nimport std.range;\nimport std.array;\nimport std.functional;\nimport std.algorithm;\nimport std.conv;\nimport std.container;\nimport std.math;\nimport std.numeric;\nimport std.string;\nimport std.c.string;\nimport std.random;\nimport std.regex;\nimport std.typecons;\n\nvoid main() {\n int N; scanf(\"%d\\n\", &N);\n auto xs = readln.chomp.split(\" \").map!(to!int).array;\n xs.sort;\n\n auto ys = new int[N];\n if (N == 1 && xs[0] == 1) {\n writeln(2);\n return;\n }\n ys[0] = 1;\n for (int i = 1; i < N; i++) {\n ys[i] = xs[i - 1];\n }\n writefln(\"%(%s %)\", ys);\n}\n"}], "src_uid": "1cd10f01347d869be38c08ad64266870"} {"nl": {"description": "Recently Max has got himself into popular CCG \"BrainStone\". As \"BrainStone\" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:Max owns n creatures, i-th of them can be described with two numbers — its health hpi and its damage dmgi. Max also has two types of spells in stock: Doubles health of the creature (hpi := hpi·2); Assigns value of health of the creature to its damage (dmgi := hpi). Spell of first type can be used no more than a times in total, of the second type — no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.", "input_spec": "The first line contains three integers n, a, b (1 ≤ n ≤ 2·105, 0 ≤ a ≤ 20, 0 ≤ b ≤ 2·105) — the number of creatures, spells of the first type and spells of the second type, respectively. The i-th of the next n lines contain two number hpi and dmgi (1 ≤ hpi, dmgi ≤ 109) — description of the i-th creature.", "output_spec": "Print single integer — maximum total damage creatures can deal.", "sample_inputs": ["2 1 1\n10 15\n6 1", "3 0 3\n10 8\n7 11\n5 2"], "sample_outputs": ["27", "26"], "notes": "NoteIn the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6·2 = 27.In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nimmutable long INF = 1L << 59;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto A = s[1];\n auto B = s[2];\n auto H = new long[](N);\n auto D = new long[](N);\n foreach (i; 0..N) {\n auto t = readln.split.map!(to!long);\n H[i] = t[0];\n D[i] = t[1];\n }\n\n auto S = D.sum;\n auto DD = new long[](N);\n foreach (i; 0..N) DD[i] = max(0L, H[i] - D[i]);\n DD.sort!\"a > b\"();\n auto DDS = DD[0..min(N, B)].sum;\n long ans = 0;\n\n if (A == 0) {\n writeln(S + DDS);\n return;\n }\n\n if (B == 0) {\n writeln(S);\n return;\n }\n\n foreach (i; 0..N) {\n long tmp = (H[i] << A) + S - D[i] + DDS;\n long dd = max(0L, H[i] - D[i]);\n if (B >= N || dd > DD[B]) {\n tmp = tmp - dd;\n } else {\n tmp = tmp - DD[B-1];\n }\n ans = max(ans, tmp);\n }\n\n ans.writeln;\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n b = min (b, n);\n \n long [][] arr;\n foreach ( _; 0..n ) arr ~= readln.splitter.map! (to!long).array;\n \n arr.sort! (q{ a[0] - a[1] > b[0] - b[1] });\n \n debug { writeln (arr); }\n \n auto valUseB = (long[] e) => max (e[0], e[1]);\n \n auto ansNoA = arr\n .enumerate\n .map! (t => t.index < b ? valUseB (t.value) : t.value[1])\n .sum;\n \n if (b == 0) {\n writeln (ansNoA);\n return;\n }\n \n auto smallestGainOnB = valUseB (arr[b-1]) - arr[b-1][1];\n auto valUseAB = (long[] e) => max (e[0] * 2^^a, e[1]);\n auto gainOnA = (ulong i, long[] e) => valUseAB (e) - (i < b ? valUseB (e) : e[1] + smallestGainOnB);\n auto ans = ansNoA + arr.enumerate.map! (t => gainOnA (t.index, t.value)).maxElement;\n \n debug { writeln (ansNoA, ' ', arr.enumerate.map! (t => gainOnA (t.index, t.value))); }\n writeln (ans);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n b = min (b, n);\n \n Tuple!(long, long) [] arr;\n foreach ( _; 0..n ) {\n auto r = readln.splitter.map! (to!long).array;\n arr ~= tuple(r[0], r[1]);\n }\n \n arr.sort! (q{ a[0] - a[1] > b[0] - b[1] });\n \n debug { writeln (arr); }\n \n auto valUseB = (Tuple!(long, long) e) => max (e[0], e[1]);\n \n auto ansNoA = arr\n .enumerate\n .map! (t => t.index < b ? valUseB (t.value) : t.value[1])\n .sum;\n \n if (b == 0) {\n writeln (ansNoA);\n return;\n }\n \n auto smallestGainOnB = valUseB (arr[b-1]) - arr[b-1][1];\n auto valUseAB = (Tuple!(long, long) e) => max (e[0] * 2^^a, e[1]);\n auto gainOnA = (ulong i, Tuple!(long, long) e) => valUseAB (e) - (i < b ? valUseB (e) : e[1] + smallestGainOnB);\n auto ans = ansNoA + arr.enumerate.map! (t => gainOnA (t.index, t.value)).maxElement;\n \n debug { writeln (ansNoA, ' ', arr.enumerate.map! (t => gainOnA (t.index, t.value))); }\n writeln (ans);\n}"}], "negative_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;\n\nimmutable long INF = 1L << 59;\n\nvoid main() {\n auto s = readln.split.map!(to!int);\n auto N = s[0];\n auto A = s[1];\n auto B = s[2];\n auto H = new long[](N);\n auto D = new long[](N);\n foreach (i; 0..N) {\n auto t = readln.split.map!(to!long);\n H[i] = t[0];\n D[i] = t[1];\n }\n\n auto S = D.sum;\n auto DD = new long[](N);\n foreach (i; 0..N) DD[i] = max(0L, H[i] - D[i]);\n DD.sort!\"a > b\"();\n auto DDS = DD[0..min(N, B)].sum;\n long ans = 0;\n\n if (A == 0) {\n writeln(S + DDS);\n return;\n }\n\n foreach (i; 0..N) {\n long tmp = (H[i] << A) + S - D[i] + DDS;\n long dd = max(0L, H[i] - D[i]);\n if (B >= N) {\n tmp -= dd;\n } else if (dd > DD[B]) {\n tmp = tmp - dd + DD[B];\n }\n ans = max(ans, tmp);\n }\n\n ans.writeln;\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n \n long [][] arr;\n foreach (_; 0..n) arr ~= readln.splitter.map!(to!long).array;\n \n debug { writeln (arr); }\n \n long mult = 2 ^^ a;\n auto gainMult = (long[] x) => x[0] * mult - x[1];\n int mxind = cast(int) arr.map!(a => gainMult(a)).maxIndex;\n arr[mxind][0] *= mult;\n \n debug { writeln (mxind, ' ', arr[mxind][0]); }\n \n auto gain = (long[] x) => x[0] - x[1];\n long[] gains = arr.map!(x => gain(x)).array;\n debug { writeln (arr, ' ', gains); }\n auto ans = arr.map!(q{ a[1] }).sum(0L) +\n gains\n .sort!(q{ a > b })\n .until!(q{ a <= 0 })\n .take(b)\n .sum(0L);\n \n writeln (ans);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n \n long [][] arr;\n foreach (_; 0..n) arr ~= readln.splitter.map! (to!long).array;\n \n debug { writeln (arr); }\n \n auto gain = (long[] x) => x[0] * 2^^a - x[1];\n auto powerUpOrder = arr.sort! ((a, b) => gain (a) > gain (b));\n \n debug { writeln (powerUpOrder); }\n \n auto ans = \n arr.map! (q{ a[1] }).sum\n + powerUpOrder\n .take (b)\n .map! (x => gain (x))\n .until! (q{ a <= 0 })\n .sum;\n \n writeln (ans);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n b = min (b, n);\n \n long [][] arr;\n foreach ( _; 0..n ) arr ~= readln.splitter.map! (to!long).array;\n \n arr.sort! (q{ a[0] - a[1] > b[0] - b[1] });\n \n debug { writeln (arr); }\n \n auto valUseB = (long[] e) => max (e[0], e[1]);\n \n auto ansNoA = arr\n .enumerate\n .map! (t => t.index < b ? valUseB (t.value) : t.value[1])\n .sum;\n \n if (b == 0) {\n writeln (ansNoA);\n return;\n }\n \n auto smallestGainOnB = valUseB (arr[b-1]) - arr[b-1][1];\n auto valUseAB = (long[] e) => max (e[0] * 2^^a, e[1]);\n auto gainOnA = (ulong i, long[] e) => valUseAB (e) - valUseB (e) - (i < b ? 0 : smallestGainOnB);\n auto ans = ansNoA + arr.enumerate.map! (t => gainOnA (t.index, t.value)).maxElement;\n \n debug { writeln (ansNoA, ' ', arr.enumerate.map! (t => gainOnA (t.index, t.value))); }\n writeln (ans);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n \n long [][] arr;\n foreach (_; 0..n) arr ~= readln.splitter.map!(to!long).array;\n \n long ans = arr.map!(q{ a[1] }).sum;\n \n debug { writeln (arr, ' ', ans); }\n \n int mult = 2 ^^ a;\n auto gainMult = (long[] x) => x[0] * mult - x[1];\n int mxind = cast(int) arr.map!(a => gainMult(a)).maxIndex;\n arr[mxind][0] *= mult;\n \n debug { writeln (mxind, ' ', arr[mxind][0]); }\n \n auto gain = (long[] x) => x[0] - x[1];\n long[] gains = arr.map!(x => gain(x)).array;\n ans += gains\n .sort!(q{ a > b })\n .until!(q{ a <= 0 })\n .take(b)\n .sum;\n \n writeln (ans);\n}"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main()\n{\n int n, a, b;\n readf (\"%s %s %s\", &n, &a, &b);\n readln;\n \n long [][] arr;\n foreach (_; 0..n) arr ~= readln.splitter.map!(to!long).array;\n \n long ans = arr.map!(q{ a[1] }).sum;\n \n debug { writeln (arr, ' ', ans); }\n \n long mult = 2 ^^ a;\n auto gainMult = (long[] x) => x[0] * mult - x[1];\n int mxind = cast(int) arr.map!(a => gainMult(a)).maxIndex;\n arr[mxind][0] *= mult;\n \n debug { writeln (mxind, ' ', arr[mxind][0]); }\n \n auto gain = (long[] x) => x[0] - x[1];\n long[] gains = arr.map!(x => gain(x)).array;\n ans += gains\n .sort!(q{ a > b })\n .until!(q{ a <= 0 })\n .take(b)\n .sum;\n \n writeln (ans);\n}"}], "src_uid": "87f4b8523d0047935931906ccc2ea911"} {"nl": {"description": "There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?Note that outcome of a match can not be a draw, it has to be either win or loss.", "input_spec": "The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.", "output_spec": "For each test case, output a single line containing either \"yes\" if it is possible to have no winner of tournament, or \"no\" otherwise (without quotes).", "sample_inputs": ["5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2"], "sample_outputs": ["yes\nyes\nyes\nno\nno"], "notes": "NoteSample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is \"yes\".Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins)."}, "positive_code": [{"source_code": "module main;\n\nimport std.stdio, std.string, std.algorithm;\n\nint check(long n, long k, long d1, long d2)\n{\n long tmp = d1 * 2 + d2 + k;\n if (tmp < 0 || tmp % 3 != 0)\n {\n return 0;\n }\n long x = tmp / 3;\n tmp = k + d2 - d1;\n if (tmp < 0 || tmp % 3 != 0)\n {\n return 0;\n }\n long y = tmp / 3;\n tmp = k - d1 - d2 * 2;\n if (tmp < 0 || tmp % 3 != 0)\n {\n return 0;\n }\n long z = tmp / 3;\n long left = n - k;\n auto p = [x, y, z];\n sort(p);\n long need = p[2] - p[0] + p[2] - p[1];\n if (need > left)\n {\n return 0;\n }\n left -= need;\n if (left % 3 == 0)\n {\n writeln(\"yes\");\n return 1;\n }\n return 0;\n}\n\nvoid solve(long n, long k, long d1, long d2)\n{\n if (check(n, k, d1, d2)) return;\n if (check(n, k, -d1, d2)) return;\n if (check(n, k, d1, -d2)) return;\n if (check(n, k, -d1, -d2)) return;\n writeln(\"no\");\n}\n\nint main(string[] args)\n{\n int t;\n while (scanf(\"%d\", &t) == 1)\n {\n foreach (i; 0 .. t)\n {\n long n, k, d1, d2;\n scanf(\"%lld%lld%lld%lld\", &n, &k, &d1, &d2);\n solve(n, k, d1, d2);\n }\n }\n return 0;\n}"}, {"source_code": "import core.thread;\nimport std.conv, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.math, std.range;\n\n//\tInput\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }\nint readInt() { return to!int(readToken); }\nlong readLong() { return to!long(readToken); }\nreal readReal() { return to!real(readToken); }\n\n//\tchmin/chmax\nvoid chmin(T)(ref T t, const T f) { if (t > f) t = f; }\nvoid chmax(T)(ref T t, const T f) { if (t < f) t = f; }\n\n//\tPair\nstruct Pair(S, T) {\n\tS x; T y;\n\tint opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }\n\tstring toString() const { return \"(\" ~ to!string(x) ~ \", \" ~ to!string(y) ~ \")\"; }\n}\nauto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }\n\n//\tArray\nint binarySearch(T)(T[] as, 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; }\nint lowerBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a < val); }); }\nint upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }\nT[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }\n\n\n\nbool check(long n, long a, long b, long c) {\n\tlong x = min(a, b, c);\n\ta -= x;\n\tb -= x;\n\tc -= x;\n\tlong rem = n - a - b - c;\n\treturn (rem >= 0 && rem % 3 == 0);\n}\n\nbool solve(long N, long K, long D1, long D2) {\n\tforeach (s; [ +1, -1 ]) foreach (t; [ +1, -1 ]) {\n\t\tif (check(K, D1 * s, 0, D2 * t) && check(N - K, D1 * -s, 0, D2 * -t)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid main(string[] args) {\n\t// try {\n\tfor (int TC = readInt; TC--; ) {\n\t\tlong N = readLong;\n\t\tlong K = readLong;\n\t\tlong D1 = readLong;\n\t\tlong D2 = readLong;\n\t\tbool res = solve(N, K, D1, D2);\n\t\twriteln(res ? \"yes\" : \"no\");\n\t}\n\t// } catch (EOFException) {}\n}\n\n"}], "negative_code": [{"source_code": "module main;\n\nimport std.stdio, std.string, std.algorithm;\n\nint check(long n, long k, long d1, long d2)\n{\n long tmp = d1 * 2 + d2 + k;\n if (tmp < 0 || tmp % 3 != 0)\n {\n return 0;\n }\n long x = tmp / 3;\n tmp = k + d2 - d1;\n if (tmp < 0 || tmp % 3 != 0)\n {\n return 0;\n }\n long y = tmp / 3;\n tmp = k - d1 - d2 * 2;\n if (tmp < 0 || tmp % 3 != 0)\n {\n return 0;\n }\n long z = tmp / 3;\n //writeln(x, \" \", y, \" \", z);\n long left = n - k;\n auto p = [x, y, z];\n sort(p);\n long need = p[2] - p[0] + p[2] - p[1];\n if (need > left)\n {\n return 0;\n }\n left -= need;\n if (left % 3 == 0)\n {\n writeln(\"yes\");\n return 1;\n }\n return 0;\n}\n\nvoid solve(long n, long k, long d1, long d2)\n{\n if (check(n, k, d1, d2)) return;\n if (check(n, k, -d1, d2)) return;\n if (check(n, k, d1, -d2)) return;\n if (check(n, k, -d1, -d2)) return;\n writeln(\"no\");\n}\n\nint main(string[] args)\n{\n int t;\n while (scanf(\"%d\", &t) == 1)\n {\n foreach (i; 0 .. t)\n {\n long n, k, d1, d2;\n scanf(\"%d%d%d%d\", &n, &k, &d1, &d2);\n solve(n, k, d1, d2);\n }\n }\n return 0;\n}"}, {"source_code": "module main;\n\nimport std.stdio, std.string;\n\nvoid solve(long n, long k, long d1, long d2)\n{\n long cnt = d1 + d1 + d2;\n long left = n - k;\n if (cnt > left)\n {\n writeln(\"no\");\n }\n else\n {\n left -= cnt;\n if (left % 3 == 0)\n {\n writeln(\"yes\");\n }\n else\n {\n writeln(\"no\");\n }\n }\n}\n\nint main(string[] args)\n{\n int t;\n while (scanf(\"%d\", &t) == 1)\n {\n foreach (i; 0 .. t)\n {\n long n, k, d1, d2;\n scanf(\"%d%d%d%d\", &n, &k, &d1, &d2);\n solve(n, k, d1, d2);\n }\n }\n return 0;\n}"}], "src_uid": "324298100f3e36d289ef6ca8178ac6d3"} {"nl": {"description": "You are given an array $$$a$$$ that contains $$$n$$$ integers. You can choose any proper subsegment $$$a_l, a_{l + 1}, \\ldots, a_r$$$ of this array, meaning you can choose any two integers $$$1 \\le l \\le r \\le n$$$, where $$$r - l + 1 < n$$$. We define the beauty of a given subsegment as the value of the following expression:$$$$$$\\max(a_{1}, a_{2}, \\ldots, a_{l-1}, a_{r+1}, a_{r+2}, \\ldots, a_{n}) - \\min(a_{1}, a_{2}, \\ldots, a_{l-1}, a_{r+1}, a_{r+2}, \\ldots, a_{n}) + \\max(a_{l}, \\ldots, a_{r}) - \\min(a_{l}, \\ldots, a_{r}).$$$$$$Please find the maximum beauty among all proper subsegments.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. Then follow the descriptions of each test case. The first line of each test case contains a single integer $$$n$$$ $$$(4 \\leq n \\leq 10^5)$$$ — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_{i} \\leq 10^9$$$) — the elements of the given array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each testcase print a single integer — the maximum beauty of a proper subsegment.", "sample_inputs": ["4\n8\n1 2 2 3 1 5 6 1\n5\n1 2 3 100 200\n4\n3 3 3 3\n6\n7 8 3 1 1 8"], "sample_outputs": ["9\n297\n0\n14"], "notes": "NoteIn the first test case, the optimal segment is $$$l = 7$$$, $$$r = 8$$$. The beauty of this segment equals to $$$(6 - 1) + (5 - 1) = 9$$$.In the second test case, the optimal segment is $$$l = 2$$$, $$$r = 4$$$. The beauty of this segment equals $$$(100 - 2) + (200 - 1) = 297$$$."}, "positive_code": [{"source_code": "// cheese-cracker [2022-08-18]\n\nvoid solve(){\n int n = scan!int;\n auto arr = scanArray;\n arr.sort;\n long res = arr[n-1] + arr[n-2] - (arr[0] + arr[1]);\n writeln(res);\n}\n\nvoid main(){\n long tests = scan; // Toggle!\n while(tests--) solve;\n}\n\n/*_________________________*That's All Folks!*__________________________*/\nimport std.stdio, std.range, std.conv, std.typecons, std.algorithm;\nimport std.container, std.math, std.numeric, std.functional;\nstring[] tk; alias tup = Tuple!(long, long);\nT scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}\nT[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }\nvoid show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, \"| \");} stderr.writeln; } }\n"}], "negative_code": [], "src_uid": "1e54530bc8cff81199b9cc1a6e51d638"} {"nl": {"description": "Nicholas, a painter is going to paint several new canvases. Nicholas is sure that the canvases will turn out so great that each one will need framing and being hung on the wall. Frames are what Nicholas decided to begin with. Nicholas has n sticks whose lengths equal a1, a2, ... an. Nicholas does not want to break the sticks or glue them together. To make a h × w-sized frame, he needs two sticks whose lengths equal h and two sticks whose lengths equal w. Specifically, to make a square frame (when h = w), he needs four sticks of the same length.Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of sticks. The second line contains n space-separated integers. The i-th integer equals the length of the i-th stick ai (1 ≤ ai ≤ 100).", "output_spec": "Print the single number — the maximum number of frames Nicholas can make for his future canvases.", "sample_inputs": ["5\n2 4 3 2 3", "13\n2 2 4 4 4 4 6 6 6 7 7 9 9", "4\n3 3 3 5"], "sample_outputs": ["1", "3", "0"], "notes": null}, "positive_code": [{"source_code": "module _template;\nimport std.stdio;\nimport std.string;\nimport std.algorithm;\nimport std.container;\nimport std.range;\nimport std.math;\nimport std.numeric;\nimport std.conv;\nimport std.typecons;\nimport std.format;\n\nstruct IO {\n string[] tk;\n string readString() {\n while (tk.empty) \n tk = readln.split;\n auto tkt = tk.front;\n tk.popFront;\n return tkt;\n }\n int readInt() { \n return readString.to!int; \n }\n double readDouble() { \n return readString.to!double; \n }\n}\n\nvoid main() {\n IO cin;\n int n_Cases = 1;\n // n_Cases = cin.readInt;\n \n\tfor (int case_i = 1; case_i <= n_Cases; case_i++) {\n int n = cin.readInt;\n int[] arr = new int[101];\n for (int i = 0; i < n; i++) {\n int input = cin.readInt;\n arr[input]++;\n }\n int k = 0;\n foreach (i; arr) k += i / 2;\n writeln(k / 2);\n\t} \n}"}], "negative_code": [], "src_uid": "f9b56b3fddcd5db0d0671714df3f8646"} {"nl": {"description": "You are organizing a boxing tournament, where $$$n$$$ boxers will participate ($$$n$$$ is a power of $$$2$$$), and your friend is one of them. All boxers have different strength from $$$1$$$ to $$$n$$$, and boxer $$$i$$$ wins in the match against boxer $$$j$$$ if and only if $$$i$$$ is stronger than $$$j$$$.The tournament will be organized as follows: $$$n$$$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $$$\\frac{n}{2}$$$ winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.Furthermore, during each stage you distribute the boxers into pairs as you wish.The boxer with strength $$$i$$$ can be bribed if you pay him $$$a_i$$$ dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?", "input_spec": "The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 2^{18}$$$) — the number of boxers. $$$n$$$ is a power of $$$2$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ is the number of dollars you have to pay if you want to bribe the boxer with strength $$$i$$$. Exactly one of $$$a_i$$$ is equal to $$$-1$$$ — it means that the boxer with strength $$$i$$$ is your friend. All other values are in the range $$$[1, 10^9]$$$.", "output_spec": "Print one integer — the minimum number of dollars you have to pay so your friend wins.", "sample_inputs": ["4\n3 9 1 -1", "8\n11 -1 13 19 24 7 17 5"], "sample_outputs": ["0", "12"], "notes": "NoteIn the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.In the second test case you can distribute boxers as follows (your friend is number $$$2$$$):$$$1 : 2, 8 : 5, 7 : 3, 6 : 4$$$ (boxers $$$2, 8, 7$$$ and $$$6$$$ advance to the next stage);$$$2 : 6, 8 : 7$$$ (boxers $$$2$$$ and $$$8$$$ advance to the next stage, you have to bribe the boxer with strength $$$6$$$);$$$2 : 8$$$ (you have to bribe the boxer with strength $$$8$$$);"}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint;\n\tlong[] as = rlong(n);\n\n\tauto ah = new RedBlackTree!(long, \"a 1);\r\n\t\t\t}\r\n\t\t\tforeach (s; divs.find (last))\r\n\t\t\t{\r\n\t\t\t\tif (y < s)\r\n\t\t\t\t{\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t\tif (y % s == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (recur (y / s, s))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\treturn true;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn false;\r\n\t\t}\r\n\r\n\t\twriteln (recur (x, d) ? \"YES\" : \"NO\");\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "ee27020ca43546993b82357527585831"} {"nl": {"description": " This is an interactive taskWilliam has a certain sequence of integers $$$a_1, a_2, \\dots, a_n$$$ in his mind, but due to security concerns, he does not want to reveal it to you completely. William is ready to respond to no more than $$$2 \\cdot n$$$ of the following questions: What is the result of a bitwise AND of two items with indices $$$i$$$ and $$$j$$$ ($$$i \\neq j$$$) What is the result of a bitwise OR of two items with indices $$$i$$$ and $$$j$$$ ($$$i \\neq j$$$) You can ask William these questions and you need to find the $$$k$$$-th smallest number of the sequence.Formally the $$$k$$$-th smallest number is equal to the number at the $$$k$$$-th place in a 1-indexed array sorted in non-decreasing order. For example in array $$$[5, 3, 3, 10, 1]$$$ $$$4$$$th smallest number is equal to $$$5$$$, and $$$2$$$nd and $$$3$$$rd are $$$3$$$.", "input_spec": "It is guaranteed that for each element in a sequence the condition $$$0 \\le a_i \\le 10^9$$$ is satisfied.", "output_spec": null, "sample_inputs": ["7 6\n\n2\n\n7"], "sample_outputs": ["and 2 5\n\nor 5 6\n\nfinish 5"], "notes": "NoteIn the example, the hidden sequence is $$$[1, 6, 4, 2, 3, 5, 4]$$$.Below is the interaction in the example.Query (contestant's program)Response (interactor)Notesand 2 52$$$a_2=6$$$, $$$a_5=3$$$. Interactor returns bitwise AND of the given numbers.or 5 67$$$a_5=3$$$, $$$a_6=5$$$. Interactor returns bitwise OR of the given numbers.finish 5$$$5$$$ is the correct answer. Note that you must find the value and not the index of the kth smallest number. "}, "positive_code": [{"source_code": "immutable multi = false;\n\nvoid solve(int tc)\n{\n\tint[] testArray = null;\n\tauto n = testArray is null? readInt!int : cast(int)testArray.length;\n\tauto k = testArray is null? readInt!int : 1;\n\tauto or = new int[](n-1);\n\tauto nd = new int[](n-1);\n\tauto ans = new int[](n);\n\tint query(string op, int i, int j)\n\t{\n\t\tif (testArray is null)\n\t\t{\n\t\t\twriteln(op, \" \", i, \" \", j);\n\t\t\tstdout.flush;\n\t\t\treturn readInt!int;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tswitch(op)\n\t\t\t{\n\t\t\tcase \"or\":\n\t\t\t\treturn testArray[i-1] | testArray[j-1];\n\t\t\tcase \"and\":\n\t\t\t\treturn testArray[i-1] & testArray[j-1];\n\t\t\tdefault: assert(0);\n\t\t\t}\n\t\t}\n\t}\n\tauto or23 = query(\"or\", 2, 3);\n\tforeach(i; 1 .. n)\n\t{\n\t\tor[i-1] = query(\"or\", 1, i + 1);\n\t\tnd[i-1] = query(\"and\", 1, i + 1);\n\t}\n\tauto andedOrs = or.fold!((a, b) => a & b);\n\tauto oredAns = nd.fold!((a, b) => a | b);\n\tvoid solveBit(int q)\n\t{\n\t\tif (andedOrs&(1<> b) & 1, (andab >> b) & 1, (orbc >> b) & 1, (andbc >> b) & 1, (andac >> b) & 1, (orac >> b) & 1) << b;\n }\n return ans;\n}\n\nvoid main()\n{\n int n, k;\n readln.formattedRead!\" %d %d \"(n, k);\n long[] a_xor, a_or, a_and;\n\n foreach (i ; 0 .. n - 1) {\n int idx1 = (i + 0) % n + 1;\n int idx2 = (i + 1) % n + 1;\n long qor = query(0, idx1, idx2);\n long qand = query(1, idx1, idx2);\n a_or ~= qor;\n a_and ~= qand;\n a_xor ~= (qor - qand);\n }\n\n long orab = a_or[0];\n long andab = a_and[0];\n long orbc = a_or[1];\n long andbc = a_and[1];\n long orac = query(0, 1, 3);\n long andac = query(1, 1, 3);\n long a0 = recover_a32(orab, andab, orbc, andbc, andac, orac);\n long[] ans = [a0];\n foreach (i ; 1 .. n) {\n ans ~= ans[i - 1] ^ a_xor[i - 1];\n }\n ans.sort;\n writefln(\"finish %d\", ans[k - 1]);\n}\n"}, {"source_code": "import std.stdio, std.string;\r\nimport std.conv, std.typecons;\r\nimport std.array, std.range;\r\nimport std.math, std.algorithm;\r\n\r\nint main(string[] args)\r\n{\r\n int n, k;\r\n readf(\"%d %d\\n\", &n, &k);\r\n auto r = new int[][][](3, 3, 2);\r\n foreach (i; 1 .. 4) foreach (j; i + 1 .. 4)\r\n {\r\n writeln(\"or \", i, \" \", j);\r\n stdout.flush;\r\n r[i - 1][j - 1][0] = to!int(readln.strip);\r\n if (i == 1 && j == 3) continue;\r\n writeln(\"and \", i, \" \", j);\r\n stdout.flush;\r\n r[i - 1][j - 1][1] = to!int(readln.strip);\r\n }\r\n auto a = new int[n];\r\n foreach (i; 0 .. 30)\r\n {\r\n if (r[0][1][1] & (1 << i))\r\n {\r\n a[0] |= 1 << i;\r\n continue;\r\n }\r\n if (!(r[0][1][0] & (1 << i)) || !(r[0][2][0] & (1 << i))) continue;\r\n if (!(r[1][2][1] & (1 << i))) a[0] |= 1 << i;\r\n }\r\n foreach (i; 1 .. 3) foreach (j; 0 .. 30)\r\n {\r\n if (r[i - 1][i][1] & (1 << j)) a[i] |= 1 << j;\r\n else if ((r[i - 1][i][0] & (1 << j)) && !(a[i - 1] & (1 << j))) a[i] |= 1 << j;\r\n }\r\n foreach (i; 3 .. n)\r\n {\r\n writeln(\"or \", i, \" \", i + 1);\r\n stdout.flush;\r\n auto ro = to!int(readln.strip);\r\n writeln(\"and \", i, \" \", i + 1);\r\n stdout.flush;\r\n auto ra = to!int(readln.strip);\r\n foreach (j; 0 .. 30)\r\n {\r\n if (ra & (1 << j)) a[i] |= 1 << j;\r\n else if ((ro & (1 << j)) && !(a[i - 1] & (1 << j))) a[i] |= 1 << j;\r\n }\r\n }\r\n a.sort;\r\n writeln(\"finish \", a[k - 1]);\r\n stdout.flush;\r\n return 0;\r\n}"}, {"source_code": "import std.algorithm, std.array, std.container, std.conv, std.format, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;\r\n\r\nlong query(int type, int idx1, int idx2)\r\n{\r\n if (type == 0) {\r\n writefln(\"or %d %d\", idx1, idx2);\r\n stdout.flush();\r\n return readln.strip.to!long;\r\n } else {\r\n writefln(\"and %d %d\", idx1, idx2);\r\n stdout.flush();\r\n return readln.strip.to!long;\r\n }\r\n}\r\n\r\n/* 1-bit bruteforce version */\r\n\r\nlong recover_a1(long orab, long andab, long orbc, long andbc, long andac, long orac)\r\n{\r\n long ans;\r\n long cnt;\r\n foreach (a ; 0 .. 2) {\r\n foreach (b ; 0 .. 2) {\r\n foreach (c ; 0 .. 2) {\r\n if (andbc == (b & c) && orbc == (b | c) && andab == (a & b) && orab == (a | b) && andac == (a & c) && orac == (a | c)) {\r\n ans = a;\r\n cnt++;\r\n }\r\n }\r\n }\r\n }\r\n assert(cnt == 1);\r\n return ans;\r\n}\r\n\r\n/*\r\n * We know: A | B, A & B, B | C, B & C, A | C, A & C\r\n * Need to recover and return a 32-bit value A.\r\n */\r\n \r\nlong recover_a32(long orab, long andab, long orbc, long andbc, long andac, long orac)\r\n{\r\n long ans;\r\n foreach (b ; 0 .. 32) {\r\n ans |= recover_a1((orab >> b) & 1, (andab >> b) & 1, (orbc >> b) & 1, (andbc >> b) & 1, (andac >> b) & 1, (orac >> b) & 1) << b;\r\n }\r\n return ans;\r\n}\r\n\r\nvoid main()\r\n{\r\n int n, k;\r\n readln.formattedRead!\" %d %d \"(n, k);\r\n long[] a_xor, a_or, a_and;\r\n\r\n foreach (i ; 0 .. n - 1) {\r\n int idx1 = (i + 0) % n + 1;\r\n int idx2 = (i + 1) % n + 1;\r\n long qor = query(0, idx1, idx2);\r\n long qand = query(1, idx1, idx2);\r\n a_or ~= qor;\r\n a_and ~= qand;\r\n a_xor ~= (qor - qand);\r\n }\r\n\r\n long orab = a_or[0];\r\n long andab = a_and[0];\r\n long orbc = a_or[1];\r\n long andbc = a_and[1];\r\n long orac = query(0, 1, 3);\r\n long andac = query(1, 1, 3);\r\n long a0 = recover_a32(orab, andab, orbc, andbc, andac, orac);\r\n long[] ans = [a0];\r\n foreach (i ; 1 .. n) {\r\n ans ~= ans[i - 1] ^ a_xor[i - 1];\r\n }\r\n ans.sort;\r\n writefln(\"finish %d\", ans[k - 1]);\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\nimport std.meta;\r\n\r\nint ask (string op) (int a, int b)\r\n{\r\n\twriteln (op, \" \", a + 1, \" \", b + 1);\r\n\tstdout.flush ();\r\n\treturn readln.strip.to !(int);\r\n}\r\n\r\nvoid main ()\r\n{\r\n\tint n, k;\r\n\twhile (readf !(\" %s %s\") (n, k) > 0)\r\n\t{\r\n\t\tk -= 1;\r\n\t\treadln;\r\n\r\n\t\tint [3] [3] or;\r\n\t\tint [3] [3] and;\r\n\t\tstatic foreach (s; AliasSeq !(\"or\", \"and\"))\r\n\t\t{\r\n\t\t\tforeach (i; 0..3)\r\n\t\t\t{\r\n\t\t\t\tforeach (j; i + 1..3)\r\n\t\t\t\t{\r\n\t\t\t\t\tmixin (format !(`%s[i][j] = ` ~\r\n\t\t\t\t\t `ask !(\"%s\") (i, j);`) (s, s));\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tbool checkBit (int [3] bits)\r\n\t\t{\r\n\t\t\tforeach (i; 0..3)\r\n\t\t\t{\r\n\t\t\t\tforeach (j; i + 1..3)\r\n\t\t\t\t{\r\n\t\t\t\t\tif ((bits[i] | bits[j]) !=\r\n\t\t\t\t\t (or[i][j] & 1))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\treturn false;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif ((bits[i] & bits[j]) !=\r\n\t\t\t\t\t (and[i][j] & 1))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\treturn false;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn true;\r\n\t\t}\r\n\r\n\t\tauto a = new int [n];\r\n\r\nbits_loop:\r\n\t\tforeach (b; 0..30)\r\n\t\t{\r\n\t\t\tscope (exit)\r\n\t\t\t{\r\n\t\t\t\tstatic foreach (s; AliasSeq !(\"or\", \"and\"))\r\n\t\t\t\t{\r\n\t\t\t\t\tforeach (i; 0..3)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tforeach (j; i + 1..3)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tmixin (format\r\n\t\t\t\t\t\t\t !(`%s[i][j] ` ~\r\n\t\t\t\t\t\t\t `>>= 1;`) (s));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tforeach (x; 0..2)\r\n\t\t\t{\r\n\t\t\t\tforeach (y; 0..2)\r\n\t\t\t\t{\r\n\t\t\t\t\tforeach (z; 0..2)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (checkBit ([x, y, z]))\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\ta[0] |= x << b;\r\n\t\t\t\t\t\t\ta[1] |= y << b;\r\n\t\t\t\t\t\t\ta[2] |= z << b;\r\n\t\t\t\t\t\t\tcontinue bits_loop;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tassert (false);\r\n\t\t}\r\n\t\tdebug {writeln (a);}\r\n\r\n\t\tforeach (i; 3..n)\r\n\t\t{\r\n\t\t\tauto theOr = ask !(\"or\") (0, i);\r\n\t\t\tauto theAnd = ask !(\"and\") (0, i);\r\n\t\t\ta[i] = theAnd | (theOr & ~a[0]);\r\n\t\t\tdebug {writeln (a);}\r\n\t\t}\r\n\r\n\t\tsort (a);\r\n\t\twriteln (\"finish\", \" \", a[k]);\r\n\t\tstdout.flush ();\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "immutable multi = false;\n\nvoid solve(int tc)\n{\n\tint[] testArray = null;\n\tauto n = testArray is null? readInt!int : cast(int)testArray.length;\n\tauto k = testArray is null? readInt!int : 1;\n\tauto or = new int[](n-1);\n\tauto nd = new int[](n-1);\n\tauto ans = new int[](n);\n\tint query(string op, int i, int j)\n\t{\n\t\tif (testArray is null)\n\t\t{\n\t\t\twriteln(op, \" \", i, \" \", j);\n\t\t\tstdout.flush;\n\t\t\treturn readInt!int;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tswitch(op)\n\t\t\t{\n\t\t\tcase \"or\":\n\t\t\t\treturn testArray[i-1] | testArray[j-1];\n\t\t\tcase \"and\":\n\t\t\t\treturn testArray[i-1] & testArray[j-1];\n\t\t\tdefault: assert(0);\n\t\t\t}\n\t\t}\n\t}\n\tauto or23 = query(\"or\", 2, 3);\n\tforeach(i; 1 .. n)\n\t{\n\t\tor[i-1] = query(\"or\", 1, i + 1);\n\t\tnd[i-1] = query(\"and\", 1, i + 1);\n\t}\n\tauto andedOrs = or.fold!((a, b) => a & b);\n\tauto oredAns = nd.fold!((a, b) => a | b);\n\tvoid solveBit(int q)\n\t{\n\t\tif (andedOrs&(1< a & b);\n\tauto oredAns = nd.fold!((a, b) => a | b);\n\tvoid solveBit(int q)\n\t{\n\t\tif (andedOrs&(1<> b) & 1, (andab >> b) & 1, (orbc >> b) & 1, (andbc >> b) & 1, (andac >> b) & 1, (orac >> b) & 1) << b;\n }\n return ans;\n}\n\nvoid main()\n{\n int n, k;\n readln.formattedRead!\" %d %d \"(n, k);\n long[] a_xor, a_or, a_and;\n\n foreach (i ; 0 .. n - 1) {\n int idx1 = (i + 0) % n + 1;\n int idx2 = (i + 1) % n + 1;\n long qor = query(0, idx1, idx2);\n long qand = query(1, idx1, idx2);\n a_or ~= qor;\n a_and ~= qand;\n a_xor ~= (qor - qand);\n }\n\n long orab = a_or[0];\n long andab = a_and[0];\n long orbc = a_or[1];\n long andbc = a_and[1];\n long orac = query(0, 1, 3);\n long andac = query(1, 1, 3);\n long a0 = recover_a32(orab, andab, orbc, andbc, andac, orac);\n long[] ans = [a0];\n foreach (i ; 1 .. n) {\n ans ~= ans[i - 1] ^ a_xor[i - 1];\n }\n writeln(ans[k - 1]);\n}\n"}], "src_uid": "7fb8b73fa2948b360644d40b7035ce4a"} {"nl": {"description": "Tokitsukaze has two colorful tapes. There are $$$n$$$ distinct colors, numbered $$$1$$$ through $$$n$$$, and each color appears exactly once on each of the two tapes. Denote the color of the $$$i$$$-th position of the first tape as $$$ca_i$$$, and the color of the $$$i$$$-th position of the second tape as $$$cb_i$$$.Now Tokitsukaze wants to select each color an integer value from $$$1$$$ to $$$n$$$, distinct for all the colors. After that she will put down the color values in each colored position on the tapes. Denote the number of the $$$i$$$-th position of the first tape as $$$numa_i$$$, and the number of the $$$i$$$-th position of the second tape as $$$numb_i$$$. For example, for the above picture, assuming that the color red has value $$$x$$$ ($$$1 \\leq x \\leq n$$$), it appears at the $$$1$$$-st position of the first tape and the $$$3$$$-rd position of the second tape, so $$$numa_1=numb_3=x$$$.Note that each color $$$i$$$ from $$$1$$$ to $$$n$$$ should have a distinct value, and the same color which appears in both tapes has the same value. After labeling each color, the beauty of the two tapes is calculated as $$$$$$\\sum_{i=1}^{n}|numa_i-numb_i|.$$$$$$Please help Tokitsukaze to find the highest possible beauty. ", "input_spec": "The first contains a single positive integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$1\\leq n \\leq 10^5$$$) — the number of colors. The second line contains $$$n$$$ integers $$$ca_1, ca_2, \\ldots, ca_n$$$ ($$$1 \\leq ca_i \\leq n$$$) — the color of each position of the first tape. It is guaranteed that $$$ca$$$ is a permutation. The third line contains $$$n$$$ integers $$$cb_1, cb_2, \\ldots, cb_n$$$ ($$$1 \\leq cb_i \\leq n$$$) — the color of each position of the second tape. It is guaranteed that $$$cb$$$ is a permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^{5}$$$.", "output_spec": "For each test case, print a single integer — the highest possible beauty.", "sample_inputs": ["3\n\n6\n\n1 5 4 3 2 6\n\n5 3 1 4 6 2\n\n6\n\n3 5 4 6 2 1\n\n3 6 4 5 2 1\n\n1\n\n1\n\n1"], "sample_outputs": ["18\n10\n0"], "notes": "NoteAn optimal solution for the first test case is shown in the following figure: The beauty is $$$\\left|4-3 \\right|+\\left|3-5 \\right|+\\left|2-4 \\right|+\\left|5-2 \\right|+\\left|1-6 \\right|+\\left|6-1 \\right|=18$$$.An optimal solution for the second test case is shown in the following figure: The beauty is $$$\\left|2-2 \\right|+\\left|1-6 \\right|+\\left|3-3 \\right|+\\left|6-1 \\right|+\\left|4-4 \\right|+\\left|5-5 \\right|=10$$$."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new int[N]; foreach (i; 0 .. N) A[i] = readInt - 1;\n auto B = new int[N]; foreach (i; 0 .. N) B[i] = readInt - 1;\n \n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. N) {\n uf.connect(A[i], B[i]);\n }\n \n int[] cs;\n foreach (r; 0 .. N) if (uf[r] < 0) {\n cs ~= -uf[r];\n }\n cs.sort.reverse;\n debug {\n writeln(\"cs = \", cs);\n }\n \n long ans;\n {\n int num;\n int l = 0, r = N - 1;\n int next() {\n return (num++ % 2 == 0) ? l++ : r--;\n }\n foreach (c; cs) {\n const cc = c / 2 * 2;\n if (cc <= 1) continue;\n const s = next();\n int u = s;\n foreach (_; 1 .. cc) {\n const v = next();\n ans += abs(u - v);\n u = v;\n }\n ans += abs(u - s);\n }\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int NA = -1;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tauto n = readln.strip.to !(int);\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\ta[] -= 1;\r\n\t\tauto b = readln.splitter.map !(to !(int)).array;\r\n\t\tb[] -= 1;\r\n\r\n\t\tauto p = new int [n];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tp[a[i]] = i;\r\n\t\t}\r\n\t\tauto q = new int [n];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tq[b[i]] = i;\r\n\t\t}\r\n\t\tauto c = new int [n];\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tc[p[i]] = q[i];\r\n\t\t}\r\n\r\n\t\tlong res = 0;\r\n\t\tint lo = 0;\r\n\t\tint hi = n - 1;\r\n\t\tforeach (i; 0..n)\r\n\t\t{\r\n\t\t\tif (c[i] != NA)\r\n\t\t\t{\r\n\t\t\t\tint len = 0;\r\n\t\t\t\tint x = i;\r\n\t\t\t\twhile (c[x] != NA)\r\n\t\t\t\t{\r\n\t\t\t\t\tlen += 1;\r\n\t\t\t\t\tint y = c[x];\r\n\t\t\t\t\tc[x] = NA;\r\n\t\t\t\t\tx = y;\r\n\t\t\t\t}\r\n\t\t\t\tlen /= 2;\r\n/*\r\n\t\t\t\t 5 4 3 4\r\n\t\t\t\t1 6 2 5 1\r\n\t\t\t\t+10 +6\r\n*/\r\n\t\t\t\tforeach (step; 0..len)\r\n\t\t\t\t{\r\n\t\t\t\t\tres += hi - lo;\r\n\t\t\t\t\tlo += 1;\r\n\t\t\t\t\thi -= 1;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\twriteln (res * 2);\r\n\t}\r\n}\r\n"}], "negative_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new int[N]; foreach (i; 0 .. N) A[i] = readInt - 1;\n auto B = new int[N]; foreach (i; 0 .. N) B[i] = readInt - 1;\n \n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. N) {\n uf.connect(A[i], B[i]);\n }\n \n int[] cs;\n foreach (r; 0 .. N) if (uf[r] < 0) {\n cs ~= -uf[r];\n }\n cs.sort.reverse;\n debug {\n writeln(\"cs = \", cs);\n }\n \n long ans;\n {\n int num;\n int l = 0, r = N - 1;\n int next() {\n return (num++ % 2 == 0) ? l++ : r--;\n }\n foreach (c; cs) {\n const s = next();\n int u = s;\n foreach (_; 1 .. c) {\n const v = next();\n ans += abs(u - v);\n u = v;\n }\n ans += abs(u - s);\n }\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\n\nint root(int[] uf, int u) {\n return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));\n}\nbool connect(int[] uf, int u, int v) {\n u = uf.root(u);\n v = uf.root(v);\n if (u == v) return false;\n if (uf[u] > uf[v]) swap(u, v);\n uf[u] += uf[v];\n uf[v] = u;\n return true;\n}\n\n\nvoid main() {\n try {\n for (; ; ) {\n const numCases = readInt;\n foreach (caseId; 0 .. numCases) {\n const N = readInt;\n auto A = new int[N]; foreach (i; 0 .. N) A[i] = readInt - 1;\n auto B = new int[N]; foreach (i; 0 .. N) B[i] = readInt - 1;\n \n auto uf = new int[N];\n uf[] = -1;\n foreach (i; 0 .. N) {\n uf.connect(A[i], B[i]);\n }\n \n int[] cs;\n foreach (r; 0 .. N) if (uf[r] < 0) {\n if (-uf[r] >= 2) {\n cs ~= -uf[r];\n }\n }\n cs.sort;\n debug {\n writeln(\"cs = \", cs);\n }\n \n long ans;\n {\n int num;\n int l = 0, r = N - 1;\n int next() {\n return (num++ % 2 == 0) ? l++ : r--;\n }\n foreach (c; cs) {\n const s = next();\n int u = s;\n foreach (_; 0 .. c - 1) {\n const v = next();\n ans += abs(u - v);\n u = v;\n }\n ans += abs(u - s);\n }\n }\n writeln(ans);\n }\n }\n } catch (EOFException e) {\n }\n}\n"}], "src_uid": "4bcaa910cce687f0881a36231aa1a2c8"} {"nl": {"description": "Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office.The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lanes is known.Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly k offices, in each of them he wants to gift presents to the employees.The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the i-th lane passes near the office on the x-th crossroad if and only if min(ui, vi) < x < max(ui, vi))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly k - 1 bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office.Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty.", "input_spec": "The first line contains two integers n and k (1 ≤ n, k ≤ 80) — the number of crossroads (and offices) and the number of offices Oleg wants to visit. The second line contains single integer m (0 ≤ m ≤ 2000) — the number of bicycle lanes in Bankopolis. The next m lines contain information about the lanes. The i-th of these lines contains three integers ui, vi and ci (1 ≤ ui, vi ≤ n, 1 ≤ ci ≤ 1000), denoting the crossroads connected by the i-th road and its difficulty.", "output_spec": "In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths.", "sample_inputs": ["7 4\n4\n1 6 2\n6 2 2\n2 4 2\n2 7 1", "4 3\n4\n2 1 2\n1 3 2\n3 4 2\n4 1 1"], "sample_outputs": ["6", "3"], "notes": "NoteIn the first example Oleg visiting banks by path 1 → 6 → 2 → 4.Path 1 → 6 → 2 → 7 with smaller difficulity is incorrect because crossroad 2 → 7 passes near already visited office on the crossroad 6.In the second example Oleg can visit banks by path 4 → 1 → 3."}, "positive_code": [{"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tauto dp=new int[81][81][81][81];\n\tint n,k;\nloop:while(read(n,k))\n\t{\n\t\tint m;\n\t\tread(m);\n\t\tauto g=new pii[][n+1];\n\t\tforeach(i;0..m)\n\t\t{\n\t\t\tint u,v,c;\n\t\t\tread(u,v,c);\n\t\t\tg[u]~=mp(v,c);\n\t\t\t//g[v]~=mp(u,c);\n\t\t}\n\t\t\n\t\tint go(int v,int l,int r,int h)\n\t\t{\n\t\t\tif(h==k)return 0;\n\t\t\tif(dp[v][l][r][h])return dp[v][l][r][h];\n\t\t\tint ans=1000000;\n\t\t\tforeach(x; g[v]) {\n\t\t\t\tif(x.fir)continue;\n\t\t\t\tif(x.fi=1000000)writeln(-1);\n\t\telse writeln(ans);\n\t}\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tauto dp=new int[81][81][81][81];\n\tint n,k;\n\tauto g=new pii[][80+1];\nloop:while(read(n,k))\n\t{\n\t\tint m;\n\t\tread(m);\n\t\t\n\t\tforeach(i;0..m)\n\t\t{\n\t\t\tint u,v,c;\n\t\t\tread(u,v,c);\n\t\t\tg[u]~=mp(v,c);\n\t\t\t//g[v]~=mp(u,c);\n\t\t}\n\t\t\n\t\tint go(int v,int l,int r,int h)\n\t\t{\n\t\t\tif(h==k)return 0;\n\t\t\tif(dp[v][l][r][h])return dp[v][l][r][h];\n\t\t\tint ans=1000000;\n\t\t\tforeach(x; g[v]) {\n\t\t\t\tif(x.fir)continue;\n\t\t\t\tif(x.fi=1000000)writeln(-1);\n\t\telse writeln(ans);\n\t}\n}"}, {"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tint n,k;\n/*loop:while(read(n,k))\n\t{*/\n\tread(n,k);\n\t\tint m;\n\t\tread(m);\n\t\tauto g=new pii[][n+1];\n\t\tforeach(i;0..m)\n\t\t{\n\t\t\tint u,v,c;\n\t\t\tread(u,v,c);\n\t\t\tg[u]~=mp(v,c);\n\t\t\t//g[v]~=mp(u,c);\n\t\t}\n\t\tauto dp=new int[81][81][81][81];\n\t\tint go(int v,int l,int r,int h)\n\t\t{\n\t\t\tif(h==k)return 0;\n\t\t\tif(dp[v][l][r][h])return dp[v][l][r][h];\n\t\t\tint ans=1000000;\n\t\t\tforeach(x; g[v]) {\n\t\t\t\tif(x.fir)continue;\n\t\t\t\tif(x.fi=1000000)writeln(-1);\n\t\telse writeln(ans);\n\t//}\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int infinity = int.max / 4;\n\nvoid main ()\n{\n\tint n, k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\tn += 2;\n\t\tint m;\n\t\treadf (\" %s\", &m);\n\t\tauto u = new int [m];\n\t\tauto v = new int [m];\n\t\tauto c = new int [m];\n\t\tauto d = new int [] [] (n, n);\n\t\tforeach (ref dLine; d)\n\t\t{\n\t\t\tdLine[] = infinity;\n\t\t}\n\t\tforeach (i; 0..m)\n\t\t{\n\t\t\treadf (\" %s %s %s\", &u[i], &v[i], &c[i]);\n\t\t\td[u[i]][v[i]] = min (d[u[i]][v[i]], c[i]);\n\t\t}\n\n\t\tauto f = new int [2] [] [] [] (2, n, n);\n\t\tint b = 0;\n\t\tforeach (ref fLine; f[b])\n\t\t{\n\t\t\tfLine[] = [infinity, infinity];\n\t\t}\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tf[b][0][i][1] = 0;\n\t\t\tf[b][i][n - 1][0] = 0;\n\t\t}\n\n\t\tforeach (step; 0..k - 1)\n\t\t{\n\t\t\tb ^= 1;\n\t\t\tforeach (ref fLine; f[b])\n\t\t\t{\n\t\t\t\tfLine[] = [infinity, infinity];\n\t\t\t}\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tforeach (j; i..n)\n\t\t\t\t{\n\t\t\t\t\t// from [i* -- j]\n\t\t\t\t\tint cur0 = f[!b][i][j][0];\n\t\t\t\t\t// from [i -- *j]\n\t\t\t\t\tint cur1 = f[!b][i][j][1];\n\t\t\t\t\tdebug {writeln (step, \" \", i, \" \",\n\t\t\t\t\t j, \": \", cur0, \" \", cur1);}\n\t\t\t\t\tif (cur0 != infinity)\n\t\t\t\t\t{\n\t\t\t\t\t\tforeach (p; i + 1..j)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// to [i -- *p]\n\t\t\t\t\t\t\tf[b][i][p][1] =\n\t\t\t\t\t\t\t min (f[b][i][p][1],\n\t\t\t\t\t\t\t cur0 + d[i][p]);\n\t\t\t\t\t\t\t// to [p* -- j]\n\t\t\t\t\t\t\tf[b][p][j][0] =\n\t\t\t\t\t\t\t min (f[b][p][j][0],\n\t\t\t\t\t\t\t cur0 + d[i][p]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (cur1 != infinity)\n\t\t\t\t\t{\n\t\t\t\t\t\tforeach (p; i + 1..j)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// to [p* -- j]\n\t\t\t\t\t\t\tf[b][p][j][0] =\n\t\t\t\t\t\t\t min (f[b][p][j][0],\n\t\t\t\t\t\t\t cur1 + d[j][p]);\n\t\t\t\t\t\t\t// to [i -- *p]\n\t\t\t\t\t\t\tf[b][i][p][1] =\n\t\t\t\t\t\t\t min (f[b][i][p][1],\n\t\t\t\t\t\t\t cur1 + d[j][p]);\n\t\t\t\t\t\t}\n\t \t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint res = infinity;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tforeach (j; 0..n)\n\t\t\t{\n\t\t\t\tres = min (res, f[b][i][j][0], f[b][i][j][1]);\n\t\t\t}\n\t\t}\n\t\tif (res == infinity)\n\t\t{\n\t\t\tres = -1;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [{"source_code": "import std.bigint,std.container,std.array,std.algorithm,std.conv,\n\tstd.functional,std.math,std.numeric,std.string,std.range,std.complex,\n\tstd.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,\n\tcore.stdc.stdio : freopen;\nalias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;\nalias pair(X,Y)=Tuple!(X,\"fi\",Y,\"se\");alias long lint;immutable int mod=1000000007;\nalias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;\nalias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;\npure nothrow \n{\n\tT lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}\n\tpair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,\"fi\",Y,\"se\")(x_,y_);}\n\tbig gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}\n\t@property X sqr(X)(in X a_) {return a_*a_;}\n\t@property X cub(X)(in X a_) {return a_*a_*a_;}\n\tint sz(T)(T a) if(hasLength!T){return cast(int)a.length;}\n\tsize_t lowb(T,X)(in T a,auto ref X g)\n\t if(isRandomAccessRange!T && hasLength!T && is(typeof(g1)\n\t\t{\n\t\t\tauto m=(l+r)>>1;\n\t\t\tif(!(a[m] 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treturn s==ptrs.length;\n}\nbool read(T...)(auto ref T ptrs) if (ptrs.length > 0)\n{\n\tsize_t s=0;\n\tforeach(ref e;ptrs)\n\t{\n\t\tif(isSomeChar!(Unqual!(typeof(e))))s+=readf(\" %c\",&e);\n\t\telse s+=readf(\" %s\",&e);\n\t}\n\treadln;\n\treturn s==ptrs.length;\n}\nnothrow auto inf(in char* name) {return freopen(name,\"r\",core.stdc.stdio.stdin);}\nnothrow auto ouf(in char* name) {return freopen(name,\"w\",core.stdc.stdio.stdout);}\n@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}\n//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin\n//cumulativeFold schwartzSort multiSort partialSort heapify join filter foreach\n//------------------------------------------program beginning--------------------------\n\nvoid main()\n{\n\tversion(ONLINE_JUDGE){}\n\telse\n\t{\n\t\tassert(freopen(\"input.txt\",\"r\",core.stdc.stdio.stdin));\n\t\tassert(freopen(\"output.txt\",\"w\",core.stdc.stdio.stdout));\n\t}\n\tint n,k;\nloop:while(read(n,k))\n\t{\n\t\tint m;\n\t\tread(m);\n\t\tauto g=new pii[][n+1];\n\t\tforeach(i;0..m)\n\t\t{\n\t\t\tint u,v,c;\n\t\t\tread(u,v,c);\n\t\t\tg[u]~=mp(v,c);\n\t\t\tg[v]~=mp(u,c);\n\t\t}\n\t\tauto dp=new int[80][80][80][80];\n\t\tint go(int v,int l,int r,int h)\n\t\t{\n\t\t\tif(h==k)return 0;\n\t\t\tif(dp[v][l][r][h])return dp[v][l][r][h];\n\t\t\tint ans=1000000;\n\t\t\tforeach(x; g[v]) {\n\t\t\t\tif(x.fir)continue;\n\t\t\t\tif(x.fi=1000000)writeln(-1);\n\t\telse writeln(ans);\n\t}\n}"}, {"source_code": "import core.bitop;\nimport std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nimmutable int infinity = int.max / 4;\n\nvoid main ()\n{\n\tint n, k;\n\twhile (readf (\" %s %s\", &n, &k) > 0)\n\t{\n\t\tn += 2;\n\t\tint m;\n\t\treadf (\" %s\", &m);\n\t\tauto u = new int [m];\n\t\tauto v = new int [m];\n\t\tauto c = new int [m];\n\t\tauto d = new int [] [] (n, n);\n\t\tforeach (ref dLine; d)\n\t\t{\n\t\t\tdLine[] = infinity;\n\t\t}\n\t\tforeach (i; 0..m)\n\t\t{\n\t\t\treadf (\" %s %s %s\", &u[i], &v[i], &c[i]);\n\t\t\td[u[i]][v[i]] = min (d[u[i]][v[i]], c[i]);\n\t\t}\n\n\t\tauto f = new int [2] [] [] [] (2, n, n);\n\t\tint b = 0;\n\t\tforeach (ref fLine; f[b])\n\t\t{\n\t\t\tfLine[] = [infinity, infinity];\n\t\t}\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tf[b][0][i][1] = 0;\n\t\t\tf[b][i][n - 1][0] = 0;\n\t\t}\n\n\t\tforeach (step; 0..k - 1)\n\t\t{\n\t\t\tb ^= 1;\n\t\t\tforeach (ref fLine; f[b])\n\t\t\t{\n\t\t\t\tfLine[] = [infinity, infinity];\n\t\t\t}\n\t\t\tforeach (i; 0..n)\n\t\t\t{\n\t\t\t\tforeach (j; i..n)\n\t\t\t\t{\n\t\t\t\t\t// from [i* -- j]\n\t\t\t\t\tint cur0 = f[!b][i][j][0];\n\t\t\t\t\t// from [i -- *j]\n\t\t\t\t\tint cur1 = f[!b][i][j][1];\n\t\t\t\t\tdebug {writeln (step, \" \", i, \" \",\n\t\t\t\t\t j, \": \", cur0, \" \", cur1);}\n\t\t\t\t\tif (cur0 != infinity)\n\t\t\t\t\t{\n\t\t\t\t\t\tforeach (p; i + 1..j)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// to [i -- *p]\n\t\t\t\t\t\t\tf[b][i][p][1] =\n\t\t\t\t\t\t\t min (f[b][i][p][1],\n\t\t\t\t\t\t\t cur0 + d[i][p]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (cur1 != infinity)\n\t\t\t\t\t{\n\t\t\t\t\t\tforeach (p; i + 1..j)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// to [p* -- j]\n\t\t\t\t\t\t\tf[b][p][j][0] =\n\t\t\t\t\t\t\t min (f[b][p][j][0],\n\t\t\t\t\t\t\t cur1 + d[j][p]);\n\t\t\t\t\t\t}\n\t \t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint res = infinity;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tforeach (j; 0..n)\n\t\t\t{\n\t\t\t\tres = min (res, f[b][i][j][0], f[b][i][j][1]);\n\t\t\t}\n\t\t}\n\t\tif (res == infinity)\n\t\t{\n\t\t\tres = -1;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "src_uid": "aa0e8640f3930cfde32d98b82e789ff3"} {"nl": {"description": "Sasha reaches the work by car. It takes exactly k minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the i-th song Sasha gets a pleasure which equals ai. The i-th song lasts for ti minutes. Before the beginning of his way Sasha turns on some song x and then he listens to the songs one by one: at first, the song x, then the song (x + 1), then the song number (x + 2), and so on. He listens to songs until he reaches the work or until he listens to the last song in his playlist. Sasha can listen to each song to the end or partly.In the second case he listens to the song for integer number of minutes, at least half of the song's length. Formally, if the length of the song equals d minutes, Sasha listens to it for no less than minutes, then he immediately switches it to the next song (if there is such). For example, if the length of the song which Sasha wants to partly listen to, equals 5 minutes, then he should listen to it for at least 3 minutes, if the length of the song equals 8 minutes, then he should listen to it for at least 4 minutes.It takes no time to switch a song.Sasha wants to listen partly no more than w songs. If the last listened song plays for less than half of its length, then Sasha doesn't get pleasure from it and that song is not included to the list of partly listened songs. It is not allowed to skip songs. A pleasure from a song does not depend on the listening mode, for the i-th song this value equals ai.Help Sasha to choose such x and no more than w songs for partial listening to get the maximum pleasure. Write a program to find the maximum pleasure Sasha can get from the listening to the songs on his way to the work.", "input_spec": "The first line contains three integers n, w and k (1 ≤ w ≤ n ≤ 2·105, 1 ≤ k ≤ 2·109) — the number of songs in the playlist, the number of songs Sasha can listen to partly and time in minutes which Sasha needs to reach work. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 104), where ai equals the pleasure Sasha gets after listening to the i-th song. The third line contains n positive integers t1, t2, ..., tn (2 ≤ ti ≤ 104), where ti equals the length of the i-th song in minutes.", "output_spec": "Print the maximum pleasure Sasha can get after listening to the songs on the way to work. ", "sample_inputs": ["7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9", "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8", "1 1 5\n6\n9", "1 1 3\n4\n7"], "sample_outputs": ["12", "19", "6", "0"], "notes": "NoteIn the first example Sasha needs to start listening from the song number 2. He should listen to it partly (for 4 minutes), then listen to the song number 3 to the end (for 3 minutes) and then partly listen to the song number 4 (for 3 minutes). After listening to these songs Sasha will get pleasure which equals 4 + 3 + 5 = 12. Sasha will not have time to listen to the song number 5 because he will spend 4 + 3 + 3 = 10 minutes listening to songs number 2, 3 and 4 and only 1 minute is left after that. "}, "positive_code": [{"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Audio {\n int delight, dur;\n bool used = false;\n}\n\nint n, halfCount, totalTime;\nAudio[200_000] _audios;\nAudio[ ] audios;\n\nvoid main() {\n while (read(&n, &halfCount, &totalTime)) {\n audios = _audios[0 .. n];\n foreach (ref a; audios) {\n read(&a.delight);\n version (LocalProject)\n a.used = false;\n }\n foreach (ref a; audios)\n read(&a.dur);\n\n auto used = redBlackTree!(`a.dur != b.dur? a.dur < b.dur : a < b`, Audio*);\n auto unused = redBlackTree!(`a.dur != b.dur? a.dur < b.dur : a < b`, Audio*);\n long available = totalTime, spent = 0, delight = 0;\n int y = n;\n long result = 0;\n foreach_reverse (x, ref a; audios) {\n spent += a.dur;\n delight += a.delight;\n if (used.length < halfCount) {\n a.used = true;\n used.insert(&a);\n available += a.dur >> 1;\n } else if (a.dur > used.front.dur) {\n a.used = true;\n available -= used.front.dur >> 1;\n used.front.used = false;\n unused.insert(used.front);\n used.removeFront();\n used.insert(&a);\n available += a.dur >> 1;\n } else {\n a.used = false;\n unused.insert(&a);\n }\n\n while (y && spent > available) {\n y--;\n spent -= audios[y].dur;\n delight -= audios[y].delight;\n if (!audios[y].used)\n unused.removeKey(&audios[y]);\n else {\n available -= audios[y].dur >> 1;\n used.removeKey(&audios[y]);\n if (!unused.empty) {\n used.insert(unused.back);\n unused.back.used = true;\n available += unused.back.dur >> 1;\n unused.removeBack();\n }\n }\n }\n\n debug writefln(\"y = %s, delight = %s, spent = %s, available = %s\",\n y, delight, spent, available);\n result = max(result, delight);\n }\n writeln(result);\n debug writeln();\n }\n}\n"}], "negative_code": [{"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Audio {\n int delight, dur;\n bool used = false;\n}\n\nint n, halfCount, totalTime;\nAudio[200_000] _audios;\nAudio[ ] audios;\n\nvoid main() {\n while (read(&n, &halfCount, &totalTime)) {\n audios = _audios[0 .. n];\n foreach (ref a; audios) {\n read(&a.delight);\n version (LocalProject)\n a.used = false;\n }\n foreach (ref a; audios)\n read(&a.dur);\n\n auto used = redBlackTree!(`a.dur < b.dur`, true, Audio*);\n auto unused = redBlackTree!(`a.dur < b.dur`, true, Audio*);\n long available = totalTime, spent = 0, delight = 0;\n int y = n;\n long result = 0;\n foreach_reverse (x, ref a; audios) {\n spent += a.dur;\n delight += a.delight;\n if (used.length < halfCount) {\n a.used = true;\n used.insert(&a);\n available += a.dur >> 1;\n } else if (a.dur > used.front.dur) {\n a.used = true;\n available -= used.front.dur >> 1;\n unused.insert(used.front);\n used.removeFront();\n used.insert(&a);\n available += a.dur >> 1;\n } else {\n a.used = false;\n unused.insert(&a);\n }\n\n while (y && spent > available) {\n y--;\n spent -= audios[y].dur;\n delight -= audios[y].delight;\n if (!audios[y].used)\n unused.removeKey(&audios[y]);\n else {\n available -= audios[y].dur >> 1;\n used.removeKey(&audios[y]);\n if (!unused.empty) {\n used.insert(unused.back);\n available += unused.back.dur >> 1;\n unused.removeBack();\n }\n }\n }\n\n long t = delight;\n debug writefln(\"t = %s, y = %s, delight = %s, spent = %s, available = %s\",\n t, y, delight, spent, available);\n result = max(result, t);\n }\n writeln(result);\n debug writeln();\n }\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Audio {\n int delight, dur;\n bool used = false;\n}\n\nint n, halfCount, totalTime;\nAudio[200_000] _audios;\nAudio[ ] audios;\n\nvoid main() {\n while (read(&n, &halfCount, &totalTime)) {\n audios = _audios[0 .. n];\n foreach (ref a; audios) {\n read(&a.delight);\n version (LocalProject)\n a.used = false;\n }\n foreach (ref a; audios)\n read(&a.dur);\n\n auto used = redBlackTree!(`a.dur < b.dur? a.dur < b.dur : a < b`, true, Audio*);\n auto unused = redBlackTree!(`a.dur < b.dur? a.dur < b.dur : a < b`, true, Audio*);\n long available = totalTime, spent = 0, delight = 0;\n int y = n;\n long result = 0;\n foreach_reverse (x, ref a; audios) {\n spent += a.dur;\n delight += a.delight;\n if (used.length < halfCount) {\n a.used = true;\n used.insert(&a);\n available += a.dur >> 1;\n } else if (a.dur > used.front.dur) {\n a.used = true;\n available -= used.front.dur >> 1;\n unused.insert(used.front);\n used.removeFront();\n used.insert(&a);\n available += a.dur >> 1;\n } else {\n a.used = false;\n unused.insert(&a);\n }\n\n while (y && spent > available) {\n y--;\n spent -= audios[y].dur;\n delight -= audios[y].delight;\n if (!audios[y].used)\n unused.removeKey(&audios[y]);\n else {\n available -= audios[y].dur >> 1;\n used.removeKey(&audios[y]);\n if (!unused.empty) {\n used.insert(unused.back);\n unused.back.used = true;\n available += unused.back.dur >> 1;\n unused.removeBack();\n }\n }\n }\n\n long t = delight;\n debug writefln(\"t = %s, y = %s, delight = %s, spent = %s, available = %s\",\n t, y, delight, spent, available);\n result = max(result, t);\n }\n writeln(result);\n debug writeln();\n }\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Audio {\n int delight, dur;\n bool used = false;\n}\n\nint n, halfCount, totalTime;\nAudio[200_000] _audios;\nAudio[ ] audios;\n\nvoid main() {\n while (read(&n, &halfCount, &totalTime)) {\n audios = _audios[0 .. n];\n foreach (ref a; audios) {\n read(&a.delight);\n version (LocalProject)\n a.used = false;\n }\n foreach (ref a; audios)\n read(&a.dur);\n\n auto used = redBlackTree!(`a.dur < b.dur? a.dur < b.dur : a < b`, true, Audio*);\n auto unused = redBlackTree!(`a.dur < b.dur? a.dur < b.dur : a < b`, true, Audio*);\n long available = totalTime, spent = 0, delight = 0;\n int y = n;\n long result = 0;\n foreach_reverse (x, ref a; audios) {\n spent += a.dur;\n delight += a.delight;\n if (used.length < halfCount) {\n a.used = true;\n used.insert(&a);\n available += a.dur >> 1;\n } else if (a.dur > used.front.dur) {\n a.used = true;\n available -= used.front.dur >> 1;\n unused.insert(used.front);\n used.removeFront();\n used.insert(&a);\n available += a.dur >> 1;\n } else {\n a.used = false;\n unused.insert(&a);\n }\n\n while (y && spent > available) {\n y--;\n spent -= audios[y].dur;\n delight -= audios[y].delight;\n if (!audios[y].used)\n unused.removeKey(&audios[y]);\n else {\n available -= audios[y].dur >> 1;\n used.removeKey(&audios[y]);\n if (!unused.empty) {\n used.insert(unused.back);\n available += unused.back.dur >> 1;\n unused.removeBack();\n }\n }\n }\n\n long t = delight;\n debug writefln(\"t = %s, y = %s, delight = %s, spent = %s, available = %s\",\n t, y, delight, spent, available);\n result = max(result, t);\n }\n writeln(result);\n debug writeln();\n }\n}\n"}, {"source_code": "import core.bitop;\nimport core.checkedint;\nimport core.simd;\nimport core.stdc.stdlib;\nimport core.stdc.string;\nimport std.algorithm;\nimport std.array;\nimport std.ascii;\nimport std.bigint;\nimport std.bitmanip;\nimport std.complex;\nimport std.container;\nimport std.conv;\nimport std.datetime;\nimport std.format;\nimport std.functional;\nimport std.math;\nimport std.meta;\nimport std.numeric;\nimport std.random;\nimport std.range;\nimport std.regex;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport std.variant;\n\n__gshared:\n\nbool read(T...)(T ptrs) if (ptrs.length > 0) {\n return readf(' ' ~ replicate(\"%s \", ptrs.length), ptrs) == ptrs.length;\n}\n\nT[ ] allocate(T)(size_t n) {\n return (cast(T*)malloc(n * T.sizeof))[0 .. n];\n}\n\nauto pairwise(R)(R range) if (isForwardRange!R) {\n return lockstep(range, dropOne(range));\n}\n\nstruct Audio {\n int delight, dur;\n bool used = false;\n}\n\nint n, halfCount, totalTime;\nAudio[200_000] _audios;\nAudio[ ] audios;\n\nvoid main() {\n while (read(&n, &halfCount, &totalTime)) {\n audios = _audios[0 .. n];\n foreach (ref a; audios) {\n read(&a.delight);\n version (LocalProject)\n a.used = false;\n }\n foreach (ref a; audios)\n read(&a.dur);\n\n auto used = redBlackTree!(`a.dur < b.dur? a.dur < b.dur : a < b`, Audio*);\n auto unused = redBlackTree!(`a.dur < b.dur? a.dur < b.dur : a < b`, Audio*);\n long available = totalTime, spent = 0, delight = 0;\n int y = n;\n long result = 0;\n foreach_reverse (x, ref a; audios) {\n spent += a.dur;\n delight += a.delight;\n if (used.length < halfCount) {\n a.used = true;\n used.insert(&a);\n available += a.dur >> 1;\n } else if (a.dur > used.front.dur) {\n a.used = true;\n available -= used.front.dur >> 1;\n used.front.used = false;\n unused.insert(used.front);\n used.removeFront();\n used.insert(&a);\n available += a.dur >> 1;\n } else {\n a.used = false;\n unused.insert(&a);\n }\n\n while (y && spent > available) {\n y--;\n spent -= audios[y].dur;\n delight -= audios[y].delight;\n if (!audios[y].used)\n unused.removeKey(&audios[y]);\n else {\n available -= audios[y].dur >> 1;\n used.removeKey(&audios[y]);\n if (!unused.empty) {\n used.insert(unused.back);\n unused.back.used = true;\n available += unused.back.dur >> 1;\n unused.removeBack();\n }\n }\n }\n\n long t = delight;\n debug writefln(\"t = %s, y = %s, delight = %s, spent = %s, available = %s\",\n t, y, delight, spent, available);\n result = max(result, t);\n }\n writeln(result);\n debug writeln();\n }\n}\n"}], "src_uid": "0f61add90bbfe757c6f237c4bd11c903"} {"nl": {"description": "Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings \"pop\", \"noon\", \"x\", and \"kkkkkk\" are palindromes, while strings \"moon\", \"tv\", and \"abab\" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le m \\le 50$$$) — the number of strings and the length of each string. Next $$$n$$$ lines contain a string of length $$$m$$$ each, consisting of lowercase Latin letters only. All strings are distinct.", "output_spec": "In the first line, print the length of the longest palindrome string you made. In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.", "sample_inputs": ["3 3\ntab\none\nbat", "4 2\noo\nox\nxo\nxx", "3 5\nhello\ncodef\norces", "9 4\nabab\nbaba\nabcd\nbcde\ncdef\ndefg\nwxyz\nzyxw\nijji"], "sample_outputs": ["6\ntabbat", "6\noxxxxo", "0", "20\nababwxyzijjizyxwbaba"], "notes": "NoteIn the first example, \"battab\" is also a valid answer.In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.In the third example, the empty string is the only valid palindrome string."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint, m = rint;\n\n\tbool[string] sz;\n\tstring[] us;\n\tstring v;\n\tforeach(i; 0 .. n){\n\t\tstring s = readln.chomp;\n\t\tstring t = \"\";\n\t\tforeach_reverse(c; s) t ~= c;\n\t\tif(t in sz) us ~= s;\n\t\telse if(s == t) v = s;\n\t\tsz[s] = 1;\n\t}\n\n\tstring ans;\n\tforeach(u; us) ans ~= u;\n\tans ~= v;\n\tforeach_reverse(u; us) foreach_reverse(c; u) ans ~= c;\n\n\t(us.length * 2 * m + v.length).writeln;\n\tans.writeln;\n\n}"}, {"source_code": "import std.container;\nimport std.algorithm;\nimport std.range;\nimport std.array;\nimport std.stdio;\nimport std.utf;\nimport std.math;\nimport std.typecons;\n\nalias type = single;\nstruct TestCase\n{\n mixin prettyPrinting;\n\n long n, m;\n @(\"n\") string[] str;\n\n void solve(long tc = -1)\n {\n auto pairs = appender!(Tuple!(long, long)[])();\n string pal = \"\";\n fori:foreach(i; 0 .. n)\n {\n forj:foreach(j; i + 1 .. n)\n {\n if (iota(0, m).all!(k => str.at(i).at(k) == str.at(j).at(m - 1 - k)))\n {\n pairs.put(tuple(i, j));\n continue fori;\n }\n }\n if (iota(0, m).all!(k => str.at(i).at(k) == str.at(i).at(m - 1 - k)))\n pal = str.at(i);\n }\n auto res = pairs[].fold!((curr, pair) => str.at(pair[0]) ~ curr ~ str.at(pair[1]))(pal).array.toUTF8;\n writeln(res.length);\n writeln(res);\n }\n}\n\nmixin template prettyPrinting()\n{\n string toString()\n {\n alias structType = typeof(this);\n auto res = appender!(string)();\n res.put(structType.stringof);\n res.put(\"(\");\n static foreach(fieldName; FieldNameTuple!structType)\n {\n res.put(text(\" \", fieldName, \": \", mixin(fieldName)));\n }\n res.put(\" )\");\n res.put(\"\\n\");\n return res[];\n }\n}\n\nstruct Graph(N)\n{\n size_t size;\n this(S)(S size)\n {\n adjacencyList.length = cast(size_t) size;\n this.size = cast(size_t) size;\n this.visited = makeSlice!bool(size);\n }\n N[][] adjacencyList;\n alias ne = neighbours;\n N[] neighbours(N n)\n {\n return adjacencyList.at(n);\n }\n alias bcon = biconnect;\n void biconnect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n adjacencyList.at(b) ~= a;\n }\n alias con = connect;\n void connect(N a, N b)\n {\n adjacencyList.at(a) ~= b;\n }\n void connect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n connect(edge[0], edge[1]);\n }\n }\n void biconnect(R)(R edges)\n {\n foreach(ref edge; edges)\n {\n biconnect(edge[0], edge[1]);\n }\n }\n alias deg = degree!long;\n auto degree(T)(N a)\n {\n return cast(T) adjacencyList.at(a).length;\n }\n\n auto visited = new bool[0];\n void dfs(Visitor)(long startNode)\n {\n auto visitor = Visitor.init;\n void internalDfs(long node)\n {\n visited.set(node, true);\n static if (is(typeof({visitor.n = node;})))\n visitor.n = node;\n static if (is(typeof({visitor.pre;})))\n visitor.pre;\n foreach(w; ne(node))\n if (!visited.at(w))\n {\n static if (is(typeof({visitor.w = w;})))\n visitor.w = w;\n static if (is(typeof(visitor.pren)))\n visitor.pren;\n internalDfs(w);\n static if (is(typeof(visitor.postn)))\n visitor.postn;\n }\n static if (is(typeof(visitor.post)))\n visitor.post;\n }\n internalDfs(startNode);\n }\n void dfs(long startNode)\n {\n struct DefVisitor {}\n dfs!DefVisitor(startNode);\n }\n}\n\nDList!string _words;\n\ntemplate baseType(W)\n{\n static if (isDynamicArray!W && !is(W == string))\n alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));\n else\n alias baseType = W;\n}\n\nauto makeSlice(E, W, T...)(W size, T otherSizes)\n{\n static if (T.length == 0)\n {\n E[] res;\n res.length = cast(size_t) size;\n return res;\n }\n else\n {\n typeof(makeSlice!E(otherSizes))[] res;\n res.length = cast(size_t) size;\n foreach(ref row; res)\n row = makeSlice!E(otherSizes);\n return res;\n }\n}\n\nvoid read(T...)(ref T t)\n{\n void basicTypeRead(W)(ref W w)\n {\n import std.stdio: readln;\n while(_words.empty)\n {\n import std.array: split;\n foreach(word; readln.split)\n {\n _words.insertBack(word);\n }\n }\n import std.conv: to;\n string word = _words.front;\n _words.removeFront;\n w = to!W(word);\n }\n void iterableRead(W)(ref W w)\n {\n foreach(ref element; w)\n read(element);\n }\n void tupleRead(W)(ref W w)\n {\n static foreach(j; 0 .. W.length)\n read(w[j]);\n }\n void structRead(W)(ref W readStruct)\n {\n static foreach(fieldName; FieldNameTuple!W)\n {\n with(readStruct)\n {\n alias fieldSymbol = mixin(W.stringof, \".\", fieldName);\n alias typeOfField = typeof(mixin(fieldName));\n static if (hasUDA!(fieldSymbol, string))\n {\n static assert(isDynamicArray!(typeOfField)\n , \" A field with dimension initialization UDA must be a dynamic array.\");\n enum uda = getUDAs!(fieldSymbol, string)[0];\n mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda, q{)});\n }\n else static if (isDynamicArray!typeOfField && !is(typeOfField == string))\n {\n pragma(msg, \"Warning: You probably want to add dimension to input \", fieldName);\n }\n read(mixin(fieldName));\n }\n }\n }\n static foreach(i; 0 .. T.length)\n {\n static if (isBasicType!(T[i]) || is(T[i] == string))\n basicTypeRead(t[i]);\n else static if (is(typeof({foreach(ref e; t[i]){}})))\n iterableRead(t[i]);\n else static if (isTuple!(T[i]))\n tupleRead(t[i]);\n else static if (isAggregateType!(T[i]))\n structRead(t[i]);\n }\n}\n\nauto next(T)()\n{\n T t;\n read(t);\n return t;\n}\n\n\nsize_t ind(T)(T t)\n{\n return cast(size_t)t;\n}\n\nref auto at(R, I)(ref R r, I i)\n{\n return r[cast(size_t)i];\n}\n\nvoid set(R, I, V)(ref R r, I i, V v)\n{\n r[cast(size_t)i] = v;\n}\n\nimport std.conv;\nimport std.traits;\nimport std.meta;\nint _loglevel = -1;\nvoid _log(string functionName, argSeq...)()\n{\n foreach(i; 0 .. _loglevel * 4)\n write(' ');\n write(functionName);\n static foreach(arg; argSeq)\n {\n write(\" \", arg.stringof, \" = \", arg);\n }\n writeln;\n}\nenum logCall =\n q{\n debug\n {\n {\n _loglevel++;\n alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, \".\").back));\n static if (_parameterTuple.length > 0)\n mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(\", \").array, `);`);\n else\n _log!(__FUNCTION__);\n }\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('{');\n scope(exit)\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n writeln('}');\n _loglevel--;\n }\n }\n};\n\nvoid logSym(S...)(int line = __LINE__, string file = __FUNCTION__)\n{\n debug\n {\n foreach(i; 0 .. _loglevel * 4) write(' ');\n write(file, \"(\", line, \"):\");\n static foreach(s; S)\n write(\" \", s.stringof, \" = \", s);\n writeln;\n\n }\n}\n\nArray!E makeArray(E, S)(S size, lazy E value)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n foreach(ref ai; a)\n ai = value();\n return a;\n}\nArray!E makeArray(E, S)(S size)\n{\n auto a = Array!E();\n a.length = cast(size_t) size;\n return a;\n}\n\ntemplate mem(alias F)\n{\n ReturnType!F[Tuple!(Parameters!F)] table;\n ReturnType!F mem(Parameters!F args)\n {\n return table.require(tuple(args), F(args));\n }\n}\n\nstring input(string names, string type = \"long\")\n{\n return text(type, \" \", names, \"; read(\", names, \");\");\n}\n\nauto pmod(T, W)(T t, W w)\n{\n return (t%w + w) % w;\n}\n\nvoid rep(S, F)(F f, S times)\n{\n static if (is(typeof(f(i))))\n foreach(i; 0 .. times)\n f(i);\n else\n foreach(i; 0 .. times)\n f();\n}\n\nvoid multi()\n{\n foreach(tc; 0 .. next!long)\n next!TestCase.solve(tc);\n}\n\nvoid single()\n{\n auto tc = next!TestCase;\n tc.solve;\n}\n\nvoid main()\n{\n type;\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(T)(ref T x, T y) { x = (x + y) % mod; }\nvoid mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(T)(ref T x, T y) { x = (x * y) % mod; }\nvoid 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); }\n\nvoid main()\n{\n\tauto n = RD!int;\n\tauto m = RD!int;\n\tauto s = new string[](n);\n\tint[string] set, set2;\n\tstring ans1, ans2;\n\tforeach (i; 0..n)\n\t{\n\t\ts[i] = RD!string;\n\t\tauto tt = s[i].dup;\n\t\ttt.reverse;\n\t\tauto t = tt.idup;\n\t\tif (set.get(s[i], 0))\n\t\t{\n\t\t\tans1 ~= s[i];\n\t\t\tans2 = (t ~ ans2).idup;\n\t\t\t--set[s[i]];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbool ok = true;\n\t\t\tforeach (j; 0..m/2)\n\t\t\t{\n\t\t\t\tif (s[i][j] != s[i][$-j-1])\n\t\t\t\t\tok = false;\n\t\t\t}\n\t\t\tif (ok)\n\t\t\t\t++set2[s[i]];\n\t\t\telse\n\t\t\t\t++set[t];\n\t\t}\n\t}\n\n\tstring bestKey;\n\tlong cnt;\n\tforeach (key; set2.keys)\n\t{\n\t\tif (set2[key] > cnt)\n\t\t{\n\t\t\tcnt = set2[key];\n\t\t\tbestKey = key;\n\t\t}\n\t}\n\tauto ans = ans1;\n\tforeach (i; 0..cnt)\n\t{\n\t\tans ~= bestKey;\n\t}\n\tans ~= ans2;\n\twriteln(ans.length);\n\twriteln(ans);\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [{"source_code": "import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;\nimport std.math, std.random, std.bigint, std.datetime, std.format;\nvoid main(string[] args){ if(args.length > 1) if(args[1] == \"-debug\") DEBUG = 1; solve(); }\nvoid log()(){ writeln(\"\"); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, \" \"), log(a); } bool DEBUG = 0; \nstring rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }\nT rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;\nT[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;\nT[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;\nT chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }\n\n// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //\n\nvoid solve(){\n\tint n = rint, m = rint;\n\n\tbool[string] sz;\n\tstring[] us;\n\tstring v;\n\tforeach(i; 0 .. n){\n\t\tstring s = readln.chomp;\n\t\tstring t = \"\";\n\t\tforeach_reverse(c; s) t ~= c;\n\t\tif(t in sz) us ~= s;\n\t\telse if(s == t) v = s;\n\t\tsz[s] = 1;\n\t}\n\n\tstring ans;\n\tforeach(u; us) ans ~= u;\n\tans ~= v;\n\tforeach(u; us) foreach_reverse(c; u) ans ~= c;\n\n\t(us.length * 2 * m + v.length).writeln;\n\tans.writeln;\n\n}"}], "src_uid": "554115bec46bb436a0a1ddf8c05a2d08"} {"nl": {"description": "As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has. Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.", "output_spec": "Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .", "sample_inputs": ["2\n100 30\n40 10", "4\n1 1\n9 7\n1 4\n10 7"], "sample_outputs": ["942477.796077000", "3983.539484752"], "notes": "NoteIn first sample, the optimal way is to choose the cake number 1.In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.string;\nimport std.array;\nimport std.conv;\nimport std.math;\n\nlong bit[100005];\n\nint getPos(T)(T[] s, T value) {\n int lo = -1, hi = to!int(s.length);\n while (lo + 1 < hi) {\n int mid = (lo + hi) >> 1;\n if (s[mid] < value) lo = mid;\n else hi = mid;\n }\n return hi;\n}\n\nvoid update(int pos, long value) {\n for (int i = pos + 1; i < 100005; i += i & -i) {\n bit[i] = max(bit[i], value);\n }\n}\n\nlong query(int pos) {\n long ret = 0;\n for (int i = pos + 1; i > 0; i -= i & -i) {\n ret = max(ret, bit[i]);\n }\n return ret;\n}\n\nvoid main() {\n int n;\n readf(\" %s\", &n);\n auto a = new long[n];\n foreach (i; 0..n) {\n int r, h;\n readf(\" %s %s\", &r, &h);\n a[i] = 1L * r * r * h;\n }\n auto b = a.dup.sort.uniq.array;\n long ans = 0;\n foreach (v; a) {\n int pos = getPos(b, v);\n long cur = v + query(pos - 1);\n ans = max(ans, cur);\n update(pos, cur);\n }\n writefln(\"%.10f\", acos(-1.0) * ans);\n}\n"}], "negative_code": [], "src_uid": "49a647a99eab59a61b42515d4289d3cd"} {"nl": {"description": "You are given a rectangular grid with $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{ij}$$$ written in it.You can perform the following operation any number of times (possibly zero): Choose any two adjacent cells and multiply the values in them by $$$-1$$$. Two cells are called adjacent if they share a side. Note that you can use a cell more than once in different operations.You are interested in $$$X$$$, the sum of all the numbers in the grid. What is the maximum $$$X$$$ you can achieve with these operations?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$,$$$m$$$ ($$$2 \\le n$$$, $$$m \\le 10$$$). The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line is $$$a_{ij}$$$ ($$$-100\\leq a_{ij}\\le 100$$$).", "output_spec": "For each testcase, print one integer $$$X$$$, the maximum possible sum of all the values in the grid after applying the operation as many times as you want.", "sample_inputs": ["2\n2 2\n-1 1\n1 1\n3 4\n0 -1 -2 -3\n-1 -2 -3 -4\n-2 -3 -4 -5"], "sample_outputs": ["2\n30"], "notes": "NoteIn the first test case, there will always be at least one $$$-1$$$, so the answer is $$$2$$$. In the second test case, we can use the operation six times to elements adjacent horizontally and get all numbers to be non-negative. So the answer is: $$$2\\times 1 + 3\\times2 + 3\\times 3 + 2\\times 4 + 1\\times 5 = 30$$$."}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\n\n//long mod = 10^^9 + 7;\nlong mod = 998_244_353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\nvoid 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); }\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new long[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto n = RD!int;\n\t\tauto m = RD!int;\n\t\tlong cnt, x = long.max;\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tauto a = RDA;\n\t\t\tforeach (j; 0..m)\n\t\t\t{\n\t\t\t\tx.chmin(abs(a[j]));\n\t\t\t\tif (a[j] < 0)\n\t\t\t\t\t++cnt;\n\t\t\t\tans[ti] += abs(a[j]);\n\t\t\t}\n\t\t}\n\t\tif (cnt % 2)\n\t\t{\n\t\t\tans[ti] -= x*2;\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e);\n\tstdout.flush;\n\tdebug readln;\n}\n"}], "negative_code": [], "src_uid": "7fce446de3b01aff8f4aa420a92a096d"} {"nl": {"description": "You are given a rooted tree with vertices numerated from $$$1$$$ to $$$n$$$. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.Ancestors of the vertex $$$i$$$ are all vertices on the path from the root to the vertex $$$i$$$, except the vertex $$$i$$$ itself. The parent of the vertex $$$i$$$ is the nearest to the vertex $$$i$$$ ancestor of $$$i$$$. Each vertex is a child of its parent. In the given tree the parent of the vertex $$$i$$$ is the vertex $$$p_i$$$. For the root, the value $$$p_i$$$ is $$$-1$$$. An example of a tree with $$$n=8$$$, the root is vertex $$$5$$$. The parent of the vertex $$$2$$$ is vertex $$$3$$$, the parent of the vertex $$$1$$$ is vertex $$$5$$$. The ancestors of the vertex $$$6$$$ are vertices $$$4$$$ and $$$5$$$, the ancestors of the vertex $$$7$$$ are vertices $$$8$$$, $$$3$$$ and $$$5$$$ You noticed that some vertices do not respect others. In particular, if $$$c_i = 1$$$, then the vertex $$$i$$$ does not respect any of its ancestors, and if $$$c_i = 0$$$, it respects all of them.You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex $$$v$$$, all children of $$$v$$$ become connected with the parent of $$$v$$$. An example of deletion of the vertex $$$7$$$. Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of vertices in the tree. The next $$$n$$$ lines describe the tree: the $$$i$$$-th line contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$1 \\le p_i \\le n$$$, $$$0 \\le c_i \\le 1$$$), where $$$p_i$$$ is the parent of the vertex $$$i$$$, and $$$c_i = 0$$$, if the vertex $$$i$$$ respects its parents, and $$$c_i = 1$$$, if the vertex $$$i$$$ does not respect any of its parents. The root of the tree has $$$-1$$$ instead of the parent index, also, $$$c_i=0$$$ for the root. It is guaranteed that the values $$$p_i$$$ define a rooted tree with $$$n$$$ vertices.", "output_spec": "In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer $$$-1$$$.", "sample_inputs": ["5\n3 1\n1 1\n-1 0\n2 1\n3 0", "5\n-1 0\n1 1\n1 1\n2 0\n3 0", "8\n2 1\n-1 0\n1 0\n1 1\n1 1\n4 0\n5 1\n7 0"], "sample_outputs": ["1 2 4", "-1", "5"], "notes": "NoteThe deletion process in the first example is as follows (see the picture below, the vertices with $$$c_i=1$$$ are in yellow): first you will delete the vertex $$$1$$$, because it does not respect ancestors and all its children (the vertex $$$2$$$) do not respect it, and $$$1$$$ is the smallest index among such vertices; the vertex $$$2$$$ will be connected with the vertex $$$3$$$ after deletion; then you will delete the vertex $$$2$$$, because it does not respect ancestors and all its children (the only vertex $$$4$$$) do not respect it; the vertex $$$4$$$ will be connected with the vertex $$$3$$$; then you will delete the vertex $$$4$$$, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth); you will just delete the vertex $$$4$$$; there are no more vertices to delete. In the second example you don't need to delete any vertex: vertices $$$2$$$ and $$$3$$$ have children that respect them; vertices $$$4$$$ and $$$5$$$ respect ancestors. In the third example the tree will change this way: "}, "positive_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint;\n\nalias Path = Tuple!(int, \"to\", bool, \"c\");\n\nPath[][10^^5] GP;\n\nvoid main()\n{\n auto N = readln.chomp.to!int;\n int root;\n foreach (int i; 0..N) {\n auto pc = readln.split.to!(int[]);\n if (pc[0] == -1) {\n root = i;\n } else {\n GP[pc[0]-1] ~= Path(i, pc[1] == 1);\n }\n }\n\n auto ss = [Path(root, false)];\n int[] rs;\n while (!ss.empty) {\n auto head = ss[0];\n ss = ss[1..$];\n if (head.c) {\n bool del = true;\n foreach (path; GP[head.to]) {\n if (!path.c) del = false;\n ss ~= path;\n }\n if (del) rs ~= head.to + 1;\n } else {\n ss ~= GP[head.to];\n }\n }\n sort(rs);\n writeln(rs.empty ? \"-1\" : rs.to!(string[]).join(\" \"));\n}"}, {"source_code": "//prewritten code: https://github.com/antma/algo\nimport std.algorithm;\nimport std.array;\nimport std.conv;\nimport std.math;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.traits;\n\nclass InputReader {\n private:\n ubyte[] p;\n ubyte[] buffer;\n size_t cur;\n public:\n this () {\n buffer = uninitializedArray!(ubyte[])(16<<20);\n p = stdin.rawRead (buffer);\n }\n final ubyte skipByte (ubyte lo) {\n while (true) {\n auto a = p[cur .. $];\n auto r = a.find! (c => c >= lo);\n if (!r.empty) {\n cur += a.length - r.length;\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n cur = 0;\n if (p.empty) return 0;\n }\n }\n final ubyte nextByte () {\n if (cur < p.length) {\n return p[cur++];\n }\n p = stdin.rawRead (buffer);\n if (p.empty) return 0;\n cur = 1;\n return p[0];\n }\n\n template next(T) if (isSigned!T) {\n final T next () {\n T res;\n ubyte b = skipByte (45);\n if (b == 45) {\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 - (b - 48);\n }\n } else {\n res = b - 48;\n while (true) {\n b = nextByte ();\n if (b < 48 || b >= 58) {\n return res;\n }\n res = res * 10 + (b - 48);\n }\n }\n }\n }\n template next(T) if (isUnsigned!T) {\n final T next () {\n T res = skipByte (48) - 48;\n while (true) {\n ubyte b = nextByte ();\n if (b < 48 || b >= 58) {\n break;\n }\n res = res * 10 + (b - 48);\n }\n return res;\n }\n }\n}\n\nvoid main() {\n auto r = new InputReader;\n immutable n = r.next!uint;\n auto p = new int[n+1];\n auto c = new int[n+1];\n auto good = new bool[n+1];\n debug stderr.writeln (n);\n foreach (i; 1 .. n+1) {\n p[i] = r.next!int;\n c[i] = r.next!uint;\n debug stderr.writeln (p[i], ' ', c[i]);\n if (!c[i]) {\n if (p[i] >= 0) {\n good[p[i]] = true;\n }\n }\n }\n int[] z;\n foreach (i; 1 .. n + 1) {\n if (!good[i] && c[i]) {\n z ~= i;\n }\n }\n if (z.empty) writeln (-1);\n else writefln (\"%(%s %)\", z);\n}\n\n"}], "negative_code": [{"source_code": "import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint;\n\nalias Path = Tuple!(int, \"to\", bool, \"c\");\n\nPath[][10^^5] GP;\n\nvoid main()\n{\n auto N = readln.chomp.to!int;\n int root;\n foreach (int i; 0..N) {\n auto pc = readln.split.to!(int[]);\n if (pc[0] == -1) {\n root = i;\n } else {\n GP[pc[0]-1] ~= Path(i, pc[1] == 1);\n }\n }\n\n auto ss = [Path(root, false)];\n int[] rs;\n while (!ss.empty) {\n auto head = ss[0];\n ss = ss[1..$];\n if (head.c) {\n bool del = true;\n foreach (path; GP[head.to]) {\n if (!path.c) del = false;\n ss ~= path;\n }\n if (del) rs ~= head.to + 1;\n } else {\n ss ~= GP[head.to];\n }\n }\n writeln(rs.empty ? \"-1\" : rs.to!(string[]).join(\" \"));\n}"}], "src_uid": "1b975c5a13a2ad528b668a7c68c089f6"} {"nl": {"description": "Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must: split the array into 2n - qi parts, where each part is a subarray consisting of 2qi numbers; the j-th subarray (1 ≤ j ≤ 2n - qi) should contain the elements a[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi]; reverse each of the subarrays; join them into a single array in the same order (this array becomes new array a); output the number of inversions in the new a. Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.", "input_spec": "The first line of input contains a single integer n (0 ≤ n ≤ 20). The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array. The third line of input contains a single integer m (1 ≤ m ≤ 106). The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.", "output_spec": "Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.", "sample_inputs": ["2\n2 1 4 3\n4\n1 2 0 2", "1\n1 2\n3\n0 1 1"], "sample_outputs": ["0\n6\n6\n0", "0\n1\n0"], "notes": "NoteIf we reverse an array x[1], x[2], ..., x[n] it becomes new array y[1], y[2], ..., y[n], where y[i] = x[n - i + 1] for each i.The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indices i, j such that: i < j and x[i] > x[j]."}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nlong merge (int [] a, int [] b, int [] c)\n{\n\tlong res = 0;\n\tforeach (p; 0..c.length)\n\t{\n\t\tif (a.length > 0 && (b.length == 0 || a[0] <= b[0]))\n\t\t{\n\t\t\tc[p] = a[0];\n\t\t\ta = a[1..$];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tres += cast (long) (a.length);\n\t\t\tc[p] = b[0];\n\t\t\tb = b[1..$];\n\t\t}\n\t\tp++;\n\t}\n\treturn res;\n}\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tint m = 1 << n;\n\t\tauto a = new int [m];\n\t\tauto b = new int [m];\n\t\tforeach (ref x; a)\n\t\t{\n\t\t\treadf (\" %s\", &x);\n\t\t}\n\n\t\tlong [2] [] s = new long [2] [n + 1];\n\t\ts[] = [0, 0];\n\t\tlong res = s[0][0];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint p = 1 << i;\n\t\t\tfor (int k = 0; k < m; k += p * 2)\n\t\t\t{\n\t\t\t\ts[i + 1][0] += merge (a[k..k + p],\n\t\t\t\t a[k + p..k + p * 2],\n\t\t\t\t b[k..k + p * 2]);\n\t\t\t\ts[i + 1][1] += merge (a[k + p..k + p * 2],\n\t\t\t\t a[k..k + p],\n\t\t\t\t b[k..k + p * 2]);\n\t\t\t\ta[k..k + p * 2] = b[k..k + p * 2];\n\t\t\t}\n\t\t\tres += s[i + 1][0];\n\t\t\tdebug {writeln (s[i + 1]);}\n\t\t\tdebug {writeln (a);}\n\t\t}\n\n\t\tint q;\n\t\treadf (\" %s\", &q);\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\tfor ( ; x >= 0; x--)\n\t\t\t{\n\t\t\t\tres -= s[x][0];\n\t\t\t\tswap (s[x][0], s[x][1]);\n\t\t\t\tres += s[x][0];\n\t }\n\t\t\twriteln (res);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.stdio;\n\nlong merge (int [] a, int [] b, int [] c)\n{\n\tlong res;\n\tforeach (ref x; c)\n\t{\n\t\tif (a.length && (!b.length || a[0] <= b[0]))\n\t\t{\n\t\t\tx = a[0];\n\t\t\ta = a[1..$];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tres += a.length;\n\t\t\tx = b[0];\n\t\t\tb = b[1..$];\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid main ()\n{\n\tint n;\n\treadf (\" %s\", &n);\n\tint m = 1 << n;\n\tauto a = new int [m];\n\tauto b = new int [m];\n\tforeach (ref x; a)\n\t\treadf (\" %s\", &x);\n\n\tauto s = new long [2] [n + 1];\n\tlong res;\n\tforeach (i; 0..n)\n\t{\n\t\tint p = 1 << i;\n\t\tint lo, me, hi;\n\t\tfor (lo = 0; lo < m; lo = hi)\n\t\t{\n\t\t\tme = lo + p;\n\t\t\thi = me + p;\n\t\t\ts[i + 1][0] += merge (a[lo..me], a[me..hi], b[lo..hi]);\n\t\t\ts[i + 1][1] += merge (a[me..hi], a[lo..me], b[lo..hi]);\n\t\t\ta[lo..hi] = b[lo..hi];\n\t\t}\n\t\tres += s[i + 1][0];\n\t}\n\n\tint q;\n\treadf (\" %s\", &q);\n\tforeach (r; 0..q)\n\t{\n\t\tint x;\n\t\tfor (readf (\" %s\", &x); x; x--)\n\t\t{\n\t\t\tres -= s[x][0];\n\t\t\tswap (s[x][0], s[x][1]);\n\t\t\tres += s[x][0];\n }\n\t\twriteln (res);\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.stdio;\n\nlong merge (int [] a, int [] b, int [] c)\n{\n\tlong res = 0;\n\tforeach (p; 0..c.length)\n\t{\n\t\tif (a.length > 0 && (b.length == 0 || a[0] <= b[0]))\n\t\t{\n\t\t\tc[p] = a[0];\n\t\t\ta = a[1..$];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tres += cast (long) (a.length);\n\t\t\tc[p] = b[0];\n\t\t\tb = b[1..$];\n\t\t}\n\t\tp++;\n\t}\n\treturn res;\n}\n\nvoid main ()\n{\n\tint n;\n\twhile (readf (\" %s\", &n) > 0)\n\t{\n\t\tint m = 1 << n;\n\t\tauto a = new int [m];\n\t\tauto b = new int [m];\n\t\tforeach (ref x; a)\n\t\t{\n\t\t\treadf (\" %s\", &x);\n\t\t}\n\n\t\tlong [2] [] s = new long [2] [n + 1];\n\t\ts[] = [0, 0];\n\t\tlong res = s[0][0];\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\tint p = 1 << i;\n\t\t\tfor (int k = 0; k < m; k += p * 2)\n\t\t\t{\n\t\t\t\ts[i + 1][0] += merge (a[k..k + p], a[k + p..k + p * 2], b[k..k + p * 2]);\n\t\t\t\ts[i + 1][1] += merge (a[k + p..k + p * 2], a[k..k + p], b[k..k + p * 2]);\n\t\t\t\ta[k..k + p * 2] = b[k..k + p * 2];\n\t\t\t}\n\t\t\tres += s[i + 1][0];\n\t\t}\n\n\t\tint q;\n\t\treadf (\" %s\", &q);\n\t\tforeach (r; 0..q)\n\t\t{\n\t\t\tint x;\n\t\t\treadf (\" %s\", &x);\n\t\t\tfor ( ; x >= 0; x--)\n\t\t\t{\n\t\t\t\tres -= s[x][0];\n\t\t\t\tswap (s[x][0], s[x][1]);\n\t\t\t\tres += s[x][0];\n\t }\n\t\t\twriteln (res);\n\t\t}\n\t}\n}\n"}, {"source_code": "import std.algorithm, std.stdio;\n\nlong merge (int [] a, int [] b, int [] c)\n{\n\tlong res;\n\tforeach (ref x; c)\n\t{\n\t\tif (a.length && (!b.length || a[0] <= b[0]))\n\t\t{\n\t\t\tx = a[0];\n\t\t\ta = a[1..$];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tres += a.length;\n\t\t\tx = b[0];\n\t\t\tb = b[1..$];\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid main ()\n{\n\tint n;\n\treadf (\" %s\", &n);\n\tint m = 1 << n;\n\tauto a = new int [m];\n\tauto b = new int [m];\n\tforeach (ref x; a)\n\t\treadf (\" %s\", &x);\n\n\tlong [2] [] s = new long [2] [n + 1];\n\tlong res;\n\tforeach (i; 0..n)\n\t{\n\t\tint p = 1 << i;\n\t\tint lo, me, hi;\n\t\tfor (lo = 0; lo < m; lo = hi)\n\t\t{\n\t\t\tme = lo + p;\n\t\t\thi = me + p;\n\t\t\ts[i + 1][0] += merge (a[lo..me], a[me..hi], b[lo..hi]);\n\t\t\ts[i + 1][1] += merge (a[me..hi], a[lo..me], b[lo..hi]);\n\t\t\ta[lo..hi] = b[lo..hi];\n\t\t}\n\t\tres += s[i + 1][0];\n\t}\n\n\tint q;\n\treadf (\" %s\", &q);\n\tforeach (r; 0..q)\n\t{\n\t\tint x;\n\t\tfor (readf (\" %s\", &x); x; x--)\n\t\t{\n\t\t\tres -= s[x][0];\n\t\t\tswap (s[x][0], s[x][1]);\n\t\t\tres += s[x][0];\n }\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "ea7f8bd397f80ba7d3add6f9609dcc4a"} {"nl": {"description": "Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.Polycarp decided to store the hash of the password, generated by the following algorithm: take the password $$$p$$$, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain $$$p'$$$ ($$$p'$$$ can still be equal to $$$p$$$); generate two random strings, consisting of lowercase Latin letters, $$$s_1$$$ and $$$s_2$$$ (any of these strings can be empty); the resulting hash $$$h = s_1 + p' + s_2$$$, where addition is string concatenation. For example, let the password $$$p =$$$ \"abacaba\". Then $$$p'$$$ can be equal to \"aabcaab\". Random strings $$$s1 =$$$ \"zyx\" and $$$s2 =$$$ \"kjh\". Then $$$h =$$$ \"zyxaabcaabkjh\".Note that no letters could be deleted or added to $$$p$$$ to obtain $$$p'$$$, only the order could be changed.Now Polycarp asks you to help him to implement the password check module. Given the password $$$p$$$ and the hash $$$h$$$, check that $$$h$$$ can be the hash for the password $$$p$$$.Your program should answer $$$t$$$ independent test cases.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains a non-empty string $$$p$$$, consisting of lowercase Latin letters. The length of $$$p$$$ does not exceed $$$100$$$. The second line of each test case contains a non-empty string $$$h$$$, consisting of lowercase Latin letters. The length of $$$h$$$ does not exceed $$$100$$$.", "output_spec": "For each test case print the answer to it — \"YES\" if the given hash $$$h$$$ could be obtained from the given password $$$p$$$ or \"NO\" otherwise.", "sample_inputs": ["5\nabacaba\nzyxaabcaabkjh\nonetwothree\nthreetwoone\none\nzzonneyy\none\nnone\ntwenty\nten"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO"], "notes": "NoteThe first test case is explained in the statement.In the second test case both $$$s_1$$$ and $$$s_2$$$ are empty and $$$p'=$$$ \"threetwoone\" is $$$p$$$ shuffled.In the third test case the hash could not be obtained from the password.In the fourth test case $$$s_1=$$$ \"n\", $$$s_2$$$ is empty and $$$p'=$$$ \"one\" is $$$p$$$ shuffled (even thought it stayed the same). In the fifth test case the hash could not be obtained from the password."}, "positive_code": [{"source_code": "import std.stdio, std.array, std.string, std.conv, std.algorithm;\nimport std.typecons, std.range, std.random, std.math, std.container;\nimport std.numeric, std.bigint, core.bitop, core.stdc.stdio;\n\nvoid main() {\n auto T = readln.chomp.to!int;\n\n auto A = new int[](26);\n auto B = new int[](26);\n\n bool ok() {\n return 26.iota.map!(i => A[i] >= B[i]).all;\n }\n\n while (T--) {\n A[] = 0;\n B[] = 0;\n auto P = readln.chomp;\n auto S = readln.chomp;\n if (P.length > S.length) {\n writeln(\"NO\");\n continue;\n }\n foreach (p; P) A[p-'a'] += 1;\n foreach (i; 0..P.length) B[S[i]-'a'] += 1;\n if (ok) {\n writeln(\"YES\");\n continue;\n }\n bool hoge = false;\n foreach (i; P.length..S.length) {\n B[S[i]-'a'] += 1;\n B[S[i-P.length.to!int]-'a'] -= 1;\n if (ok) {\n writeln(\"YES\");\n hoge = true;\n break;\n }\n }\n if (!hoge) {\n writeln(\"NO\");\n }\n }\n}\n"}, {"source_code": "import std.stdio, std.conv, std.functional, std.string;\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\nimport std.bigint, std.numeric, std.math, std.random;\nimport core.bitop;\n\nstring FMT_F = \"%.10f\";\n\nstatic File _f;\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\nstatic string[] s_rd;\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\nT _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; }\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\n\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\n\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\nlong lcm(long x, long y) { return x * (y / gcd(x, y)); }\n\nlong mod = 10^^9 + 7;\n//long mod = 998244353;\n//long mod = 1_000_003;\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\n\nvoid main()\n{\n\tauto t = RD!int;\n\tauto ans = new bool[](t);\n\tforeach (ti; 0..t)\n\t{\n\t\tauto p = RD!string;\n\t\tauto h = RD!string;\n\t\tauto cnt1 = new int[](26);\n\t\tforeach (c; p)\n\t\t\t++cnt1[c-'a'];\n\t\n\t\tauto cnt2 = new int[][](h.length+1, 26);\n\t\tforeach (i, c; h)\n\t\t{\n\t\t\tcnt2[i+1] = cnt2[i].dup;\n\t\t\t++cnt2[i+1][c-'a'];\n\t\t}\n\t\tforeach (i; p.length..h.length+1)\n\t\t{\n\t\t\tbool ok = true;\n\t\t\tforeach (j; 0..26)\n\t\t\t{\n\t\t\t\tif (cnt2[i][j] - cnt2[i-p.length][j] != cnt1[j])\n\t\t\t\t{\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok)\n\t\t\t{\n\t\t\t\tans[ti] = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach (e; ans)\n\t\twriteln(e ? \"YES\" : \"NO\");\n\tstdout.flush;\n\tdebug readln;\n}"}], "negative_code": [], "src_uid": "48151011c3d380ab303ae38d0804176a"} {"nl": {"description": "You have a bag of balls of n different colors. You have ai balls of the i-th color.While there are at least two different colored balls in the bag, perform the following steps: Take out two random balls without replacement one by one. These balls might be the same color. Color the second ball to the color of the first ball. You are not allowed to switch the order of the balls in this step. Place both balls back in the bag. All these actions take exactly one second. Let M = 109 + 7. It can be proven that the expected amount of time needed before you stop can be represented as a rational number , where P and Q are coprime integers and where Q is not divisible by M. Return the value .", "input_spec": "The first line of input will contain a single integer n (1 ≤ n ≤ 2 500) — the number of colors. The next line of input will contain n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the number of balls of each color.", "output_spec": "Print a single integer, the answer to the problem.", "sample_inputs": ["2\n1 1", "3\n1 2 3"], "sample_outputs": ["1", "750000026"], "notes": "NoteIn the first sample, no matter what happens, the balls will become the same color after one step.For the second sample, we have 6 balls. Let’s label the balls from 1 to 6, and without loss of generality, let’s say balls 1,2,3 are initially color 1, balls 4,5 are color 2, and ball 6 are color 3.Here is an example of how these steps can go: We choose ball 5 and ball 6. Ball 6 then becomes color 2. We choose ball 4 and ball 5. Ball 5 remains the same color (color 2). We choose ball 1 and ball 5. Ball 5 becomes color 1. We choose ball 6 and ball 5. Ball 5 becomes color 2. We choose ball 3 and ball 4. Ball 4 becomes color 1. We choose ball 4 and ball 6. Ball 6 becomes color 1. We choose ball 2 and ball 5. Ball 5 becomes color 1. At this point, the game ends since all the balls are the same color. This particular sequence took 7 seconds.It can be shown that the answer to this case is ."}, "positive_code": [{"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\nimport core.bitop;\n\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\nstring[] tokens;\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\nint readInt() { return readToken.to!int; }\nlong readLong() { return readToken.to!long; }\nreal readReal() { return readToken.to!real; }\n\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\n\nint 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; }\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\n\nstruct ModInt(int M_) {\n import std.conv : to;\n alias M = M_;\n int x;\n this(ModInt a) { x = a.x; }\n this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }\n ref ModInt opAssign(long a) { return (this = ModInt(a)); }\n ref ModInt opOpAssign(string op)(ModInt a) {\n static if (op == \"+\") { x += a.x; if (x >= M) x -= M; }\n else static if (op == \"-\") { x -= a.x; if (x < 0) x += M; }\n else static if (op == \"*\") { x = cast(int)((cast(long)(x) * a.x) % M); }\n else static if (op == \"/\") { this *= a.inv(); }\n else static assert(false);\n return this;\n }\n ref ModInt opOpAssign(string op)(long a) {\n static if (op == \"^^\") {\n if (a < 0) return (this = inv()^^(-a));\n ModInt t2 = this, te = ModInt(1);\n for (long e = a; e > 0; e >>= 1) {\n if (e & 1) te *= t2;\n t2 *= t2;\n }\n x = cast(int)(te.x);\n return this;\n } else return mixin(\"this \" ~ op ~ \"= ModInt(a)\");\n }\n ModInt inv() const {\n int a = x, b = M, y = 1, z = 0, t;\n for (; ; ) {\n t = a / b; a -= t * b;\n if (a == 0) {\n assert(b == 1 || b == -1);\n return ModInt(b * z);\n }\n y -= t * z;\n t = b / a; b -= t * a;\n if (b == 0) {\n assert(a == 1 || a == -1);\n return ModInt(a * y);\n }\n z -= t * y;\n }\n }\n ModInt opUnary(string op: \"-\")() const { return ModInt(-x); }\n ModInt opBinary(string op, T)(T a) const {\n return mixin(\"ModInt(this) \" ~ op ~ \"= a\");\n }\n ModInt opBinaryRight(string op)(long a) const {\n return mixin(\"ModInt(a) \" ~ op ~ \"= this\");\n }\n bool opCast(T: bool)() const { return (x != 0); }\n string toString() const { return x.to!string; }\n}\n\nenum MO = 10^^9 + 7;\nalias Mint = ModInt!MO;\n\n/*\n p[i] = a[i] (S - a[i]) / (S (S - 1))\n e[i] = 1 + p[i] e[i - 1] + (1 - 2 p[i]) e[i] + p[i] e[i + 1]\n e[0] = 0, e[S] = 0\n \n e[i - 1] = -1 / p[i] + 2 e[i] - e[i + 1]\n e[i + 1] - e[i] = (e[i] - e[i - 1]) - 1 / p[i]\n*/\n\nvoid main() {\n try {\n for (; ; ) {\n const N = readInt();\n auto A = new int[N];\n foreach (i; 0 .. N) {\n A[i] = readInt();\n }\n const S = A.sum;\n const maxA = A.maxElement;\n \n const d = Mint(S - 1) * Mint(S - 1) / S;\n auto es = new Mint[maxA + 1];\n es[0] = 0;\n Mint dd = d;\n foreach (i; 1 .. maxA + 1) {\n es[i] = es[i - 1] + dd;\n dd -= Mint(S - 1) * Mint(S - i).inv;\n }\n debug {\n writeln(\"es = \", es);\n }\n \n Mint ans;\n foreach (i; 0 .. N) {\n ans += es[A[i]];\n }\n writeln(ans);\n }\n } catch (EOFException e) {\n }\n}\n"}], "negative_code": [], "src_uid": "09f18022a913ce3735c4c8c21a8ea69e"} {"nl": {"description": "Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes, and the second — y (y ≥ 1) potatoes. Valera — very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater than n, and, secondly, was divisible by k.Help Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order.", "input_spec": "The first line of input contains three integers y, k, n (1 ≤ y, k, n ≤ 109;  ≤ 105).", "output_spec": "Print the list of whitespace-separated integers — all possible values of x in ascending order. You should print each possible value of x exactly once. If there are no such values of x print a single integer -1.", "sample_inputs": ["10 1 10", "10 6 40"], "sample_outputs": ["-1", "2 8 14 20 26"], "notes": null}, "positive_code": [{"source_code": "module cf_239A;\n\nimport std.stdio;\n\nvoid main() {\n int y, k, n, x = 1;\n\n readf(\"%d %d %d\", &y, &k, &n);\n\n x = k - y % k;\n if (x + y > n) {\n writeln(-1);\n } else {\n while (x + y <= n) {\n writef(\"%d \", x);\n x += k;\n }\n writeln();\n }\n}"}], "negative_code": [], "src_uid": "2deda3a05740e1184735bf437e3850a8"} {"nl": {"description": "This is the easy version of the problem. The difference is the constraints on $$$n$$$, $$$m$$$ and $$$t$$$. You can make hacks only if all versions of the problem are solved.Alice and Bob are given the numbers $$$n$$$, $$$m$$$ and $$$k$$$, and play a game as follows:The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially $$$0$$$. The game consists of $$$n$$$ turns. Each turn, Alice picks a real number from $$$0$$$ to $$$k$$$ (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least $$$m$$$ out of the $$$n$$$ turns.Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn).If Alice and Bob play optimally, what will the final score of the game be?", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The description of test cases follows. Each test case consists of a single line containing the three integers, $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \\le m \\le n \\le 2000, 0 \\le k < 10^9 + 7$$$) — the number of turns, how many of those turns Bob has to add, and the biggest number Alice can choose, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.", "output_spec": "For each test case output a single integer number — the score of the optimal game modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \\not \\equiv 0 \\pmod{M}$$$. Output the integer equal to $$$p \\cdot q^{-1} \\bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \\le x < M$$$ and $$$x \\cdot q \\equiv p \\pmod{M}$$$.", "sample_inputs": ["7\n\n3 3 2\n\n2 1 10\n\n6 3 10\n\n6 4 10\n\n100 1 1\n\n4 4 0\n\n69 4 20"], "sample_outputs": ["6\n5\n375000012\n500000026\n958557139\n0\n49735962"], "notes": "NoteIn the first test case, the entire game has $$$3$$$ turns, and since $$$m = 3$$$, Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is $$$k = 2$$$, every turn.In the third test case, Alice has a strategy to guarantee a score of $$$\\frac{75}{8} \\equiv 375000012 \\pmod{10^9 + 7}$$$.In the fourth test case, Alice has a strategy to guarantee a score of $$$\\frac{45}{2} \\equiv 500000026 \\pmod{10^9 + 7}$$$."}, "positive_code": [{"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nimmutable int mod = 10 ^^ 9 + 7;\r\nimmutable int half = mod / 2 + 1;\r\nstatic assert ((half * 2L) % mod == 1);\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, m, k;\r\n\t\treadf !(\" %s %s %s\") (n, m, k);\r\n\t\tauto f = new int [] [] (n + 1, m + 1);\r\n\t\tforeach (i; 0..n + 1)\r\n\t\t{\r\n\t\t\tforeach (j; 0..min (i, m) + 1)\r\n\t\t\t{\r\n\t\t\t\tif (j == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tf[i][j] = 0;\r\n\t\t\t\t}\r\n\t\t\t\telse if (j == i)\r\n\t\t\t\t{\r\n\t\t\t\t\tf[i][j] = j;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tf[i][j] = (half * 1L * (f[i - 1][j] +\r\n\t\t\t\t\t f[i - 1][j - 1])) % mod;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tint res = (f[n][m] * 1L * k) % mod;\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "ee4038a896565a82d2d0d5293fce2a18"} {"nl": {"description": "While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level.Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.", "input_spec": "The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m (). The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), indicating that robot ui beat robot vi in the i-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all m relations.", "output_spec": "Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1.", "sample_inputs": ["4 5\n2 1\n1 3\n2 3\n4 2\n4 3", "3 2\n1 2\n3 2"], "sample_outputs": ["4", "-1"], "notes": "NoteIn the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles.In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles."}, "positive_code": [{"source_code": "import std.stdio;\nimport std.algorithm;\nimport std.typecons;\nimport std.string;\nimport std.array;\nimport std.conv;\nimport std.math;\n\nalias Pair = Tuple!(int, q{a}, int, q{b});\n\nvoid main() {\n int n, m;\n readf(\" %s %s\", &n, &m);\n Pair[] edges;\n foreach (i; 0..m) {\n int u, v;\n readf(\" %s %s\", &u, &v);\n u--; v--;\n edges ~= Pair(u, v);\n }\n\n bool can(int x) {\n auto adj = new int[][](n);\n auto inDegree = new int[n];\n foreach (i; 0..x) {\n adj[edges[i].a] ~= edges[i].b;\n inDegree[edges[i].b]++;\n }\n\n int cur = -1;\n int visited = 0;\n foreach (i; 0..n) {\n if (inDegree[i] == 0) {\n if (cur != -1) {\n return false;\n }\n cur = i;\n }\n }\n\n while (visited < n && cur != -1) {\n int next = -1;\n foreach (v; adj[cur]) {\n inDegree[v]--;\n if (inDegree[v] == 0) {\n if (next != -1) {\n return false;\n }\n next = v;\n }\n }\n visited++;\n cur = next;\n }\n\n return visited == n;\n }\n \n int lo = -1, hi = m + 1;\n while (lo + 1 < hi) {\n int mid = (lo + hi) >> 1;\n if (can(mid)) hi = mid;\n else lo = mid;\n }\n\n writeln(hi == m + 1 ? -1 : hi);\n}\n\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.exception;\nimport std.functional;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\nimport core.bitop;\n\nvoid main ()\n{\n\tint n;\n\tint m;\n\twhile (readf (\" %s %s\", &n, &m) > 0)\n\t{\n\t\tauto a = new int [int] [n];\n\t\tforeach (i; 0..m)\n\t\t{\n\t\t\tint u, v;\n\t\t\treadf (\" %s %s\", &u, &v);\n\t\t\tu--;\n\t\t\tv--;\n\t\t\ta[u][v] = i;\n\t\t}\n\n\t\tauto b = new bool [n];\n\t\tb[] = false;\n\t\tint [] order;\n\t\tint cur = 0;\n\n\t\tvoid recur (int u)\n\t\t{\n\t\t\tif (b[u])\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tb[u] = true;\n\t\t\tforeach (k, v; a[u])\n\t\t\t{\n\t\t\t\trecur (k);\n\t\t\t}\n\t\t\torder ~= u;\n\t\t}\n\n\t\tforeach (i; 0..n)\n\t\t{\n\t\t\trecur (i);\n\t\t}\n\n\t\tassert (order.length == n);\n\t\tint res = 0;\n\t\tforeach (i; 1..n)\n\t\t{\n\t\t\tauto t = order[i - 1] in a[order[i]];\n\t\t\tif (t is null)\n\t\t\t{\n\t\t\t\tres = m + 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tres = max (res, (*t) + 1);\n\t\t\t}\n\t\t}\n\n\t\tif (res > m)\n\t\t{\n\t\t\tres = -1;\n\t\t}\n\t\twriteln (res);\n\t}\n}\n"}], "negative_code": [], "src_uid": "5337df1fb8a9b96ab7b66aa197a5b910"} {"nl": {"description": "There is a graph of $$$n$$$ rows and $$$10^6 + 2$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ and columns from $$$0$$$ to $$$10^6 + 1$$$: Let's denote the node in the row $$$i$$$ and column $$$j$$$ by $$$(i, j)$$$.Initially for each $$$i$$$ the $$$i$$$-th row has exactly one obstacle — at node $$$(i, a_i)$$$. You want to move some obstacles so that you can reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs $$$u$$$ or $$$v$$$ coins, as below: If there is an obstacle in the node $$$(i, j)$$$, you can use $$$u$$$ coins to move it to $$$(i-1, j)$$$ or $$$(i+1, j)$$$, if such node exists and if there is no obstacle in that node currently. If there is an obstacle in the node $$$(i, j)$$$, you can use $$$v$$$ coins to move it to $$$(i, j-1)$$$ or $$$(i, j+1)$$$, if such node exists and if there is no obstacle in that node currently. Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from $$$(1,1)$$$ to $$$(0,1)$$$. Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$u$$$ and $$$v$$$ ($$$2 \\le n \\le 100$$$, $$$1 \\le u, v \\le 10^9$$$) — the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$) — where $$$a_i$$$ represents that the obstacle in the $$$i$$$-th row is in node $$$(i, a_i)$$$. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^4$$$.", "output_spec": "For each test case, output a single integer — the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles. It can be shown that under the constraints of the problem there is always a way to make such a trip possible.", "sample_inputs": ["3\n2 3 4\n2 2\n2 3 4\n3 2\n2 4 3\n3 2"], "sample_outputs": ["7\n3\n3"], "notes": "NoteIn the first sample, two obstacles are at $$$(1, 2)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(2, 2)$$$ to $$$(2, 3)$$$, then to $$$(1, 3)$$$. The total cost is $$$u+v = 7$$$ coins. In the second sample, two obstacles are at $$$(1, 3)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(1, 3)$$$ to $$$(2, 3)$$$. The cost is $$$u = 3$$$ coins. "}, "positive_code": [{"source_code": "import std.stdio, std.conv, std.functional, std.string;\r\nimport std.algorithm, std.array, std.container, std.range, std.typecons;\r\nimport std.bigint, std.numeric, std.math, std.random;\r\nimport core.bitop;\r\n\r\nstring FMT_F = \"%.10f\";\r\n\r\nstatic File _f;\r\nvoid file_io(string fn) { _f = File(fn, \"r\"); }\r\nstatic string[] s_rd;\r\nT _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }\r\nT _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; }\r\nT[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }\r\nT RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }\r\nT[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }\r\n\r\nsize_t[] MAKE_IDX(alias less = \"a < b\", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}\r\nsize_t MIN_POS(alias less = \"a < b\", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }\r\n\r\nvoid chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }\r\nbool inside(T)(T x, T b, T e) { return x >= b && x < e; }\r\nT lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }\r\n\r\n//long mod = 10^^9 + 7;\r\nlong mod = 998_244_353;\r\n//long mod = 1_000_003;\r\nvoid moda(ref long x, long y) { x = (x + y) % mod; }\r\nvoid mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }\r\nvoid modm(ref long x, long y) { x = (x * y) % mod; }\r\nvoid 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); }\r\nvoid modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }\r\n\r\nvoid main()\r\n{\r\n\tauto t = RD!int;\r\n\tauto ans = new long[](t);\r\n\tforeach (ti; 0..t)\r\n\t{\r\n\t\tauto n = RD!int;\r\n\t\tauto u = RD;\r\n\t\tauto v = RD;\r\n\t\tauto a = RDA;\r\n\r\n\t\tbool ok;\r\n\t\tforeach (i; 1..n)\r\n\t\t{\r\n\t\t\tauto d = a[i] - a[i-1];\r\n\t\t\tif (d.abs > 1)\r\n\t\t\t\tok = true;\r\n\t\t}\r\n\t\tif (ok) continue;\r\n\r\n\t\tbool[long] used;\r\n\t\tforeach (i; 0..n)\r\n\t\t\tused[a[i]] = true;\r\n\t\tif (used.length == 1)\r\n\t\t\tans[ti] = min(v * 2, u + v);\r\n\t\telse\r\n\t\t\tans[ti] = min(u, v);\r\n\t}\r\n\r\n\tforeach (e; ans)\r\n\t\twriteln(e);\r\n\tstdout.flush;\r\n\tdebug readln;\r\n}"}, {"source_code": "import std.conv, std.functional, std.range, std.stdio, std.string;\r\nimport std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;\r\nimport core.bitop;\r\n\r\nclass EOFException : Throwable { this() { super(\"EOF\"); } }\r\nstring[] tokens;\r\nstring readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }\r\nint readInt() { return readToken.to!int; }\r\nlong readLong() { return readToken.to!long; }\r\nreal readReal() { return readToken.to!real; }\r\n\r\nbool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }\r\nbool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }\r\n\r\nint 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; }\r\nint lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }\r\nint upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }\r\n\r\n\r\n\r\n\r\nvoid main() {\r\n try {\r\n for (; ; ) {\r\n const numCases = readInt();\r\n foreach (caseId; 0 .. numCases) {\r\n const N = readInt();\r\n const U = readLong();\r\n const V = readLong();\r\n auto A = new int[N];\r\n foreach (i; 0 .. N) {\r\n A[i] = readInt();\r\n }\r\n \r\n bool bad = true;\r\n bool same = true;\r\n foreach (i; 0 .. N - 1) {\r\n bad = bad && (abs(A[i + 1] - A[i]) <= 1);\r\n same = same && (abs(A[i + 1] - A[i]) <= 0);\r\n }\r\n long ans;\r\n if (bad) {\r\n if (same) {\r\n ans = min(V + V, V + U);\r\n } else {\r\n ans = min(V, U);\r\n }\r\n } else {\r\n ans = 0;\r\n }\r\n writeln(ans);\r\n }\r\n }\r\n } catch (EOFException e) {\r\n }\r\n}\r\n"}, {"source_code": "import std.algorithm;\r\nimport std.conv;\r\nimport std.math;\r\nimport std.range;\r\nimport std.stdio;\r\nimport std.string;\r\n\r\nvoid main ()\r\n{\r\n\tauto tests = readln.strip.to !(int);\r\n\tforeach (test; 0..tests)\r\n\t{\r\n\t\tint n, u, v;\r\n\t\treadf !(\" %s %s %s\") (n, u, v);\r\n\t\treadln;\r\n\t\tauto a = readln.splitter.map !(to !(int)).array;\r\n\t\tauto d = 0;\r\n\t\tforeach (i; 1..n)\r\n\t\t{\r\n\t\t\td = max (d, abs (a[i] - a[i - 1]));\r\n\t\t}\r\n\t\tint res = 0;\r\n\t\tif (d <= 0)\r\n\t\t{\r\n\t\t\tres += v;\t\r\n\t\t}\r\n\t\tif (d <= 1)\r\n\t\t{\r\n\t\t\tres += min (u, v);\r\n\t\t}\r\n\t\twriteln (res);\r\n\t}\r\n}\r\n"}], "negative_code": [], "src_uid": "7f502f2fd150a2ded948826960d123cd"} {"nl": {"description": "You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \\le a, b \\le 1000$$$).", "output_spec": "Print $$$t$$$ integers — the required numbers $$$a+b$$$.", "sample_inputs": ["4\n\n1 5\n\n314 15\n\n-99 99\n\n123 987"], "sample_outputs": ["6\n329\n0\n1110"], "notes": null}, "positive_code": [{"source_code": "import std.algorithm;\nimport std.conv;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main ()\n{\n\tauto tests = readln.strip.to !(int);\n\tforeach (test; 0..tests)\n\t{\n\t\treadln.splitter.map !(to !(int)).sum.writeln;\n\t}\n}\n"}, {"source_code": "import std.algorithm;\nimport std.array;\nimport std.container;\nimport std.conv;\nimport std.math;\nimport std.numeric;\nimport std.range;\nimport std.stdio;\nimport std.string;\nimport std.typecons;\n\nvoid main() {\n int t; scan(t);\n foreach (_; 0..t) {\n int a, b; scan(a, b);\n writeln(a + b);\n }\n}\n\nvoid scan(T...)(ref T a) {\n string[] ss = readln.split;\n foreach (i, t; T) a[i] = ss[i].to!t;\n}\nT read(T=string)() { return readln.chomp.to!T; }\nT[] reads(T)() { return readln.split.to!(T[]); }\nalias readints = reads!int;\n"}, {"source_code": "import std.stdio;\nint main()\n{\n\tint t=0;\n\treadf(\" %d\", &t);\n\tfor (int i=0; i