(values: &mut [T], start: usize, gap: usize) {\n for i in ((start + gap)..values.len()).step_by(gap) {\n let val_current = values[i];\n let mut pos = i;\n\n while pos >= gap && values[pos - gap] > val_current {\n values[pos] = values[pos - gap];\n pos -= gap;\n }\n values[pos] = val_current;\n }\n }\n\n let mut count_sublist = values.len() / 2;\n while count_sublist > 0 {\n for pos_start in 0..count_sublist {\n insertion(values, pos_start, count_sublist);\n }\n count_sublist /= 2;\n }\n}\n", "difficulty": "medium"}
{"problem_id": "0060", "problem_description": "Prime Number
\n\n\nWrite a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.\n
\n\nInput
\n\n\nInput consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.\n
\n\n\nThe number of datasets is less than or equal to 30.\n
\n\nOutput
\n\n\nFor each dataset, prints the number of prime numbers.\n
\n\nSample Input
\n\n\n10\n3\n11\n
\n\nOutput for the Sample Input
\n\n\n4\n2\n5\n
", "c_code": "int solution() {\n while (1) {\n int n;\n int i;\n int cnt = 0;\n int sw = 1;\n int sosuu[100000];\n int t;\n if (scanf(\"%d\", &n) == EOF) {\n break;\n }\n if (n <= 1) {\n cnt = 0;\n } else {\n sosuu[cnt] = 2;\n cnt++;\n for (i = 3; i <= n; i += 2) {\n sw = 1;\n for (t = 0; t < cnt && sosuu[t] * sosuu[t] <= i; t++) {\n if (i % sosuu[t] == 0) {\n sw = 0;\n break;\n }\n }\n if (sw == 1) {\n sosuu[cnt] = i;\n cnt++;\n }\n }\n }\n printf(\"%d\\n\", cnt);\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let end: i32 = match s.trim().parse() {\n Ok(end) => end,\n Err(_) => return,\n };\n if end < 2 {\n println!(\"{}\", 0);\n continue;\n }\n\n let mut flags = vec![true; ((end + 1) / 2) as usize];\n let flags_len = flags.len();\n flags[0] = false;\n for i in 1..flags_len {\n if !flags[i] {\n continue;\n }\n let n = i * 2 + 1;\n let mut j = 3;\n let mut comp_index = (n * j - 1) / 2;\n while comp_index < flags_len {\n flags[comp_index] = false;\n j += 2;\n comp_index = (n * j - 1) / 2;\n }\n }\n\n println!(\"{}\", flags.iter().filter(|x| **x).count() + 1);\n }\n}", "difficulty": "medium"}
{"problem_id": "0061", "problem_description": "博士が愛した2進数
\n\n\n 「君の靴のサイズはいくつかね」
\n
\n 初対面の私に、いきなり博士は尋ねました。
\n
\n 「 23.5 です」
\n\n 「ほお、実にきりのいい数字だ。2 の 4 乗に 2 の 2 乗と 2 の 1 乗と 2 の 0 乗と 2 の -1 乗を 加えた数だ」
\n
\n 続けて博士は尋ねました。
\n
\n 「君、身長はいくつかね」
\n\n 「はい、158.1 です」
\n
\n 博士は腕組みをして目を閉じました。しばらくの沈黙の後、口を開きました。
\n
\n 「ナァ~」
\n
\n その後一緒に過ごした時間の中で、博士の行動がだんだん理解できるようになりました。\n
\n\n\n まず、 私が博士の求めに応じて実数を言います。実数が整数部 8 桁以内で小数部 4 桁以内の 2 進数で表される場合、博士は 2 進数への変換結果を満足げに語ります。そうでない場合、悲しげに「ナァ~」 と鳴きます。これは、私が負の実数を言うまで繰り返されるのです。\n
\n\n\n さて、博士は年齢とともにだんだんに長い計算が難しくなってきました。そこで、みなさんが博士に代わって、実数を入力し 2 進数に変換・出力するプログラムを作ってあげてください。ただし、2 進表現が制限桁数 (整数部 8 桁以内 + 小数部 4 桁以内) に収まらない場合は、NA (半角英字) を出力してください。入力される実数は整数部 8 桁以内、小数部 4 桁以内に収まるものとし、出力する 2 進表現は整数部 8 桁、小数部 4 桁で出力してください。\n
\n\nInput
\n\n\n複数のデータセットの並びが入力として与えられます。入力の終わりは負の実数ひとつの行で示されます。\n各データセットとして1つの実数 n が1行に与えられます。\n
\n\n\nデータセットの数は 1200 を超えません。\n
\n\n\nOutput
\n\n\n入力データセットごとに 2 進数への変換結果を出力します。\n
\n\nSample Input
\n\n\n23.5\n158.1\n-1.0\n
\n\nOutput for the Sample Input
\n\n\n00010111.1000\nNA\n
", "c_code": "int solution() {\n double n;\n\n while (1) {\n scanf(\"%lf\", &n);\n\n if (n < 0) {\n return 0;\n }\n\n n *= 2 * 2 * 2 * 2;\n\n int m = (int)n;\n\n if (n - (double)m == 0 && (m >> 12) == 0) {\n int i = 0;\n for (; i < 12; ++i) {\n if (i == 8) {\n printf(\".\");\n }\n\n printf(\"%d\", (m >> (12 - i - 1) & 1));\n }\n\n puts(\"\");\n }\n\n else {\n puts(\"NA\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let lines = stdin.lock().lines();\n\n for line in lines {\n let n: f64 = line.unwrap().parse().unwrap();\n if n < 0. {\n return;\n }\n\n let nf = n * 16.;\n let ni = nf as u64;\n\n if ni > 0xfff || nf.fract() != 0. {\n println!(\"NA\");\n } else {\n println!(\"{:08b}.{:04b}\", ni >> 4, ni & 0b1111);\n }\n }\n}", "difficulty": "medium"}
{"problem_id": "0062", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
\nThere are N people numbered 1 to N. Each person wears a red hat or a blue hat.
\nYou are given a string s representing the colors of the people. Person i wears a red hat if s_i is R, and a blue hat if s_i is B.
\nDetermine if there are more people wearing a red hat than people wearing a blue hat.
\n\n
\n\n
\nConstraints
\n- 1 \\leq N \\leq 100
\n- |s| = N
\n- s_i is
R or B. \n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\ns\n
\n\n
\n
\n
\nOutput
If there are more people wearing a red hat than there are people wearing a blue hat, print Yes; otherwise, print No.
\n\n
\n
\n
\n\n
\nSample Input 1
4\nRRBR\n
\n\n
\n\n
\nSample Output 1
Yes\n
\n\n- There are three people wearing a red hat, and one person wearing a blue hat.
\n- Since there are more people wearing a red hat than people wearing a blue hat, the answer is
Yes. \n
\n\n
\n
\n\n
\nSample Input 2
4\nBRBR\n
\n\n
\n\n
\nSample Output 2
No\n
\n\n- There are two people wearing a red hat, and two people wearing a blue hat.
\n- Since there are as many people wearing a red hat as people wearing a blue hat, the answer is
No. \n
\n
\n", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n char s[100];\n scanf(\"%s\", s);\n int i = 0;\n int countR = 0;\n while (i < n) {\n if (s[i] == 'R') {\n countR++;\n }\n i++;\n }\n if (countR > n - countR) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n\n let (r, b) = buf.chars().fold((0, 0), |(mut r, mut b), c| {\n match c {\n 'R' => r += 1,\n 'B' => b += 1,\n _ => (),\n }\n\n (r, b)\n });\n\n if r > b {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"}
{"problem_id": "0063", "problem_description": "\nDay of Week
\n\n\n\n The 9th day of September 2017 is Saturday. Then, what day of the week is the X-th of September 2017? \n
\n\n\n Given a day in September 2017, write a program to report what day of the week it is.\n
\n\n\nInput
\n\n\n The input is given in the following format.\n
\n\n\nX\n
\n\n The input line specifies a day X (1 ≤ X ≤ 30) in September 2017.\n
\n\n\nOutput
\n\n\nOutput what day of the week it is in a line. Use the following conventions in your output: \"mon\" for Monday, \"tue\" for Tuesday, \"wed\" for Wednesday, \"thu\" for Thursday, \"fri\" for Friday, \"sat\" for Saturday, and \"sun\" for Sunday.\n
\n\nSample Input 1
\n\n\n1\n
\n\nSample Output 1
\n\nfri\n
\n\nSample Input 2
\n\n9\n
\n\nSample Output 2
\n\nsat\n
\n\nSample Input 3
\n\n30\n
\n\nSample Output 3
\n\nsat\n
", "c_code": "int solution(void) {\n char data[7][4] = {\"thu\", \"fri\", \"sat\", \"sun\", \"mon\", \"tue\", \"wed\"};\n int X = 0;\n scanf(\"%d\", &X);\n printf(\"%s\\n\", data[X % 7]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n\n let a: usize = iter.next().unwrap().parse().unwrap();\n\n let days = [\"mon\", \"tue\", \"wed\", \"thu\", \"fri\", \"sat\", \"sun\"];\n\n let x = (a % 7 + 3) % 7;\n println!(\"{}\", days[x]);\n}", "difficulty": "easy"}
{"problem_id": "0064", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
We will call a string that can be obtained by concatenating two equal strings an even string.\nFor example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.
\nYou are given an even string S consisting of lowercase English letters.\nFind the length of the longest even string that can be obtained by deleting one or more characters from the end of S.\nIt is guaranteed that such a non-empty string exists for a given input.
\n\n
\n\n
\nConstraints
\n- 2 \\leq |S| \\leq 200
\n- S is an even string consisting of lowercase English letters.
\n- There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nS\n
\n\n
\n
\n
\nOutput
Print the length of the longest even string that can be obtained.
\n\n
\n
\n
\n\n
\nSample Input 1
abaababaab\n
\n\n
\n\n
\nSample Output 1
6\n
\n\nabaababaab itself is even, but we need to delete at least one character. \nabaababaa is not even. \nabaababa is not even. \nabaabab is not even. \nabaaba is even. Thus, we should print its length, 6. \n
\n\n
\n
\n\n\n
\nSample Output 2
2\n
\n\nxxx is not even. \nxx is even. \n
\n\n
\n
\n\n
\nSample Input 3
abcabcabcabc\n
\n\n
\n\n
\nSample Output 3
6\n
\nThe longest even string that can be obtained is abcabc, whose length is 6.
\n\n
\n
\n\n
\nSample Input 4
akasakaakasakasakaakas\n
\n\n
\n\n
\nSample Output 4
14\n
\nThe longest even string that can be obtained is akasakaakasaka, whose length is 14.
\n
\n", "c_code": "int solution() {\n char str[200] = {0};\n int len = 0;\n\n scanf(\"%s\", str);\n len = strlen(str);\n\n while (1) {\n char tmp1[100] = {0};\n char tmp2[100] = {0};\n\n if ((strlen(str) % 2) != 0) {\n len -= 1;\n } else {\n len -= 2;\n }\n\n strncpy(tmp1, &str[0], len / 2);\n strncpy(tmp2, &str[len / 2], len / 2);\n\n if (strcmp(tmp1, tmp2) == 0) {\n break;\n }\n }\n\n printf(\"%d\\n\", len);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n\n let mut it = buf.split_whitespace().map(|n| String::from_str(n).unwrap());\n let mut s = it.next().unwrap();\n\n while !s.is_empty() {\n s = s[0..s.len() - 2].to_string();\n if s.len() % 2 == 1 {\n continue;\n }\n if s[0..s.len() / 2 - 1] == s[s.len() / 2..s.len() - 1] {\n println!(\"{}\", s.len());\n return;\n }\n }\n}", "difficulty": "medium"}
{"problem_id": "0065", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
There are N stones arranged in a row. Every stone is painted white or black.\nA string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.
\nTakahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.\nFind the minimum number of stones that needs to be recolored.
\n\n
\n\n
\nConstraints
\n- 1 \\leq N \\leq 2\\times 10^5
\n- S is a string of length N consisting of
. and #. \n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\nS\n
\n\n
\n
\n
\nOutput
Print the minimum number of stones that needs to be recolored.
\n\n
\n
\n
\n\n
\nSample Input 1
3\n#.#\n
\n\n
\n\n
\nSample Output 1
1\n
\nIt is enough to change the color of the first stone to white.
\n\n
\n
\n\n
\nSample Input 2
5\n#.##.\n
\n\n
\n\n
\n\n
\nSample Input 3
9\n.........\n
\n\n
\n\n", "c_code": "int solution(void) {\n int n;\n int i;\n char s[200002];\n int a[200001];\n scanf(\"%d%s\", &n, s + 1);\n\n a[0] = 0;\n for (i = 1; i <= n; i++) {\n a[i] = a[i - 1] + (s[i] == '#');\n }\n int min = INT_MAX;\n for (i = 0; i <= n; i++) {\n int now = a[i] + (n - i - a[n] + a[i]);\n if (now < min) {\n min = now;\n }\n }\n printf(\"%d\\n\", min);\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let s = buf.trim();\n\n let mut nr_wb = Vec::<(usize, usize)>::with_capacity(n);\n\n for c in s.chars() {\n let (w, b) = *nr_wb.last().unwrap_or(&(0usize, 0usize));\n if c == '.' {\n nr_wb.push((w + 1, b));\n } else {\n nr_wb.push((w, b + 1));\n }\n }\n\n let mut ans_wb: usize = n;\n let (all_w, _) = *nr_wb.last().unwrap();\n for i in 0..(n - 1) {\n let (cur_w, cur_b) = nr_wb[i];\n let rem_w = all_w - cur_w;\n ans_wb = std::cmp::min(ans_wb, cur_b + rem_w);\n }\n\n let ans_w: usize = nr_wb.last().unwrap().1;\n let ans_b: usize = nr_wb.last().unwrap().0;\n\n println!(\"{}\", std::cmp::min(std::cmp::min(ans_w, ans_b), ans_wb));\n}", "difficulty": "hard"}
{"problem_id": "0066", "problem_description": "Polycarp found under the Christmas tree an array $$$a$$$ of $$$n$$$ elements and instructions for playing with it: At first, choose index $$$i$$$ ($$$1 \\leq i \\leq n$$$) — starting position in the array. Put the chip at the index $$$i$$$ (on the value $$$a_i$$$). While $$$i \\leq n$$$, add $$$a_i$$$ to your score and move the chip $$$a_i$$$ positions to the right (i.e. replace $$$i$$$ with $$$i + a_i$$$). If $$$i > n$$$, then Polycarp ends the game. For example, if $$$n = 5$$$ and $$$a = [7, 3, 1, 2, 3]$$$, then the following game options are possible: Polycarp chooses $$$i = 1$$$. Game process: $$$i = 1 \\overset{+7}{\\longrightarrow} 8$$$. The score of the game is: $$$a_1 = 7$$$. Polycarp chooses $$$i = 2$$$. Game process: $$$i = 2 \\overset{+3}{\\longrightarrow} 5 \\overset{+3}{\\longrightarrow} 8$$$. The score of the game is: $$$a_2 + a_5 = 6$$$. Polycarp chooses $$$i = 3$$$. Game process: $$$i = 3 \\overset{+1}{\\longrightarrow} 4 \\overset{+2}{\\longrightarrow} 6$$$. The score of the game is: $$$a_3 + a_4 = 3$$$. Polycarp chooses $$$i = 4$$$. Game process: $$$i = 4 \\overset{+2}{\\longrightarrow} 6$$$. The score of the game is: $$$a_4 = 2$$$. Polycarp chooses $$$i = 5$$$. Game process: $$$i = 5 \\overset{+3}{\\longrightarrow} 8$$$. The score of the game is: $$$a_5 = 3$$$. Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.", "c_code": "int solution(void) {\n\n int *arr = malloc(200000 * sizeof(int));\n int *a = arr;\n\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n unsigned long long maximum_score = 0;\n\n for (int i = 0; i < n; i++) {\n int j = i;\n unsigned long long current_score = 0;\n\n while ((j < n) && (a[j])) {\n current_score += a[j];\n\n int temp = a[j];\n a[j] = 0;\n j += temp;\n }\n\n if (current_score > maximum_score) {\n maximum_score = current_score;\n }\n }\n\n printf(\"%llu\\n\", maximum_score);\n\n a += n;\n }\n\n free(arr);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n stdin().read_line(&mut t).unwrap();\n let t: i32 = t.trim().parse().unwrap();\n\n let out = &mut BufWriter::new(stdout());\n\n for _i in 0..t {\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n let n: usize = n.trim().parse().unwrap();\n\n let mut a = String::new();\n stdin().read_line(&mut a).unwrap();\n let mut a: Vec = a.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n for ith in (0..n).rev() {\n let jth = ith + a[ith] as usize;\n if jth < n {\n a[ith] += a[jth];\n }\n }\n a.sort();\n writeln!(out, \"{}\", a[n - 1]).ok();\n }\n}", "difficulty": "medium"}
{"problem_id": "0067", "problem_description": "You are given a string $$$s$$$ consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and has exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'. Note that each of the characters '?' is replaced independently from the others.A string $$$t$$$ of length $$$n$$$ is called a palindrome if the equality $$$t[i] = t[n-i+1]$$$ is true for all $$$i$$$ ($$$1 \\le i \\le n$$$).For example, if $$$s=$$$\"01?????0\", $$$a=4$$$ and $$$b=4$$$, then you can replace the characters '?' in the following ways: \"01011010\"; \"01100110\". For the given string $$$s$$$ and the numbers $$$a$$$ and $$$b$$$, replace all the characters with '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and has exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'.", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n for (long long int i = 0; i < t; i++) {\n int isPal = 1;\n long long int nulls_ones[2];\n scanf(\"%lld%lld\\n\", &nulls_ones[0], &nulls_ones[1]);\n\n long long int len = nulls_ones[0] + nulls_ones[1];\n char str[200001];\n scanf(\"%s\", str);\n\n long long int j = 0;\n long long int m = len - 1;\n while (j < m) {\n if (str[j] != str[m]) {\n if (str[j] == '?') {\n str[j] = str[m];\n } else if (str[m] == '?') {\n str[m] = str[j];\n } else {\n isPal = 0;\n }\n nulls_ones[str[m] - '0'] -= 2;\n } else if (str[j] != '?') {\n nulls_ones[str[m] - '0'] -= 2;\n }\n j++;\n m--;\n }\n\n if (j == m) {\n long long int k = len / 2;\n if (str[k] == '?') {\n if (nulls_ones[0] % 2 != 0) {\n str[k] = '0';\n } else if (nulls_ones[1] % 2 != 0) {\n str[k] = '1';\n }\n }\n nulls_ones[str[m] - '0']--;\n }\n\n j = 0;\n m = len - 1;\n while (j < m) {\n if (str[j] == '?') {\n if (nulls_ones[0] > 0) {\n str[j] = str[m] = '0';\n nulls_ones[str[m] - '0'] -= 2;\n } else if (nulls_ones[1] > 0) {\n str[j] = str[m] = '1';\n nulls_ones[str[m] - '0'] -= 2;\n }\n }\n j++;\n m--;\n }\n\n if ((nulls_ones[0] >= 0) && (nulls_ones[1] >= 0) && isPal) {\n for (j = 0; j < len; j++) {\n printf(\"%c\", str[j]);\n }\n printf(\"\\n\");\n } else {\n printf(\"-1\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut inp = stdin.lock().lines();\n let t = inp.next().unwrap().unwrap().parse::().unwrap();\n 'outer: for _ in 0..t {\n let line = inp.next().unwrap().unwrap();\n let (a, b): (i64, i64) = {\n let mut iter = line.split(' ').map(|x| x.parse().unwrap());\n (iter.next().unwrap(), iter.next().unwrap())\n };\n let mut res = inp.next().unwrap().unwrap().chars().collect::>();\n let (mut aa, mut bb): (i64, i64) = (0, 0);\n let mid = res.len() / 2 + res.len() % 2;\n for &ch in res.iter() {\n match ch {\n '0' => {\n aa += 1;\n }\n '1' => {\n bb += 1;\n }\n _ => {}\n }\n }\n for id1 in 0..mid {\n let ch1 = res[id1];\n let id2 = res.len() - id1 - 1;\n let ch2 = res[id2];\n if (ch1 == '?' && ch2 != '?') || (ch2 == '?' && ch1 != '?') {\n if ch1 == '0' || ch2 == '0' {\n aa += 1;\n res[id1] = '0';\n res[id2] = '0';\n } else if ch1 == '1' || ch2 == '1' {\n bb += 1;\n res[id1] = '1';\n res[id2] = '1';\n }\n }\n }\n for id1 in 0..mid {\n let ch1 = res[id1];\n let id2 = res.len() - id1 - 1;\n let ch2 = res[id2];\n if id1 == id2 {\n if ch1 == '?' {\n if a % 2 > 0 {\n aa += 1;\n res[id1] = '0';\n } else if b % 2 > 0 {\n bb += 1;\n res[id1] = '1';\n } else {\n println!(\"-1\");\n continue 'outer;\n }\n }\n continue;\n }\n if ch1 != '?' && ch2 != '?' {\n if ch1 != ch2 {\n println!(\"-1\");\n continue 'outer;\n }\n continue;\n }\n if ch1 == '?' && ch2 == '?' {\n if aa + 2 <= a {\n aa += 2;\n res[id1] = '0';\n res[id2] = '0';\n } else if bb + 2 <= b {\n bb += 2;\n res[id1] = '1';\n res[id2] = '1';\n } else {\n println!(\"-1\");\n continue 'outer;\n }\n } else {\n if ch1 == '0' || ch2 == '0' {\n aa += 1;\n res[id1] = '0';\n res[id2] = '0';\n } else if ch1 == '1' || ch2 == '1' {\n bb += 1;\n res[id1] = '1';\n res[id2] = '1';\n }\n }\n }\n if aa == a && bb == b {\n for &ch in res.iter() {\n print!(\"{}\", ch);\n }\n println!();\n } else {\n println!(\"-1\");\n }\n }\n}", "difficulty": "easy"}
{"problem_id": "0068", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.
\nShe will concatenate all of the strings in some order, to produce a long string.
\nAmong all strings that she can produce in this way, find the lexicographically smallest one.
\nHere, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:\n
\n- There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.
\n- s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.
\n
\n\n
\n\n
\nConstraints
\n- 1 ≦ N, L ≦ 100
\n- For each i, the length of S_i equals L.
\n- For each i, S_i consists of lowercase letters.
\n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nN L\nS_1\nS_2\n:\nS_N\n
\n\n
\n
\n
\nOutput
Print the lexicographically smallest string that Iroha can produce.
\n\n
\n
\n
\n\n
\nSample Input 1
3 3\ndxx\naxx\ncxx\n
\n\n
\n\n
\nSample Output 1
axxcxxdxx\n
\nThe following order should be used: axx, cxx, dxx.
\n
\n", "c_code": "int solution() {\n int n;\n int l;\n scanf(\"%d%d\", &n, &l);\n char str[n][l + 1];\n char a[(l * n) + 1];\n int ids[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", &str[i][0]);\n ids[i] = i;\n }\n for (int i = n - 1; i > 0; i--) {\n for (int j = 0; j < i; j++) {\n if (strcmp(&str[ids[j]][0], &str[ids[j + 1]][0]) > 0) {\n int buf = ids[j];\n ids[j] = ids[j + 1];\n ids[j + 1] = buf;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%s\", &str[ids[i]][0]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n\n let mut iter = buf.split_whitespace();\n\n iter.next();\n iter.next();\n let mut strings: Vec<&str> = iter.collect();\n\n strings.sort();\n println!(\"{}\", strings.concat());\n}", "difficulty": "hard"}
{"problem_id": "0069", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
There is a grid of squares with H horizontal rows and W vertical columns.\nThe square at the i-th row from the top and the j-th column from the left is represented as (i, j).\nEach square is black or white.\nThe color of the square is given as an H-by-W matrix (a_{i, j}).\nIf a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.
\nSnuke is compressing this grid.\nHe will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:
\n\n- Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.
\n
\nIt can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.\nFind the final state of the grid.
\n\n
\n\n
\nConstraints
\n- 1 \\leq H, W \\leq 100
\n- a_{i, j} is
. or #. \n- There is at least one black square in the whole grid.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nH W\na_{1, 1}...a_{1, W}\n:\na_{H, 1}...a_{H, W}\n\n\n
\n
\n
\nOutput
Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.
\n\n
\n
\n
\n\n
\nSample Input 1
4 4\n##.#\n....\n##.#\n.#.#\n
\n\n
\n\n
\nSample Output 1
###\n###\n.##\n
\nThe second row and the third column in the original grid will be removed.
\n\n
\n
\n\n
\nSample Input 2
3 3\n#..\n.#.\n..#\n
\n\n
\n\n
\nSample Output 2
#..\n.#.\n..#\n
\nAs there is no row or column that consists only of white squares, no operation will be performed.
\n\n
\n
\n\n
\nSample Input 3
4 5\n.....\n.....\n..#..\n.....\n
\n\n
\n\n
\n\n
\nSample Input 4
7 6\n......\n....#.\n.#....\n..#...\n..#...\n......\n.#..#.\n
\n\n
\n\n
\nSample Output 4
..#\n#..\n.#.\n.#.\n#.#\n
\n
\n", "c_code": "int solution(void) {\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n char gazou[h][w + 1];\n\n for (int i = 0; i < h; i++) {\n scanf(\"%s\", gazou[i]);\n }\n\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n for (int k = 0; k < h; k++) {\n if (strstr(gazou[i], \"#\") != NULL && gazou[k][j] != '.') {\n printf(\"%c\", gazou[i][j]);\n break;\n }\n }\n if (j == w - 1 && strstr(gazou[i], \"#\") != NULL) {\n printf(\"\\n\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n\n std::io::stdin().read_to_string(&mut buf).unwrap();\n\n let mut iter = buf.split_whitespace();\n let h: usize = iter.next().unwrap().parse().unwrap();\n let w: usize = iter.next().unwrap().parse().unwrap();\n let a: Vec> = (0..h)\n .map(|_| iter.next().unwrap().chars().collect())\n .collect();\n let mut rows = vec![false; h];\n let mut cols = vec![false; w];\n for i in 0..h {\n for j in 0..w {\n if a[i][j] == '#' {\n rows[i] = true;\n cols[j] = true;\n }\n }\n }\n for i in 0..h {\n if !rows[i] {\n continue;\n }\n for j in 0..w {\n if !cols[j] {\n continue;\n }\n print!(\"{}\", a[i][j]);\n }\n println!();\n }\n}", "difficulty": "easy"}
{"problem_id": "0070", "problem_description": "Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$=\"wowwo\", the following strings are subsequences: \"wowwo\", \"wowo\", \"oo\", \"wow\", \"\", and others, but the following are not subsequences: \"owoo\", \"owwwo\", \"ooo\".The wow factor of a string is the number of its subsequences equal to the word \"wow\". Bob wants to write a string that has a large wow factor. However, the \"w\" key on his keyboard is broken, so he types two \"v\"s instead. Little did he realise that he may have introduced more \"w\"s than he thought. Consider for instance the string \"ww\". Bob would type it as \"vvvv\", but this string actually contains three occurrences of \"w\": \"vvvv\" \"vvvv\" \"vvvv\" For example, the wow factor of the word \"vvvovvv\" equals to four because there are four wows: \"vvvovvv\" \"vvvovvv\" \"vvvovvv\" \"vvvovvv\" Note that the subsequence \"vvvovvv\" does not count towards the wow factor, as the \"v\"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing \"w\" with \"vv\". For example, $$$s$$$ can be equal to \"vov\".", "c_code": "int solution() {\n char s[1000001];\n scanf(\"%s\", s);\n long long int c = 0;\n long long int temp = 0;\n long long int i = 0;\n long long int sum = 0;\n while (s[i + 1] != '\\0') {\n if ((s[i] == 'v') && s[i + 1] == 'v') {\n c = c + 1;\n }\n i = i + 1;\n }\n i = 0;\n while (s[i + 1] != '\\0') {\n if (s[i] == 'o') {\n sum = sum + (temp * (c - temp));\n } else {\n if (s[i + 1] == 'v') {\n temp = temp + 1;\n }\n }\n i = i + 1;\n }\n printf(\"%lld\", sum);\n}", "rust_code": "fn solution() {\n let mut stdin = String::new();\n std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap();\n let mut stdin = stdin.split_whitespace();\n let mut get = || stdin.next().unwrap();\n\n let s = get().as_bytes();\n\n let mut ans = 0_u128;\n let mut ac = 0;\n let mut vc = 0;\n let mut oc = 0;\n let mut wc = 0;\n for i in 0..s.len() {\n if s[i] == b'o' {\n if vc > 0 {\n vc -= 1;\n ans += ac * vc;\n wc += vc;\n vc = 0;\n }\n oc += 1;\n } else {\n if oc > 0 {\n ac += wc * oc;\n oc = 0;\n }\n vc += 1;\n }\n }\n if vc > 0 {\n vc -= 1;\n ans += ac * vc;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"}
{"problem_id": "0071", "problem_description": "Berland shop sells $$$n$$$ kinds of juices. Each juice has its price $$$c_i$$$. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin \"A\", vitamin \"B\" and vitamin \"C\". Each juice can contain one, two or all three types of vitamins in it.Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.", "c_code": "int solution() {\n int n;\n int AF = 0;\n int BF = 0;\n int CF = 0;\n scanf(\"%d\", &n);\n int price[n];\n char vit[n][3];\n int Ab = 0;\n int Bb = 0;\n int Cb = 0;\n int A = 100000000;\n int B = 1000000;\n int C = 1000000;\n int AB = 1000000;\n int AC = 1000000;\n int BC = 1000000;\n int ABC = 1000000;\n for (int i = 0; i < n; i++) {\n Ab = 0, Bb = 0, Cb = 0;\n scanf(\"%d\", &price[i]);\n scanf(\"%s\", vit[i]);\n for (int j = 0; j < 3; j++) {\n if (vit[i][j] == 'A') {\n Ab = 1;\n AF = 1;\n }\n if (vit[i][j] == 'B') {\n Bb = 1;\n BF = 1;\n }\n if (vit[i][j] == 'C') {\n Cb = 1;\n CF = 1;\n }\n }\n if ((Ab == 1) && (Bb == 1) && (Cb == 1)) {\n\n if (price[i] < ABC) {\n ABC = price[i];\n }\n }\n if ((Ab == 1) && (Bb == 1) && (Cb == 0)) {\n if (price[i] < AB) {\n AB = price[i];\n }\n }\n if ((Ab == 1) && (Bb == 0) && (Cb == 1)) {\n if (price[i] < AC) {\n AC = price[i];\n }\n }\n if ((Ab == 0) && (Bb == 1) && (Cb == 1)) {\n if (price[i] < BC) {\n BC = price[i];\n }\n }\n if ((Ab == 1) && (Bb == 0) && (Cb == 0)) {\n if (price[i] < A) {\n A = price[i];\n }\n }\n if ((Ab == 0) && (Bb == 1) && (Cb == 0)) {\n if (price[i] < B) {\n B = price[i];\n }\n }\n if ((Ab == 0) && (Bb == 0) && (Cb == 1)) {\n if (price[i] < C) {\n C = price[i];\n }\n }\n }\n\n int min = A + B + C;\n if ((AB + C) < min) {\n min = AB + C;\n }\n if ((AC + B) < min) {\n min = AC + B;\n }\n if ((BC + A) < min) {\n min = BC + A;\n }\n if (ABC < min) {\n min = ABC;\n }\n if ((AB + AC) < min) {\n min = AB + AC;\n }\n if ((AB + BC) < min) {\n min = AB + BC;\n }\n if ((BC + AC) < min) {\n min = BC + AC;\n }\n if ((AB + A + C) < min) {\n min = AB + A + C;\n }\n if ((BC + A + C) < min) {\n min = A + C + BC;\n }\n if ((A + B + AC) < min) {\n min = A + B + AC;\n }\n if ((A + B + BC) < min) {\n min = A + B + BC;\n }\n if ((C + B + AC) < min) {\n min = C + B + AC;\n }\n if ((C + B + AB) < min) {\n min = C + B + AB;\n }\n\n if (!((AF == 1) && (BF == 1) && (CF == 1))) {\n min = -1;\n }\n printf(\"%d\", min);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let stdin = io::stdin();\n let mut handle = stdin.lock();\n\n handle.read_to_string(&mut buffer).expect(\"stdin\");\n\n let arr: Vec<_> = buffer.split_whitespace().collect();\n\n let n = arr[0].parse::().unwrap();\n\n let mut comb = vec![];\n\n for _i in 0..2 * 2 * 2 {\n comb.push(1_000_000_000);\n }\n\n use std::cmp;\n\n for i in 0..n {\n let c = arr[1 + i * 2].parse::().unwrap();\n let ss = arr[1 + i * 2 + 1].chars();\n let mut id = 0;\n for j in ss {\n if j == 'A' {\n id |= 1;\n }\n if j == 'B' {\n id |= 2;\n }\n if j == 'C' {\n id |= 4;\n }\n }\n\n comb[id] = cmp::min(comb[id], c);\n }\n\n let mut ans = comb[7];\n\n for i in 1..8 {\n for j in i + 1..8 {\n let b = i | j;\n if b == 7 {\n ans = cmp::min(ans, comb[i] + comb[j]);\n }\n }\n }\n\n for i in 1..8 {\n for j in i + 1..8 {\n for k in j + 1..8 {\n let b = i | j | k;\n if b == 7 {\n ans = cmp::min(ans, comb[i] + comb[j] + comb[k]);\n }\n }\n }\n }\n\n if ans >= 1_000_000_000 {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"}
{"problem_id": "0072", "problem_description": "$$$n$$$ robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the $$$i$$$-th robot is currently located at the point having coordinates ($$$x_i$$$, $$$y_i$$$). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers $$$X$$$ and $$$Y$$$, and when each robot receives this command, it starts moving towards the point having coordinates ($$$X$$$, $$$Y$$$). The robot stops its movement in two cases: either it reaches ($$$X$$$, $$$Y$$$); or it cannot get any closer to ($$$X$$$, $$$Y$$$). Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as ($$$x_c$$$, $$$y_c$$$). Then the movement system allows it to move to any of the four adjacent points: the first action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c - 1$$$, $$$y_c$$$); the second action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c + 1$$$); the third action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c + 1$$$, $$$y_c$$$); the fourth action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c - 1$$$). Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers $$$X$$$ and $$$Y$$$ so that each robot can reach the point ($$$X$$$, $$$Y$$$). Is it possible to find such a point?", "c_code": "int solution(void) {\n int q;\n int n = 0;\n scanf(\"%d\", &q);\n while (q != 0) {\n q--;\n scanf(\"%d\", &n);\n int a[n][6];\n int maxX = 100000;\n int maxY = 100000;\n int minX = -100000;\n int minY = -100000;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 6; j++) {\n scanf(\"%d\", &a[i][j]);\n }\n if (a[i][0] < maxX && a[i][4] == 0) {\n maxX = a[i][0];\n }\n if (a[i][0] > minX && a[i][2] == 0) {\n minX = a[i][0];\n }\n if (a[i][1] < maxY && a[i][3] == 0) {\n maxY = a[i][1];\n }\n if (a[i][1] > minY && a[i][5] == 0) {\n minY = a[i][1];\n }\n }\n if (minX > maxX || minY > maxY) {\n printf(\"0\\n\");\n } else {\n printf(\"1 %d %d\\n\", minX, minY);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let q = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n iter.next().unwrap().parse::().unwrap()\n };\n\n for _ in 0..q {\n let n = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n iter.next().unwrap().parse::().unwrap()\n };\n\n let mut min_x: i32 = -100000;\n let mut max_x: i32 = 100000;\n\n let mut min_y: i32 = -100000;\n let mut max_y: i32 = 100000;\n\n for _ in 0..n {\n let (x, y, f1, f2, f3, f4) = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n (\n iter.next().unwrap().parse::().unwrap(),\n iter.next().unwrap().parse::().unwrap(),\n iter.next().unwrap().parse::().unwrap(),\n iter.next().unwrap().parse::().unwrap(),\n iter.next().unwrap().parse::().unwrap(),\n iter.next().unwrap().parse::().unwrap(),\n )\n };\n\n if f1 == 0 {\n min_x = min_x.max(x)\n }\n if f2 == 0 {\n max_y = max_y.min(y)\n }\n if f3 == 0 {\n max_x = max_x.min(x)\n }\n if f4 == 0 {\n min_y = min_y.max(y)\n }\n }\n if min_x <= max_x && min_y <= max_y {\n println!(\"1 {} {}\", min_x, min_y);\n } else {\n println!(\"0\")\n }\n }\n}", "difficulty": "medium"}
{"problem_id": "0073", "problem_description": "Due to the coronavirus pandemic, city authorities obligated citizens to keep a social distance. The mayor of the city Semyon wants to light up Gluharniki park so that people could see each other even at night to keep the social distance.The park is a rectangular table with $$$n$$$ rows and $$$m$$$ columns, where the cells of the table are squares, and the boundaries between the cells are streets. External borders are also streets. Every street has length $$$1$$$. For example, park with $$$n=m=2$$$ has $$$12$$$ streets.You were assigned to develop a plan for lighting the park. You can put lanterns in the middle of the streets. The lamp lights two squares near it (or only one square if it stands on the border of the park). The park sizes are: $$$n=4$$$, $$$m=5$$$. The lighted squares are marked yellow. Please note that all streets have length $$$1$$$. Lanterns are placed in the middle of the streets. In the picture not all the squares are lit. Semyon wants to spend the least possible amount of money on lighting but also wants people throughout the park to keep a social distance. So he asks you to find the minimum number of lanterns that are required to light all the squares.", "c_code": "int solution(void) {\n int x;\n scanf(\"%d\", &x);\n int n[x][2];\n int v[x];\n for (int i = 0; i < x; i++) {\n for (int z = 0; z < 2; z++) {\n scanf(\"%d\", &n[i][z]);\n }\n }\n for (int i = 0; i < x; i++) {\n if ((n[i][0] * n[i][1]) % 2 == 0) {\n v[i] = (n[i][0] * n[i][1]) / 2;\n } else {\n v[i] = (n[i][0] * n[i][1] + 1) / 2;\n }\n printf(\"%d\\n\", v[i]);\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"Failed to read line\");\n\n let t: i32 = t.trim().parse().expect(\"Failed to cast to i32\");\n\n for _ in 0..t {\n let mut buff = String::new();\n io::stdin()\n .read_line(&mut buff)\n .expect(\"Failed to read line\");\n let dims: Vec = buff\n .trim()\n .split(\" \")\n .map(|x| x.parse().expect(\"Failed to cast to i32\"))\n .collect();\n\n let sequares_amount = dims[0] * dims[1];\n let mut sequares_amount = sequares_amount as f64 / 2.0;\n sequares_amount = sequares_amount.ceil();\n\n println!(\"{}\", sequares_amount);\n }\n}", "difficulty": "hard"}
{"problem_id": "0074", "problem_description": "A number is called 2050-number if it is $$$2050$$$, $$$20500$$$, ..., ($$$2050 \\cdot 10^k$$$ for integer $$$k \\ge 0$$$).Given a number $$$n$$$, you are asked to represent $$$n$$$ as the sum of some (not necessarily distinct) 2050-numbers. Compute the minimum number of 2050-numbers required for that.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n long long int a[n + 5];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n if (a[i] < 2050 || a[i] % 2050 != 0) {\n printf(\"%d\\n\", -1);\n } else {\n long long int d = a[i] / 2050;\n long int k = 0;\n while (d != 0) {\n long int v = 0;\n v = d % 10;\n k = k + v;\n d = d / 10;\n }\n printf(\"%ld\\n\", k);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut scan = Scanner::new(stdin.lock());\n let mut writer = std::io::BufWriter::new(stdout.lock());\n\n let t: i32 = scan.token();\n for _ in 0..t {\n let n: i64 = scan.token();\n if n % 2050 > 0 {\n writeln!(writer, \"-1\").unwrap();\n } else {\n let mut x = n / 2050;\n let mut ans = 0;\n while x > 0 {\n ans += x % 10;\n x /= 10;\n }\n writeln!(writer, \"{}\", ans).unwrap();\n }\n }\n}", "difficulty": "easy"}
{"problem_id": "0075", "problem_description": "Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.For example, the following matrices are palindromic: The following matrices are not palindromic because they change after the order of rows is reversed: The following matrices are not palindromic because they change after the order of columns is reversed: You are given $$$n^2$$$ integers. Put them into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print \"NO\".", "c_code": "int solution() {\n int n;\n int i;\n int j;\n int k = 0;\n int l;\n int num;\n int o = 0;\n int arr[1001] = {0};\n scanf(\"%d\", &n);\n for (i = 0; i < n * n; i++) {\n scanf(\"%d\", &num);\n arr[num]++;\n }\n\n {\n for (i = 0; i <= 1000; i++) {\n if (arr[i] % 4 != 0 && n % 2 == 0) {\n k = 1;\n break;\n }\n }\n }\n if (n % 2 == 1) {\n for (i = 0; i <= 1000; i++) {\n if (arr[i] % 2 != 0) {\n o++;\n }\n }\n }\n\n int ll = 0;\n if ((n % 2 == 0 && (k == 1)) || (n % 2 == 1 && o != 1)) {\n printf(\"No\\n\");\n ll = 1;\n } else {\n\n i = 1;\n int idx = 1;\n int t = 1;\n int res[1001][1001] = {0};\n while (i <= 1000 && idx <= n / 2) {\n if (arr[i] != 0 && arr[i] >= 4 && t <= n / 2) {\n res[idx][t] = i;\n res[idx][n - t + 1] = i;\n res[n - idx + 1][t] = i;\n res[n - idx + 1][n - t + 1] = i;\n t++;\n arr[i] -= 4;\n }\n if (arr[i] == 0 || arr[i] < 4) {\n i++;\n }\n if (t > n / 2) {\n t = 1;\n idx++;\n }\n }\n int m = (n / 2) + 1;\n int p;\n if (n % 2 == 1) {\n i = 1;\n t = 1;\n p = 1;\n\n while (i <= 1000 && (t <= n / 2)) {\n\n if (arr[i] > 1 && res[t][m] == 0 && res[n - t + 1][m] == 0) {\n\n res[t][m] = i;\n res[n - t + 1][m] = i;\n arr[i] -= 2;\n t++;\n }\n if (arr[i] == 0 || arr[i] < 2) {\n i++;\n }\n }\n i = 1;\n while (i <= 1000 && (p <= n / 2)) {\n\n if (arr[i] != 0 && arr[i] != 1 && res[m][p] == 0 &&\n res[m][n - p + 1] == 0) {\n\n res[m][p] = i;\n res[m][n - p + 1] = i;\n arr[i] -= 2;\n p++;\n }\n if (arr[i] == 0 || arr[i] < 2) {\n i++;\n }\n }\n for (i = 0; i <= 1000; i++) {\n if (arr[i] != 0) {\n res[m][m] = i;\n }\n }\n }\n int z = 0;\n for (i = 1; i <= n; i++) {\n if (z == 1) {\n break;\n }\n for (j = 1; j <= n; j++) {\n if (res[i][j] == 0) {\n printf(\"No\\n\");\n z = 1;\n break;\n }\n if (z == 1) {\n break;\n }\n }\n if (z == 1) {\n break;\n }\n }\n\n if (z == 0) {\n printf(\"Yes\\n\");\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= n; j++) {\n printf(\"%d \", res[i][j]);\n }\n printf(\"\\n\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut i = stdin();\n let mut li = BufReader::new(i.lock());\n\n let mut input = String::new();\n li.read_to_string(&mut input);\n\n let parsed: Vec> = input\n .lines()\n .map(|l| l.split_whitespace().map(|n| n.parse().unwrap()).collect())\n .collect();\n\n let n = parsed[0][0];\n let odd = (n % 2) == 1;\n let nums = {\n let mut r = parsed[1].clone();\n r.sort();\n r\n };\n let l = nums.len();\n\n let s = n / 2 + if odd { 1 } else { 0 };\n let mut matrix: Vec> = (0..=s).map(|_| (0..s).map(|_| 0).collect()).collect();\n\n let mut fourth = ((0..s - if odd { 1 } else { 0 })\n .map(|n| (0..s - if odd { 1 } else { 0 }).map(move |m| (n, m))))\n .flatten();\n let mut twice = (0..s - 1)\n .map(|m| (s - 1, m))\n .chain((0..s - 1).map(|m| (m, s - 1)));\n let mut single = (0..1).map(|_| (s - 1, s - 1));\n let mut i = 0usize;\n\n while i < l {\n if i + 3 < l && nums[i] == nums[i + 3] {\n match fourth.next() {\n Some((x, y)) => {\n matrix[x][y] = nums[i];\n i += 4;\n }\n _ if odd => {\n let o1 = twice.next();\n let o2 = twice.next();\n match (o1, o2) {\n (Some((x1, y1)), Some((x2, y2))) => {\n matrix[x1][y1] = nums[i];\n matrix[x2][y2] = nums[i];\n i += 4;\n }\n _ => {\n println!(\"NO\");\n return;\n }\n }\n }\n _ => {\n println!(\"NO\");\n return;\n }\n }\n } else if odd && i + 1 < l && nums[i] == nums[i + 1] {\n match twice.next() {\n Some((x, y)) => {\n matrix[x][y] = nums[i];\n i += 2;\n }\n None => {\n println!(\"NO\");\n return;\n }\n }\n } else if odd {\n match single.next() {\n Some((x, y)) => {\n matrix[x][y] = nums[i];\n i += 1;\n }\n None => {\n println!(\"NO\");\n return;\n }\n }\n } else {\n println!(\"NO\");\n return;\n }\n }\n\n let mut res: Vec> = (0..n).map(|_| Vec::new()).collect();\n for i in 0usize..s {\n let mut r = matrix[i].clone();\n if odd {\n r.extend(matrix[i].iter().rev().skip(1));\n } else {\n r.extend(matrix[i].iter().rev());\n }\n res[i] = r.clone();\n if !odd || i != s - 1 {\n res[n - 1 - i] = r;\n }\n }\n\n println!(\"YES\");\n for v in res {\n for i in v {\n print!(\"{} \", i);\n }\n println!();\n }\n}", "difficulty": "medium"}
{"problem_id": "0076", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.
\nAfter finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.
\n\n
\n\n
\nConstraints
\n- S is a string of length 10.
\n- The first eight characters in S are
2017/01/. \n- The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nS\n
\n\n
\n
\n
\nOutput
Replace the first four characters in S with 2018 and print it.
\n\n
\n
\n
\n\n
\nSample Input 1
2017/01/07\n
\n\n
\n\n
\nSample Output 1
2018/01/07\n
\n\n
\n
\n\n
\nSample Input 2
2017/01/31\n
\n\n
\n\n
\nSample Output 2
2018/01/31\n
\n
\n", "c_code": "int solution(void) {\n char s[11];\n scanf(\"%s\", s);\n char *p = &s[4];\n printf(\"2018%s\\n\", p);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"failed to read line\");\n\n println!(\"2018{}\", &s[4..])\n}", "difficulty": "easy"}
{"problem_id": "0077", "problem_description": "\nScore : 500 points
\n\n
\nProblem Statement
There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.
\nTakahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.
\nProcess the following Q queries:
\n\n- The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.
\n
\n\n
\n\n
\nConstraints
\n- All values in input are integers.
\n- 2 \\leq N \\leq 300
\n- 0 \\leq M \\leq \\frac{N(N-1)}{2}
\n- 1 \\leq L \\leq 10^9
\n- 1 \\leq A_i, B_i \\leq N
\n- A_i \\neq B_i
\n- \\left(A_i, B_i\\right) \\neq \\left(A_j, B_j\\right) (if i \\neq j)
\n- \\left(A_i, B_i\\right) \\neq \\left(B_j, A_j\\right) (if i \\neq j)
\n- 1 \\leq C_i \\leq 10^9
\n- 1 \\leq Q \\leq N\\left(N-1\\right)
\n- 1 \\leq s_i, t_i \\leq N
\n- s_i \\neq t_i
\n- \\left(s_i, t_i\\right) \\neq \\left(s_j, t_j\\right) (if i \\neq j)
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN M L\nA_1 B_1 C_1\n:\nA_M B_M C_M\nQ\ns_1 t_1\n:\ns_Q t_Q\n
\n\n
\n
\n
\nOutput
Print Q lines.
\nThe i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.
\n\n
\n
\n
\n\n
\nSample Input 1
3 2 5\n1 2 3\n2 3 3\n2\n3 2\n1 3\n
\n\n
\n\n
\nSample Output 1
0\n1\n
\nTo travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.
\nTo travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.
\n\n
\n
\n\n
\nSample Input 2
4 0 1\n1\n2 1\n
\n\n
\n\n
\nSample Output 2
-1\n
\nThere may be no road at all.
\n\n
\n
\n\n
\nSample Input 3
5 4 4\n1 2 2\n2 3 2\n3 4 3\n4 5 2\n20\n2 1\n3 1\n4 1\n5 1\n1 2\n3 2\n4 2\n5 2\n1 3\n2 3\n4 3\n5 3\n1 4\n2 4\n3 4\n5 4\n1 5\n2 5\n3 5\n4 5\n
\n\n
\n\n
\nSample Output 3
0\n0\n1\n2\n0\n0\n1\n2\n0\n0\n0\n1\n1\n1\n0\n0\n2\n2\n1\n0\n
\n
\n", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int c;\n int N;\n int M;\n int L;\n int dist[302][302] = {};\n scanf(\"%d %d %d\", &N, &M, &L);\n for (u = 1; u < N; u++) {\n for (w = u + 1; w <= N; w++) {\n if (dist[u][w] == 0) {\n dist[u][w] = L + 1;\n dist[w][u] = L + 1;\n }\n }\n }\n for (i = 1; i <= M; i++) {\n scanf(\"%d %d %d\", &u, &w, &c);\n if (c <= L) {\n dist[u][w] = c;\n dist[w][u] = c;\n }\n }\n\n int v;\n for (v = 1; v <= N; v++) {\n for (u = 1; u < N; u++) {\n for (w = u + 1; w <= N; w++) {\n if (dist[u][v] + dist[v][w] < dist[u][w]) {\n dist[u][w] = dist[u][v] + dist[v][w];\n dist[w][u] = dist[u][v] + dist[v][w];\n }\n }\n }\n }\n\n int ans[302][302] = {};\n int q[302];\n int head;\n int tail;\n for (v = 1; v <= N; v++) {\n q[0] = v;\n ans[v][v] = 1;\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (w = 1; w <= N; w++) {\n if (ans[v][w] == 0 && dist[u][w] <= L) {\n ans[v][w] = ans[v][u] + 1;\n q[tail++] = w;\n }\n }\n }\n }\n\n int Q;\n int s;\n int t;\n scanf(\"%d\", &Q);\n for (i = 1; i <= Q; i++) {\n scanf(\"%d %d\", &s, &t);\n printf(\"%d\\n\", ((ans[s][t] > 0) ? ans[s][t] : 1) - 2);\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let (N, M, L): (usize, usize, i64) = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n (\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n )\n };\n let (A, B, C): (Vec, Vec, Vec) = {\n let (mut A, mut B, mut C) = (vec![], vec![], vec![]);\n for _ in 0..M {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n A.push(iter.next().unwrap().parse().unwrap());\n B.push(iter.next().unwrap().parse().unwrap());\n C.push(iter.next().unwrap().parse().unwrap());\n }\n (A, B, C)\n };\n let Q: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n let (s, t): (Vec, Vec) = {\n let (mut s, mut t) = (vec![], vec![]);\n for _ in 0..Q {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n s.push(iter.next().unwrap().parse().unwrap());\n t.push(iter.next().unwrap().parse().unwrap());\n }\n (s, t)\n };\n\n const INF: i64 = 1_000_000_000_000_000;\n let mut dp = vec![vec![INF; N + 1]; N + 1];\n for i in 0..M {\n dp[A[i]][B[i]] = C[i];\n dp[B[i]][A[i]] = C[i];\n }\n for k in 1..(N + 1) {\n for i in 1..(N + 1) {\n for j in 1..(N + 1) {\n dp[i][j] = std::cmp::min(dp[i][j], dp[i][k] + dp[k][j]);\n }\n }\n }\n let mut dp2 = (0..(N + 1))\n .map(|i| {\n (0..(N + 1))\n .map(|j| if dp[i][j] <= L { 1 } else { INF })\n .collect::>()\n })\n .collect::>();\n for k in 1..(N + 1) {\n for i in 1..(N + 1) {\n for j in 1..(N + 1) {\n dp2[i][j] = std::cmp::min(dp2[i][j], dp2[i][k] + dp2[k][j]);\n }\n }\n }\n\n let ans = (0..Q)\n .map(|i| {\n if dp2[s[i]][t[i]] == INF {\n -1\n } else {\n dp2[s[i]][t[i]] - 1\n }\n })\n .map(|res| res.to_string())\n .collect::>()\n .join(\"\\n\");\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"}
{"problem_id": "0078", "problem_description": "Card Game
\n\n\n Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card.\n The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.\n
\n\n\n Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.\n
\n\nInput
\n\n\n In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.\n
\n\n\nConstraints
\n\n- n ≤ 1000
\n- The length of the string ≤ 100
\n
\n\nOutput
\n\n\n Print the final scores of Taro and Hanako respectively. Put a single space character between them.\n
\n\nSample Input
\n\n\n3\ncat dog\nfish fish\nlion tiger\n
\n\nSample Output
\n\n\n1 7\n
", "c_code": "int solution(void) {\n#define WIN_POINT 3\n#define DRAW_POINT 1\n\n int n = 0;\n char t_card[101];\n char h_card[101];\n int i = 0;\n int check = 0;\n int t_point = 0;\n int h_point = 0;\n\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%s %s\", t_card, h_card);\n check = strcmp(t_card, h_card);\n if (check < 0) {\n h_point += WIN_POINT;\n } else if (check > 0) {\n t_point += WIN_POINT;\n } else {\n t_point += DRAW_POINT;\n h_point += DRAW_POINT;\n }\n }\n printf(\"%d %d\\n\", t_point, h_point);\n\n#undef WIN_POINT\n#undef DRAW_POINT\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut buf = String::new();\n stdin.read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n\n let score = stdin.lock().lines().take(n).fold((0, 0), |acc, line| {\n let line = line.unwrap();\n let mut ite = line.split_whitespace();\n let w1 = ite.next().unwrap();\n let w2 = ite.next().unwrap();\n\n match w1.cmp(w2) {\n Ordering::Less => (acc.0, acc.1 + 3),\n Ordering::Equal => (acc.0 + 1, acc.1 + 1),\n Ordering::Greater => (acc.0 + 3, acc.1),\n }\n });\n\n println!(\"{} {}\", score.0, score.1);\n}", "difficulty": "hard"}
{"problem_id": "0079", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
X and A are integers between 0 and 9 (inclusive).
\nIf X is less than A, print 0; if X is not less than A, print 10.
\n\n
\n\n
\nConstraints
\n- 0 \\leq X, A \\leq 9
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nX A\n
\n\n
\n
\n
\nOutput
If X is less than A, print 0; if X is not less than A, print 10.
\n\n
\n
\n
\n\n\n
\nSample Output 1
0\n
\n3 is less than 5, so we should print 0.
\n\n
\n
\n\n\n
\nSample Output 2
10\n
\n7 is not less than 5, so we should print 10.
\n\n
\n
\n\n\n
\nSample Output 3
10\n
\n6 is not less than 6, so we should print 10.
\n
\n", "c_code": "int solution(void) {\n\n int x = 0;\n int a = 0;\n scanf(\"%d %d\", &x, &a);\n\n printf(\"%d\", x < a ? 0 : 10);\n}", "rust_code": "fn solution() {\n let mut xa = String::new();\n stdin().read_line(&mut xa).unwrap();\n let xa: Vec = xa.split_whitespace().flat_map(str::parse).collect();\n println!(\"{}\", if xa[0] >= xa[1] { 10 } else { 0 });\n}", "difficulty": "easy"}
{"problem_id": "0080", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \\dots , a_n$$$.In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $$$[2, 1, 4]$$$ you can obtain the following arrays: $$$[3, 4]$$$, $$$[1, 6]$$$ and $$$[2, 5]$$$.Your task is to find the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times.You have to answer $$$t$$$ independent queries.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n = 0;\n int r = 0;\n\n scanf(\"%d\", &n);\n\n int arr[n];\n\n for (int i = 0, tmp; i < n; i++) {\n scanf(\"%d\", &tmp);\n arr[i] = tmp % 3;\n }\n\n int j = 0;\n int k = 0;\n\n for (int i = 0; i < n; i++) {\n if (arr[i] == 0) {\n r++;\n } else if (arr[i] == 1) {\n j++;\n } else {\n k++;\n }\n }\n\n if (j >= k) {\n r += k + ((j - k) / 3);\n } else {\n r += j + ((k - j) / 3);\n }\n\n printf(\"%d\\n\", r);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut str_in = String::new();\n stdin.lock().read_line(&mut str_in);\n let mut tc: i32 = str_in.trim().parse::().unwrap();\n while tc > 0 {\n str_in = String::new();\n stdin.lock().read_line(&mut str_in);\n let _nn = str_in.trim().parse::().unwrap();\n str_in = String::new();\n stdin.lock().read_line(&mut str_in);\n let nums: Vec = str_in\n .split(' ')\n .map(|s| s.trim())\n .map(|s| s.parse::().unwrap())\n .collect();\n let mut cc: [i32; 3] = [0, 0, 0];\n for n in nums {\n if n % 3 == 0 {\n cc[0] += 1;\n } else if n % 3 == 1 {\n cc[1] += 1;\n } else if n % 3 == 2 {\n cc[2] += 1;\n }\n }\n println!(\n \"{:?}\",\n cc[0] + cmp::min(cc[1], cc[2]) + (cmp::max(cc[1], cc[2]) - cmp::min(cc[1], cc[2])) / 3\n );\n tc -= 1;\n }\n}", "difficulty": "medium"}
{"problem_id": "0081", "problem_description": "Cake Party
\n\n \n I’m planning to have a party on my birthday. Many of my friends will come to the party. Some of them will come with one or more pieces of cakes, but it is not certain if the number of the cakes is a multiple of the number of people coming.\n
\n\n I wish to enjoy the cakes equally among the partiers. So, I decided to apply the following rules. First, all the party attendants are given the same number of cakes. If some remainder occurs, a piece goes on a priority basis to the party host (that’s me!). How many pieces of cake can I enjoy?\n
\n\n\n\n Given the number of my friends and cake information, make a program to calculate how many pieces of cake I can enjoy. Note that I am not counted in the number of my friends.\n
\n\nInput
\n\nThe input is given in the following format.\n
\n\n$N$ $C$\n$p_1$ $p_2$ ... $p_C$\n
\n\n\nThe first line provides the number of my friends $N$ ($1 \\leq N \\leq 100$) and the number of those among them who brought one or more pieces of cake with them $C$ ($1 \\leq C \\leq N$). The second line provides an array of integers $p_i$ ($1 \\leq p_i \\leq100$), each of which shows the number of cakes of the $i$-th friend of mine who was willing to come up with one or more pieces of cake.\n
\n\nOutput
\n\n Output the number of cakes I can enjoy.\n
\n\nSample Input 1
\n\n5 4\n5 5 6 5\n
\nSample Output 1
\n\n4\n
\n\nSample Input 2
\n\n7 5\n8 8 8 8 8\n
\n\nSample Output 2
\n\n5\n
\n\nSample Input 3
\n\n100 3\n3 3 3\n
\n\nSample Output 3
\n\n1\n
", "c_code": "int solution(void) {\n\n int i = 0;\n\n int n = 0;\n\n int c = 0;\n\n int p[100];\n\n int total = 0;\n\n int count = 0;\n\n int Surplus = 0;\n\n for (i = 0; i < 100; i++) {\n p[i] = 0;\n }\n\n scanf(\"%d %d\", &n, &c);\n\n for (i = 0; i < c; i++) {\n scanf(\"%d\", &p[i]);\n total = total + p[i];\n }\n\n count = total / (n + 1);\n\n Surplus = total % (n + 1);\n\n if (Surplus == 0) {\n printf(\"%d\\n\", count);\n }\n\n else {\n printf(\"%d\\n\", count + 1);\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut lines = stdin.lock().lines().map(|l| l.unwrap());\n\n let n: u32 = {\n let line = lines.next().unwrap();\n let mut iter = line.split_whitespace().map(|s| s.parse().unwrap());\n\n iter.next().unwrap()\n };\n\n let sum_p: u32 = {\n let line = lines.next().unwrap();\n line.split_whitespace()\n .map(|s| s.parse::().unwrap())\n .sum()\n };\n\n let got = (sum_p as f64 / (n + 1) as f64).ceil();\n\n println!(\"{}\", got);\n}", "difficulty": "hard"}
{"problem_id": "0082", "problem_description": "You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones and an integer $$$k$$$. In one operation you can do one of the following: Select $$$2$$$ consecutive elements of $$$a$$$ and replace them with their minimum (that is, let $$$a := [a_{1}, a_{2}, \\ldots, a_{i-1}, \\min(a_{i}, a_{i+1}), a_{i+2}, \\ldots, a_{n}]$$$ for some $$$1 \\le i \\le n-1$$$). This operation decreases the size of $$$a$$$ by $$$1$$$. Select $$$k$$$ consecutive elements of $$$a$$$ and replace them with their maximum (that is, let $$$a := [a_{1}, a_{2}, \\ldots, a_{i-1}, \\max(a_{i}, a_{i+1}, \\ldots, a_{i+k-1}), a_{i+k}, \\ldots, a_{n}]$$$ for some $$$1 \\le i \\le n-k+1$$$). This operation decreases the size of $$$a$$$ by $$$k-1$$$. Determine if it's possible to turn $$$a$$$ into $$$[1]$$$ after several (possibly zero) operations.", "c_code": "int solution() {\n int t;\n\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n int count = 0;\n scanf(\"%d %d\", &n, &k);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] == 1) {\n count++;\n }\n }\n if (count > 0) {\n\n printf(\"YES\\n\");\n }\n\n else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdin_lock = stdin.lock();\n let mut line_iter = stdin_lock.lines();\n let t = line_iter\n .next()\n .unwrap()\n .unwrap()\n .as_str()\n .parse::()\n .unwrap();\n for _ in 0..t {\n let (_n_str, _k_str) = line_iter.next().unwrap().unwrap().split_once(' ').unwrap();\n\n let any_non_zero = line_iter\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .any(|digit| digit == \"1\");\n let output = if any_non_zero { \"YES\" } else { \"NO\" };\n println!(\"{}\", output);\n }\n}", "difficulty": "easy"}
{"problem_id": "0083", "problem_description": "Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $$$n$$$ players arranged in a circle, so that for all $$$j$$$ such that $$$2 \\leq j \\leq n$$$, player $$$j - 1$$$ is to the left of the player $$$j$$$, and player $$$j$$$ is to the right of player $$$j - 1$$$. Additionally, player $$$n$$$ is to the left of player $$$1$$$, and player $$$1$$$ is to the right of player $$$n$$$.Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $$$0$$$, $$$1$$$, or $$$2$$$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $$$1$$$ other player, then they should logically attack that player in response. If instead a player is being attacked by $$$0$$$ or $$$2$$$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $$$n$$$ players in the game to make them instead attack another player — i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.", "c_code": "int solution() {\n long long int a = 1;\n long long int i;\n long long int t;\n long long int n;\n long long int j;\n long long int k;\n long long int max = 0;\n long long int min = 0;\n long long int ans = 0;\n char ar[200010];\n scanf(\"%lld\", &t);\n\n for (i = 0; i < t; i++) {\n scanf(\"%lld %s\", &n, ar);\n\n for (j = 0; j < n; j++) {\n\n if (ar[j] != ar[j + 1] && j < (n - 1)) {\n if (a) {\n max = j + 1;\n }\n a = 0;\n }\n\n if (ar[j] == ar[j + 1] && ar[j + 1] == ar[j + 2]) {\n ans++;\n j += 2;\n if (ar[j] != ar[j + 1] && j < (n - 1)) {\n if (a) {\n max = j + 1;\n }\n a = 0;\n }\n }\n }\n\n if (a) {\n if (n % 3 == 0) {\n ans = n / 3;\n } else {\n ans = (n / 3) + 1;\n }\n } else {\n for (j = n - 1; j >= 0; j--) {\n if (ar[j] != ar[j - 1]) {\n break;\n }\n }\n\n j = n - j;\n\n j %= 3;\n max %= 3;\n if (ar[0] == ar[n - 1]) {\n ans += (j + max) / 3;\n }\n }\n\n printf(\"%lld\\n\", ans);\n ans = 0;\n a = 1;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n\n for _ in 0..t {\n let n: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n\n let mut s: VecDeque = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n\n if (0..(n - 1)).all(|i| s[i] == s[i + 1]) {\n println!(\"{}\", n.div_ceil(3));\n continue;\n }\n\n while s.front().unwrap() == s.back().unwrap() {\n let c = s.pop_front().unwrap();\n s.push_back(c);\n }\n\n let mut ans = 0;\n let mut i = 0;\n while i < n {\n let mut j = i + 1;\n while j < n && s[i] == s[j] {\n j += 1;\n }\n ans += (j - i) / 3;\n i = j;\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"}
{"problem_id": "0084", "problem_description": "Swapping Two Numbers
\n\n\n Write a program which reads two integers x and y, and prints them in ascending order. \n
\n\n\nInput
\n\n\nThe input consists of multiple datasets. Each dataset consists of two integers x and y separated by a single space.\n
\n\n\nThe input ends with two 0 (when both x and y are zero). Your program should not process for these terminal symbols.\n
\n\nOutput
\n\n\nFor each dataset, print x and y in ascending order in a line. Put a single space between x and y.\n
\n\nConstraints
\n\n\n - 0 ≤ x, y ≤ 10000
\n - the number of datasets ≤ 3000
\n
\n\nSample Input
\n\n3 2\n2 2\n5 3\n0 0\n
\nSample Output
\n\n2 3\n2 2\n3 5\n
", "c_code": "int solution(void) {\n\n int i = 0;\n int x = 0;\n int y = 0;\n\n for (i = 1;; i++) {\n scanf(\"%d %d\", &x, &y);\n\n if (x <= 0 && x > 10000 && y <= 0 && y > 10000) {\n\n printf(\"Invalid Input\\n\");\n return 0;\n }\n\n if (x != 0 || y != 0) {\n if (x < y) {\n printf(\"%d %d\\n\", x, y);\n } else {\n printf(\"%d %d\\n\", y, x);\n }\n } else {\n break;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut v: Vec = {\n let mut b = String::new();\n let s = std::io::stdin();\n s.read_line(&mut b).unwrap();\n b.split_whitespace().flat_map(str::parse).collect()\n };\n if v == [0, 0] {\n break;\n }\n v.sort();\n println!(\"{} {}\", v[0], v[1]);\n }\n}", "difficulty": "hard"}
{"problem_id": "0085", "problem_description": "Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.For instance, let's say the garland is represented by \"kooomo\", and Brother Koyomi's favourite colour is \"o\". Among all subsegments containing pieces of \"o\" only, \"ooo\" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n char a[n + 1];\n scanf(\"%s\", a);\n int q;\n scanf(\"%d\", &q);\n for (int i = 0; i < q; i++) {\n int k;\n char c;\n scanf(\"%d\", &k);\n scanf(\"%c\", &c);\n scanf(\"%c\", &c);\n int l = 0;\n int r = -1;\n int check = 0;\n int ans = 0;\n int max = 0;\n while (r + 1 < n) {\n r++;\n\n if (a[r] != c && check < k) {\n check++;\n } else if (a[r] != c && check == k) {\n r--;\n break;\n }\n }\n\n max = r - l + 1;\n while (r + 1 < n) {\n r++;\n while (a[l] == c) {\n l++;\n }\n l++;\n while (a[r + 1] == c && r + 1 < n) {\n r++;\n }\n ans = r - l + 1;\n\n if (ans > max) {\n max = ans;\n }\n }\n printf(\"%d\\n\", max);\n }\n}", "rust_code": "fn solution() {\n let n = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input.trim().parse::().unwrap()\n };\n\n let s = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input.trim().bytes().map(|b| b - b'a').collect::>()\n };\n\n let mut dp: Vec> =\n std::iter::repeat_n(std::iter::repeat_n(0, n + 1).collect(), 26).collect();\n\n for c in 0..26 {\n for i in 0..n {\n let mut replace_cnt = 0usize;\n for j in i..n {\n if s[j] != c {\n replace_cnt += 1;\n }\n let c = c as usize;\n dp[c][replace_cnt] = cmp::max(dp[c][replace_cnt], j - i + 1);\n }\n }\n\n let c = c as usize;\n for i in 1..(n + 1) {\n dp[c][i] = cmp::max(dp[c][i], dp[c][i - 1]);\n }\n }\n\n let q = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input.trim().parse::().unwrap()\n };\n\n for _ in 0..q {\n let (m, c) = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut it = input.split_whitespace();\n (\n it.next().unwrap().parse::().unwrap(),\n it.next().unwrap().as_bytes()[0] - b'a',\n )\n };\n\n let ans = dp[c as usize][m];\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"}
{"problem_id": "0086", "problem_description": "You are playing a variation of game 2048. Initially you have a multiset $$$s$$$ of $$$n$$$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset.During each operation you choose two equal integers from $$$s$$$, remove them from $$$s$$$ and insert the number equal to their sum into $$$s$$$.For example, if $$$s = \\{1, 2, 1, 1, 4, 2, 2\\}$$$ and you choose integers $$$2$$$ and $$$2$$$, then the multiset becomes $$$\\{1, 1, 1, 4, 4, 2\\}$$$.You win if the number $$$2048$$$ belongs to your multiset. For example, if $$$s = \\{1024, 512, 512, 4\\}$$$ you can win as follows: choose $$$512$$$ and $$$512$$$, your multiset turns into $$$\\{1024, 1024, 4\\}$$$. Then choose $$$1024$$$ and $$$1024$$$, your multiset turns into $$$\\{2048, 4\\}$$$ and you win.You have to determine if you can win this game.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int i = 0;\n int j = 0;\n int k = 0;\n int t = 0;\n int n = 0;\n int a = 0;\n scanf(\"%d\", &t);\n while (t--) {\n k = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n if (a <= 2048) {\n k = k + a;\n }\n }\n if (k >= 2048) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let t = input.trim().parse::().unwrap();\n\n let pow2 = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048];\n for _ in 0..t {\n let mut vals = [0u8; 12];\n let mut _input = String::new();\n io::stdin()\n .read_line(&mut _input)\n .expect(\"failed to read input\");\n\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n for n in input.split_whitespace() {\n let n = n.parse::().unwrap();\n if n <= 2048 {\n let log_n = pow2.binary_search(&n).unwrap();\n vals[log_n] += 1;\n }\n }\n\n let mut rem = 1u16;\n for n in vals.iter().rev() {\n if rem <= *n as u16 {\n rem = 0;\n break;\n } else {\n rem -= *n as u16;\n rem *= 2;\n }\n }\n\n if rem == 0 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"}
{"problem_id": "0087", "problem_description": "Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 \\cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right. Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.", "c_code": "int solution() {\n int n;\n int i;\n scanf(\"%d\", &n);\n long long num1[2][n];\n\n long long dp2[2][n];\n\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &num1[0][i]);\n }\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &num1[1][i]);\n }\n\n if (n == 1) {\n printf(\"%lld\\n\", num1[0][0] > num1[1][0] ? num1[0][0] : num1[1][0]);\n } else if (n == 2) {\n printf(\"%lld\\n\", num1[0][0] + num1[1][1] > num1[0][1] + num1[1][0]\n ? (long)num1[0][0] + num1[1][1]\n : (long)num1[0][1] + num1[1][0]);\n } else {\n\n dp2[0][0] = num1[0][0];\n dp2[1][0] = num1[1][0];\n dp2[0][1] = num1[0][1] + num1[1][0] > num1[0][0] ? num1[0][1] + num1[1][0]\n : num1[0][0];\n dp2[1][1] = num1[1][1] + num1[0][0] > num1[1][0] ? num1[1][1] + num1[0][0]\n : num1[1][0];\n\n for (i = 2; i < n; i++) {\n dp2[0][i] = num1[0][i] + dp2[1][i - 1] > dp2[0][i - 1]\n ? num1[0][i] + dp2[1][i - 1]\n : dp2[0][i - 1];\n dp2[1][i] = num1[1][i] + dp2[0][i - 1] > dp2[1][i - 1]\n ? num1[1][i] + dp2[0][i - 1]\n : dp2[1][i - 1];\n }\n\n printf(\"%lld\\n\",\n dp2[0][n - 1] > dp2[1][n - 1] ? dp2[0][n - 1] : dp2[1][n - 1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n let n: i64 = n.trim().parse().unwrap();\n\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let a: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let b: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut x = 0;\n let mut y = 0;\n let mut z = 0;\n let mut tempx;\n let mut tempy;\n\n for i in 0..n as usize {\n tempx = x;\n tempy = y;\n x = max(y + a[i], z + a[i]);\n y = max(tempx + b[i], z + b[i]);\n z = max(tempx, tempy);\n }\n\n println!(\"{}\", max(x, max(y, z)));\n}", "difficulty": "hard"}
{"problem_id": "0088", "problem_description": "You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the $$$XY$$$ plane. You are starting at point $$$(0, 0)$$$, and Planetforces is located in point $$$(p_x, p_y)$$$.The piloting system of your spaceship follows its list of orders which can be represented as a string $$$s$$$. The system reads $$$s$$$ from left to right. Suppose you are at point $$$(x, y)$$$ and current order is $$$s_i$$$: if $$$s_i = \\text{U}$$$, you move to $$$(x, y + 1)$$$; if $$$s_i = \\text{D}$$$, you move to $$$(x, y - 1)$$$; if $$$s_i = \\text{R}$$$, you move to $$$(x + 1, y)$$$; if $$$s_i = \\text{L}$$$, you move to $$$(x - 1, y)$$$. Since string $$$s$$$ could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from $$$s$$$ but you can't change their positions.Can you delete several orders (possibly, zero) from $$$s$$$ in such a way, that you'll reach Planetforces after the system processes all orders?", "c_code": "int solution() {\n int a;\n scanf(\"%d\", &a);\n for (int i = 0; i < a; i++) {\n int px = 0;\n int py = 0;\n int l = 0;\n int r = 0;\n int u = 0;\n int d = 0;\n int e = 0;\n int x = 0;\n int y = 0;\n scanf(\"%d %d\", &px, &py);\n char s[100000];\n scanf(\"%s\", s);\n for (e = 0; s[e] != '\\0'; e++) {\n if (s[e] == 'L') {\n l++;\n }\n if (s[e] == 'R') {\n r++;\n }\n if (s[e] == 'U') {\n u++;\n }\n if (s[e] == 'D') {\n d++;\n }\n }\n for (int j = 0; j <= l; j++) {\n for (int k = 0; k <= r; k++) {\n if ((k - j) == px) {\n x = k - j;\n }\n }\n }\n\n for (int j = 0; j <= u; j++) {\n for (int k = 0; k <= d; k++) {\n if ((j - k) == py) {\n y = j - k;\n }\n }\n }\n if ((px == x) && (py == y)) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n let mut i = i.lock().lines().skip(1);\n\n while let Some(l1) = i.next() {\n if let [xf, yf] = l1\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>()[..]\n {\n let xd = if xf < 0 { 'L' } else { 'R' };\n let yd = if yf < 0 { 'D' } else { 'U' };\n let (x, y) = i\n .next()\n .unwrap()\n .unwrap()\n .chars()\n .filter(|&c| c == xd || c == yd)\n .fold((0, 0), |(x, y), c| match c {\n 'U' | 'D' => (x, y + 1),\n _ => (x + 1, y),\n });\n writeln!(\n o,\n \"{}\",\n if xf.abs() <= x && yf.abs() <= y {\n \"YES\"\n } else {\n \"NO\"\n }\n )\n .ok();\n }\n }\n}", "difficulty": "easy"}
{"problem_id": "0089", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
In some other world, today is the day before Christmas Eve.
\nMr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \\leq i \\leq N) is p_i yen (the currency of Japan).
\nHe has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?
\n\n
\n\n
\nConstraints
\n- 2 \\leq N \\leq 10
\n- 100 \\leq p_i \\leq 10000
\n- p_i is an even number.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\np_1\np_2\n:\np_N\n
\n\n
\n
\n
\nOutput
Print the total amount Mr. Takaha will pay.
\n\n
\n
\n
\n\n
\nSample Input 1
3\n4980\n7980\n6980\n
\n\n
\n\n
\nSample Output 1
15950\n
\nThe 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.
\nNote that outputs such as 15950.0 will be judged as Wrong Answer.
\n\n
\n
\n\n
\nSample Input 2
4\n4320\n4320\n4320\n4320\n
\n\n
\n\n
\nSample Output 2
15120\n
\nOnly one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.
\n
\n", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\\n\", &n);\n int max = 0;\n int sum = 0;\n int buf = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &buf);\n sum += buf;\n if (buf > max) {\n max = buf;\n }\n }\n printf(\"%d\\n\", sum - (max / 2));\n return 0;\n}", "rust_code": "fn solution() {\n let N: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n let p: Vec = (0..N)\n .map(|_| {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n })\n .collect();\n\n let ans = p.iter().sum::() - p.iter().max().unwrap() / 2;\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"}
{"problem_id": "0090", "problem_description": "Amugae has a hotel consisting of $$$10$$$ rooms. The rooms are numbered from $$$0$$$ to $$$9$$$ from left to right.The hotel has two entrances — one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they are assigned to an empty room closest to the right entrance.One day, Amugae lost the room assignment list. Thankfully Amugae's memory is perfect, and he remembers all of the customers: when a customer arrived, from which entrance, and when they left the hotel. Initially the hotel was empty. Write a program that recovers the room assignment list from Amugae's memory.", "c_code": "int solution() {\n char kamar[11];\n int num;\n char in[100000];\n for (int i = 0; i <= 9; i++) {\n kamar[i] = '0';\n }\n scanf(\"%d\", &num);\n getchar();\n for (int i = 1; i <= num; i++) {\n scanf(\"%c\", &in[i]);\n }\n int nom = 0;\n int nim = 9;\n for (int i = 1; i <= num; i++) {\n if (in[i] == 'L') {\n while (kamar[nom] == '1') {\n nom++;\n }\n kamar[nom] = '1';\n } else if (in[i] == 'R') {\n while (kamar[nim] == '1') {\n nim--;\n }\n kamar[nim] = '1';\n } else if (in[i] >= '0' && in[i] <= '9') {\n int nom = in[i] - 48;\n kamar[nom] = '0';\n }\n nom = 0;\n nim = 9;\n }\n for (int i = 0; i <= 9; i++) {\n printf(\"%c\", kamar[i]);\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut _input = String::new();\n io::stdin()\n .read_line(&mut _input)\n .expect(\"failed to read input\");\n let mut rooms: Vec = vec![false; 10];\n let mut l: usize = 0;\n let mut r: usize = 9;\n\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n for i in input.trim().chars() {\n match i {\n 'L' => {\n rooms[l] = true;\n if l == r {\n l = 9;\n r = 0;\n } else {\n l = ((l + 1)..10).find(|&x| !rooms[x]).unwrap_or(9);\n }\n }\n 'R' => {\n rooms[r] = true;\n if l == r {\n l = 9;\n r = 0;\n } else {\n r = (0..r).rev().find(|&x| !rooms[x]).unwrap_or(0);\n }\n }\n _ => {\n let room = (i as usize) - ('0' as usize);\n rooms[room] = false;\n\n if room < l {\n l = room;\n }\n if room > r {\n r = room;\n }\n }\n }\n }\n\n rooms\n .iter()\n .for_each(|&x| print!(\"{}\", if x { 1 } else { 0 }));\n println!();\n}", "difficulty": "medium"}
{"problem_id": "0091", "problem_description": "Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes.As the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each d meters of the path. We know that Valera starts at the point with coordinates (0, 0) and runs counter-clockwise. That is, when Valera covers a meters, he reaches the point with coordinates (a, 0). We also know that the length of the marathon race equals nd + 0.5 meters. Help Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers d, 2·d, ..., n·d meters.", "c_code": "int solution() {\n double a;\n double b;\n int c;\n scanf(\"%lf%lf\", &a, &b);\n scanf(\"%d\", &c);\n double d = 0.0;\n while (c--) {\n long long t = ((b + d) / (4 * a));\n d = (b + d) - t * 4 * a;\n if (d >= 0 && d <= a) {\n printf(\"%.10lf 0.0\\n\", d);\n } else if (d > a && d <= 2 * a) {\n printf(\"%.10lf %.10lf\\n\", a, d - a);\n } else if (d > 2 * a && d <= 3 * a) {\n printf(\"%.10lf %.10lf\\n\", (3 * a) - d, a);\n } else {\n printf(\"0.0 %.10lf\\n\", (4 * a) - d);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let input_handle = io::stdin();\n let input = &mut String::new();\n input_handle.read_line(input);\n let ad = &mut input\n .split_whitespace()\n .map(|word| word.parse::().unwrap());\n let (a, d) = (ad.next().unwrap(), ad.next().unwrap());\n let input = &mut String::new();\n input_handle.read_line(input);\n let n = input\n .split_whitespace()\n .map(|word| word.parse::().unwrap())\n .next()\n .unwrap();\n\n let mut answer = String::new();\n let mut pos = 0f64;\n for _n in 0..n {\n pos += d;\n pos %= 4f64 * a;\n if pos > 3f64 * a {\n answer += \"0 \";\n answer += &((4f64 * a - pos).to_string());\n } else if pos > 2f64 * a {\n answer += &((3f64 * a - pos).to_string());\n answer += \" \";\n answer += &(a.to_string());\n } else if pos > a {\n answer += &(a.to_string());\n answer += \" \";\n answer += &((pos - a).to_string());\n } else {\n answer += &((pos).to_string());\n answer += \" 0\";\n }\n answer += \"\\n\";\n }\n print!(\"{}\", answer);\n}", "difficulty": "hard"}
{"problem_id": "0092", "problem_description": "New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.", "c_code": "int solution() {\n\n int numberOfCells = 0;\n int wantToMove = 0;\n\n scanf(\"%d\", &numberOfCells);\n scanf(\"%d\", &wantToMove);\n\n int flag = 0;\n int movable = 1;\n\n int i = 0;\n for (i = 1; i < numberOfCells; i++) {\n\n int temp = 0;\n scanf(\"%d\", &temp);\n\n if (i == movable) {\n\n movable = (i + temp);\n\n if (wantToMove == movable) {\n flag = 1;\n }\n }\n }\n\n if (flag) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let v: Vec = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim()\n .split(\" \")\n .map(|s| s.parse::().unwrap())\n .collect()\n };\n let (n, t) = (v[0], v[1]);\n let v: Vec = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim()\n .split(\" \")\n .map(|s| s.parse::().unwrap())\n .collect()\n };\n\n let mut current = 0;\n while current != t - 1 && current < n - 1 {\n current += v[current as usize];\n }\n println!(\"{}\", if current == t - 1 { \"YES\" } else { \"NO\" });\n}", "difficulty": "hard"}
{"problem_id": "0093", "problem_description": "\nScore : 400 points
\n\n
\nProblem Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2, \\cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \\cdots, N: P_1, P_2, \\cdots, P_N.
\nNow, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):
\n\n- In one move, if the piece is now on Square i (1 \\leq i \\leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
\n
\nHelp him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)
\n\n
\n\n
\nConstraints
\n- 2 \\leq N \\leq 5000
\n- 1 \\leq K \\leq 10^9
\n- 1 \\leq P_i \\leq N
\n- P_i \\neq i
\n- P_1, P_2, \\cdots, P_N are all different.
\n- -10^9 \\leq C_i \\leq 10^9
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN K\nP_1 P_2 \\cdots P_N\nC_1 C_2 \\cdots C_N\n
\n\n
\n
\n
\nOutput
Print the maximum possible score at the end of the game.
\n\n
\n
\n
\n\n
\nSample Input 1
5 2\n2 4 5 1 3\n3 4 -10 -8 8\n
\n\n
\n\n
\nSample Output 1
8\n
\nWhen we start at some square of our choice and make at most two moves, we have the following options:
\n\n- If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.
\n- If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.
\n- If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.
\n- If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.
\n- If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.
\n
\nThe maximum score achieved is 8.
\n\n
\n
\n\n
\nSample Input 2
2 3\n2 1\n10 -7\n
\n\n
\n\n
\n\n
\nSample Input 3
3 3\n3 1 2\n-1000 -2000 -3000\n
\n\n
\n\n
\nSample Output 3
-1000\n
\nWe have to make at least one move.
\n\n
\n
\n\n
\nSample Input 4
10 58\n9 1 6 7 8 4 3 2 10 5\n695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719\n
\n\n
\n\n
\nSample Output 4
29507023469\n
\nThe absolute value of the answer may be enormous.
\n
\n", "c_code": "int solution(void) {\n int N;\n int K = 0;\n scanf(\"%d %d\", &N, &K);\n int PList[N];\n int CList[N];\n int i = 0;\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &PList[i]);\n }\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &CList[i]);\n }\n long long ans = -1e18;\n int si = 0;\n for (si = 0; si < N; si++) {\n int x = si;\n int s[N];\n long long total = 0;\n long long l = 0;\n int idx = -1;\n while (1) {\n x = PList[x] - 1;\n s[++idx] = CList[x];\n l += 1;\n total += CList[x];\n if (x == si) {\n break;\n }\n }\n long long t = 0;\n for (i = 0; i < l; i++) {\n t += s[i];\n if (i + 1 > K) {\n break;\n }\n long long now = t;\n if (total > 0) {\n long long e = (K - (i + 1)) / l;\n now += total * e;\n }\n if (ans < now) {\n ans = now;\n }\n }\n }\n printf(\"%lld\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, k): (usize, usize) = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n (\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n )\n };\n\n let p: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let iter = buf.split_whitespace();\n iter.map(|x| x.parse::().unwrap() - 1).collect()\n };\n\n let c: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let iter = buf.split_whitespace();\n iter.map(|x| x.parse().unwrap()).collect()\n };\n\n let mut ans = std::i64::MIN;\n for mut i in 0..n {\n let mut v = vec![0; n + 1];\n let mut w = vec![0; n];\n let mut check = vec![false; n];\n let mut d = 0;\n for j in 0..n {\n check[i] = true;\n v[j + 1] = v[j] + c[p[i]];\n w[j] = c[p[i]];\n i = p[i];\n if check[i] {\n d = j + 1;\n break;\n }\n }\n\n let res = {\n if k <= d {\n (1..=(cmp::min(k, d))).fold(std::i64::MIN, |acc, j| cmp::max(acc, v[j]))\n } else if v[d] <= 0 {\n (1..=(cmp::min(k, d))).fold(std::i64::MIN, |acc, j| cmp::max(acc, v[j]))\n } else {\n let p = (k - d) % d;\n let (mut max, mut sum) = (0, 0);\n for j in 0..d {\n sum += w[(j + p) % d];\n max = cmp::max(max, sum);\n }\n v[d] * ((k - d) / d) as i64 + v[p] + max\n }\n };\n ans = cmp::max(ans, res);\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"}
{"problem_id": "0094", "problem_description": "You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k > 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\\sum\\limits_{i = 1}^{n}{n^{n - i} \\cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int max = 0;\n int c = 0;\n int pos = 0;\n scanf(\"%d\", &n);\n int a[n];\n int b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] > max) {\n max = a[i];\n b[pos] = i;\n pos++;\n }\n }\n pos--;\n int i = b[pos];\n for (; i < n; i++) {\n c++;\n printf(\"%d \", a[i]);\n if (i == n - 1) {\n pos--;\n n -= c;\n c = 0;\n i = b[pos] - 1;\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut testcases: String = String::new();\n\n io::stdin().read_line(&mut testcases).expect(\"\");\n\n let testcases: usize = testcases.trim().parse().unwrap();\n\n for _testcase in 0..testcases {\n let mut input: String = String::new();\n\n let mut deck: Vec = Vec::new();\n let mut result: Vec = Vec::new();\n let mut indexes: HashMap = HashMap::new();\n\n io::stdin().read_line(&mut input).expect(\"\");\n\n let size: usize = input.trim().parse().unwrap();\n\n input.clear();\n io::stdin().read_line(&mut input).expect(\"\");\n\n for (index, item) in input.trim().split(\" \").enumerate() {\n let n: usize = item.parse().unwrap();\n deck.push(n);\n indexes.insert(n, index);\n }\n\n let mut current_pos: usize = size;\n\n for i in (1..size + 1).rev() {\n let pos: usize = indexes.get(&i).unwrap().to_owned();\n if pos < current_pos {\n result.extend_from_slice(&deck[pos..current_pos]);\n\n current_pos = pos;\n }\n }\n\n for n in result {\n print!(\"{} \", n);\n }\n\n println!();\n }\n}", "difficulty": "hard"}
{"problem_id": "0095", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
You are given a string s.\nAmong the different substrings of s, print the K-th lexicographically smallest one.
\nA substring of s is a string obtained by taking out a non-empty contiguous part in s.\nFor example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not.\nAlso, we say that substrings are different when they are different as strings.
\nLet X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \\neq y_{j}.
\n\n
\n\n
\nConstraints
\n- 1 ≤ |s| ≤ 5000
\n- s consists of lowercase English letters.
\n- 1 ≤ K ≤ 5
\n- s has at least K different substrings.
\n
\n\n
\n\n
\nPartial Score
\n- 200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\ns\nK\n
\n\n
\n
\n
\nOutput
Print the K-th lexicographically smallest substring of K.
\n\n
\n
\n
\n\n
\nSample Input 1
aba\n4\n
\n\n
\n\n
\nSample Output 1
b\n
\ns has five substrings: a, b, ab, ba and aba.\nAmong them, we should print the fourth smallest one, b.\nNote that we do not count a twice.
\n\n
\n
\n\n
\nSample Input 2
atcoderandatcodeer\n5\n
\n\n
\n\n
\nSample Output 2
andat\n
\n\n
\n
\n\n\n", "c_code": "int solution() {\n char s[5001];\n scanf(\"%s\", s);\n int K;\n scanf(\"%d\", &K);\n\n int l = strlen(s);\n int c = 0;\n for (int i = 0; i < K; i++) {\n c += l - i;\n }\n\n char sub[c][K + 1];\n\n int cc = 0;\n for (int i = 1; i <= K; i++) {\n for (int j = 0; j < l - i + 1; j++) {\n for (int k = 0; k < i; k++) {\n sub[cc][k] = s[j + k];\n if (k == i - 1) {\n for (k; k < K; k++) {\n sub[cc][k + 1] = '\\0';\n }\n }\n }\n cc++;\n }\n }\n\n int r = c;\n int swapped = 0;\n while (r > 1 || swapped == 1) {\n if (r > 1) {\n r = r * 10 / 13;\n }\n swapped = 0;\n for (int i = 0; i + r < c; i++) {\n if (strcmp(sub[i], sub[i + r]) > 0) {\n char tmp[K + 1];\n for (int j = 0; j < K + 1; j++) {\n tmp[j] = sub[i][j];\n }\n for (int j = 0; j < K + 1; j++) {\n sub[i][j] = sub[i + r][j];\n }\n for (int j = 0; j < K + 1; j++) {\n sub[i + r][j] = tmp[j];\n }\n swapped = 1;\n }\n }\n }\n\n int k = 1;\n for (int i = 0; i < c; i++) {\n if (i > 0 && strcmp(sub[i - 1], sub[i]) != 0) {\n k++;\n }\n if (k == K) {\n printf(\"%s\\n\", sub[i]);\n return 0;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let s = lines.next().unwrap().unwrap();\n let k: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut ss: BTreeSet<&str> = BTreeSet::new();\n for i in 0..s.len() {\n let len = min(5, s.len() - i);\n for j in 0..len {\n ss.insert(&s[i..(i + j + 1)]);\n }\n }\n let mut it = ss.iter();\n for _ in 0..(k - 1) {\n it.next();\n }\n println!(\"{}\", it.next().unwrap());\n}", "difficulty": "medium"}
{"problem_id": "0096", "problem_description": "You are given a table $$$a$$$ of size $$$n \\times m$$$. We will consider the table rows numbered from top to bottom from $$$1$$$ to $$$n$$$, and the columns numbered from left to right from $$$1$$$ to $$$m$$$. We will denote a cell that is in the $$$i$$$-th row and in the $$$j$$$-th column as $$$(i, j)$$$. In the cell $$$(i, j)$$$ there is written a number $$$(i - 1) \\cdot m + j$$$, that is $$$a_{ij} = (i - 1) \\cdot m + j$$$.A turtle initially stands in the cell $$$(1, 1)$$$ and it wants to come to the cell $$$(n, m)$$$. From the cell $$$(i, j)$$$ it can in one step go to one of the cells $$$(i + 1, j)$$$ or $$$(i, j + 1)$$$, if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path. For example, with $$$n = 2$$$ and $$$m = 3$$$ the table will look as shown above. The turtle can take the following path: $$$(1, 1) \\rightarrow (1, 2) \\rightarrow (1, 3) \\rightarrow (2, 3)$$$. The cost of such way is equal to $$$a_{11} + a_{12} + a_{13} + a_{23} = 12$$$. On the other hand, the paths $$$(1, 1) \\rightarrow (1, 2) \\rightarrow (2, 2) \\rightarrow (2, 1)$$$ and $$$(1, 1) \\rightarrow (1, 3)$$$ are incorrect, because in the first path the turtle can't make a step $$$(2, 2) \\rightarrow (2, 1)$$$, and in the second path it can't make a step $$$(1, 1) \\rightarrow (1, 3)$$$.You are asked to tell the turtle a minimal possible cost of a path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$. Please note that the cells $$$(1, 1)$$$ and $$$(n, m)$$$ are a part of the way.", "c_code": "int solution(void) {\n int test = 0;\n\n scanf(\"%d\", &test);\n\n while (test--) {\n int a;\n int b;\n scanf(\"%d %d\", &a, &b);\n long long n = a;\n long long m = b;\n long long r = (m * n * (n + 1) + m * m - m) / 2;\n\n printf(\"%lld\\n\", r);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut round = String::new();\n io::stdin().read_line(&mut round).expect(\"inputing\");\n\n let round: usize = round.trim().parse().expect(\"parsing\");\n let mut i = 1;\n\n while i <= round {\n let mut size = String::new();\n io::stdin().read_line(&mut size).expect(\"loop input\");\n\n let space_index = size.find(\" \").expect(\" \");\n let n: usize = size[..space_index].trim().parse().expect(\"loop parsing\");\n let m: usize = size[space_index..].trim().parse().expect(\"loop parsing\");\n\n let mut sum = 0;\n let mut j = 1;\n let mut k = 2;\n\n while j <= m {\n sum += j;\n j += 1;\n }\n\n while k <= n {\n sum += k * m;\n k += 1;\n }\n\n println!(\"{}\", sum);\n i += 1;\n }\n}", "difficulty": "hard"}
{"problem_id": "0097", "problem_description": "You have an array $$$a$$$ of length $$$n$$$. For every positive integer $$$x$$$ you are going to perform the following operation during the $$$x$$$-th second: Select some distinct indices $$$i_{1}, i_{2}, \\ldots, i_{k}$$$ which are between $$$1$$$ and $$$n$$$ inclusive, and add $$$2^{x-1}$$$ to each corresponding position of $$$a$$$. Formally, $$$a_{i_{j}} := a_{i_{j}} + 2^{x-1}$$$ for $$$j = 1, 2, \\ldots, k$$$. Note that you are allowed to not select any indices at all. You have to make $$$a$$$ nondecreasing as fast as possible. Find the smallest number $$$T$$$ such that you can make the array nondecreasing after at most $$$T$$$ seconds.Array $$$a$$$ is nondecreasing if and only if $$$a_{1} \\le a_{2} \\le \\ldots \\le a_{n}$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int c;\n scanf(\"%d\", &c);\n long long a;\n long long cMax = 9223372036854775807 * -1;\n int bitMax = 0;\n for (int j = 0; j < c; j++) {\n scanf(\"%lli\", &a);\n if (a >= cMax) {\n cMax = a;\n } else {\n long long diff = cMax - a;\n int bitDiff = (int)log2(diff) + 1;\n\n if (bitDiff > bitMax) {\n bitMax = bitDiff;\n }\n }\n }\n printf(\"%d\\n\", bitMax);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let mut ans: usize = 0;\n for i in 1..n {\n if xs[i - 1] > xs[i] {\n let mut count: usize = 0;\n let mut d = xs[i - 1] - xs[i];\n while d > 0 {\n d >>= 1;\n count += 1;\n }\n ans = max(ans, count);\n xs[i] = xs[i - 1];\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"}
{"problem_id": "0098", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
Takahashi received otoshidama (New Year's money gifts) from N of his relatives.
\nYou are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.
\nFor example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.
\nIf we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?
\n\n
\n\n
\nConstraints
\n- 2 \\leq N \\leq 10
\n- u_i =
JPY or BTC. \n- If u_i =
JPY, x_i is an integer such that 1 \\leq x_i \\leq 10^8. \n- If u_i =
BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \\leq x_i \\leq 100.00000000. \n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\nx_1 u_1\nx_2 u_2\n:\nx_N u_N\n
\n\n
\n
\n
\nOutput
If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).
\nOutput will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.
\n\n
\n
\n
\n\n
\nSample Input 1
2\n10000 JPY\n0.10000000 BTC\n
\n\n
\n\n
\nSample Output 1
48000.0\n
\nThe otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.
\nOutputs such as 48000 and 48000.1 will also be judged correct.
\n\n
\n
\n\n
\nSample Input 2
3\n100000000 JPY\n100.00000000 BTC\n0.00000001 BTC\n
\n\n
\n\n
\nSample Output 2
138000000.0038\n
\nIn this case, outputs such as 138001000 and 1.38e8 will also be judged correct.
\n
\n", "c_code": "int solution(void) {\n int N = 0;\n scanf(\"%d\", &N);\n double x = 0;\n char u[4];\n double sum = 0;\n for (int i = 0; i < N; i++) {\n scanf(\"%lf %s\", &x, u);\n if (u[0] == 'J' && u[1] == 'P' && u[2] == 'Y') {\n sum += x;\n } else {\n sum += 380000.0 * x;\n }\n }\n printf(\"%f\\n\", sum);\n return 0;\n}", "rust_code": "fn solution() {\n let input = {\n use std::io::*;\n let mut input = String::new();\n let _ = stdin().read_to_string(&mut input);\n input\n .replace(\"JPY\", \"1.0\")\n .replace(\"BTC\", \"380000.0\")\n .split_whitespace()\n .skip(1)\n .map(|w| w.parse::().unwrap())\n .collect::>()\n };\n let total = input.chunks(2).fold(0.0, |r, x| r + x[0] * x[1]);\n println!(\"{:?}\", total);\n}", "difficulty": "hard"}
{"problem_id": "0099", "problem_description": "Polycarp has a string $$$s$$$ consisting of lowercase Latin letters.He encodes it using the following algorithm.He goes through the letters of the string $$$s$$$ from left to right and for each letter Polycarp considers its number in the alphabet: if the letter number is single-digit number (less than $$$10$$$), then just writes it out; if the letter number is a two-digit number (greater than or equal to $$$10$$$), then it writes it out and adds the number 0 after. For example, if the string $$$s$$$ is code, then Polycarp will encode this string as follows: 'c' — is the $$$3$$$-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3); 'o' — is the $$$15$$$-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150); 'd' — is the $$$4$$$-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504); 'e' — is the $$$5$$$-th letter of the alphabet. Therefore, Polycarp adds 5 to the code (the code becomes 315045). Thus, code of string code is 315045.You are given a string $$$t$$$ resulting from encoding the string $$$s$$$. Your task is to decode it (get the original string $$$s$$$ by $$$t$$$).", "c_code": "int solution(void) {\n int q;\n char s[64];\n char o[64];\n scanf(\"%d\", &q);\n\n while (q--) {\n int len;\n scanf(\"%d\", &len);\n scanf(\"%s\", s);\n\n int j = 62;\n o[63] = '\\0';\n for (int i = len - 1; i >= 0;) {\n if ('0' == s[i]) {\n\n o[j] = 96 + (s[i - 1] - '0') + (char)10 * (s[i - 2] - '0');\n\n j--;\n i -= 3;\n } else {\n\n o[j] = (s[i] - '0') + 96;\n j--;\n i--;\n }\n }\n\n printf(\"%s\\n\", o + j + 1);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut num = String::new();\n io::stdin().read_line(&mut num).expect(\"panic input num\");\n let trimmed = num.trim().parse::().unwrap();\n let alph: Vec = \"abcdefghijklmnopqrstuvwxyz\".chars().collect();\n for _ in 0..trimmed {\n let mut _ln: String = String::new();\n io::stdin().read_line(&mut _ln).unwrap();\n\n let mut row = String::new();\n io::stdin().read_line(&mut row).unwrap();\n\n row = row.trim().to_string();\n\n let mut n: Vec = row.chars().map(|chr| chr.to_digit(10).unwrap()).collect();\n\n n = n.into_iter().rev().collect();\n let mut c: u8 = 0;\n let mut res: String = String::new();\n for i in 0..n.len() {\n if c != 0 {\n c -= 1;\n continue;\n }\n if n[i] == 0 {\n res.push(alph[(n[i + 1] + n[i + 2] * 10 - 1) as usize]);\n c = 2;\n } else {\n res.push(alph[(n[i] - 1) as usize]);\n }\n }\n res = res.chars().rev().collect::();\n println!(\"{}\", res);\n }\n}", "difficulty": "hard"}
{"problem_id": "0100", "problem_description": "Stepan likes to repeat vowel letters when he writes words. For example, instead of the word \"pobeda\" he can write \"pobeeeedaaaaa\".Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowel letters are \"a\", \"e\", \"i\", \"o\", \"u\" and \"y\".There are exceptions: if letters \"e\" or \"o\" repeat in a row exactly 2 times, like in words \"feet\" and \"foot\", the program must skip them and do not transform in one vowel. For example, the word \"iiiimpleeemeentatiioon\" must be converted to the word \"implemeentatioon\".Sergey is very busy and asks you to help him and write the required program.", "c_code": "int solution() {\n char s[100100];\n int n;\n int i;\n int num = 1;\n scanf(\"%d\", &n);\n scanf(\"%s\", s);\n for (i = 0; i < n; i++) {\n if (s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' &&\n s[i] != 'u' && s[i] != 'y') {\n printf(\"%c\", s[i]);\n } else {\n num = 1;\n while (s[i] == s[i + 1] && i < n) {\n num++;\n i++;\n }\n if (num == 2 && (s[i] == 'e' || s[i] == 'o')) {\n printf(\"%c%c\", s[i], s[i]);\n } else {\n printf(\"%c\", s[i]);\n }\n }\n }\n puts(\"\");\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n\n std::io::stdin()\n .read_line(&mut n)\n .expect(\"Reading n error!\");\n\n let _n: u32 = n.trim().parse().expect(\"Error while parsing!\");\n\n let mut cur_line = String::new();\n std::io::stdin()\n .read_line(&mut cur_line)\n .expect(\"Can't get current line!\");\n\n let mut d = '!';\n let mut t = 0;\n\n for (_i, c) in cur_line.chars().enumerate() {\n if d != c {\n if t == 2 && (d == 'e' || d == 'o') {\n print!(\"{}\", d);\n }\n\n if c == 'a' {\n print!(\"{}\", c);\n }\n if c == 'e' {\n print!(\"{}\", c);\n }\n if c == 'i' {\n print!(\"{}\", c);\n }\n if c == 'o' {\n print!(\"{}\", c);\n }\n if c == 'u' {\n print!(\"{}\", c);\n }\n if c == 'y' {\n print!(\"{}\", c);\n }\n\n d = c;\n t = 1;\n } else {\n t += 1;\n }\n\n if c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != 'y' {\n print!(\"{}\", c);\n }\n }\n if t == 2 && (d == 'e' || d == 'o') {\n print!(\"{}\", d);\n }\n}", "difficulty": "easy"}
{"problem_id": "0101", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.
\nA pixel is the smallest element of an image, and in this problem it is a square of size 1×1.
\nAlso, the given images are binary images, and the color of each pixel is either white or black.
\nIn the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.
\nThe image A is given as N strings A_1,...,A_N.
\nThe j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).
\nSimilarly, the template image B is given as M strings B_1,...,B_M.
\nThe j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).
\nDetermine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.
\n\n
\n\n
\nConstraints
\n- 1≦M≦N≦50
\n- A_i is a string of length N consisting of
# and .. \n- B_i is a string of length M consisting of
# and .. \n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nN M\nA_1\nA_2\n: \nA_N\nB_1\nB_2\n: \nB_M\n
\n\n
\n
\n
\nOutput
Print Yes if the template image B is contained in the image A. Print No otherwise.
\n\n
\n
\n
\n\n
\nSample Input 1
3 2\n#.#\n.#.\n#.#\n#.\n.#\n
\n\n
\n\n
\nSample Output 1
Yes\n
\nThe template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.
\n\n
\n
\n\n
\nSample Input 2
4 1\n....\n....\n....\n....\n#\n
\n\n
\n\n
\nSample Output 2
No\n
\nThe template image B, composed of a black pixel, is not contained in the image A composed of white pixels.
\n
\n", "c_code": "int solution(void) {\n\n int m = 1;\n int n = 1;\n int dif = 0;\n int match = 0;\n scanf(\"%d %d\", &n, &m);\n\n dif = n - m;\n char a[n][51];\n char b[m][51];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", a[i]);\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%s\", b[i]);\n }\n\n for (int i = 0; i <= dif; i++) {\n for (int j = 0; j <= dif; j++) {\n for (int k = 0; k < m; k++) {\n for (int l = 0; l < m; l++) {\n if (a[i + k][j + l] == b[k][l]) {\n match++;\n } else {\n match = 0;\n break;\n }\n if (match == (m * m)) {\n break;\n }\n }\n if (match == 0 || match == (m * m)) {\n break;\n }\n }\n if (match == (m * m)) {\n break;\n }\n }\n if (match == (m * m)) {\n break;\n }\n }\n\n if (match == (m * m)) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut lines = String::new();\n let _ = stdin.read_to_string(&mut lines);\n let vec = lines.lines().collect::>();\n let firstline = vec[0].split(' ').collect::>();\n let n: usize = firstline[0].trim().parse().unwrap();\n let m: usize = firstline[1].trim().parse().unwrap();\n let mkono1: u64 = (1 << m) - 1;\n let mut a: Vec = Vec::new();\n for i in 0..n {\n let mut thisline: u64 = 0;\n for (j, c) in vec[i + 1].trim().chars().enumerate() {\n if c == '#' {\n thisline += 1 << j;\n }\n }\n a.push(thisline);\n }\n let a = a;\n let mut b: Vec = Vec::new();\n for i in 0..m {\n let mut thisline: u64 = 0;\n for (j, c) in vec[i + n + 1].trim().chars().enumerate() {\n if c == '#' {\n thisline += 1 << j;\n }\n }\n b.push(thisline);\n }\n let b = b;\n 'tate: for i in 0..(n - m + 1) {\n 'yoko: for j in 0..(n - m + 1) {\n 'chekku: for k in 0..m {\n let bline = b[k];\n let aline = (a[k + i] >> j) & mkono1;\n if aline ^ bline != 0 {\n continue 'yoko;\n }\n }\n println!(\"Yes\");\n return;\n }\n }\n println!(\"No\");\n}", "difficulty": "medium"}
{"problem_id": "0102", "problem_description": "マルバツスタンプ (Circle Cross Stamps)
\n\n\n問題文
\n\n\nJOI 君はマルスタンプ,バツスタンプ,マルバツスタンプの3種類のスタンプをそれぞれ 0 個以上持っている.これらはマルやバツのマークを紙に印字することができるスタンプである.\n
\n\n\nマルスタンプを使うとマルが 1 つ印字され,バツスタンプを使うとバツが 1 つ印字される.マルバツスタンプを使うとマルとバツが横一列に 1 つずつ印字され,スタンプの向きを変えることで,マルの右にバツが来るようにも,バツの右にマルが来るようにも印字できる.\n
\n\n\nJOI 君は,持っているスタンプをそれぞれちょうど 1 回ずつ適当な順番で使い,紙に横一列にマルとバツを印字した.印字されたマルとバツの列は文字列 S で表される.S は O と X から構成された長さ N の文字列であり,S_i = O ならば JOI 君が印字したマークのうち左から i 番目のものがマルであることを表し,S_i = X ならばそれがバツであることを表す (1 ≦ i ≦ N).\n
\n\n\nあなたは,JOI 君が持っているスタンプの個数は分からないが,JOI 君が印字したマルとバツの列は知っている.印字されたマルとバツの列から,JOI 君が持っているマルバツスタンプの個数としてあり得るもののうち最大値を求めよ.\n
\n\n 制約
\n\n\n- 1 ≦ N ≦ 100000 (= 10^5)
\n- S は長さ N の文字列である.
\n- S の各文字は
O か X である. \n
\n\n 入力・出力
\n\n\n入力
\n入力は以下の形式で標準入力から与えられる.
\nN
\nS\n
\n\n\n出力
\nJOI 君が持っているマルバツスタンプの個数としてあり得るもののうち最大値を出力せよ.\n
\n\n入出力例
\n\n入力例 1
\n\n5\nOXXOX\n
\n\n出力例 1
\n\n2\n
\n\n\nJOI 君が印字したマークは,左から順に,マル,バツ,バツ,マル,バツである.JOI 君がマルスタンプ,バツスタンプ,マルバツスタンプをそれぞれ 0, 1, 2 個持っているとすると,以下の順番でスタンプを使えば,そのようにマークを印字することができる.\n
\n\n\n- 1 つ目のマルバツスタンプを使ってマルとバツをこの順に印字する.
\n- この右に,2 つ目のマルバツスタンプを使ってバツとマルをこの順に印字する.
\n- 最後に,この右に,バツスタンプを使ってバツを印字する.
\n
\n\n\nマルバツスタンプを 3 個以上持っているケースは考えられないので,2 を出力する.\n
\n\n入力例 2
\n\n14\nOXOXOXOXXOXOXO\n
\n\n出力例 2
\n\n7\n
\n\n入力例 3
\n\n10\nOOOOOOOOOO\n
\n\n出力例 3
\n\n0\n
\n\n
\n", "c_code": "int solution(void) {\n int N;\n char S[1000 * 100];\n int res = 0;\n\n scanf(\"%d\", &N);\n scanf(\"%s\", S);\n\n for (int i = 0; i < N;) {\n if ((S[i] == 'O' && S[i + 1] == 'X') || (S[i] == 'X' && S[i + 1] == 'O')) {\n res++;\n i++;\n i++;\n } else {\n i++;\n }\n }\n\n printf(\"%d\\n\", res);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = io::stdin();\n let mut lock = stdin.lock();\n\n lock.read_to_string(&mut buf);\n\n let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let s: Vec = iter.next().unwrap().into();\n\n let mut i = 1;\n let mut answer = 0;\n while i < n {\n if s[i - 1] != s[i] {\n answer += 1;\n i += 2;\n } else {\n i += 1;\n }\n }\n\n println!(\"{}\", answer);\n}", "difficulty": "hard"}
{"problem_id": "0103", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
Takahashi has N days of summer vacation.
\nHis teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.
\nHe cannot do multiple assignments on the same day, or hang out on a day he does an assignment.
\nWhat is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?
\nIf Takahashi cannot finish all the assignments during the vacation, print -1 instead.
\n\n
\n\n
\nConstraints
\n- 1 \\leq N \\leq 10^6
\n- 1 \\leq M \\leq 10^4
\n- 1 \\leq A_i \\leq 10^4
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN M\nA_1 ... A_M\n
\n\n
\n
\n
\nOutput
Print the maximum number of days Takahashi can hang out during the vacation, or -1.
\n\n
\n
\n
\n\n
\nSample Input 1
41 2\n5 6\n
\n\n
\n\n
\nSample Output 1
30\n
\nFor example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.
\n\n
\n
\n\n
\nSample Input 2
10 2\n5 6\n
\n\n
\n\n
\nSample Output 2
-1\n
\nHe cannot finish his assignments.
\n\n
\n
\n\n
\nSample Input 3
11 2\n5 6\n
\n\n
\n\n
\nSample Output 3
0\n
\nHe can finish his assignments, but he will have no time to hang out.
\n\n
\n
\n\n
\nSample Input 4
314 15\n9 26 5 35 8 9 79 3 23 8 46 2 6 43 3\n
\n\n
\n\n", "c_code": "int solution() {\n int time = 0;\n int NumTask = 0;\n scanf(\"%d %d\", &time, &NumTask);\n int temp = 0;\n for (int i = 0; i < NumTask; i++) {\n scanf(\"%d\", &temp);\n time -= temp;\n }\n if (time >= 0) {\n printf(\"%d\", time);\n } else {\n printf(\"-1\");\n }\n}", "rust_code": "fn solution() {\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let (n, _m) = {\n let nm: Vec<_> = st\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect();\n (nm[0], nm[1])\n };\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let a: Vec = st.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n let result: i64 = a.iter().sum();\n if result <= n {\n println!(\"{}\", n - result);\n } else {\n println!(\"-1\");\n };\n}", "difficulty": "hard"}
{"problem_id": "0104", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.
\nOn this sequence, Snuke can perform the following operation:
\n\n- Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.
\n
\nSnuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.\nFind the minimum number of operations required.\nIt can be proved that, Under the constraints of this problem, this objective is always achievable.
\n\n
\n\n
\nConstraints
\n- 2 \\leq K \\leq N \\leq 100000
\n- A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN K\nA_1 A_2 ... A_N\n
\n\n
\n
\n
\nOutput
Print the minimum number of operations required.
\n\n
\n
\n
\n\n
\nSample Input 1
4 3\n2 3 1 4\n
\n\n
\n\n
\nSample Output 1
2\n
\nOne optimal strategy is as follows:
\n\n- \n
In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.
\n \n- \n
In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.
\n \n
\n\n
\n
\n\n
\nSample Input 2
3 3\n1 2 3\n
\n\n
\n\n
\n\n
\nSample Input 3
8 3\n7 3 1 8 4 6 2 5\n
\n\n
\n\n", "c_code": "int solution(void) {\n int n = 0;\n int k = 0;\n int count = 0;\n scanf(\"%d %d\", &n, &k);\n while (n > 0) {\n count++;\n n -= k;\n if (n) {\n n++;\n }\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let n: u32 = iter.next().unwrap().parse().unwrap();\n let k: u32 = iter.next().unwrap().parse().unwrap();\n let ans = (n - 1) / (k - 1) + if (n - 1).is_multiple_of(k - 1) { 0 } else { 1 };\n println!(\"{}\", ans);\n}", "difficulty": "hard"}
{"problem_id": "0105", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.
\nIn the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.
\nJiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.
\n\n
\n\n
\nConstraints
\n- 1 ≦ |S| ≦ 10^5
\n- Each character in S is
B or W. \n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nS\n
\n\n
\n
\n
\nOutput
Print the minimum number of new stones that Jiro needs to place for his purpose.
\n\n
\n
\n
\n\n
\nSample Input 1
BBBWW\n
\n\n
\n\n
\nSample Output 1
1\n
\nBy placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.
\nIn either way, Jiro's purpose can be achieved by placing one stone.
\n\n
\n
\n\n
\nSample Input 2
WWWWWW\n
\n\n
\n\n
\nSample Output 2
0\n
\nIf all stones are already of the same color, no new stone is necessary.
\n\n
\n
\n\n
\nSample Input 3
WBWBWBWBWB\n
\n\n
\n\n", "c_code": "int solution() {\n char S[100000];\n int i = 0;\n int count = 0;\n while (1) {\n scanf(\"%c\", &S[i]);\n if (S[i] == '\\n') {\n break;\n }\n\n if (i != 0 && S[i - 1] != S[i]) {\n count++;\n }\n i++;\n }\n\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut reader = std::io::BufReader::new(stdin.lock());\n\n let mut s = String::new();\n reader.read_line(&mut s).unwrap();\n let mut s: Vec = s.trim().chars().collect();\n\n s.dedup();\n\n println!(\"{}\", s.len() - 1);\n}", "difficulty": "medium"}
{"problem_id": "0106", "problem_description": "\n\n\nMaximum Profit
\n\n\nYou can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen.\n
\n\n\n Write a program which reads values of a currency $R_t$ at a certain time $t$ ($t = 0, 1, 2, ... n-1$), and reports the maximum value of $R_j - R_i$ where $j > i$ .\n
\n\nInput
\n\n\nThe first line contains an integer $n$. In the following $n$ lines, $R_t$ ($t = 0, 1, 2, ... n-1$) are given in order.\n
\n\nOutput
\n\n\nPrint the maximum value in a line.\n
\n\nConstraints
\n\n\n - $2 \\leq n \\leq 200,000$
\n - $1 \\leq R_t \\leq 10^9$
\n
\n\n\nSample Input 1
\n\n6\n5\n3\n1\n3\n4\n3\n
\nSample Output 1
\n\n3\n
\n\nSample Input 2
\n\n3\n4\n3\n2\n
\nSample Output 2
\n\n-1\n
", "c_code": "int solution(void) {\n int i = 0;\n\n int n = 0;\n\n int maxv = 0;\n int minv = 0;\n\n int buf = 0;\n\n scanf(\"%d\", &n);\n\n scanf(\"%d\", &minv);\n\n for (i = 1; i < n; i++) {\n scanf(\"%d\", &buf);\n\n if (i == 1) {\n maxv = buf - minv;\n }\n\n maxv = maxv > buf - minv ? maxv : buf - minv;\n minv = minv < buf ? minv : buf;\n }\n\n printf(\"%d\\n\", maxv);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n\n let mut buf: String = String::new();\n\n stdin.lock().read_line(&mut buf).expect(\"read_line()\");\n stdin.lock().read_line(&mut buf).expect(\"read_line()\");\n let vec: Vec<&str> = buf.split_whitespace().collect();\n\n let n = vec[0].parse().unwrap();\n\n let mut r_old: i32 = vec[1].parse().unwrap();\n\n let mut min = r_old;\n\n let mut gain_max: i32 = -1000000000;\n\n let mut gain = 0;\n\n let mut buf = String::new();\n for _t in 1..n {\n stdin.lock().read_line(&mut buf).expect(\"read_line()\");\n }\n let r_series: Vec<&str> = buf.split_whitespace().collect();\n for t in 1..n {\n let rt = r_series[t - 1].parse().unwrap();\n let diff = rt - r_old;\n if min > rt {\n min = rt;\n if gain_max < diff {\n gain_max = diff;\n }\n gain = 0;\n } else {\n gain += diff;\n if gain_max < gain {\n gain_max = gain;\n }\n }\n r_old = rt;\n }\n println!(\"{}\", gain_max);\n}", "difficulty": "medium"}
{"problem_id": "0107", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:
\n\n- Choose two among A, B and C, then increase both by 1.
\n- Choose one among A, B and C, then increase it by 2.
\n
\nIt can be proved that we can always make A, B and C all equal by repeatedly performing these operations.
\n\n
\n\n
\nConstraints
\n- 0 \\leq A,B,C \\leq 50
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nA B C\n
\n\n
\n
\n
\nOutput
Print the minimum number of operations required to make A, B and C all equal.
\n\n
\n
\n
\n\n
\nSample Input 1
2 5 4\n
\n\n
\n\n
\nSample Output 1
2\n
\nWe can make A, B and C all equal by the following operations:
\n\n- Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.
\n- Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.
\n
\n\n
\n
\n\n
\nSample Input 2
2 6 3\n
\n\n
\n\n
\n\n
\nSample Input 3
31 41 5\n
\n\n
\n\n", "c_code": "int solution() {\n int x[3];\n int count = 0;\n scanf(\"%d %d %d\", &x[0], &x[1], &x[2]);\n for (int i = 0; i < 2; i++) {\n if (x[i] > x[i + 1]) {\n int t = x[i] + x[i + 1];\n x[i] = t - x[i];\n x[i + 1] = t - x[i + 1];\n }\n }\n for (int i = 0; i < 2; i++) {\n if (x[i] > x[i + 1]) {\n int t = x[i] + x[i + 1];\n x[i] = t - x[i];\n x[i + 1] = t - x[i + 1];\n }\n }\n int xa = x[0] % 2;\n int xb = x[1] % 2;\n int xc = x[2] % 2;\n if ((xa != xb) && ((xb == xc) || (xa == xc))) {\n count += 2;\n } else if ((xa == xb) && (xb == xc)) {\n\n } else {\n count++;\n }\n count += (x[2] - x[1]) / 2;\n count += (x[2] - x[0]) / 2;\n printf(\"%d\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n std::io::stdin().read_to_string(&mut s).ok();\n let mut itr = s.split_whitespace();\n let mut a: Vec = (0..3)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n a.sort();\n\n let max = a[2] * 3;\n let sum = a.iter().sum::();\n if (max - sum).is_multiple_of(2) {\n println!(\"{}\", (max - sum) / 2);\n } else {\n println!(\"{}\", (max - sum) / 2 + 2);\n }\n}", "difficulty": "hard"}
{"problem_id": "0108", "problem_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.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int arr[2 * n];\n int check[2 * n];\n for (int i = 0; i < 2 * n; ++i) {\n check[i] = 0;\n }\n int parity[2 * n];\n int n_o = 0;\n int n_e = 0;\n for (int i = 0; i < 2 * n; ++i) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] % 2 == 0) {\n parity[i] = 0;\n\n } else {\n parity[i] = 1;\n }\n\n if (arr[i] % 2) {\n n_o++;\n } else {\n n_e++;\n }\n }\n\n if (n_o % 2 == 1) {\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] && check[i] == 0) {\n\n check[i] = 1;\n n_o--;\n break;\n }\n }\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] == 0 && check[i] == 0) {\n\n check[i] = 1;\n n_e--;\n break;\n }\n }\n\n } else {\n if (n_o) {\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] && check[i] == 0) {\n\n check[i] = 1;\n n_o--;\n break;\n }\n }\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] && check[i] == 0) {\n\n check[i] = 1;\n n_o--;\n break;\n }\n }\n } else {\n for (int i = 0; i < 2 * n; ++i) {\n if ((!parity[i]) && check[i] == 0) {\n\n check[i] = 1;\n n_e--;\n break;\n }\n }\n for (int i = 0; i < 2 * n; ++i) {\n if ((!parity[i]) && check[i] == 0) {\n\n check[i] = 1;\n n_e--;\n break;\n }\n }\n }\n }\n for (int j = 1; j <= n_e / 2; ++j) {\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] == 0 && check[i] == 0) {\n printf(\"%d \", i + 1);\n check[i] = 1;\n break;\n }\n }\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] == 0 && check[i] == 0) {\n printf(\"%d \", i + 1);\n check[i] = 1;\n break;\n }\n }\n printf(\"\\n\");\n }\n for (int j = 1; j <= n_o / 2; ++j) {\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] != 0 && check[i] == 0) {\n printf(\"%d \", i + 1);\n check[i] = 1;\n break;\n }\n }\n for (int i = 0; i < 2 * n; ++i) {\n if (parity[i] != 0 && check[i] == 0) {\n printf(\"%d \", i + 1);\n check[i] = 1;\n break;\n }\n }\n printf(\"\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().parse().unwrap();\n for _ in 0..t {\n let _: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let evens: Vec<(usize, usize)> = xs\n .iter()\n .cloned()\n .enumerate()\n .filter(|x| x.1 % 2 == 0)\n .collect();\n let odds: Vec<(usize, usize)> = xs\n .iter()\n .cloned()\n .enumerate()\n .filter(|x| x.1 % 2 == 1)\n .collect();\n if evens.len().is_multiple_of(2) {\n if !evens.is_empty() {\n for i in 1..(evens.len() / 2) {\n println!(\"{} {}\", evens[i * 2].0 + 1, evens[i * 2 + 1].0 + 1);\n }\n for i in 0..(odds.len() / 2) {\n println!(\"{} {}\", odds[i * 2].0 + 1, odds[i * 2 + 1].0 + 1);\n }\n } else {\n for i in 0..(evens.len() / 2) {\n println!(\"{} {}\", evens[i * 2].0 + 1, evens[i * 2 + 1].0 + 1);\n }\n for i in 1..(odds.len() / 2) {\n println!(\"{} {}\", odds[i * 2].0 + 1, odds[i * 2 + 1].0 + 1);\n }\n }\n } else {\n for i in 0..(evens.len() / 2) {\n println!(\"{} {}\", evens[i * 2 + 1].0 + 1, evens[i * 2 + 2].0 + 1);\n }\n for i in 0..(odds.len() / 2) {\n println!(\"{} {}\", odds[i * 2 + 1].0 + 1, odds[i * 2 + 2].0 + 1);\n }\n }\n }\n}", "difficulty": "easy"}
{"problem_id": "0109", "problem_description": "Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.", "c_code": "int solution() {\n int n;\n int x;\n long long s = 0;\n long long min = 1e10;\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d\", &x);\n s += x;\n if (x & 1 && x < min) {\n min = x;\n }\n }\n if (s & 1) {\n s -= min;\n }\n printf(\"%lld\\n\", s);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let n = s.trim().parse::().unwrap();\n s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut ints = s.split_whitespace().map(|x| x.parse::().unwrap());\n\n let mut odd_count = 0u64;\n let mut minodd = 1u64 << (64 - 1);\n let mut sum = 0u64;\n\n for _i in 0..n {\n let a = ints.next().unwrap();\n if a % 2 == 0 {\n sum += a;\n } else {\n odd_count = (odd_count + 1) % 2;\n if a < minodd {\n minodd = a;\n }\n sum += a;\n }\n }\n println!(\"{}\", if odd_count == 1 { sum - minodd } else { sum });\n}", "difficulty": "hard"}
{"problem_id": "0110", "problem_description": "\nScore : 500 points
\n\n
\nProblem Statement
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
\nLet A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).\nFind \\displaystyle{\\sum_{i=1}^N A_i}.
\n\n
\n\n
\nConstraints
\n- 1 \\leq N \\leq 10^{10}
\n- 0 \\leq X < M \\leq 10^5
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN X M\n
\n\n
\n
\n
\nOutput
Print \\displaystyle{\\sum_{i=1}^N A_i}.
\n\n
\n
\n
\n\n
\nSample Input 1
6 2 1001\n
\n\n
\n\n
\nSample Output 1
1369\n
\nThe sequence A begins 2,4,16,256,471,620,\\ldots Therefore, the answer is 2+4+16+256+471+620=1369.
\n\n
\n
\n\n
\nSample Input 2
1000 2 16\n
\n\n
\n\n
\nSample Output 2
6\n
\nThe sequence A begins 2,4,0,0,\\ldots Therefore, the answer is 6.
\n\n
\n
\n\n
\nSample Input 3
10000000000 10 99959\n
\n\n
\n\n
\nSample Output 3
492443256176507\n
\n
\n", "c_code": "int solution(void) {\n\n long long N;\n long long X;\n long long M;\n\n scanf(\"%lld%lld%lld\", &N, &X, &M);\n\n long long c[M];\n\n for (long long i = 0; i < M; i++) {\n c[i] = 0;\n }\n\n long long x = X;\n long long f = 0;\n long long t = 0;\n long long a = 1;\n long long s;\n\n while (1) {\n if (c[x % M] > 0) {\n t = a - 1;\n s = x % M;\n break;\n }\n a++;\n c[x % M] = 1;\n x = (x * x) % M;\n }\n\n long long y = X;\n a = 1;\n\n while (1) {\n if (y % M == s) {\n f = a - 1;\n break;\n }\n a++;\n y = (y * y) % M;\n }\n\n long long ans = 0;\n\n for (long long i = 0; i < f; i++) {\n ans += X % M;\n X = (X * X) % M;\n }\n\n long long z = X;\n\n long long k = 0;\n for (long long i = f; i < t; i++) {\n k += X % M;\n X = (X * X) % M;\n }\n\n ans += k * ((N - f) / (t - f));\n\n for (long long j = 1; j <= (N - f) % (t - f); j++) {\n ans += z % M;\n z = (z * z) % M;\n }\n\n printf(\"%lld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n std::io::stdin().read_to_string(&mut s).ok();\n let mut itr = s.split_whitespace();\n let n: usize = itr.next().unwrap().parse().unwrap();\n let x: usize = itr.next().unwrap().parse().unwrap();\n let m: usize = itr.next().unwrap().parse().unwrap();\n\n let mut used: Vec = vec![false; m];\n let mut cycle: Vec = vec![];\n\n let mut start = 0;\n let mut now = x;\n for _ in 0..n {\n used[now] = true;\n cycle.push(now);\n now = now * now % m;\n if used[now] {\n for j in 0..cycle.len() {\n if cycle[j] == now {\n start = j;\n break;\n }\n }\n break;\n }\n }\n\n let mut s1 = 0;\n let mut s2 = 0;\n let len = cycle.len() - start;\n for i in 0..start {\n s1 += cycle[i];\n }\n for i in start..cycle.len() {\n s2 += cycle[i];\n }\n\n let mut ans = s1 + (n - start) / len * s2;\n for _ in 0..(n - start) % len {\n ans += now;\n now = now * now % m;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"}
{"problem_id": "0111", "problem_description": "The city where Mocha lives in is called Zhijiang. There are $$$n+1$$$ villages and $$$2n-1$$$ directed roads in this city. There are two kinds of roads: $$$n-1$$$ roads are from village $$$i$$$ to village $$$i+1$$$, for all $$$1\\leq i \\leq n-1$$$. $$$n$$$ roads can be described by a sequence $$$a_1,\\ldots,a_n$$$. If $$$a_i=0$$$, the $$$i$$$-th of these roads goes from village $$$i$$$ to village $$$n+1$$$, otherwise it goes from village $$$n+1$$$ to village $$$i$$$, for all $$$1\\leq i\\leq n$$$. Mocha plans to go hiking with Taki this weekend. To avoid the trip being boring, they plan to go through every village exactly once. They can start and finish at any villages. Can you help them to draw up a plan?", "c_code": "int solution() {\n int t;\n int n[20];\n int one[20];\n int temp;\n\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n int got_one = 0;\n scanf(\"%d\", &n[i]);\n one[i] = n[i];\n for (int j = 0; j < n[i]; j++) {\n\n scanf(\"%d\", &temp);\n\n if (temp == 1 && got_one == 0) {\n one[i] = j;\n got_one = 1;\n }\n }\n }\n\n for (int i = 0; i < t; i++) {\n int gone_n = 0;\n for (int j = 0; j < n[i]; j++) {\n if (gone_n == 0 && j == one[i]) {\n printf(\"%d \", n[i] + 1);\n j--;\n gone_n = 1;\n } else {\n printf(\"%d \", j + 1);\n }\n }\n if (gone_n == 0) {\n printf(\"%d \", n[i] + 1);\n }\n printf(\"\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let in_lock = std_in.lock();\n let input = BufReader::new(in_lock);\n\n let std_out = stdout();\n let out_lock = std_out.lock();\n let mut output = BufWriter::new(out_lock);\n\n let mut lines = input.lines().map(|r| r.unwrap());\n let t = lines.next().unwrap().parse::().unwrap();\n\n 'cases: for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n let s = lines.next().unwrap();\n\n let a: Vec<_> = s.split_whitespace().map(|s| s == \"1\").collect();\n\n if a.first().cloned().unwrap() {\n write!(&mut output, \"{}\", n + 1).unwrap();\n for i in 1..=n {\n write!(&mut output, \" {}\", i).unwrap()\n }\n } else if !a.last().cloned().unwrap() {\n for i in 1..=n {\n write!(&mut output, \"{} \", i).unwrap()\n }\n write!(&mut output, \"{}\", n + 1).unwrap();\n } else {\n let i = a.into_iter().position(|x| x).unwrap();\n for j in 0..i {\n write!(&mut output, \"{} \", j + 1).unwrap()\n }\n write!(&mut output, \"{}\", n + 1).unwrap();\n for j in i..n {\n write!(&mut output, \" {}\", j + 1).unwrap();\n }\n }\n\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "medium"}
{"problem_id": "0112", "problem_description": "You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ positive integers each. You can apply the following operation to them any number of times: Select an index $$$i$$$ ($$$1\\leq i\\leq n$$$) and swap $$$a_i$$$ with $$$b_i$$$ (i. e. $$$a_i$$$ becomes $$$b_i$$$ and vice versa). Find the minimum possible value of $$$\\max(a_1, a_2, \\ldots, a_n) \\cdot \\max(b_1, b_2, \\ldots, b_n)$$$ you can get after applying such operation any number of times (possibly zero).", "c_code": "int solution() {\n int n;\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n int a[n];\n int b[n];\n int x = 0;\n int y = 0;\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", a + j);\n if (a[j] > x) {\n x = a[j];\n }\n }\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", b + j);\n if (b[j] > a[j] && b[j] <= x) {\n int t = a[j];\n a[j] = b[j];\n b[j] = t;\n }\n if (b[j] > y) {\n y = b[j];\n }\n }\n x = 0;\n for (int j = 0; j < n; j++) {\n if (a[j] > b[j] && a[j] <= y) {\n int t = a[j];\n a[j] = b[j];\n b[j] = t;\n }\n if (a[j] > x) {\n x = a[j];\n }\n }\n printf(\"%d\\n\", x * y);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: i32 = line.trim().parse().unwrap();\n for _i in 0..t {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let _n: i32 = line.trim().parse().unwrap();\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let a: Vec = line\n .split_ascii_whitespace()\n .map(|t| t.parse().unwrap())\n .collect();\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let b: Vec = line\n .split_ascii_whitespace()\n .map(|t| t.parse().unwrap())\n .collect();\n let c = a.iter().zip(b.iter()).map(|(x, y)| x.max(y)).max().unwrap();\n let d = a.iter().zip(b.iter()).map(|(x, y)| x.min(y)).max().unwrap();\n println!(\"{}\", c * d);\n }\n}", "difficulty": "medium"}
{"problem_id": "0113", "problem_description": "You are given two binary strings $$$a$$$ and $$$b$$$ of the same length. You can perform the following two operations on the string $$$a$$$: Swap any two bits at indices $$$i$$$ and $$$j$$$ respectively ($$$1 \\le i, j \\le n$$$), the cost of this operation is $$$|i - j|$$$, that is, the absolute difference between $$$i$$$ and $$$j$$$. Select any arbitrary index $$$i$$$ ($$$1 \\le i \\le n$$$) and flip (change $$$0$$$ to $$$1$$$ or $$$1$$$ to $$$0$$$) the bit at this index. The cost of this operation is $$$1$$$. Find the minimum cost to make the string $$$a$$$ equal to $$$b$$$. It is not allowed to modify string $$$b$$$.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n\n char a[1000001];\n char b[1000001];\n scanf(\"%s %s\", a, b);\n\n int cost = 0;\n for (int i = 0; i < n; i++) {\n if (a[i] != b[i]) {\n cost += 1;\n if (i < n - 1 && a[i + 1] == b[i] && b[i + 1] == a[i]) {\n i++;\n }\n }\n }\n\n printf(\"%d\\n\", cost);\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let stdin = io::stdin();\n let mut handle = stdin.lock();\n\n handle.read_to_string(&mut buffer).expect(\"stdin\");\n\n let arr: Vec<_> = buffer.split_whitespace().collect();\n\n let _n = *arr.get(0).unwrap();\n\n let s1 = arr.get(1).unwrap().chars().collect::>();\n let s2 = arr.get(2).unwrap().chars().collect::>();\n\n let mut dp = vec![0];\n for (i, v) in s1.iter().enumerate() {\n if s2[i] != *v {\n let val = *dp.last().unwrap() + 1;\n dp.push(val);\n } else {\n let val = *dp.last().unwrap();\n dp.push(val);\n }\n\n if i >= 1 && s1[i - 1] == s2[i] && s1[i] == s2[i - 1] {\n let index_one_based = i + 1;\n use std::cmp;\n dp[index_one_based] = cmp::min(dp[index_one_based - 2] + 1, dp[index_one_based]);\n }\n }\n\n let ans = *dp.last().unwrap();\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"}
{"problem_id": "0114", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
There are N people standing in a row from west to east.\nEach person is facing east or west.\nThe directions of the people is given as a string S of length N.\nThe i-th person from the west is facing east if S_i = E, and west if S_i = W.
\nYou will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.\nHere, we do not care which direction the leader is facing.
\nThe people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.\nFind the minimum number of people who have to change their directions.
\n\n
\n\n
\nConstraints
\n- 2 \\leq N \\leq 3 \\times 10^5
\n- |S| = N
\n- S_i is
E or W. \n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\nS\n
\n\n
\n
\n
\nOutput
Print the minimum number of people who have to change their directions.
\n\n
\n
\n
\n\n
\nSample Input 1
5\nWEEWW\n
\n\n
\n\n
\nSample Output 1
1\n
\nAssume that we appoint the third person from the west as the leader.\nThen, the first person from the west needs to face east and has to turn around.\nThe other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.\nIt is not possible to have 0 people who have to change their directions, so the answer is 1.
\n\n
\n
\n\n
\nSample Input 2
12\nWEWEWEEEWWWE\n
\n\n
\n\n
\n\n
\nSample Input 3
8\nWWWWWEEE\n
\n\n
\n\n", "c_code": "int solution() {\n int Num = 0;\n char chara[3 * 100000];\n int Sum[3 * 100000] = {0};\n scanf(\"%d\", &Num);\n scanf(\"%s\", chara);\n for (int i = 0; i < Num; i++) {\n if (i == 0) {\n for (int j = 1; j < Num; j++) {\n if (chara[j] == 'E') {\n Sum[i]++;\n }\n }\n } else {\n Sum[i] = Sum[i - 1];\n if (chara[i - 1] == 'W') {\n Sum[i]++;\n }\n if (chara[i] == 'E') {\n Sum[i]--;\n }\n }\n }\n\n int minimum = 3 * 100000;\n\n for (int i = 0; i < Num; i++) {\n if (minimum > Sum[i]) {\n minimum = Sum[i];\n }\n }\n printf(\"%d\\n\", minimum);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let _: usize = buf.trim().parse().unwrap();\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut s = buf.trim().chars().collect::>();\n\n let mut east_cost = vec![0];\n let mut west_cost = vec![0];\n\n let mut cost = 0;\n for &c in &s {\n if c == 'W' {\n cost += 1;\n }\n east_cost.push(cost);\n }\n cost = 0;\n s.reverse();\n for &c in &s {\n if c == 'E' {\n cost += 1;\n }\n west_cost.push(cost);\n }\n east_cost.pop();\n west_cost.pop();\n west_cost.reverse();\n println!(\n \"{}\",\n east_cost\n .iter()\n .zip(west_cost.iter())\n .map(|(a, b)| a + b)\n .min()\n .unwrap()\n );\n}", "difficulty": "easy"}
{"problem_id": "0115", "problem_description": "Once upon a time there was only one router in the well-known company Bmail. Years went by and over time new routers were purchased. Every time they bought a new router, they connected it to one of the routers bought before it. You are given the values $$$p_i$$$ — the index of the router to which the $$$i$$$-th router was connected after being purchased ($$$p_i < i$$$).There are $$$n$$$ routers in Boogle in total now. Print the sequence of routers on the path from the first to the $$$n$$$-th router.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n\n int a[n + 1];\n for (int i = 2; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n int k = n;\n int way[n + 1];\n\n for (int i = 1; i <= n; i++) {\n way[i] = 0;\n }\n\n while (k != 1) {\n way[k] = 1;\n\n k = a[k];\n }\n\n way[1] = 1;\n\n for (int i = 1; i <= n; i++) {\n if (way[i] == 1) {\n printf(\"%d \", i);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let stdin = io::stdin();\n let mut handle = stdin.lock();\n\n handle.read_to_string(&mut buffer).expect(\"stdin\");\n\n let arr: Vec<_> = buffer.split_whitespace().collect();\n let n = arr[0].parse::().unwrap();\n let mut p: [usize; 200010] = [0; 200010];\n for i in 2..=n {\n p[i] = arr[i - 1].parse::().unwrap();\n }\n let mut x: usize = n;\n let mut buf: VecDeque = VecDeque::new();\n buf.push_front(x);\n while x != 1 {\n x = p[x];\n buf.push_front(x);\n }\n for i in buf.iter() {\n println!(\"{}\", *i);\n }\n}", "difficulty": "medium"}
{"problem_id": "0116", "problem_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?", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n int arr[T][2];\n int res[T];\n\n for (int i = 0; i < T; i++) {\n scanf(\"%d %d\", &arr[i][0], &arr[i][1]);\n }\n\n for (int i = 0; i < T; i++) {\n res[i] = (arr[i][1] == 0 && arr[i][0] % 2 != 0) ? 1\n : (arr[i][0] >= arr[i][1] &&\n ((arr[i][0] % 2 == 0 && arr[i][1] % 2 != 0) ||\n (arr[i][0] % 2 != 0 && arr[i][1] % 2 == 0)))\n ? 1\n : (arr[i][0] >= arr[i][1]) ? 0\n : (arr[i][1] - arr[i][0]);\n }\n\n for (int i = 0; i < T; i++) {\n printf(\"%d\\n\", res[i]);\n }\n}", "rust_code": "fn solution() {\n let mut inp = String::new();\n io::stdin().read_line(&mut inp).expect(\"Input Error\");\n let t: u32 = inp.trim().parse().expect(\"Input Error\");\n for _ in 0..t {\n inp = String::new();\n io::stdin().read_line(&mut inp).expect(\"Input Error\");\n let v: Vec<&str> = inp.trim().split(' ').collect();\n let n: u32 = v[0].parse().expect(\"Error\");\n let k: u32 = v[1].parse().expect(\"Error\");\n if n < k {\n println!(\"{}\", k - n);\n } else if (n + k).is_multiple_of(2) {\n println!(\"{}\", 0);\n } else {\n println!(\"{}\", 1);\n }\n }\n}", "difficulty": "hard"}
{"problem_id": "0117", "problem_description": "You are given a number $$$n$$$ (divisible by $$$3$$$) and an array $$$a[1 \\dots n]$$$. In one move, you can increase any of the array elements by one. Formally, you choose the index $$$i$$$ ($$$1 \\le i \\le n$$$) and replace $$$a_i$$$ with $$$a_i + 1$$$. You can choose the same index $$$i$$$ multiple times for different moves.Let's denote by $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ the number of numbers from the array $$$a$$$ that have remainders $$$0$$$, $$$1$$$ and $$$2$$$ when divided by the number $$$3$$$, respectively. Let's say that the array $$$a$$$ has balanced remainders if $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ are equal.For example, if $$$n = 6$$$ and $$$a = [0, 2, 5, 5, 4, 8]$$$, then the following sequence of moves is possible: initially $$$c_0 = 1$$$, $$$c_1 = 1$$$ and $$$c_2 = 4$$$, these values are not equal to each other. Let's increase $$$a_3$$$, now the array $$$a = [0, 2, 6, 5, 4, 8]$$$; $$$c_0 = 2$$$, $$$c_1 = 1$$$ and $$$c_2 = 3$$$, these values are not equal. Let's increase $$$a_6$$$, now the array $$$a = [0, 2, 6, 5, 4, 9]$$$; $$$c_0 = 3$$$, $$$c_1 = 1$$$ and $$$c_2 = 2$$$, these values are not equal. Let's increase $$$a_1$$$, now the array $$$a = [1, 2, 6, 5, 4, 9]$$$; $$$c_0 = 2$$$, $$$c_1 = 2$$$ and $$$c_2 = 2$$$, these values are equal to each other, which means that the array $$$a$$$ has balanced remainders. Find the minimum number of moves needed to make the array $$$a$$$ have balanced remainders.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n\n int arr[n];\n for (int j = 0; j < n; j++) {\n\n scanf(\"%d\", &arr[j]);\n }\n int count = 0;\n int remainders[3] = {0};\n for (int j = 0; j < n; j++) {\n remainders[arr[j] % 3]++;\n }\n\n if (remainders[0] > n / 3) {\n count = count + remainders[0] - (n / 3);\n\n remainders[1] += (remainders[0] - n / 3);\n remainders[0] = n / 3;\n }\n if (remainders[1] > n / 3) {\n count += (remainders[1] - n / 3);\n\n remainders[2] += (remainders[1] - n / 3);\n remainders[1] = n / 3;\n }\n if (remainders[2] > n / 3) {\n count += (remainders[2] - n / 3);\n\n remainders[0] += (remainders[2] - n / 3);\n remainders[2] = n / 3;\n }\n if (remainders[0] > n / 3) {\n count += (remainders[0] - n / 3);\n\n remainders[1] += (remainders[0] - n / 3);\n remainders[0] = n / 3;\n }\n printf(\"%d\\n\", count);\n }\n}", "rust_code": "fn solution() {\n let (stdin, stdout) = (io::stdin(), io::stdout());\n let mut sc = cf_scanner::Scanner::new(stdin.lock());\n let mut out = io::BufWriter::new(stdout.lock());\n\n let tc: usize = sc.next();\n for _ in 0..tc {\n let n: usize = sc.next();\n let mut c: Vec = vec![0, 0, 0];\n (0..n).for_each(|_| c[sc.next::() % 3] += 1);\n let x: usize = n / 3;\n let mut ans: usize = 0;\n\n for i in 0..6 {\n let a = min(c[i % 3], x);\n ans += c[i % 3] - a;\n c[(i + 1) % 3] += c[i % 3] - a;\n c[i % 3] = a;\n }\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "easy"}
{"problem_id": "0118", "problem_description": "How many ways?
\n\n\n Write a program which identifies the number of combinations of three integers which satisfy the following conditions:\n
\n\n - You should select three distinct integers from 1 to n.
\n - A total sum of the three integers is x.
\n
\n\n\n For example, there are two combinations for n = 5 and x = 9.\n
\n\n\n- 1 + 3 + 5 = 9
\n- 2 + 3 + 4 = 9
\n
\n\n\nInput
\n\n The input consists of multiple datasets. For each dataset, two integers n and x are given in a line.\n
\n\n\n The input ends with two zeros for n and x respectively. Your program should not process for these terminal symbols.\n
\n\nConstraints
\n\n - 3 ≤ n ≤ 100
\n - 0 ≤ x ≤ 300
\n
\n\nOutput
\n\n\n For each dataset, print the number of combinations in a line.\n
\n\nSample Input
\n\n\n5 9\n0 0\n
\n\nSample Output
\n\n\n2\n
\n\nNote
\n\n\n\n\n 解説
\n\t\n", "c_code": "int solution(void) {\n\n int n = 0;\n int x = 0;\n\n int cnt = 0;\n\n int i = 0;\n int j = 0;\n int k = 0;\n\n do {\n\n scanf(\"%d\", &n);\n scanf(\"%d\", &x);\n\n cnt = 0;\n\n for (i = 1; i <= n; i++) {\n for (j = i; j <= n; j++) {\n for (k = 1; k <= n; k++) {\n\n if (((i + j + k) == x) && (i < j) && (j < k)) {\n\n cnt++;\n }\n }\n }\n }\n\n if (n || x) {\n printf(\"%d\\n\", cnt);\n }\n\n } while (n || x);\n\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n\n let mut iter = line.split_whitespace().map(|i| i.parse::().unwrap());\n let n = iter.next().unwrap();\n let x = iter.next().unwrap();\n\n if n == 0 && x == 0 {\n break;\n }\n\n let combos = (1..n - 1)\n .flat_map(|a| (a + 1..n).flat_map(move |b| (b + 1..n + 1).map(move |c| (a, b, c))))\n .filter(|&(a, b, c)| a + b + c == x);\n\n println!(\"{}\", combos.count());\n }\n}", "difficulty": "medium"}
{"problem_id": "0119", "problem_description": "\nScore : 400 points
\n\n
\nProblem Statement
Give a pair of integers (A, B) such that A^5-B^5 = X.\nIt is guaranteed that there exists such a pair for the given integer X.
\n\n
\n\n
\nConstraints
\n- 1 \\leq X \\leq 10^9
\n- X is an integer.
\n- There exists a pair of integers (A, B) satisfying the condition in Problem Statement.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nX\n
\n\n
\n
\n
\nOutput
Print A and B, with space in between.\nIf there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.
\nA B\n
\n\n
\n
\n
\n\n\n
\nSample Output 1
2 -1\n
\nFor A=2 and B=-1, A^5-B^5 = 33.
\n\n
\n
\n\n\n", "c_code": "int solution() {\n long long x;\n scanf(\"%lld\", &x);\n\n for (long long a = 0; a >= 0; a++) {\n if (a < 64) {\n for (long long b = -64; b <= a; b++) {\n if (a * a * a * a * a - b * b * b * b * b == x) {\n printf(\"%lld %lld\", a, b);\n return 0;\n }\n }\n }\n if (a >= 64) {\n for (long long b = 1; b <= 2 * a; b++) {\n if (a * a * a * a * a - b * b * b * b * b == x) {\n printf(\"%lld %lld\", a, b);\n return 0;\n }\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let x: i128 = s.trim().parse().unwrap();\n\n 'outer: for i in -10000i128..=10000 {\n let mut l: i128 = -10000;\n let mut r: i128 = 10000;\n let mut m: i128;\n while l < r {\n m = (l + r) / 2;\n let t = i.pow(5) - m.pow(5);\n if t == x {\n println!(\"{} {}\", i, m);\n\n break 'outer;\n } else if m == r || m == l {\n break;\n } else if t < x {\n r = m;\n } else {\n l = m;\n }\n }\n }\n}", "difficulty": "hard"}
{"problem_id": "0120", "problem_description": "This problem is interactive.We decided to play a game with you and guess the number $$$x$$$ ($$$1 \\le x < n$$$), where you know the number $$$n$$$.You can make queries like this: + c: this command assigns $$$x = x + c$$$ ($$$1 \\le c < n$$$) and then returns you the value $$$\\lfloor\\frac{x}{n}\\rfloor$$$ ($$$x$$$ divide by $$$n$$$ and round down).You win if you guess the current number with no more than $$$10$$$ queries.", "c_code": "int solution() {\n int i;\n int n;\n int ask;\n int ans1;\n int ans2;\n int lower;\n int upper;\n int middle;\n int sum;\n scanf(\"%d\", &n);\n lower = 1;\n upper = n;\n ans1 = sum = 0;\n for (i = 0; i < 11; i++) {\n if (upper == lower + 1) {\n printf(\"! %d\\n\", lower + sum);\n fflush(stdout);\n break;\n }\n\n middle = (upper + lower) / 2;\n ask = (ans1 + 1) * n - sum - middle;\n sum += ask;\n\n printf(\"+ %d\\n\", ask);\n fflush(stdout);\n scanf(\"%d\", &ans2);\n if (ans2 == ans1 + 1) {\n lower = middle;\n } else {\n upper = middle;\n }\n ans1 = ans2;\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n = lines.next().unwrap().unwrap().parse::().unwrap();\n let mut pre_num = 0;\n let mut candinates: Vec = (1..n).collect::>();\n loop {\n if candinates.len() == 1 {\n println!(\"! {}\", candinates[0]);\n break;\n }\n let mid = candinates.len() / 2;\n let add_num = n - (candinates[mid] % n);\n println!(\"+ {}\", add_num);\n io::stdout().flush().unwrap();\n let round_num = lines.next().unwrap().unwrap().parse::().unwrap();\n let size = candinates.len();\n if round_num == pre_num {\n candinates = candinates[0..(size / 2)].to_vec();\n } else if round_num == pre_num + 1 {\n candinates = candinates[(size / 2)..].to_vec();\n pre_num = round_num;\n }\n candinates\n .iter_mut()\n .for_each(|canndinate| *canndinate += add_num);\n }\n}", "difficulty": "medium"}
{"problem_id": "0121", "problem_description": "AquaMoon has two binary sequences $$$a$$$ and $$$b$$$, which contain only $$$0$$$ and $$$1$$$. AquaMoon can perform the following two operations any number of times ($$$a_1$$$ is the first element of $$$a$$$, $$$a_2$$$ is the second element of $$$a$$$, and so on): Operation 1: if $$$a$$$ contains at least two elements, change $$$a_2$$$ to $$$\\operatorname{min}(a_1,a_2)$$$, and remove the first element of $$$a$$$. Operation 2: if $$$a$$$ contains at least two elements, change $$$a_2$$$ to $$$\\operatorname{max}(a_1,a_2)$$$, and remove the first element of $$$a$$$.Note that after a removal of the first element of $$$a$$$, the former $$$a_2$$$ becomes the first element of $$$a$$$, the former $$$a_3$$$ becomes the second element of $$$a$$$ and so on, and the length of $$$a$$$ reduces by one.Determine if AquaMoon can make $$$a$$$ equal to $$$b$$$ by using these operations.", "c_code": "int solution() {\n int t;\n int m;\n int n;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d\", &n, &m);\n char a[n + 1];\n char b[m + 1];\n scanf(\"%s\", a);\n scanf(\"%s\", b);\n int j = n - 1;\n int last_equal = 1;\n for (int i = m - 1; i > 0; i--) {\n if (b[i] != a[j--]) {\n last_equal = 0;\n break;\n }\n }\n int first_equal = 0;\n for (int i = 0; i < n - m + 1; i++) {\n if (a[i] == b[0]) {\n first_equal = 1;\n break;\n }\n }\n if (first_equal == 1 && last_equal == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let input = stdin();\n let mut line = String::new();\n let mut res: Vec = vec![];\n\n let _ = input.read_line(&mut line);\n let num: i32 = line.trim().parse().unwrap();\n for _ in 0..num {\n let mut line = String::new();\n let _ = input.read_line(&mut line);\n let mut a = String::new();\n let mut b = String::new();\n let _ = input.read_line(&mut a);\n let _ = input.read_line(&mut b);\n a = a.trim().to_string();\n b = b.trim().to_string();\n let mut condition: bool = false;\n while a.len() >= b.len() {\n if b == a {\n condition = true;\n break;\n }\n let c: char;\n if a.chars().next().unwrap() == b.chars().next().unwrap() {\n c = a.chars().next().unwrap();\n } else {\n if a.len() <= 1 {\n break;\n }\n c = a.chars().nth(1).unwrap();\n }\n if a.len() >= 2 {\n a = a[2..].to_string();\n } else if a.len() == 1 {\n a = a[1..].to_string();\n } else {\n break;\n }\n a.insert(0, c);\n }\n if condition {\n res.push(\"YES\".to_string());\n } else {\n res.push(\"NO\".to_string());\n }\n }\n for word in res {\n println!(\"{}\", word)\n }\n}", "difficulty": "hard"}
{"problem_id": "0122", "problem_description": "気象予報士 (Weather Forecaster)
\n\n問題
\n\n\nJOI 市は南北方向に H キロメートル,東西方向に W キロメートルの長方形の形をしており,H × W 個の 1 キロメートル四方の小区画に区切られている.北から i 番目,西から j 番目の小区画を (i, j) と表す.\n
\n\n\n各小区画は上空に雲があるか雲がないかのどちらかである.すべての雲は,1 分経つごとに 1 キロメートル東に移動する.今日は実に天気が良いため,JOI 市の外から JOI 市内に雲が移動してくることはない.\n
\n\n\n今,各小区画の上空に雲があるかないかがわかっている.気象予報士であるあなたは,各小区画について,今から何分後に初めてその小区画の上空に雲が来るかを予測することになった.\n
\n\n\n各小区画について,今から何分後に初めてその小区画の上空に雲が来るか求めよ.\n
\n\n入力
\n\n\n入力は 1 + H 行からなる.\n
\n\n\n1 行目には,整数 H, W (1 ≤ H ≤ 100, 1 ≤ W ≤ 100) が空白を区切りとして書かれている.これは,JOI 市が H × W 個の 1 キロメートル四方の小区画に区切られていることを表す.\n
\n\n\n続く H 行のうちの i 行目 (1 ≤ i ≤ H) には W 文字からなる文字列が書かれている.W 文字のうちの j 文字目 (1 ≤ j ≤ W) は,小区画 (i, j) の上空に,今,雲があるかどうかを表す.雲がある場合は文字 'c' (英小文字) が,雲がない場合は文字 '.' (ピリオド) が書かれている.\n
\n\n出力
\n\n出力は H 行からなり,それぞれの行は空白を区切りとした W 個の整数からなる.出力の i 行目の j 番目の整数 (1 ≤ i ≤ H, 1 ≤ j ≤ W) は,今から何分後に初めて小区画 (i, j) の上空に雲が来るかを表さなければならない.ただし,今すでに小区画 (i, j) の上空に雲がある場合は 0 を,何分経っても小区画 (i, j) の上空に雲が来ない場合は -1 を出力せよ.\n
\n\n\n出力の各行の行頭と行末には余計な空白を入れないこと.\n
\n\n入出力例
\n\n 入力例 1
\n\n \n3 4\nc..c\n..c.\n....\n
\n 出力例 1
\n \n0 1 2 0\n-1 -1 0 1\n-1 -1 -1 -1\n
\n\n 入力例 2
\n \n6 8\n.c......\n........\n.ccc..c.\n....c...\n..c.cc..\n....c...\n
\n\n 出力例 2
\n\n \n-1 0 1 2 3 4 5 6\n-1 -1 -1 -1 -1 -1 -1 -1\n-1 0 0 0 1 2 0 1\n-1 -1 -1 -1 0 1 2 3\n-1 -1 0 1 0 0 1 2\n-1 -1 -1 -1 0 1 2 3\n
\n\n\n\n\n入出力例 1 では,JOI 市は 3 × 4 個の小区画に区切られている.今の JOI 市の雲の状況は以下の通りである.図の上が北を表す.\n
\n\n\n
\n\n\n\nこの後,1 分ごとに雲は以下のように移動する.\n
\n\n
\n
\n
\n\n\n\n\n
\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n
\n
", "c_code": "int solution(void) {\n int H;\n int W;\n scanf(\"%d %d\\n\", &H, &W);\n\n char weather;\n\n int i;\n int j;\n for (i = 0; i < H; ++i) {\n scanf(\"%c\", &weather);\n\n int out = -1;\n for (j = 1; j <= W; ++j) {\n if (weather == 'c') {\n out = 0;\n }\n\n else {\n ++out;\n\n if (out < 1) {\n out = -1;\n }\n }\n\n printf(\"%d\", out);\n\n if (j == W) {\n printf(\"\\n\");\n } else {\n printf(\" \");\n }\n\n scanf(\"%c\", &weather);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let input = {\n let mut buf = vec![];\n stdin().read_to_end(&mut buf);\n unsafe { String::from_utf8_unchecked(buf) }\n };\n let mut lines = input.split('\\n');\n\n let (h, w) = {\n let line = lines.next().unwrap();\n let mut iter = line.split(' ').map(|s| s.parse::().unwrap());\n\n (iter.next().unwrap(), iter.next().unwrap())\n };\n\n let mut result = String::with_capacity(h * w * 3);\n\n for line in lines.take(h) {\n let mut cnt = -1;\n\n for ch in line.bytes() {\n if ch == b'c' {\n cnt = 0;\n result.push_str(\"0 \");\n } else {\n if cnt == -1 {\n result.push_str(\"-1 \");\n } else {\n cnt += 1;\n write!(result, \"{} \", cnt);\n }\n }\n }\n result.pop();\n result.push('\\n');\n }\n print!(\"{}\", result);\n}", "difficulty": "medium"}
{"problem_id": "0123", "problem_description": "Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into $$$n$$$ sub-tracks. You are given an array $$$a$$$ where $$$a_i$$$ represents the number of traffic cars in the $$$i$$$-th sub-track. You define the inconvenience of the track as $$$\\sum\\limits_{i=1}^{n} \\sum\\limits_{j=i+1}^{n} \\lvert a_i-a_j\\rvert$$$, where $$$|x|$$$ is the absolute value of $$$x$$$. You can perform the following operation any (possibly zero) number of times: choose a traffic car and move it from its current sub-track to any other sub-track.Find the minimum inconvenience you can achieve.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n long long a[n];\n long long sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n sum += a[i];\n }\n long long rest = sum % n;\n printf(\"%lld\\n\", rest * (n - rest) < rest * (n - 1) ? rest * (n - rest)\n : rest * (n - 1));\n }\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let in_lock = std_in.lock();\n let input = BufReader::new(in_lock);\n\n let std_out = stdout();\n let out_lock = std_out.lock();\n let mut output = BufWriter::new(out_lock);\n\n let mut lines = input.lines().map(|r| r.unwrap());\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let t = it.next().unwrap();\n\n for _case_index in 1..=t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let n = it.next().unwrap();\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let s = it.sum::();\n\n let x = s % n;\n\n let ans = x * (n - x);\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"}
{"problem_id": "0124", "problem_description": "In the $$$2022$$$ year, Mike found two binary integers $$$a$$$ and $$$b$$$ of length $$$n$$$ (both of them are written only by digits $$$0$$$ and $$$1$$$) that can have leading zeroes. In order not to forget them, he wanted to construct integer $$$d$$$ in the following way: he creates an integer $$$c$$$ as a result of bitwise summing of $$$a$$$ and $$$b$$$ without transferring carry, so $$$c$$$ may have one or more $$$2$$$-s. For example, the result of bitwise summing of $$$0110$$$ and $$$1101$$$ is $$$1211$$$ or the sum of $$$011000$$$ and $$$011000$$$ is $$$022000$$$; after that Mike replaces equal consecutive digits in $$$c$$$ by one digit, thus getting $$$d$$$. In the cases above after this operation, $$$1211$$$ becomes $$$121$$$ and $$$022000$$$ becomes $$$020$$$ (so, $$$d$$$ won't have equal consecutive digits). Unfortunately, Mike lost integer $$$a$$$ before he could calculate $$$d$$$ himself. Now, to cheer him up, you want to find any binary integer $$$a$$$ of length $$$n$$$ such that $$$d$$$ will be maximum possible as integer.Maximum possible as integer means that $$$102 > 21$$$, $$$012 < 101$$$, $$$021 = 21$$$ and so on.", "c_code": "int solution() {\n int t;\n int n;\n char b[100000];\n char a[100000];\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n scanf(\"%s\", b);\n if (n == 1) {\n printf(\"%d\\n\", 1);\n continue;\n }\n a[0] = '1';\n for (int k = 1; k < n; k++) {\n if (b[k - 1] == '1' && a[k - 1] == '1') {\n if (b[k] == '1') {\n a[k] = '0';\n } else {\n a[k] = '1';\n }\n } else if (b[k - 1] == '1' || a[k - 1] == '1') {\n if (b[k] == '0') {\n a[k] = '0';\n } else {\n a[k] = '1';\n }\n } else {\n a[k] = '1';\n }\n }\n for (int x = 0; x < n; x++) {\n printf(\"%c\", a[x]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n stdin().read_line(&mut t).unwrap();\n let t: u32 = t.trim().parse().unwrap();\n\n let mut out = BufWriter::new(stdout());\n\n for _i in 0..t {\n let mut n = String::new();\n let mut b = String::new();\n\n stdin().read_line(&mut n).unwrap();\n stdin().read_line(&mut b).unwrap();\n let b = b.trim();\n let mut a = String::new();\n\n let mut before = 3;\n for i in b.chars() {\n if i == '0' {\n if before != 1 {\n a.push('1');\n before = 1;\n } else {\n a.push('0');\n before = 0;\n }\n } else {\n if before != 2 {\n a.push('1');\n before = 2;\n } else {\n a.push('0');\n before = 1;\n }\n }\n }\n writeln!(out, \"{}\", a).unwrap();\n }\n}", "difficulty": "easy"}
{"problem_id": "0125", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
Shichi-Go-San (literally \"Seven-Five-Three\") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.
\nTakahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?
\n\n
\n\n
\nConstraints
\n- 1 ≤ X ≤ 9
\n- X is an integer.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nX\n
\n\n
\n
\n
\nOutput
If Takahashi's growth will be celebrated, print YES; if it will not, print NO.
\n\n
\n
\n
\n\n\n
\nSample Output 1
YES\n
\nThe growth of a five-year-old child will be celebrated.
\n\n
\n
\n\n\n
\nSample Output 2
NO\n
\nSee you next year.
\n
\n", "c_code": "int solution() {\n int X = 0;\n scanf(\"%d\", &X);\n if (((X / 3 == 1) && (X % 3 == 0)) || ((X / 5 == 1) && (X % 5 == 0)) ||\n ((X / 7 == 1) && (X % 7 == 0))) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut x = String::new();\n stdin().read_line(&mut x).unwrap();\n println!(\n \"{}\",\n match x.trim() {\n \"7\" | \"5\" | \"3\" => \"YES\",\n _ => \"NO\",\n }\n );\n}", "difficulty": "easy"}
{"problem_id": "0126", "problem_description": "Burenka and Tonya are playing an old Buryat game with a chip on a board of $$$n \\times m$$$ cells.At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the right and up in one move). The one who cannot make a move loses.Burenka makes the first move, the players take turns. Burenka really wants to win the game, but she is too lazy to come up with a strategy, so you are invited to solve the difficult task of finding it. Name the winner of the game (it is believed that Burenka and Tonya are masters of playing with chips, so they always move in the optimal way). Chip's starting cell is green, the only cell from which chip can't move is red. if the chip is in the yellow cell, then blue cells are all options to move the chip in one move.", "c_code": "int solution() {\n long long int t;\n long long int i;\n scanf(\"%lld\", &t);\n int n[t];\n int m[t];\n for (i = 0; i < t; i++) {\n scanf(\"%d%d\", &n[i], &m[i]);\n if ((n[i] % 2 == 0 && m[i] % 2 == 0) || (n[i] % 2 != 0 && m[i] % 2 != 0)) {\n printf(\"Tonya\\n\");\n } else {\n printf(\"Burenka\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut iterator = stdin.lock().lines();\n let line = iterator.next().unwrap().unwrap();\n let amount = line.parse::().unwrap();\n for _i in 0..amount {\n let line = iterator.next().unwrap().unwrap();\n let strings: Vec<&str> = line.split(\" \").collect();\n let sum = strings.get(1).unwrap().parse::().unwrap()\n + strings.first().unwrap().parse::().unwrap();\n if sum % 2 == 0 {\n println!(\"Tonya\");\n } else {\n println!(\"Burenka\");\n }\n }\n}", "difficulty": "easy"}
{"problem_id": "0127", "problem_description": "Heat Stroke
\n\n \nWe have had record hot temperatures this summer. To avoid heat stroke, you decided to buy a quantity of drinking water at the nearby supermarket. Two types of bottled water, 1 and 0.5 liter, are on sale at respective prices there. You have a definite quantity in your mind, but are willing to buy a quantity larger than that if: no combination of these bottles meets the quantity, or, the total price becomes lower.\n
\n\n Given the prices for each bottle of water and the total quantity needed, make a program to seek the lowest price to buy greater than or equal to the quantity required.\n
\n\nInput
\n\nThe input is given in the following format.\n
\n\n$A$ $B$ $X$\n
\n\n\n The first line provides the prices for a 1-liter bottle $A$ ($1\\leq A \\leq 1000$), 500-milliliter bottle $B$ ($1 \\leq B \\leq 1000$), and the total water quantity needed $X$ ($1 \\leq X \\leq 20000$). All these values are given as integers, and the quantity of water in milliliters.\n
\n\n\nOutput
\n\n Output the total price.\n
\n\n\nSample Input 1
\n\n180 100 2400\n
\n\nSample Output 1
\n\n460\n
\n\nSample Input 2
\n\n200 90 2018\n
\n\nSample Output 2
\n\n450\n
", "c_code": "int solution() {\n int i = 0;\n int j = 0;\n\n int A = 0;\n int B = 0;\n int X = 0;\n\n int ans = 0;\n\n int MIN = 999999;\n\n scanf(\"%d %d %d\", &A, &B, &X);\n\n for (i = 0; i <= 20000 / 1000; i++) {\n for (j = 0; j <= 20000 / 500; j++) {\n\n ans = A * i + B * j;\n if (ans < MIN && 1000 * i + 500 * j >= X) {\n MIN = ans;\n }\n }\n }\n printf(\"%d\\n\", MIN);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let mut iter = line.split_whitespace().map(|s| s.parse::().unwrap());\n\n let (a, b, x) = (\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap() as f64,\n );\n\n let amount = if a <= b {\n a * ((x / 1000.0).ceil() as u32)\n } else if a < b * 2 {\n let u = (x / 500.0).ceil() as u32;\n a * (u / 2) + b * (u % 2)\n } else {\n b * ((x / 500.0).ceil() as u32)\n };\n\n println!(\"{}\", amount);\n}", "difficulty": "easy"}
{"problem_id": "0128", "problem_description": "\n\n\nGraph
\n\n\nThere are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation.\n
\n\n\n An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \\in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \\in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$.\n
\n\n\n An adjacency-matrix representation consists of $|V| \\times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \\in E$, $a_{ij} = 0$ otherwise.\n
\n\n\n Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively.\n
\n\n\nInput
\n\n\n In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format:\n
\n\n\n$u$ $k$ $v_1$ $v_2$ ... $v_k$\n
\n\n\n\n $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$.\n
\n\nOutput
\n\n\n As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$.\n
\n\nConstraints
\n\n\n- $1 \\leq n \\leq 100$
\n
\n\n\nSample Input
\n\n4\n1 2 2 4\n2 1 4\n3 0\n4 1 3\n
\n\nSample Output
\n\n0 1 0 1\n0 0 0 1\n0 0 0 0\n0 0 1 0\n
", "c_code": "int solution(void) {\n int mp[105][105] = {0};\n int n = 0;\n scanf(\"%d\", &n);\n int i = 0;\n int j = 0;\n ;\n for (i = 0; i < n; i++) {\n int u = 0;\n int k = 0;\n scanf(\"%d%d\", &u, &k);\n int p = 0;\n int m = 0;\n for (p = 0; p < k; p++) {\n scanf(\"%d\", &m);\n mp[u - 1][m - 1] = 1;\n }\n }\n for (i = 0; i < n; i++) {\n printf(\"%d\", mp[i][0]);\n for (j = 1; j < n; j++) {\n printf(\"\\x0020%d\", mp[i][j]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let lock = stdin.lock();\n let mut lines = lock.lines();\n let n = lines.next().unwrap().unwrap().parse().unwrap();\n let mut a = vec![vec![0; n]; n];\n\n for line in lines {\n let parsed: Vec = line\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n\n let i = parsed[0];\n let adj = parsed[2..].iter();\n\n for j in adj {\n a[i - 1][j - 1] = 1;\n }\n }\n\n for line in a {\n println!(\n \"{}\",\n line.iter()\n .map(ToString::to_string)\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "medium"}
{"problem_id": "0129", "problem_description": "You are given two integers $$$x$$$ and $$$y$$$. You want to choose two strictly positive (greater than zero) integers $$$a$$$ and $$$b$$$, and then apply the following operation to $$$x$$$ exactly $$$a$$$ times: replace $$$x$$$ with $$$b \\cdot x$$$.You want to find two positive integers $$$a$$$ and $$$b$$$ such that $$$x$$$ becomes equal to $$$y$$$ after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.For example: if $$$x = 3$$$ and $$$y = 75$$$, you may choose $$$a = 2$$$ and $$$b = 5$$$, so that $$$x$$$ becomes equal to $$$3 \\cdot 5 \\cdot 5 = 75$$$; if $$$x = 100$$$ and $$$y = 100$$$, you may choose $$$a = 3$$$ and $$$b = 1$$$, so that $$$x$$$ becomes equal to $$$100 \\cdot 1 \\cdot 1 \\cdot 1 = 100$$$; if $$$x = 42$$$ and $$$y = 13$$$, there is no answer since you cannot decrease $$$x$$$ with the given operations.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int x[t];\n int y[t];\n int a[t];\n int b[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%d %d\", &x[i], &y[i]);\n if (y[i] % x[i] == 0) {\n a[i] = 1;\n b[i] = y[i] / x[i];\n } else {\n a[i] = 0;\n b[i] = 0;\n }\n }\n for (int i = 0; i < t; i++) {\n printf(\"%d %d\\n\", a[i], b[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).ok();\n let mut output = String::new();\n let mut input = input.split_ascii_whitespace().flat_map(str::parse);\n let t = input.next().unwrap();\n for _ in 0..t {\n let x = input.next().unwrap();\n let y = input.next().unwrap();\n if y % x == 0 {\n writeln!(output, \"1 {}\", y / x).ok();\n } else {\n output.push_str(\"0 0\\n\");\n }\n }\n print!(\"{output}\");\n}", "difficulty": "easy"}
{"problem_id": "0130", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.
\nEating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.\nAs he wants to live, he cannot eat one in such a situation.\nEating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.
\nFind the maximum number of tasty cookies that Takahashi can eat.
\n\n
\n\n
\nConstraints
\n- 0 \\leq A,B,C \\leq 10^9
\n- A,B and C are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nA B C\n
\n\n
\n
\n
\nOutput
Print the maximum number of tasty cookies that Takahashi can eat.
\n\n
\n
\n
\n\n
\nSample Input 1
3 1 4\n
\n\n
\n\n
\nSample Output 1
5\n
\nWe can eat all tasty cookies, in the following order:
\n\n- A tasty cookie containing poison
\n- An untasty cookie containing antidotes
\n- A tasty cookie containing poison
\n- A tasty cookie containing antidotes
\n- A tasty cookie containing poison
\n- An untasty cookie containing antidotes
\n- A tasty cookie containing poison
\n
\n\n
\n
\n\n
\nSample Input 2
5 2 9\n
\n\n
\n\n
\n\n
\nSample Input 3
8 8 1\n
\n\n
\n\n", "c_code": "int solution() {\n int A = 0;\n int B = 0;\n int C = 0;\n scanf(\"%d%d%d\", &A, &B, &C);\n if (A + B >= C) {\n printf(\"%d\", B + C);\n }\n if (A + B < C) {\n printf(\"%d\", A + (2 * B) + 1);\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).ok();\n let mut it = buf.split_whitespace();\n let a = it.next().unwrap().parse::().unwrap();\n let b = it.next().unwrap().parse::().unwrap();\n let c = it.next().unwrap().parse::().unwrap();\n println!(\"{}\", b + std::cmp::min(a + b + 1, c));\n}", "difficulty": "medium"}
{"problem_id": "0131", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
There are N balls placed in a row.\nAtCoDeer the deer is painting each of these in one of the K colors of his paint cans.\nFor aesthetic reasons, any two adjacent balls must be painted in different colors.
\nFind the number of the possible ways to paint the balls.
\n\n
\n\n
\nConstraints
\n- 1≦N≦1000
\n- 2≦K≦1000
\n- The correct answer is at most 2^{31}-1.
\n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nN K\n
\n\n
\n
\n
\nOutput
Print the number of the possible ways to paint the balls.
\n\n
\n
\n
\n\n\n
\nSample Output 1
2\n
\nWe will denote the colors by 0 and 1. There are two possible ways: we can either paint the left ball in color 0 and the right ball in color 1, or paint the left in color 1 and the right in color 0.
\n\n
\n
\n\n\n
\nSample Output 2
10\n
\nSince there is only one ball, we can use any of the ten colors to paint it. Thus, the answer is ten.
\n
\n", "c_code": "int solution() {\n\n int N = 0;\n int K = 0;\n int val = 0;\n scanf(\"%d %d\", &N, &K);\n\n if (N == 1) {\n val = K;\n } else if (N == 2) {\n val = K * (K - 1);\n } else {\n val = K * pow(K - 1, N - 1);\n }\n\n printf(\"%d\", val);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (n, k): (u32, u32) = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let nk: Vec = buf\n .split_whitespace()\n .map(|e| e.trim().parse().ok().unwrap())\n .collect();\n (nk[0], nk[1])\n };\n println!(\"{}\", k * (k - 1).pow(n - 1));\n}", "difficulty": "easy"}
{"problem_id": "0132", "problem_description": "You are given $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$, where $$$n$$$ is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold: At least $$$\\frac{n - 1}{2}$$$ of the adjacent differences $$$a_{i + 1} - a_i$$$ for $$$i = 1, 2, \\dots, n - 1$$$ are greater than or equal to $$$0$$$. At least $$$\\frac{n - 1}{2}$$$ of the adjacent differences $$$a_{i + 1} - a_i$$$ for $$$i = 1, 2, \\dots, n - 1$$$ are less than or equal to $$$0$$$. Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n int n[T];\n int **b = (int **)calloc(T, sizeof(int *));\n for (int i = 0; i < T; i++) {\n scanf(\"%d\", &n[i]);\n int a[n[i]];\n *(b + i) = (int *)calloc(n[i], sizeof(int));\n for (int j = 0; j < n[i]; j++) {\n scanf(\"%d\", &a[j]);\n a[j] = abs(a[j]);\n if (j % 2) {\n a[j] = -a[j];\n }\n }\n for (int j = 0; j < n[i]; j++) {\n b[i][j] = a[j];\n }\n }\n for (int i = 0; i < T; i++) {\n for (int j = 0; j < n[i]; j++) {\n printf(\"%d \", b[i][j]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let t: usize = lines.next().unwrap().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n for i in 0..n {\n if (i % 2 == 0 && xs[i] < 0) || (i % 2 == 1 && xs[i] > 0) {\n xs[i] *= -1;\n }\n }\n for i in 0..n {\n print!(\"{} \", xs[i]);\n }\n println!();\n }\n}", "difficulty": "medium"}
{"problem_id": "0133", "problem_description": "問題 1
\n
\n\n\n\n n 分間にわたり,\nトンネルの入口と出口で,\n1分間に通過する車の数を数えたデータがある.\nそのデータは,\n全部で n+2 行からなり,\n各行には次の内容が書かれている.\n
\n\n - 第1行目には,正整数 n が書かれており,\n 調査時間が n 分間であったことを表している.
\n - 第2行目には,正整数 m が書かれており,\n 調査開始時におけるトンネル内の車の台数が m であったことを表している.
\n - 第(2+i)行目( i = 1, 2, ... , n ) には,\n 調査開始後 (i-1) 分経過した時点から i 分経過するまでの1分間に,\n 入口を通過した車の台数と出口を通過した車の台数が\n 1つの空白で区切られて書かれている.
\n
\n\n\n調査開始後 j 分経過した時点 ( j=0, 1, 2, ... , n ) \nにおけるトンネル内の車の台数を Sj とする.\nSj の最大値を出力しなさい.\nまた,\nトンネル内の車の台数が負になることは考えられないので,\nSj が一度でも負になった場合は,\n「エラー」の意味で 0 を出力しなさい.\nただし,\nn は 10000 以下で,\nトンネルの入口および出口を1分間に通過する車の台数は 100 以下である.\n
\n\n\n\n 出力ファイルにおいては,\n出力の最後の行にも改行コードを入れること.\n
\n\n入出力例
\n\n\n入力例1
\n\n\n3\n2\n2 3\n2 3\n4 1\n
\n\n出力例1
\n\n\n3\n
\n\n
\n\n入力例2
\n\n\n3\n2\n2 3\n2 4\n4 1\n
\n\n出力例2
\n\n\n0\n
\n\n
\n\n入力例3
\n\n3\n2\n2 3\n2 3\n1 0\n
\n\n出力例3
\n\n\n2\n
\n\n\n\n
\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n int i;\n scanf(\"%d%d\", &n, &m);\n int MAX = m;\n int a[2][n];\n for (i = 0; i < n; i++) {\n scanf(\"%d%d\", &a[0][i], &a[1][i]);\n }\n for (i = 0; i < n; i++) {\n m = m + a[0][i] - a[1][i];\n if (m > MAX) {\n MAX = m;\n }\n if (m < 0) {\n MAX = 0;\n break;\n }\n }\n printf(\"%d\\n\", MAX);\n return 0;\n}", "rust_code": "fn solution() {\n let input = {\n let mut buf = vec![];\n stdin().read_to_end(&mut buf);\n unsafe { String::from_utf8_unchecked(buf) }\n };\n let mut lines = input.split('\\n');\n\n let n = lines.next().unwrap().parse().unwrap();\n let mut m = lines.next().unwrap().parse().unwrap();\n\n let mut max = m;\n\n for line in lines.take(n) {\n let mut iter = line.split(' ').map(|s| s.parse().unwrap());\n let (i, o) = (iter.next().unwrap(), iter.next().unwrap());\n\n let (s, err) = usize::overflowing_sub(m + i, o);\n if err {\n max = 0;\n break;\n }\n m = s;\n if max < s {\n max = s;\n }\n }\n\n println!(\"{}\", max);\n}", "difficulty": "medium"}
{"problem_id": "0134", "problem_description": "When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a $$$2 \\times n$$$ grid where each row is a permutation of the numbers $$$1,2,3,\\ldots,n$$$.The goal of Little Alawn's puzzle is to make sure no numbers on the same column or row are the same (we'll call this state of the puzzle as solved), and to achieve this he is able to swap the numbers in any column. However, after solving the puzzle many times, Little Alawn got bored and began wondering about the number of possible solved configurations of the puzzle he could achieve from an initial solved configuration only by swapping numbers in a column.Unfortunately, Little Alawn got stuck while trying to solve this harder problem, so he was wondering if you could help him with it. Find the answer modulo $$$10^9+7$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int i;\n int N = 200001;\n long long int MOD = 1000000007;\n long long int A[N];\n A[0] = 1;\n for (i = 1; i < N; i++) {\n A[i] = (2 * A[i - 1]) % MOD;\n }\n while (t > 0) {\n int n;\n int i;\n int j;\n int k;\n int count = 0;\n scanf(\"%d\", &n);\n int a[n];\n int b[n + 1];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n j = a[i];\n scanf(\"%d\", &b[j]);\n }\n b[0] = 0;\n for (i = 0; i <= n; i++) {\n if (b[i] != 0) {\n j = i;\n k = b[j];\n while (b[k] != i) {\n b[j] = 0;\n j = k;\n k = b[k];\n }\n b[j] = b[k] = 0;\n count++;\n }\n }\n printf(\"%lld\\n\", A[count]);\n t--;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let in_lock = std_in.lock();\n let input = BufReader::new(in_lock);\n\n let std_out = stdout();\n let out_lock = std_out.lock();\n let mut output = BufWriter::new(out_lock);\n\n let mut lines = input.lines().map(|r| r.unwrap());\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let t = it.next().unwrap();\n\n 'cases: for _case_index in 1..=t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let n = it.next().unwrap();\n\n let p = 1_000_000_007;\n\n let mut a = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n let it = s\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .map(|x| (x, 0, false));\n a.extend(it);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n for (x, y) in it.zip(a.iter_mut()) {\n y.1 = x;\n }\n\n a.sort();\n\n let mut c = 0u64;\n\n for i in 0..n {\n if a[i].2 {\n continue;\n }\n c += 1;\n let mut j = a[i].1 - 1;\n while !a[j].2 {\n a[j].2 = true;\n j = a[j].1 - 1;\n }\n }\n\n let ans = {\n let mut r = 1;\n let mut x = 2u64;\n while c > 0 {\n if c % 2 == 1 {\n r = (r * x) % p;\n }\n c /= 2;\n x = (x * x) % p;\n }\n r\n };\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"}
{"problem_id": "0135", "problem_description": "You want to perform the combo on your opponent in one popular fighting game. The combo is the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in $$$s$$$. I.e. if $$$s=$$$\"abca\" then you have to press 'a', then 'b', 'c' and 'a' again.You know that you will spend $$$m$$$ wrong tries to perform the combo and during the $$$i$$$-th try you will make a mistake right after $$$p_i$$$-th button ($$$1 \\le p_i < n$$$) (i.e. you will press first $$$p_i$$$ buttons right and start performing the combo from the beginning). It is guaranteed that during the $$$m+1$$$-th try you press all buttons right and finally perform the combo.I.e. if $$$s=$$$\"abca\", $$$m=2$$$ and $$$p = [1, 3]$$$ then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.Your task is to calculate for each button (letter) the number of times you'll press it.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n char s[n + 1];\n scanf(\"%s\", s);\n int p[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &p[i]);\n p[i]--;\n }\n int counter[26];\n int ctr[n];\n for (int i = 0; i < 26; i++) {\n counter[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n ctr[i] = 0;\n }\n for (int i = 0; i < m; i++) {\n ctr[p[i]]++;\n }\n for (int i = 0; s[i] != '\\0'; i++) {\n counter[s[i] - 'a']++;\n }\n int c = 0;\n for (int i = n - 1; i >= 0; i--) {\n c += ctr[i];\n counter[s[i] - 'a'] += c;\n }\n for (int i = 0; i < 26; i++) {\n printf(\"%d \", counter[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n {\n let stdin = io::stdin();\n let mut handle = stdin.lock();\n handle.read_to_string(&mut buffer).unwrap();\n }\n\n let arr = buffer.split_whitespace().collect::>();\n\n let mut it = arr.iter();\n let t = it.next().unwrap().parse::().unwrap();\n for _ in 0..t {\n let n = it.next().unwrap().parse::().unwrap();\n let m = it.next().unwrap().parse::().unwrap();\n let s = *it.next().unwrap();\n let ps = (0..m)\n .map(|_| it.next().unwrap().parse::().unwrap() - 1)\n .collect::>();\n\n let mut count = vec![[0usize; 26]; n];\n let ss = s\n .chars()\n .map(|x| x as usize - 'a' as usize)\n .collect::>();\n for (idx, &v) in ss.iter().enumerate() {\n count[idx][v] += 1;\n }\n\n for idx in 1..n {\n for v in 0..26 {\n count[idx][v] += count[idx - 1][v]\n }\n }\n\n let mut sum = [0; 26];\n for &i in ps.iter() {\n for v in 0..26 {\n sum[v] += count[i][v];\n }\n }\n for &i in ss.iter() {\n sum[i] += 1;\n }\n for &i in sum.iter() {\n print!(\"{} \", i);\n }\n println!();\n }\n}", "difficulty": "easy"}
{"problem_id": "0136", "problem_description": "\nScore : 400 points
\n\n
\nProblem Statement
Joisino is planning to record N TV programs with recorders.
\nThe TV can receive C channels numbered 1 through C.
\nThe i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.
\nHere, there will never be more than one program that are broadcast on the same channel at the same time.
\nWhen the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).
\nFind the minimum number of recorders required to record the channels so that all the N programs are completely recorded.
\n\n
\n\n
\nConstraints
\n- 1≤N≤10^5
\n- 1≤C≤30
\n- 1≤s_i<t_i≤10^5
\n- 1≤c_i≤C
\n- If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.
\n- All input values are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN C\ns_1 t_1 c_1\n:\ns_N t_N c_N\n
\n\n
\n
\n
\nOutput
When the minimum required number of recorders is x, print the value of x.
\n\n
\n
\n
\n\n
\nSample Input 1
3 2\n1 7 2\n7 8 1\n8 12 1\n
\n\n
\n\n
\nSample Output 1
2\n
\nTwo recorders can record all the programs, for example, as follows:
\n\n- With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.
\n- With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.
\n
\n\n
\n
\n\n
\nSample Input 2
3 4\n1 3 2\n3 4 4\n1 4 3\n
\n\n
\n\n
\nSample Output 2
3\n
\nThere may be a channel where there is no program to record.
\n\n
\n
\n\n
\nSample Input 3
9 4\n56 60 4\n33 37 2\n89 90 3\n32 43 1\n67 68 3\n49 51 3\n31 32 3\n70 71 1\n11 12 3\n
\n\n
\n\n", "c_code": "int solution() {\n int n;\n int C;\n int s[100000];\n int t[100000];\n int c[100000];\n\n scanf(\"%d %d\", &n, &C);\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d %d\", &s[i], &t[i], &c[i]);\n }\n int m = 100001;\n int T[31][100001];\n for (int i = 0; i < C + 1; i++) {\n for (int j = 0; j < m; j++) {\n T[i][j] = 0;\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = s[i]; j <= t[i]; j++) {\n if (T[c[i]][j] == 0) {\n T[c[i]][j] = 1;\n }\n }\n }\n int x = 0;\n for (int i = 0; i < m; i++) {\n int y = 0;\n for (int j = 0; j < C + 1; j++) {\n y = y + T[j][i];\n }\n if (x < y) {\n x = y;\n }\n }\n printf(\"%d\\n\", x);\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n stdin().read_to_string(&mut s).ok();\n let mut itr = s.split_whitespace();\n let n: usize = itr.next().unwrap().parse().unwrap();\n let c: usize = itr.next().unwrap().parse().unwrap();\n\n let mut event = Vec::new();\n for _ in 0..n {\n let s: usize = itr.next().unwrap().parse().unwrap();\n let t: usize = itr.next().unwrap().parse().unwrap();\n let a: usize = itr.next().unwrap().parse().unwrap();\n event.push((s, 0, a));\n event.push((t, 1, a));\n }\n event.sort();\n\n let mut ans = 0;\n let mut channel = vec![0; c + 1];\n for &x in event.iter() {\n let typ = x.1;\n let chn = x.2;\n if typ == 0 {\n channel[chn] += 1;\n } else {\n channel[chn] -= 1;\n }\n\n ans = max(\n ans,\n (1..c + 1).fold(0, |acc, x| if channel[x] > 0 { acc + 1 } else { acc }),\n );\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"}
{"problem_id": "0137", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
Joisino wants to evaluate the formula \"A op B\".\nHere, A and B are integers, and the binary operator op is either + or -.\nYour task is to evaluate the formula instead of her.
\n\n
\n\n
\nConstraints
\n- 1≦A,B≦10^9
\n- op is either
+ or -. \n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nA op B\n
\n\n
\n
\n
\nOutput
Evaluate the formula and print the result.
\n\n
\n
\n
\n\n
\nSample Input 1
1 + 2\n
\n\n
\n\n
\nSample Output 1
3\n
\nSince 1 + 2 = 3, the output should be 3.
\n\n
\n
\n\n
\nSample Input 2
5 - 7\n
\n\n
\n\n", "c_code": "int solution() {\n int *a = malloc(sizeof(int));\n int *b = malloc(sizeof(int));\n char *op = malloc(sizeof(char));\n scanf(\"%d\", a);\n scanf(\" %c[^\"\n \"]\",\n op);\n scanf(\"%d\", b);\n if (*op == '+') {\n printf(\"%d\\n\", *a + *b);\n }\n if (*op == '-') {\n printf(\"%d\\n\", *a - *b);\n }\n free(a);\n free(b);\n free(op);\n}", "rust_code": "fn solution() {\n let mut input_line: String = String::new();\n io::stdin().read_line(&mut input_line).expect(\"err\");\n\n let inputs: Vec<&str> = input_line.split_whitespace().collect();\n\n let a: i64 = inputs[0].parse::().unwrap();\n let b: i64 = inputs[2].parse::().unwrap();\n let operand = inputs[1];\n\n if operand == \"+\" {\n print!(\"{}\", a + b);\n } else if operand == \"-\" {\n print!(\"{}\", a - b);\n }\n}", "difficulty": "easy"}
{"problem_id": "0138", "problem_description": "Santa has to send presents to the kids. He has a large stack of $$$n$$$ presents, numbered from $$$1$$$ to $$$n$$$; the topmost present has number $$$a_1$$$, the next present is $$$a_2$$$, and so on; the bottom present has number $$$a_n$$$. All numbers are distinct.Santa has a list of $$$m$$$ distinct presents he has to send: $$$b_1$$$, $$$b_2$$$, ..., $$$b_m$$$. He will send them in the order they appear in the list.To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $$$k$$$ presents above the present Santa wants to send, it takes him $$$2k + 1$$$ seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.Your program has to answer $$$t$$$ different test cases.", "c_code": "int solution() {\n int t;\n int n;\n int m;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d\", &n, &m);\n int a[n];\n int b[m];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &b[i]);\n }\n int c[n + 1];\n for (int i = 0; i < n; i++) {\n c[a[i]] = i;\n }\n long long int sum = 0;\n int count = 0;\n int max = c[b[0]];\n sum = c[b[0]] * 2 + 1;\n\n for (int i = 1; i < m; i++) {\n if (c[b[i]] > max) {\n max = c[b[i]];\n count++;\n sum += (c[b[i]] - count) * 2 + 1;\n } else {\n sum += 1;\n count++;\n }\n }\n printf(\"%lld\\n\", sum);\n }\n return 0;\n}", "rust_code": "fn solution() {\n ioset! { inp, buf }\n input! { inp, t: usize, }\n for _ in 0..t {\n input! { inp, n: usize, m: usize, a: [usize; n], b: [usize; m], }\n let mut MAX = 0i64;\n let mut idx = vec![0i64; n];\n for i in 0..n {\n idx[a[i] - 1] = i as i64;\n }\n let mut ans = 0i64;\n for i in 0..m {\n if idx[b[i] - 1] <= MAX {\n ans += 1;\n } else {\n MAX = idx[b[i] - 1];\n ans += (idx[b[i] - 1] - i as i64) * 2 + 1;\n }\n }\n output! { buf, \"{}\\n\", ans }\n }\n}", "difficulty": "easy"}
{"problem_id": "0139", "problem_description": "\nScore : 300 points
\n\n
\nProblem Statement
You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.\nDetermine if we can repeat the following operation zero or more times so that the sequences a and b become equal.
\nOperation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:
\n\n- Add 2 to a_i.
\n- Add 1 to b_j.
\n
\n\n
\n\n
\nConstraints
\n- 1 ≤ N ≤ 10 000
\n- 0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)
\n- All input values are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\na_1 a_2 .. a_N\nb_1 b_2 .. b_N\n
\n\n
\n
\n
\nOutput
If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.
\n\n
\n
\n
\n\n
\nSample Input 1
3\n1 2 3\n5 2 2\n
\n\n
\n\n
\nSample Output 1
Yes\n
\nFor example, we can perform three operations as follows to do our job:
\n\n- First operation: i=1 and j=2. Now we have a = \\{3,2,3\\}, b = \\{5,3,2\\}.
\n- Second operation: i=1 and j=2. Now we have a = \\{5,2,3\\}, b = \\{5,4,2\\}.
\n- Third operation: i=2 and j=3. Now we have a = \\{5,4,3\\}, b = \\{5,4,3\\}.
\n
\n\n
\n
\n\n
\nSample Input 2
5\n3 1 4 1 5\n2 7 1 8 2\n
\n\n
\n\n
\n\n
\nSample Input 3
5\n2 7 1 8 2\n3 1 4 1 5\n
\n\n
\n\n", "c_code": "int solution() {\n long long N;\n scanf(\"%lld\", &N);\n long long a[N];\n long long b[N];\n long long a_sum = 0;\n long long b_sum = 0;\n long long ap = 0;\n long long bp = 0;\n for (long long i = 0; i < N; i++) {\n scanf(\"%lld\", &a[i]);\n a_sum += a[i];\n }\n for (long long i = 0; i < N; i++) {\n scanf(\"%lld\", &b[i]);\n b_sum += b[i];\n }\n long long dif = b_sum - a_sum;\n for (long long i = 0; i < N; i++) {\n if (a[i] >= b[i]) {\n bp += a[i] - b[i];\n } else {\n ap += (b[i] - a[i] + 1) / 2;\n }\n }\n if (ap <= dif && bp <= dif) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n std::io::stdin().read_to_string(&mut s).ok();\n let mut itr = s.split_whitespace();\n\n let n: usize = itr.next().unwrap().parse().unwrap();\n let a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n let b: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut da = 0i64;\n let mut db = 0i64;\n for i in 0..n {\n if a[i] > b[i] {\n db += a[i] - b[i];\n }\n if a[i] < b[i] {\n da += (b[i] - a[i]) / 2;\n }\n }\n if db <= da {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"}
{"problem_id": "0140", "problem_description": "Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$. 2. Shoot the cannon in the row $$$2$$$. 3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n char a[n + 1][n + 1];\n char s = 0;\n getchar();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n scanf(\"%c\", &a[i][j]);\n }\n getchar();\n }\n\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - 1; j++) {\n if (a[i][j] == '1' && a[i + 1][j] == '0' && a[i][j + 1] == '0') {\n s = 1;\n break;\n }\n }\n }\n if (!s) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n std::io::stdin().read_to_string(&mut s).ok();\n let mut itr = s.split_whitespace();\n let t: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n for _ in 0..t {\n let n: usize = itr.next().unwrap().parse().unwrap();\n let grid: Vec> = (0..n)\n .map(|_| itr.next().unwrap().chars().collect())\n .collect();\n\n let mut ok = true;\n for i in 0..n - 1 {\n for j in 0..n - 1 {\n if grid[i][j] == '1' && grid[i][j + 1] == '0' && grid[i + 1][j] == '0' {\n ok = false;\n break;\n }\n }\n if !ok {\n break;\n }\n }\n writeln!(out, \"{}\", if ok { \"YES\" } else { \"NO\" }).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"}
{"problem_id": "0141", "problem_description": "Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.The definition of a rooted tree can be found here.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[10009];\n int ct[1005];\n n -= 1;\n int ch[1005];\n for (int i = 0; i < n; i++) {\n ch[i] = 0;\n }\n for (int i = 0; i < 1005; i++) {\n ct[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n ct[a[i]]++;\n }\n for (int j = 0; j < n; j++) {\n if (ch[a[j]] != 1 && a[j] > 1) {\n ct[a[a[j] - 2]]--;\n ch[a[j]] = 1;\n }\n }\n\n for (int i = 0; i < n; i++) {\n if (ct[a[i]] < 3) {\n printf(\"No\\n\");\n return 0;\n }\n }\n printf(\"Yes\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = String::new();\n io::stdin().read_to_string(&mut stdin).unwrap();\n let mut stdin = stdin.split_whitespace();\n let mut get = || stdin.next().unwrap();\n\n\n let n = get!(usize);\n\n let mut tree = vec![vec![]; n + 2];\n\n for i in 0..n - 1 {\n let v = i + 2;\n let p = get!(usize);\n tree[p].push(v);\n }\n\n for children in &tree[1..] {\n if children.is_empty() {\n continue;\n }\n let mut leaf = 0;\n for &child in children {\n if tree[child].is_empty() {\n leaf += 1;\n }\n }\n if leaf < 3 {\n println!(\"No\");\n return;\n }\n }\n println!(\"Yes\");\n}", "difficulty": "medium"}
{"problem_id": "0142", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)
\nWrite a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.
\n\n
\n\n
\nConstraints
\n- S is a string that represents a valid date in the year 2019 in the
yyyy/mm/dd format. \n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nS\n
\n\n
\n
\n
\nOutput
Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.
\n\n
\n
\n
\n\n
\nSample Input 1
2019/04/30\n
\n\n
\n\n
\nSample Output 1
Heisei\n
\n\n
\n
\n\n
\nSample Input 2
2019/11/01\n
\n\n
\n\n", "c_code": "int solution() {\n char str[12];\n\n scanf(\"%s\", str);\n\n if (str[5] == '0' && str[6] <= '4' &&\n ((str[8] < '3') || (str[8] == '3' && str[9] == '0'))) {\n\n printf(\"Heisei\");\n }\n\n else {\n printf(\"TBD\");\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let s: Vec = s.trim().split('/').flat_map(str::parse).collect();\n let y = s[0];\n let m = s[1];\n let _d = s[2];\n if y > 2019 || (y == 2019 && m >= 5) {\n println!(\"TBD\");\n } else {\n println!(\"Heisei\");\n }\n}", "difficulty": "medium"}
{"problem_id": "0143", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.
\nIn this country, 24-hour clock is used. For example, 9:00 p.m. is referred to as \"21 o'clock\".
\nThe current time is A o'clock, and a contest will begin in exactly B hours.\nWhen will the contest begin? Answer in 24-hour time.
\n\n
\n\n
\nConstraints
\n- 0 \\leq A,B \\leq 23
\n- A and B are integers.
\n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nA B\n
\n\n
\n
\n
\nOutput
Print the hour of the starting time of the contest in 24-hour time.
\n\n
\n
\n
\n\n\n
\nSample Output 1
21\n
\nIn this input, the current time is 9 o'clock, and 12 hours later it will be 21 o'clock in 24-hour time.
\n\n
\n
\n\n\n
\nSample Output 2
19\n
\nThe contest has just started.
\n\n
\n
\n\n\n
\nSample Output 3
1\n
\nThe contest will begin at 1 o'clock the next day.
\n
\n", "c_code": "int solution() {\n\n int sum = 0;\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n sum = a + b;\n if (sum > 23) {\n printf(\"%d\", sum - 24);\n } else {\n printf(\"%d\", sum);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n std::io::stdin()\n .read_line(&mut line)\n .expect(\"faild to read line\");\n let mut sum: u64 = 0;\n for elem in line.split_whitespace() {\n sum += elem.trim().parse::().unwrap();\n }\n println!(\"{}\", sum % 24);\n}", "difficulty": "medium"}
{"problem_id": "0144", "problem_description": "\nScore: 200 points
\n\n
\nProblem Statement
\nM-kun has the following three cards:
\n\n- A red card with the integer A.
\n- A green card with the integer B.
\n- A blue card with the integer C.
\n
\nHe is a genius magician who can do the following operation at most K times:
\n\n- Choose one of the three cards and multiply the written integer by 2.
\n
\nHis magic is successful if both of the following conditions are satisfied after the operations:
\n\n- The integer on the green card is strictly greater than the integer on the red card.
\n- The integer on the blue card is strictly greater than the integer on the green card.
\n
\nDetermine whether the magic can be successful.
\n\n
\n\n
\nConstraints
\n\n- 1 \\leq A, B, C \\leq 7
\n- 1 \\leq K \\leq 7
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
\nInput is given from Standard Input in the following format:
\nA B C\nK\n
\n\n
\n
\n
\nOutput
\nIf the magic can be successful, print Yes; otherwise, print No.
\n\n
\n
\n
\n\n
\nSample Input 1
7 2 5\n3\n
\n\n
\n\n
\nSample Output 1
Yes\n
\nThe magic will be successful if, for example, he does the following operations:
\n\n- First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.
\n- Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.
\n- Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.
\n
\n\n
\n
\n\n
\nSample Input 2
7 4 2\n3\n
\n\n
\n\n
\nSample Output 2
No\n
\nHe has no way to succeed in the magic with at most three operations.
\n
\n", "c_code": "int solution() {\n int a[3];\n int k;\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n scanf(\"%d\", &k);\n for (int i = 0; i < k; i++) {\n if (a[0] >= a[1]) {\n a[1] = a[1] * 2;\n } else {\n a[2] = a[2] * 2;\n }\n }\n if (a[0] < a[1] && a[1] < a[2]) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (a, mut b, mut c): (usize, usize, usize) = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n (\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n )\n };\n let k: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n\n let mut count = 0;\n while a >= b {\n b *= 2;\n count += 1;\n }\n while b >= c {\n c *= 2;\n count += 1;\n }\n\n if count <= k {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"}
{"problem_id": "0145", "problem_description": "Little C loves number «3» very much. He loves all things about it.Now he has a positive integer $$$n$$$. He wants to split $$$n$$$ into $$$3$$$ positive integers $$$a,b,c$$$, such that $$$a+b+c=n$$$ and none of the $$$3$$$ integers is a multiple of $$$3$$$. Help him to find a solution.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int p = n / 3;\n if (p % 3 != 0 && (n - (2 * p)) % 3 != 0) {\n printf(\"%d %d %d\", p, p, (n - (2 * p)));\n } else if (n % 3 == 0) {\n printf(\"%d %d %d\", p + 1, p + 1, p - 2);\n } else if (p % 3 != 0 && (n - 2 * p) % 3 == 0) {\n if (p % 3 == 1) {\n printf(\"%d %d %d\", p + 1, p + 1, (n - 2 * p) - 2);\n } else {\n printf(\"%d %d %d\", p - 1, p - 1, (n - 2 * p) + 2);\n }\n } else if (p % 3 == 0 && (n - 2 * p) != 0) {\n printf(\"%d %d %d\", p - 1, p + 1, n - (2 * p));\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let stdin = io::stdin();\n let mut handle = stdin.lock();\n handle.read_to_string(&mut buffer).unwrap();\n\n let arr: Vec<_> = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let n = arr[0];\n let a = n / 3;\n let mut b = [a + n % 3, a, a];\n for i in 0..3 {\n if b[i] % 3 == 0 {\n if i < 2 {\n b[i] -= 1;\n b[i + 1] += 1;\n } else {\n b[2] += 1;\n b[0] -= 1;\n }\n }\n }\n println!(\"{} {} {}\", b[0], b[1], b[2]);\n}", "difficulty": "medium"}
{"problem_id": "0146", "problem_description": "\nScore : 100 points
\n\n
\nProblem Statement
You are given two integers a and b.\nDetermine if a+b=15 or a\\times b=15 or neither holds.
\nNote that a+b=15 and a\\times b=15 do not hold at the same time.
\n\n
\n\n
\nConstraints
\n- 1 \\leq a,b \\leq 15
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\na b\n
\n\n
\n
\n
\nOutput
If a+b=15, print +;\nif a\\times b=15, print *;\nif neither holds, print x.
\n\n
\n
\n
\n\n\n
\nSample Output 1
+\n
\n4+11=15.
\n\n
\n
\n\n\n
\nSample Output 2
*\n
\n3\\times 5=15.
\n\n
\n
\n\n\n
\nSample Output 3
x\n
\n1+1=2 and 1\\times 1=1, neither of which is 15.
\n
\n", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n\n scanf(\"%d %d\", &a, &b);\n\n if (a + b == 15) {\n printf(\"+\\n\");\n } else if (a * b == 15) {\n printf(\"*\\n\");\n } else {\n printf(\"x\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut ab = String::new();\n std::io::stdin().read_line(&mut ab).ok();\n let mut it = ab.split_whitespace().map(|n| usize::from_str(n).unwrap());\n let (a, b) = (it.next().unwrap(), it.next().unwrap());\n if a + b == 15 {\n println!(\"+\");\n } else if a * b == 15 {\n println!(\"*\");\n } else {\n println!(\"x\");\n }\n}", "difficulty": "easy"}
{"problem_id": "0147", "problem_description": "\n\n\nRing
\n\n\n Write a program which finds a pattern $p$ in a ring shaped text $s$.\n
\n\n\n
\n\n
\n\n
\n\nInput
\n\n In the first line, the text $s$ is given.
\n In the second line, the pattern $p$ is given.\n
\n\nOutput
\n\n\n If $p$ is in $s$, print Yes in a line, otherwise No.\n
\n\nConstraints
\n\n\n- $1 \\leq $ length of $p \\leq $ length of $s \\leq 100$
\n- $s$ and $p$ consists of lower-case letters
\n
\n\nSample Input 1
\n\nvanceknowledgetoad\nadvance\n
\n\nSample Output 1
\n\n\nYes\n
\n
\nSample Input 2
\n\nvanceknowledgetoad\nadvanced\n
\n\nSample Output 2
\n\nNo\n
", "c_code": "int solution() {\n char a[105];\n char b[105];\n int i = 0;\n int j = 0;\n do {\n scanf(\"%c\", &a[i++]);\n } while (a[i - 1] != '\\n');\n\n do {\n scanf(\"%c\", &b[j++]);\n } while (b[j - 1] != '\\n');\n\n i--;\n j--;\n\n int flag = 0;\n for (int k = 0; k < i; k++) {\n if (a[k] == b[0]) {\n flag = 0;\n for (int p = k, q = 0; q < j; p++, q++) {\n if (p == i) {\n p = 0;\n }\n if (a[p] != b[q]) {\n flag = 1;\n\n break;\n }\n }\n if (flag == 0) {\n printf(\"Yes\\n\");\n return 0;\n }\n }\n }\n printf(\"No\\n\");\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n\n let mut line2 = String::new();\n let _ = scan.read_line(&mut line2);\n\n let s = [line.trim(), line.trim()].concat();\n\n if !s.contains(line2.trim()) {\n println!(\"No\");\n } else {\n println!(\"Yes\");\n }\n}", "difficulty": "medium"}
{"problem_id": "0148", "problem_description": "The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps.", "c_code": "int solution() {\n int testcase;\n scanf(\"%d\", &testcase);\n\n while (testcase--) {\n long long x;\n long long n;\n scanf(\"%lld%lld\", &x, &n);\n\n long long remainder = n % 4;\n long long cycle = (n / 4) + 1;\n if (x % 2 == 0) {\n switch (remainder) {\n case 0:\n x = x;\n break;\n case 1:\n x = x - n;\n break;\n case 2:\n x = x + 1;\n break;\n case 3:\n x = x + 4 * cycle;\n break;\n }\n } else {\n switch (remainder) {\n case 0:\n x = x;\n break;\n case 1:\n x = x + n;\n break;\n case 2:\n x = x - 1;\n break;\n case 3:\n x = x - 4 * cycle;\n break;\n }\n }\n\n printf(\"%lld\\n\", x);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n for _ in 0..(line.trim().parse().unwrap()) {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let nums: Vec = line.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n let mut ans: i64 = nums[0];\n for i in nums[1] / 4 * 4 + 1..=nums[1] {\n if ans % 2 == 0 {\n ans -= i;\n } else {\n ans += i;\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"}
{"problem_id": "0149", "problem_description": "\nScore : 600 points
\n\n
\nProblem Statement
We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller.
\n\n- Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.
\n
\nIt can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.
\nYou are given an integer K. Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.
\n\n
\n\n
\nConstraints
\n- 0 ≤ K ≤ 50 \\times 10^{16}
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nK\n
\n\n
\n
\n
\nOutput
Print a solution in the following format:
\nN\na_1 a_2 ... a_N\n
\nHere, 2 ≤ N ≤ 50 and 0 ≤ a_i ≤ 10^{16} + 1000 must hold.
\n\n
\n
\n
\n\n\n
\nSample Output 1
4\n3 3 3 3\n
\n\n
\n
\n\n\n
\nSample Output 2
3\n1 0 3\n
\n\n
\n
\n\n\n
\nSample Output 3
2\n2 2\n
\nThe operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].
\n\n
\n
\n\n\n
\nSample Output 4
7\n27 0 0 0 0 0 0\n
\n\n
\n
\n\n
\nSample Input 5
1234567894848\n
\n\n
\n\n
\nSample Output 5
10\n1000 193 256 777 0 1 1192 1234567891011 48 425\n
\n
\n", "c_code": "int solution() {\n long long n;\n long long a;\n long long i;\n scanf(\"%lld\", &n);\n a = n / 50;\n printf(\"50\\n\");\n if (a == 0) {\n for (i = 0; i < 50; i++) {\n if (i) {\n printf(\" \");\n }\n if (i < n) {\n printf(\"50\");\n } else {\n printf(\"0\");\n }\n }\n } else {\n for (i = 0; i < 50; i++) {\n if (i) {\n printf(\" \");\n }\n if (i < n % 50) {\n printf(\"%lld\", a + 50);\n } else {\n printf(\"%lld\", a + 49 - (n % 50));\n }\n }\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n input!(k: usize);\n let n = 50;\n let mut b = vec![k / n; n];\n for i in 0..(k % n) {\n b[i] += 1;\n }\n\n assert_eq!(b.iter().sum::(), k);\n let mut upper_a = vec![0; n];\n let mut lower_a = vec![0; n];\n for i in 0..n {\n lower_a[i] = ((n + 1) * b[i]).saturating_sub(k);\n upper_a[i] = (n + 1) * b[i] + (n - 1) - k;\n }\n println!(\"{}\", n);\n for i in 0..n {\n if i > 0 {\n print!(\" \");\n }\n print!(\"{}\", upper_a[i]);\n }\n println!();\n}", "difficulty": "medium"}
{"problem_id": "0150", "problem_description": "\nScore: 300 points
\n\n
\nProblem Statement
\nIn 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!
\nThere are five means of transport in this empire:
\n\n- Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.
\n- Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.
\n- Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.
\n- Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.
\n- Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.
\n
\nFor each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).
\nThere is a group of N people at City 1, and they all want to go to City 6.
\nAt least how long does it take for all of them to reach there? \nYou can ignore the time needed to transfer.
\n\n
\n\n
\nConstraints
\n\n- 1 \\leq N, A, B, C, D, E \\leq 10^{15}
\n- All values in input are integers.
\n
\n\n
\n
\n\n
\n
\nInput
\nInput is given from Standard Input in the following format:
\nN\nA\nB\nC\nD\nE\n
\n\n
\n
\n
\nOutput
\nPrint the minimum time required for all of the people to reach City 6, in minutes.
\n\n
\n
\n
\n\n
\nSample Input 1
5\n3\n2\n4\n3\n5\n
\n\n
\n\n
\nSample Output 1
7\n
\nOne possible way to travel is as follows.\nFirst, there are N = 5 people at City 1, as shown in the following image:
\n
\nIn the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.
\n
\nIn the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.
\n
\nIn the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.
\n
\nFrom then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.
\nThere is no way for them to reach City 6 in 6 minutes or less.
\n\n
\n
\n\n
\nSample Input 2
10\n123\n123\n123\n123\n123\n
\n\n
\n\n
\nSample Output 2
5\n
\nAll kinds of vehicles can occupy N = 10 people at a time.\nThus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.
\n\n
\n
\n\n
\nSample Input 3
10000000007\n2\n3\n5\n7\n11\n
\n\n
\n\n
\nSample Output 3
5000000008\n
\nNote that the input or output may not fit into a 32-bit integer type.
\n
\n", "c_code": "int solution() {\n long long int n;\n long long int vehicle[5];\n scanf(\"%lld %lld %lld %lld %lld %lld\", &n, &vehicle[0], &vehicle[1],\n &vehicle[2], &vehicle[3], &vehicle[4]);\n long long int min = LLONG_MAX;\n for (int i = 0; i < 5; i++) {\n if (min > vehicle[i]) {\n min = vehicle[i];\n }\n }\n printf(\"%lld\", ((n + (min - 1)) / min) + 4);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n std::io::stdin().read_to_string(&mut s).ok();\n let mut itr = s.split_whitespace();\n let n: usize = itr.next().unwrap().parse().unwrap();\n let mut a: Vec = (0..5)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n a.sort();\n println!(\"{}\", 5 + n.div_ceil(a[0]) - 1);\n}", "difficulty": "hard"}
{"problem_id": "0151", "problem_description": "You are given three positive (i.e. strictly greater than zero) integers $$$x$$$, $$$y$$$ and $$$z$$$.Your task is to find positive integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$x = \\max(a, b)$$$, $$$y = \\max(a, c)$$$ and $$$z = \\max(b, c)$$$, or determine that it is impossible to find such $$$a$$$, $$$b$$$ and $$$c$$$.You have to answer $$$t$$$ independent test cases. Print required $$$a$$$, $$$b$$$ and $$$c$$$ in any (arbitrary) order.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int o = 0; o < t; o++) {\n int s[3];\n for (int i = 0; i < 3; i++) {\n scanf(\"%d\", &s[i]);\n }\n\n if (s[0] == s[1] && s[2] < s[0]) {\n if (s[2] <= 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n printf(\"%d %d %d\\n\", s[0], s[2], s[2]);\n }\n } else if (s[0] == s[2] && s[1] < s[0]) {\n if (s[1] <= 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n printf(\"%d %d %d\\n\", s[0], s[1], s[1]);\n }\n } else if (s[1] == s[2] && s[0] < s[1]) {\n if (s[0] <= 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n printf(\"%d %d %d\\n\", s[1], s[0], s[0]);\n }\n } else if (s[0] == s[1] && s[2] == s[1] && s[0] > 0) {\n printf(\"YES\\n\");\n printf(\"%d %d %d\\n\", s[0], s[0], s[0]);\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut input = stdin.lock();\n let mut line = String::new();\n input.read_line(&mut line).unwrap();\n let mut count: i16 = line.trim().parse().unwrap();\n while count > 0 {\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n let first: u32 = iter.next().unwrap().parse().unwrap();\n let second: u32 = iter.next().unwrap().parse().unwrap();\n let third: u32 = iter.next().unwrap().parse().unwrap();\n match if first < second {\n second == third\n } else if first > second {\n first == third\n } else {\n first >= third\n } {\n true => println!(\n \"YES\\n{} {} 1\",\n first,\n if first == second { third } else { second }\n ),\n false => println!(\"NO\"),\n }\n count -= 1;\n }\n}", "difficulty": "medium"}
{"problem_id": "0152", "problem_description": "Mike and Joe are playing a game with some stones. Specifically, they have $$$n$$$ piles of stones of sizes $$$a_1, a_2, \\ldots, a_n$$$. These piles are arranged in a circle.The game goes as follows. Players take turns removing some positive number of stones from a pile in clockwise order starting from pile $$$1$$$. Formally, if a player removed stones from pile $$$i$$$ on a turn, the other player removes stones from pile $$$((i\\bmod n) + 1)$$$ on the next turn.If a player cannot remove any stones on their turn (because the pile is empty), they lose. Mike goes first.If Mike and Joe play optimally, who will win?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a;\n scanf(\"%d\", &a);\n int A[a];\n\n int ind = 0;\n for (int i = 0; i < a; i++) {\n scanf(\"%d\", &A[i]);\n }\n int min = A[0];\n for (int i = 1; i < a; i++) {\n if (A[i] < min) {\n min = A[i];\n ind = i;\n }\n }\n ind++;\n if (a % 2 == 0) {\n if ((min * a + (ind - 1)) % 2 == 0) {\n printf(\"Joe\\n\");\n } else {\n printf(\"Mike\\n\");\n }\n } else {\n printf(\"Mike\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let stdin = io::stdin();\n\n let mut n: i32;\n let mut aux: i32;\n let mut odd: bool = true;\n let mut min: i32;\n let mut idx: i32;\n\n stdin.read_line(&mut buffer).expect(\"bruh\");\n let t: i32 = buffer.trim().parse().unwrap();\n\n for _ in 0..t {\n buffer.clear();\n stdin.read_line(&mut buffer).expect(\"bruh\");\n n = buffer.trim().parse().unwrap();\n min = i32::MAX;\n\n buffer.clear();\n stdin.read_line(&mut buffer).expect(\"bruh\");\n buffer = buffer.trim().to_string();\n idx = 1;\n for num in buffer.split(' ') {\n aux = num.parse::().unwrap();\n if min > aux {\n min = aux;\n odd = idx % 2 != 0;\n }\n idx += 1;\n }\n\n if n % 2 == 0 && odd {\n println!(\"Joe\");\n } else {\n println!(\"Mike\");\n }\n }\n}", "difficulty": "easy"}
{"problem_id": "0153", "problem_description": "\nScore : 200 points
\n\n
\nProblem Statement
Takahashi is practicing shiritori alone again today.
\nShiritori is a game as follows:
\n\n- In the first turn, a player announces any one word.
\n- In the subsequent turns, a player announces a word that satisfies the following conditions:
\n- That word is not announced before.
\n- The first character of that word is the same as the last character of the last word announced.
\n
\n \n
\nIn this game, he is practicing to announce as many words as possible in ten seconds.
\nYou are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.
\n\n
\n\n
\nConstraints
\n- N is an integer satisfying 2 \\leq N \\leq 100.
\n- W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
\n
\n\n
\n
\n\n
\n
\nInput
Input is given from Standard Input in the following format:
\nN\nW_1\nW_2\n:\nW_N\n
\n\n
\n
\n
\nOutput
If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.
\n\n
\n
\n
\n\n
\nSample Input 1
4\nhoge\nenglish\nhoge\nenigma\n
\n\n
\n\n
\nSample Output 1
No\n
\nAs hoge is announced multiple times, the rules of shiritori was not observed.
\n\n
\n
\n\n
\nSample Input 2
9\nbasic\nc\ncpp\nphp\npython\nnadesico\nocaml\nlua\nassembly\n
\n\n
\n\n
\n\n
\nSample Input 3
8\na\naa\naaa\naaaa\naaaaa\naaaaaa\naaa\naaaaaaa\n
\n\n
\n\n
\n\n
\nSample Input 4
3\nabc\narc\nagc\n
\n\n
\n\n", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\\n\", &n);\n char s[n][12];\n for (int i = 0; i < n; i++) {\n fgets(s[i], 12, stdin);\n int length = strlen(s[i]);\n s[i][length - 1] = '\\0';\n if (i == 0) {\n continue;\n }\n if (s[i][0] != s[i - 1][strlen(s[i - 1]) - 1]) {\n printf(\"No\\n\");\n return 0;\n }\n for (int j = 0; j < i; j++) {\n if (!strcmp(s[i], s[j])) {\n printf(\"No\\n\");\n return 0;\n }\n }\n }\n printf(\"Yes\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).unwrap();\n let n: usize = n.trim().parse().unwrap();\n let mut p = std::collections::HashSet::::new();\n let mut h = Vec::::new();\n let mut r = true;\n\n for _ in 0..n {\n let mut word = String::new();\n std::io::stdin().read_line(&mut word).unwrap();\n let word: String = word.trim().into();\n if r && p.contains(&word) {\n r = false;\n } else {\n p.insert(word.clone());\n h.push(word);\n }\n }\n\n println!(\n \"{}\",\n if r && h\n .iter()\n .zip(h.iter().skip(1))\n .all(|(a, b)| a.chars().last().unwrap() == b.chars().next().unwrap())\n {\n \"Yes\"\n } else {\n \"No\"\n }\n );\n}", "difficulty": "medium"}
{"problem_id": "0154", "problem_description": "Polycarp has $$$n$$$ friends, the $$$i$$$-th of his friends has $$$a_i$$$ candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all $$$a_i$$$ to be the same. To solve this, Polycarp performs the following set of actions exactly once: Polycarp chooses $$$k$$$ ($$$0 \\le k \\le n$$$) arbitrary friends (let's say he chooses friends with indices $$$i_1, i_2, \\ldots, i_k$$$); Polycarp distributes their $$$a_{i_1} + a_{i_2} + \\ldots + a_{i_k}$$$ candies among all $$$n$$$ friends. During distribution for each of $$$a_{i_1} + a_{i_2} + \\ldots + a_{i_k}$$$ candies he chooses new owner. That can be any of $$$n$$$ friends. Note, that any candy can be given to the person, who has owned that candy before the distribution process. Note that the number $$$k$$$ is not fixed in advance and can be arbitrary. Your task is to find the minimum value of $$$k$$$.For example, if $$$n=4$$$ and $$$a=[4, 5, 2, 5]$$$, then Polycarp could make the following distribution of the candies: Polycarp chooses $$$k=2$$$ friends with indices $$$i=[2, 4]$$$ and distributes $$$a_2 + a_4 = 10$$$ candies to make $$$a=[4, 4, 4, 4]$$$ (two candies go to person $$$3$$$). Note that in this example Polycarp cannot choose $$$k=1$$$ friend so that he can redistribute candies so that in the end all $$$a_i$$$ are equal.For the data $$$n$$$ and $$$a$$$, determine the minimum value $$$k$$$. With this value $$$k$$$, Polycarp should be able to select $$$k$$$ friends and redistribute their candies so that everyone will end up with the same number of candies.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int sum = 0;\n int a[n];\n int c = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n sum += a[i];\n }\n if (sum % n == 0 && n != 1) {\n for (int i = 0; i < n; i++) {\n if (a[i] > sum / n) {\n c++;\n }\n }\n printf(\"%d\\n\", c);\n } else if (n == 1) {\n printf(\"0\\n\");\n } else {\n printf(\"-1\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let in_lock = std_in.lock();\n let input = BufReader::new(in_lock);\n\n let std_out = stdout();\n let out_lock = std_out.lock();\n let mut output = BufWriter::new(out_lock);\n\n let mut lines = input.lines().map(|r| r.unwrap());\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let t = it.next().unwrap();\n\n 'cases: for _case_index in 1..=t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let n = it.next().unwrap();\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let a: Vec<_> = it.collect();\n let s = a.iter().cloned().sum::();\n if s % n != 0 {\n writeln!(&mut output, \"-1\").unwrap();\n } else {\n let ans = a.into_iter().filter(|&x| x * n > s).count();\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n }\n}", "difficulty": "medium"}
{"problem_id": "0155", "problem_description": "\nScore : 700 points
\n\n
\nProblem Statement
Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right.\nEach square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white.\nFor every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once.
\nPhantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region?
\nProcess all the queries.
\n\n
\n\n
\nConstraints
\n- 1 ≤ N,M ≤ 2000
\n- 1 ≤ Q ≤ 200000
\n- S_{i,j} is either 0 or 1.
\n- S_{i,j} satisfies the condition explained in the statement.
\n- 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q)
\n- 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q)
\n
\n\n
\n
\n\n
\n
\nInput
The input is given from Standard Input in the following format:
\nN M Q\nS_{1,1}..S_{1,M}\n:\nS_{N,1}..S_{N,M}\nx_{1,1} y_{i,1} x_{i,2} y_{i,2}\n:\nx_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2}\n\n\n
\n
\n
\nOutput
For each query, print the number of the connected components consisting of blue squares in the region.
\n\n
\n
\n
\n\n
\nSample Input 1
3 4 4\n1101\n0110\n1101\n1 1 3 4\n1 1 3 1\n2 2 3 4\n1 2 2 4\n
\n\n
\n\n
\nSample Output 1
3\n2\n2\n2\n
\n
\nIn the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.
\nIn the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus 2 should be printed.\nNote that squares that belong to the same component in the original grid may belong to different components.
\n\n
\n
\n\n
\nSample Input 2
5 5 6\n11010\n01110\n10101\n11101\n01010\n1 1 5 5\n1 2 4 5\n2 3 3 4\n3 3 3 3\n3 1 3 5\n1 1 3 4\n
\n\n
\n\n
\nSample Output 2
3\n2\n1\n1\n3\n2\n
\n
\n", "c_code": "int solution() {\n int i;\n int j;\n int N;\n int M;\n int Q;\n char S[2002][2002] = {};\n scanf(\"%d %d %d\", &N, &M, &Q);\n for (i = 1; i <= N; i++) {\n scanf(\"%s\", &(S[i][1]));\n }\n\n short q[4000001][2];\n short par[2002][2002][2] = {};\n int k;\n int l;\n int head;\n int tail;\n for (i = 1; i <= N; i++) {\n for (j = 1; j <= M; j++) {\n if (S[i][j] == '0' || par[i][j][0] != 0) {\n continue;\n }\n par[i][j][0] = i;\n par[i][j][1] = j;\n q[0][0] = i;\n q[0][1] = j;\n for (head = 0, tail = 1; head < tail; head++) {\n k = q[head][0];\n l = q[head][1];\n if (S[k - 1][l] == '1' && par[k - 1][l][0] == 0) {\n par[k - 1][l][0] = k;\n par[k - 1][l][1] = l;\n q[tail][0] = k - 1;\n q[tail++][1] = l;\n }\n if (S[k + 1][l] == '1' && par[k + 1][l][0] == 0) {\n par[k + 1][l][0] = k;\n par[k + 1][l][1] = l;\n q[tail][0] = k + 1;\n q[tail++][1] = l;\n }\n if (S[k][l - 1] == '1' && par[k][l - 1][0] == 0) {\n par[k][l - 1][0] = k;\n par[k][l - 1][1] = l;\n q[tail][0] = k;\n q[tail++][1] = l - 1;\n }\n if (S[k][l + 1] == '1' && par[k][l + 1][0] == 0) {\n par[k][l + 1][0] = k;\n par[k][l + 1][1] = l;\n q[tail][0] = k;\n q[tail++][1] = l + 1;\n }\n }\n }\n }\n\n int root[2002][2002];\n int up[2002][2002];\n int down[2002][2002];\n int left[2002][2002];\n int right[2002][2002];\n for (j = 1, root[0][0] = 0; j <= M; j++) {\n root[0][j] = 0;\n for (i = 1, left[0][j] = 0, right[0][j] = 0; i <= N; i++) {\n left[i][j] =\n left[i - 1][j] + ((S[i][j] == '1' && par[i][j][1] == j - 1) ? 1 : 0);\n right[i][j] =\n right[i - 1][j] + ((S[i][j] == '1' && par[i][j][1] == j + 1) ? 1 : 0);\n }\n }\n for (i = 1; i <= N; i++) {\n root[i][0] = 0;\n for (j = 1, up[i][0] = 0, down[i][0] = 0; j <= M; j++) {\n root[i][j] = root[i - 1][j] + root[i][j - 1] - root[i - 1][j - 1] +\n ((par[i][j][0] == i && par[i][j][1] == j) ? 1 : 0);\n up[i][j] =\n up[i][j - 1] + ((S[i][j] == '1' && par[i][j][0] == i - 1) ? 1 : 0);\n down[i][j] =\n down[i][j - 1] + ((S[i][j] == '1' && par[i][j][0] == i + 1) ? 1 : 0);\n }\n }\n\n int m;\n int n;\n for (n = 1; n <= Q; n++) {\n scanf(\"%d %d %d %d\", &i, &j, &k, &l);\n printf(\"%d\\n\", root[k][l] - root[i - 1][l] - root[k][j - 1] +\n root[i - 1][j - 1] + up[i][l] - up[i][j - 1] +\n down[k][l] - down[k][j - 1] + left[k][j] -\n left[i - 1][j] + right[k][l] - right[i - 1][l]);\n }\n\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut lines_str = String::new();\n let _ = stdin.read_to_string(&mut lines_str);\n let mut lines = lines_str.lines();\n let mut line0itr = lines.next().expect(\"\").split_whitespace();\n let n: usize = line0itr.next().expect(\"\").trim().parse().unwrap();\n let m: usize = line0itr.next().expect(\"\").trim().parse().unwrap();\n let q: usize = line0itr.next().expect(\"\").trim().parse().unwrap();\n\n let mut dots: Vec = vec![0; (2 * n - 1) * (2 * m - 1)];\n\n for i in 0..n {\n let mut linei1itr = lines.next().expect(\"\").chars();\n for j in 0..m {\n let c = linei1itr.next().expect(\"\");\n if c != '1' {\n continue;\n }\n dots[2 * i * (2 * m - 1) + 2 * j] = 1;\n if j != 0 && dots[2 * i * (2 * m - 1) + 2 * (j - 1)] == 1 {\n dots[2 * i * (2 * m - 1) + 2 * j - 1] = -1;\n }\n if i != 0 && dots[2 * (i - 1) * (2 * m - 1) + 2 * j] == 1 {\n dots[(2 * i - 1) * (2 * m - 1) + 2 * j] = -1;\n }\n }\n }\n\n for k in 0..(2 * n - 1) {\n for l in 0..(2 * m - 1) {\n if l == 0 {\n dots[k * (2 * m - 1) + l] = dots[k * (2 * m - 1) + l];\n } else {\n dots[k * (2 * m - 1) + l] += dots[k * (2 * m - 1) + l - 1];\n }\n }\n }\n\n for k in 0..(2 * n - 1) {\n for l in 0..(2 * m - 1) {\n if k == 0 {\n dots[k * (2 * m - 1) + l] = dots[k * (2 * m - 1) + l];\n } else {\n dots[k * (2 * m - 1) + l] += dots[(k - 1) * (2 * m - 1) + l];\n }\n }\n }\n\n for _i in 0..q {\n let mut linein1itr = lines.next().expect(\"\").split_whitespace();\n let x1: usize = linein1itr.next().expect(\"\").trim().parse().unwrap();\n let y1: usize = linein1itr.next().expect(\"\").trim().parse().unwrap();\n let x2: usize = linein1itr.next().expect(\"\").trim().parse().unwrap();\n let y2: usize = linein1itr.next().expect(\"\").trim().parse().unwrap();\n let x2y2 = dots[(2 * (x2 - 1)) * (2 * m - 1) + (2 * (y2 - 1))];\n let x1y2 = if x1 == 1 {\n 0\n } else {\n dots[(2 * (x1 - 1) - 1) * (2 * m - 1) + (2 * (y2 - 1))]\n };\n let x2y1 = if y1 == 1 {\n 0\n } else {\n dots[(2 * (x2 - 1)) * (2 * m - 1) + (2 * (y1 - 1) - 1)]\n };\n let x1y1 = if x1 == 1 || y1 == 1 {\n 0\n } else {\n dots[(2 * (x1 - 1) - 1) * (2 * m - 1) + (2 * (y1 - 1) - 1)]\n };\n println!(\"{}\", x2y2 + x1y1 - x1y2 - x2y1);\n }\n}", "difficulty": "easy"}
{"problem_id": "0156", "problem_description": "Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.You are to implement the alignment in the shortest possible time. Good luck!", "c_code": "int solution() {\n int i;\n int j;\n int k;\n int l;\n int n;\n int m;\n int z[1005];\n int max = 0;\n int nu = 0;\n char c;\n char s[1005][1005];\n i = 0;\n int flag1 = 0;\n c = getchar();\n while (c != EOF) {\n l = 0;\n s[i][l] = c;\n while (c != '\\n' && c != EOF) {\n l++;\n c = getchar();\n s[i][l] = c;\n }\n s[i][l] = '\\0';\n z[i] = l;\n if (z[i] > max) {\n max = z[i];\n }\n i++;\n if (c == EOF) {\n break;\n }\n c = getchar();\n }\n for (j = 0; j < max + 2; j++) {\n printf(\"*\");\n }\n printf(\"\\n\");\n int flag = 1;\n for (j = 0; j <= i - 1; j++) {\n printf(\"*\");\n if ((max - z[j]) % 2 == 0) {\n for (k = 0; k < (max - z[j]) / 2; k++) {\n printf(\" \");\n }\n printf(\"%s\", s[j]);\n for (k = 0; k < (max - z[j]) / 2; k++) {\n printf(\" \");\n }\n printf(\"*\");\n } else {\n if (flag == 1) {\n for (k = 0; k < (max - z[j]) / 2; k++) {\n printf(\" \");\n }\n printf(\"%s\", s[j]);\n for (k = 0; k < (max - z[j]) / 2 + 1; k++) {\n printf(\" \");\n }\n flag = 0;\n } else {\n for (k = 0; k < (max - z[j]) / 2 + 1; k++) {\n printf(\" \");\n }\n printf(\"%s\", s[j]);\n for (k = 0; k < (max - z[j]) / 2; k++) {\n printf(\" \");\n }\n flag = 1;\n }\n printf(\"*\");\n }\n printf(\"\\n\");\n }\n for (j = 0; j < max + 2; j++) {\n printf(\"*\");\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let reader = BufReader::new(io::stdin());\n let lines = reader.lines().map(|x| x.unwrap()).collect::>();\n let w = lines.iter().map(|x| x.len()).max().unwrap();\n println!(\"{}\", \"*\".repeat(w + 2));\n let mut x = 0;\n for line in &lines {\n let w = w - line.len();\n let mut l = w / 2;\n let mut r = w - l;\n if l * 2 != w {\n l += x;\n r -= x;\n x = 1 - x;\n }\n println!(\"*{}{}{}*\", \" \".repeat(l), line, \" \".repeat(r));\n }\n println!(\"{}\", \"*\".repeat(w + 2));\n}", "difficulty": "medium"}
{"problem_id": "0157", "problem_description": "You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \\le i \\le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \\underline{3}, 2, \\underline{-2}, 1, -4, \\underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \\underline{3}, 2, \\underline{-2}, 1, -4, \\underline{0}]$$$; $$$[-4, -1, \\underline{3}, 2, \\underline{-2}, 1, 1, \\underline{0}]$$$; $$$[1, -1, \\underline{3}, 2, \\underline{-2}, 1, -4, \\underline{0}]$$$; $$$[1, 2, \\underline{3}, -1, \\underline{-2}, -4, 1, \\underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\\dots$$$, $$$p_n = a_1 + a_2 + \\dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \\le j \\le n$$$) such that $$$p_j < 0$$$. If there are no $$$j$$$ such that $$$p_j < 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n int array[100];\n int lock[100];\n int record[100] = {0};\n scanf(\"%d\", &n);\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &array[j]);\n }\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &lock[j]);\n }\n for (int l = 0; l < n; l++) {\n if (lock[l]) {\n printf(\"%d \", array[l]);\n } else {\n int max = -100001;\n int maxIndex = 0;\n for (int k = 0; k < n; k++) {\n if (array[k] > max && lock[k] == 0 && record[k] == 0) {\n max = array[k];\n maxIndex = k;\n }\n }\n printf(\"%d \", array[maxIndex]);\n record[maxIndex] = 1;\n }\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).expect(\"err\");\n let t: i32 = buffer.trim().parse().expect(\"parse err\");\n for _ in 0..t {\n io::stdin().read_line(&mut buffer).expect(\"err\");\n\n buffer.clear();\n io::stdin().read_line(&mut buffer).expect(\"err\");\n let v: Vec = buffer\n .split_whitespace()\n .map(|s| s.parse().expect(\"parse error\"))\n .collect();\n\n buffer.clear();\n io::stdin().read_line(&mut buffer).expect(\"err\");\n let l: Vec = buffer\n .split_whitespace()\n .map(|s| s.parse().expect(\"parse error\"))\n .collect();\n\n let mut mvec: Vec = Vec::new();\n for i in 0..v.len() {\n if l[i] == 0 {\n mvec.push(v[i]);\n }\n }\n mvec.sort_by(|a, b| b.cmp(a));\n let mut j = 0;\n for i in 0..v.len() {\n if l[i] == 0 {\n print!(\"{} \", mvec[j]);\n j += 1;\n } else {\n print!(\"{} \", v[i]);\n }\n }\n println!();\n }\n}", "difficulty": "hard"}
{"problem_id": "0158", "problem_description": "Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple.The prison can be represented as a rectangle $$$a\\times b$$$ which is divided into $$$ab$$$ cells, each representing a prison cell, common sides being the walls between cells, and sides on the perimeter being the walls leading to freedom. Before sentencing, Michael can ask his friends among the prison employees to make (very well hidden) holes in some of the walls (including walls between cells and the outermost walls). Michael wants to be able to get out of the prison after this, no matter which cell he is placed in. However, he also wants to break as few walls as possible.Your task is to find out the smallest number of walls to be broken so that there is a path to the outside from every cell after this.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n int a[t];\n int b[t];\n int p[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%d %d\\n\", &a[i], &b[i]);\n }\n for (int j = 0; j < t; j++) {\n p[j] = a[j] * b[j];\n printf(\"%d\\n\", p[j]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n let n = n.trim().parse().unwrap();\n for _i in 0..n {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let values = s\n .split_whitespace()\n .map(|x| x.parse::())\n .collect::, _>>()\n .unwrap();\n let (n, m) = (values[0], values[1]);\n let result = (m - 1) * n + n - 1 + 1;\n println!(\"{}\", result);\n }\n}", "difficulty": "hard"}
{"problem_id": "0159", "problem_description": "